Design By Contract Using JMSAssert.

Slides:



Advertisements
Similar presentations
Esempio: Tombola!.
Advertisements

Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
COP 3530 JDK Environment Variables. COP 3530 JDK Environment Variables Environment Variables Environment variables are a set of dynamic values that can.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 8.
COSC 2006 Data Structures I Instructor: S. Xu URL:
MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8.
Practice Session 5 Java: Packages Collection Classes Iterators Generics Design by Contract Test Driven Development JUnit.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
The Java Assert Statement. 2 Assert A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates a particle’s.
1 Introduction to Java and Applet. 2 Download Java Compiler (1)
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
Static and Dynamic Contract Verifiers For Java Hongming Liu.
Object-Oriented Enterprise Application Development Javadoc Last Updated: 06/30/2001.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
1 Genericity Parameterizing by Type. 2 Generic Class One that is parameterized by type  Works when feature semantics is common to a set of types On object.
Component-Based Software Engineering Components and Interfaces Paul Krause.
Ranga Rodrigo. Class is central to object oriented programming.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Servlets Environment Setup. Agenda:  Setting up Java Development Kit  Setting up Web Server: Tomcat  Setting up CLASSPATH.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Appendix E The EZJava.
The Basics of Javadoc Presented By: Wes Toland. Outline  Overview  Background  Environment  Features Javadoc Comment Format Javadoc Program HTML API.
Programming With C.
Design by Contract in Java Concept and Comparison.
The Daikon system for dynamic detection of likely invariants MIT Computer Science and Artificial Intelligence Lab. 16 January 2007 Presented by Chervet.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Preliminaries CS 310: Object-oriented Programming Java API.
OOSC - JMSAssert. Design By Contract A powerful technique for writing reliable software. Specifying the software purpose with the implementation. Key.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
22-July-2002cse142-13B-Development © 2002 University of Washington1 Development Tools CSE 142, Summer 2002 Computer Programming 1
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Verificare şi Validarea Sistemelor Soft Tem ă Laborator 1 ESC/Java2 Extended Static Checker for Java Dat ă primire laborator: Lab 1 Dat ă predare laborator:
Computer Science 209 Software Development Handing Errors and Creating Documentation.
© Spiros Mancoridis Software Engineering (Unit Testing Tools) Dependable Software Systems Topics in Unit Testing Tools Material drawn from [ junit.org,
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
OOSC – Lab 1. Welcome Object Oriented programming is first and foremost a method for software construction, whose goal is to build high-quality software.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Documentation Javadocs. Design/Documentation An essential ingredient of good Object Oriented programming is known as design by contract. This means that.
The Java Assertion. 2 Assertion A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates the a particle’s.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Chapter 7 Programming by contract: preconditions and postconditions.
Generics. Writing typed checked generic code There are many instances where the type of the object is irrelevant : –The construction of data structures.
3/5/2002e-business and Information Systems1 Java Java Java Virtual Machine (JVM) Java Application Program Interface (API) HW Kernel API Application Programs.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Practice Session 5 Java: Packages Collection Classes Iterators Generics Default Methods Anonymous Classes Generic Methods Lambdas Design by Contract JUnit.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Specifying Object Interfaces
COMPUTER 2430 Object Oriented Programming and Data Structures I
Test Driven Development
Applying OO Concepts Using Java
Test Driven Development
Java Modeling Language (JML)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Corresponds with Chapter 5
Generics, Lambdas and Reflection
Presentation transcript:

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 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.

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 Sun’s JDK 1.2 & 1.3 but not 1.4

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

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

When are condition checked?

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

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

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 method’s return value Multiple @post markers are conjugated (AND)

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

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

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

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 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

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\”

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

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 <filename> to check assertions. jmsjava makes sure method assertions are called before/after the method invocation.

JMS Execution “jmsassert” – generates help text “jmsassert –s <filename.java>” – 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 <filename.java>” – compile “jmsjava Startup <main>” - execute and check assertions

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

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

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

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

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

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

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

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

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

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

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

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

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! }