Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design By Contract Using JMSAssert. Design By Contract A method of separating the concerns of the user and implementer of the class, and giving both enough.

Similar presentations


Presentation on theme: "Design By Contract Using JMSAssert. Design By Contract A method of separating the concerns of the user and implementer of the class, and giving both enough."— Presentation transcript:

1 Design By Contract Using JMSAssert

2 Design By Contract A method of separating the concerns of the user and implementer of the class, and giving both enough information for their tasks. Work in this course will be done with Java, offers partial support for those concepts. Each class and method should be documented in Javadoc style.

3 Design By Contract In order to implement the principles of Design-by- Contract in Java, we will use a free tool from Man Machine Systems names JMSAssert. It works with Suns JDK 1.2 & 1.3 but not 1.4

4 Design By Contract A powerful technique for writing reliable software. Specifying the software purpose with the implementation. Key elements: Invariant Preconditions Postconditions

5 Design By Contract Precondition – The constraints under which the routine will function properly. Postconditions – The state of the class after the routine execution The Contract: If you call routine R() with the preconditions satisfied, R() will return satisfying the postconditions. Invariant – Always holds

6 When are condition checked?

7 JMS Syntax - Invariant Invariant - @inv May access all class members or its direct/indirect bases, including private members May appear in any JavaDoc comment Preferable in the class comment

8 JMS Syntax - Preconditions Precondition - @pre JavaDoc preceding the respective method May reference class members and arguments Multiple @pre markers are conjugated (AND)

9 JMS Syntax - Postconditions Postconditions - @post JavaDoc preceding the respective method May use $prev(expression) to access the value at the method entry. May use $ret to denote methods return value Multiple @post markers are conjugated (AND)

10 Example /** * @pre !isEmpty() * @post (top == $prev(top- 1)) * @post $ret == elems[top] * @post !isFull */ public synchronized Object pop() { return elems[--top]; } Precondition Postconditions

11 Example /** * @pre !isEmpty() * @post (top == $prev(top- 1)) * @post $ret == elems[top] * @post !isFull */ public synchronized Object pop() { return elems[--top]; } Precondition Postconditions What is wrong in the example ?

12 JMS Syntax - General Order has no meaning @macro – for complicated conditions Recursion – as expected, on every call Inner classes can access outer classes members Anonymous classes – specify invariant in one of its methods

13 Downloads & Installations Download Java J2SE SDK 1.3.1 from http://java.sun.com/downloads (~41MB) Install into C:\Program Files\jdk1.3.1_07 http://java.sun.com/downloads Download JMSAssert 1.02 from http://www.mmsindia.com (~1.2MB) Run the jmssetup-1.02.exe installation file Install into C:\Program Files\AMSAssert1.0 http://www.mmsindia.com

14 JMSAssert Installation The following lines are added to the path: REM Next two lines are added by JMSAssert SET CLASSPATH=%CLASSPATH%;C:\PROGRA~1\JMSASS~1.0\ bin\mmsclasses.jar; SET PATH=%PATH%;C:\PROGRA~1\JMSASS~1.0\bin; Copy the classic directory from C:\Program Files\jdk1.3.1_07\jre\bin\ to the directory: C:\Program Files\JavaSoft\JRE\1.3.1_07\bin\

15 Setup (cont.) Your JavaSoft directory should look like:

16 JMSAssert – how does it work Annotate source code with assertions Compile your code using javac (as usual) Preprocess the code using jmsassert: creates contract files ( *.jms ) and a Startup.jms file. *.jms files contain java code for the assertions. Execute using: jmsjava Startup to check assertions. jmsjava makes sure method assertions are called before/after the method invocation.

17 JMS Execution jmsassert – generates help text jmsassert –s – generate assertions for a class file jmsassert –r –s. – generate assertions for all class files in the directory and sub-dirs. (use for packages) javac – compile jmsjava Startup - execute and check assertions

18 Annotate source with assertions MyStack.java Stack Demo files Execution process

19 Annotate source with assertions Preprocess to generate assertion files jmsassert –s MyStack.java Startup.jms default_MyStack.jms default_MyStack_StackEnum.jms Stack Demo files Execution process

20 Annotate source with assertions Preprocess to generate assertion files jmsassert –s Compile Java file javac MyStack.java Startup.jms default_MyStack.jms default_MyStack_StackEnum.jms MyStack.class MyStack$StackEnum.class StackTest.class Stack Demo files Execution process

21 Annotate source with assertions Preprocess to generate assertion files jmsassert –s Compile Java file javac Execute using jmsjava MyStack.java Startup.jms default_MyStack.jms default_MyStack_StackEnum.jms MyStack.class MyStack$StackEnum.class jmsjava Startup StackTest.class Stack Demo files Execution process

22 Notes Execute these steps form the command line! Make sure your CLASSPATH environment variable contains the current directory. Add CLASSPATH=%CLASSPATH%;. to autoexec.bat.

23 An Example (MyStack) A stack with invariants, pre conditions and post conditions: MyStack.java A main file: StackTest.java Compile all.java files (using javac ) Generate JMSAssert triggers by: jmsassert –s MyStack.java Run and test assertions by jmsjava Startup StackTest You can always run your test program in by java StackTest

24 MyStack.java (#1) /** @inv (top >= 0 && top < max) */ class MyStack { private Object[] elems; private int top, max;

25 MyStack.java (#2). /** @pre (sz > 0) @post (max == sz && elems != null) */ public MyStack(int sz) { max = sz; elems = new Object[sz]; }

26 MyStack.java (#3) /** @pre !isFull() @post (top == $prev (top) + 1) && elems[top-1] == obj */ public void push(Object obj) { elems[top++] = obj; }

27 MyStack.java (#4) /** @pre !isEmpty() @post (top == $prev (top) - 1) && $ret == elems[top] */ public Object pop() { return elems[--top]; }

28 MyStack.java (#5) /** @post ($ret == (top == max)) */ public boolean isFull() { return top == max; }

29 MyStack.java (#6) /** @post ($ret == (top == 0)) */ public boolean isEmpty() { return top == 0; } } // End MyStack

30 StackTest.java class StackTest { public static void main(String[] args) { MyStack s = new MyStack(2); // Can push at most 2 elements s.push(new Integer(1)); s.push(new Integer(23)); s.push(new Integer(0)); // Precondition violation here! }


Download ppt "Design By Contract Using JMSAssert. Design By Contract A method of separating the concerns of the user and implementer of the class, and giving both enough."

Similar presentations


Ads by Google