Examples of Design by Contract in Java



Similar documents
Design by Contract beyond class modelling

Rigorous Software Development CSCI-GA

Software Component Specification Using Design by Contract

On the Relation between Design Contracts and Errors: A Software Development Strategy

Case studies: Outline. Requirement Engineering. Case Study: Automated Banking System. UML and Case Studies ITNP090 - Object Oriented Software Design

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Towards Contract-based Testing of Web Services

How To Write A Test Engine For A Microsoft Microsoft Web Browser (Php) For A Web Browser For A Non-Procedural Reason)

Adapting C++ Exception Handling to an Extended COM Exception Model

Rigorous Software Engineering Hoare Logic and Design by Contracts

Systems Integration: Co C mp m onent- t bas a e s d s o s ftw ft a w r a e r e ngin i eeri r n i g

Formal Specification and Verification of Avionics Software

Quotes from Object-Oriented Software Construction

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Execution of A Requirement Model in Software Development

Sequence Diagrams. Massimo Felici. Massimo Felici Sequence Diagrams c

A Framework for the Semantics of Behavioral Contracts

Programming by Contract vs. Defensive Programming: A Comparison of Run-time Performance and Complexity

Software Engineering Techniques

Programming Language Constructs as Basis for Software Architectures

PROJECT MANAGEMENT METHODOLOGY OF OBJECT- ORIENTED SOFTWARE DEVELOPMENT

Unit-testing with JML

UML TUTORIALS THE USE CASE MODEL

Design by Contract for Web Services: Architecture, Guidelines, and Mappings

Introduction to programming

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

National University of Ireland, Maynooth MAYNOOTH, CO. KILDARE, IRELAND. Testing Guidelines for Student Projects

A Meeting Room Scheduling Problem

Software Specification and Testing

Model Driven Security: Foundations, Tools, and Practice

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

GContracts Programming by Contract with Groovy. Andre Steingress

Constructing Contracts: Making Discrete Mathematics Relevant to Beginning Programmers

Formally speaking: How to apply OCL

Using UML Part Two Behavioral Modeling Diagrams

Lecture Notes on Linear Search

BCS HIGHER EDUCATION QUALIFICATIONS Level 6 Professional Graduate Diploma in IT. March 2013 EXAMINERS REPORT. Software Engineering 2

Case Studies of Running the Platform. NetBeans UML Servlet JSP GlassFish EJB

Safe Object-Oriented Software: The Verified Design-By-Contract Paradigm

Software Design. Software Design. Software design is the process that adds implementation details to the requirements.

Limitations of Data Encapsulation and Abstract Data Types

Appendix... B. The Object Constraint

Structure of Presentation. Stages in Teaching Formal Methods. Motivation (1) Motivation (2) The Scope of Formal Methods (1)

EMBEDDED SOFTWARE DEVELOPMENT: COMPONENTS AND CONTRACTS

Construct by Contract: Construct by Contract: An Approach for Developing Reliable Software

Service Design: Using a GSRM Meta-Model. Ed Buchinski Treasury Board of Canada Secretariat UN/CEFACT TBG-19 Oct. 5 th, 2006

Explicit Connectors in Component Based Software Engineering for Distributed Embedded Systems. Dietmar Schreiner and Karl M.

Using Alloy and UML/OCL to Specify Run-Time Configuration Management: A Case Study

UML-based Test Generation and Execution

Verifying Semantic of System Composition for an Aspect-Oriented Approach

Gadget: A Tool for Extracting the Dynamic Structure of Java Programs

Konzepte objektorientierter Programmierung

How To Write A Contract Guided System Development (Contract Guided Development) In Java (Java) (For A Free Download)

Semantic-enabled Software Engineering and Development

Questions? Assignment. Techniques for Gathering Requirements. Gathering and Analysing Requirements

Home Assignment 4 OCL

Model Driven Interoperability through Semantic Annotations using SoaML and ODM

UNIFACE Component-based. Development Methodology UNIFACE V Revision 0 Dec 2000 UMET

Component Based Development Methods - comparison

Object Oriented Design

Chapter 1 Java Program Design and Development

Database Design Methodology

Trends in Embedded Software Development in Europe. Dr. Dirk Muthig

Checking Access to Protected Members in the Java Virtual Machine

Case Study: Design and Implementation of an Ordering system using UML, Formal specification and Java Builder

An Approach for Generating Concrete Test Cases Utilizing Formal Specifications of Web Applications

Course Title: Software Development

Enterprise Application Development Using UML, Java Technology and XML

Umbrello UML Modeller Handbook

Aspects for Testing Aspects?

Monitoring Java Code Using ConGu

Model Transformation by Graph Transformation: A Comparative Study

CIS 771: Software Specifications. Lecture 1: Course Overview

Formal Engineering for Industrial Software Development

Construction Principles and Design Patterns. Flyweight, Bridge, Builder

Introduction to unit testing with Java, Eclipse and Subversion

History OOP languages Year Language 1967 Simula Smalltalk

Component Based Software Engineering: A Broad Based Model is Needed

A generic framework for game development

JSR-303 Bean Validation

Run-Time Assertion Checking and Monitoring Java Programs

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

UML for C# Modeling Basics

Transcription:

Object World Berlin 99 Design & Components Berlin, May 17th-20th 1999 Examples of Design by Contract in Java using Contract, the Design by Contract tm Tool for Java tm Reto Kramer, kramer@acm.org Java is a registered trademark of Sun Microsystems Inc. Design by Contract is a registered trademark of ISE Inc.

Design by Contract - What is it? Classes of a system communicate with one another on the basis of precisely defined benefits and obligations. [Bertrand Meyer, CACM, Vol. 36, No 9, 1992]

Example - Class Person /** * @invariant age_ > 0 */ class Person { protected age_; /** * @post return > 0 */ int getage() {..} /** * @pre age > 0 */ void setage( int age ){..}...} New comment-tags: @pre, @post, @invariant All instances of person must have a positive age. Clients are promised that the age is positive provided that: Clients are obligated to pass positive ages only. Service will be denied otherwise.

Meaning of pre, post and invariant If preconditions are not obeyed by the client of the class method, the service provider will deny its service! If any postcondition is violated, it uncovers a problem on the service provider side. If any class invariant is violated it uncovers a problem on the service provider side. The problem can be implementation error not specific enough preconditions

Benefits - Obligations Benefit Obligation Provider Client - no need to check output values - result guaranteed to comply to postcondition 4 - no need to check input values - input guaranteed to comply to precondition 1 2 3 satisfy preconditions satisfy postconditions

So, is it like assert.h? Assert statements are a great tool - design by contract even goes one step beyond them: assert does not provide a contract clients can not see asserts as part of the interface does not have a semantic associated with it not explicit whether they represent pre-, postconditions or invariants no OO support (e.g. inheritance), see later does not lead to automatic documentation

Example - A Simple Interface 1: interface Person { 1: class Employee implements Person { 2: 2: 3: /**age always positive 3: protected int age_; 4: * @post return > 0 4: 5: */ 5: public int getage() { 6: int getage(); 6: return age_; 7: 7: }; 8: /** age always positive 8: 9: * @pre age > 0 9: public void setage( int age ) { 10: */ 10: age_ = age; 11: void setage( int age ); 11: }; 12: } 12: }

Benefits - General failures occur close to the faults (I.e. during integration tests and field use!) interface documentation always up-to-date, can be trusted! documentation can be generated automatically (idoclet) contract specification serves as a basis for black box testing of classes (test-driver spec)

Benefits - Abstraction and Precision Precise Type (class) model with precise meaning (design by contract) Interface A... Invariant Precondition, Postcondition Ambiguous Type (class) model interaction diagrams Use cases Textual requirements description expections/ effects (pre-, post) business rules (invariants) Abstract Terms Specific Terms

Benefits - Project Phases API Scope Analysis use cases Subsystem API specification Design Subsystem Stub impl. requirements functionality business case domain model subsystems APIs Stub APIs API Implementation Unit Test against Stubs Integration & Test Stub APIs APIs Acceptance Test Rollout Maintenance Extension APIs

Benefits - Project Roles Class user postconditions guaranteed can trust documentation Class provider preconditions guaranteed automatic documentation Test manager more accurate test-effort estimation black box spec for free Project manager easier to preserve design over a long time reduced maintenance effort in the long run (failure close to fault) enables unambiguous interface specification lower documentation cost fearless reuse (enables specification of reusable classes)

References icontract: http://www.reliable-systems.com Books: Object Oriented Software Construction, 2 nd edition, Bertrand Meyer, Prentice Hall, 1997 Objects, Components and Frameworks with UML, D.F. D Souza, A. Cameron Wills, Addison Wesley, 1999 Eiffel [Interactive Software Engineering, ISE] http://www.eiffel.com UML 1.1 / Object Constraint Language (OCL) http://www.rational.com

icontract - the Tool source code pre-processor no run-time library required compatible with OCL old value, x@pre return value quantifiers: forall, exists supports Java type extension mechanisms (contract propagation)

Tool Components Used for type extension mechanisms Instrumentation Repository javac Class Instrumentation 2.class class REP_C icontract.tool source-code with @pre, @post and @invariant annotations in comment paragraphs SOURCE-CODE.java 1 class C.java 4 class REP_C 3 _I.java class C not instrumented javac - instrumented source-code which checks pre-, postconditions and invariants. instrumented 5.class class C java

Performance Tuning Check instrumentation is done per.java file (public class) Performance critical classes can be excluded from the checks Files can be instrumented with any combination of checks for: pre- post-conditions and invariants E.g. if implementation is tested thoroughly, only check preconditions

Java Language Support Interface I 2 packages Interface J package implements A Q 1 extends 3 Interface K B R Interface M implements 4 extends Interface L C X Interface N Innerclass

Java Language Support (con t) All but private methods are instrumented with invariant checks. The finalize() method is not instrumented with invariant checks. Invariant checks are synchronized Recursive invariant checks are avoided automatically Default constructors are added to classes automatically, if needed In constructors the delegation to this( ) and super( ) is put in front of the precondition check (javac demands this).

Specification Language Propositional logic with quantifiers Any expression that may appear in an if(...) condition may appear in a pre-, post- and invariant expression Scope: as if the invariant were a method of the class, interface as if the pre- and postcondition were are statement of the method

Specification Language (con t) forall Type t in <enumeration> <expr> <collection>->forall(t <expr>) exists Type t in <enumeration> <expr> <collection>->exists(t <expr>) <a> implies <b> (same as OCL) same as OCL Differences between icontract and OCL syntactic & icontract needs to know Type!

Specification Language (con t) In postconditions references to the following pseudo-variables are allowed: return denotes the return value of a method this is called result in OCL <expression>@pre denotes the value of the expression (e.g. variable) when the method was entered - notation from UML / OCL old value reference same as OCL

Example Office Management System Manage the rooms available to a company. Provide new hires with office and support employees that move from one office to another. Focus on Initial type model of domain (UML) Add business constraints and rules (OCL) Add precise meaning of operations (OCL) Generate Java (icontract)

Office Management Example (hire) 1 2 Company void Company:hire(Employee e) - rooms->isempty() implies employees.isempty() - employees -> forall (e rooms -> includes(e.office)) - employees->forall( e1 employees->forall( e2 (e1!= e2) implies (e1.room!= e2.room)) Company 0..n rooms Room pre: (e!= null) && (!employees->includes(e)) post: - employees->includes(e) - getavailableroom()@pre!= getavailableroom() // hire must call an unspecified method that will // ensure that a new, available room is choosen - e.office == getavailableroom() // SIDE EFFECT FREE! boolean roomsavailable() Room getavailableroom() void hire(employee e) void move(employee e, Room newroom). 0..n employees Employee.. 1 office Room Company:getAvailableRoom() pre: roomsavailable() post: - result!= null - rooms->includes(result) -!employees->exists(e e.office == result) - result == getavailableroom() // SIDE EFFECT FREE!. boolean Company:roomsAvailable() pre: TRUE post: result == rooms->exists( room!employees->exists( e e.office == room))

Office Management Example (move) 3 4 void Company:move(Employee e, Room newroom) Company 0..n rooms Room pre: - (e!= null) && (newroom!= null) - employees->includes(e) -!employee->exists( e e.office == newroom ) // newroom is not anyones office boolean roomsavailable() Room getavailableroom() void hire(employee e) void move(employee e, Room newroom). isavailable() 1 office post: - e.office == newroom -!employee->exists( other other.office == e.office@pre ) // the employee s (e) old office (e.office@pre) is not // used by any other employee. 0..n employees Employee 0..1 owner. Room.isAvailable()?? boolean Room:isAvailable() pre: TRUE post: - result == onwer!= null 5 void Company:move(Employee e, Room newroom) pre: - (e!= null) && (newroom!= null) - employees->includes(e) - newroom.isavailable() post: - e.office == newroom - e.office@pre.isavailable() -!newroom.isavailable()

API Specification for Subsystem Assume Office Management System to be a subsystem of a larger, total solution Hence requires proper separation of interface from implementation. Specification of previous slides is mapped to Java package containing interfaces. but what about the associations? API Impl. realizes for client Serves as a specification and provider Need to create get methods for each role in an association...

API Specification for Subsystem API. Employee <<Interface>> Room getoffice() Company <<Interface>> boolean roomsavailable() Room getavailableroom() void hire(employee e) void move(employee e, Room newroom) Vector getrooms() Vector getemployees(). Room <<Interface>> boolean isavailable() Employee getowner() Implementation Company boolean roomsavailable() Room getavailableroom() void hire(employee e) void move(employee e, Room newroom) 0..n rooms. Room isavailable() 1 office realizes. 0..n employees Employee 0..1 owner.

icontract /** * @invariant getrooms().isempty() implies getemployees.isempty() * @invariant forall Employee e in getemployees().elements() * getrooms.contains(e.getoffice()) * @invariant forall Employee e1 in getemployees().elements() * forall Employee e2 in getemployees().elements() * (e1!=e2) implies (e1.getoffice()!=e2.getoffice()) */ interface Company { /** * @pre (e!=null) && (newroom!=null) *.. * @post e.getoffice()@pre.isavailable() */ void move(employee e, Room newroom);.. } Company - rooms->isempty() implies employees.isempty() - employees -> forall (e rooms -> includes(e.office)) - employees->forall( e1 employees->forall( e2 (e1!= e2) implies (e1.room!= e2.room)) void Company:move(Employee e, Room newroom) pre: - (e!= null) && (newroom!= null) - employees->includes(e) - newroom.isavailable() post: - e.office == newroom - e.office@pre.isavailable() -!newroom.isavailable() icontract propagates API specification into implementing classes!