GContracts Programming by Contract with Groovy. Andre Steingress



Similar documents
Software Engineering Techniques

Build management & Continuous integration. with Maven & Hudson

FROM BANNER 8 TO BANNER XE. What s the story?

Viewpoint. Choosing the right automation tool and framework is critical to project success. - Harsh Bajaj, Technical Test Lead ECSIVS, Infosys

Developing Android Apps with the ArcGIS Runtime SDK for Android. Dan

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

YouTrack MPS case study

Agile Development with Groovy and Grails. Christopher M. Judd. President/Consultant Judd Solutions, LLC

Rigorous Software Development CSCI-GA

PHP vs. Java. In this paper, I am not discussing following two issues since each is currently hotly debated in various communities:

JRuby Now and Future Charles Oliver Nutter JRuby Guy Sun Microsystems

Textual Modeling Languages

Continuous Delivery for Alfresco Solutions. Satisfied customers and happy developers with!! Continuous Delivery!

VOC Documentation. Release 0.1. Russell Keith-Magee

Ruby on Rails in GlassFish Sun Microsystems

Intruduction to Groovy & Grails programming languages beyond Java

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

Shark Installation Guide Week 3 Report. Ankush Arora

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

Design by Contract beyond class modelling

Architecture Rules Enforcement and Governance Using Aspects

Dynamic website development using the Grails Platform. Joshua Davis Senior Architect Cognizant Technology Solutions

Developing modular Java applications

Rigorous Software Development CSCI-GA

OpenShift is FanPaaStic For Java EE. By Shekhar Gulati Promo Code JUDCON.IN

Continuous Integration The Full Monty Artifactory and Gradle. Yoav Landman & Frederic Simon

Continuous Integration For Fusion Middleware

Smarter Testing With Spock

Rapid Application Development. and Application Generation Tools. Walter Knesel

How To Run A Hello World On Android (Jdk) On A Microsoft Ds.Io (Windows) Or Android Or Android On A Pc Or Android 4 (

The power of root on Android emulators

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

OUR COURSES 19 November All prices are per person in Swedish Krona. Solid Beans AB Kungsgatan Göteborg Sweden

Triple-E class Continuous Delivery

Hands on exercise for

APACHE SLING & FRIENDS TECH MEETUP BERLIN, SEPTEMBER APACHE SLING & SCALA Jochen Fliedner

Event-driven plugins with Grails 3. Göran Ehrsson, Technipelago AB

Simplifying e Business Collaboration by providing a Semantic Mapping Platform

Robotium Automated Testing for Android

Solver- aided DSL with. Rui Zhao (rz2290)

Mobile Test Automation Framework

<Insert Picture Here> What's New in NetBeans IDE 7.2

Xtext Documentation. September 26, 2014

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

Java and RDBMS. Married with issues. Database constraints

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

SOFTWARE TESTING TRAINING COURSES CONTENTS

Jenkins: The Definitive Guide

Getting Started with the Internet Communications Engine

Database Migration Plugin - Reference Documentation

CUT YOUR GRAILS APPLICATION TO PIECES

BEST WEB PROGRAMMING LANGUAGES TO LEARN ON YOUR OWN TIME

Introducing Tetra: An Educational Parallel Programming System

Meister Going Beyond Maven

5. Advanced Object-Oriented Programming Language-Oriented Programming

Windmill. Automated Testing for Web Applications

Dart a modern web language

Appendix... B. The Object Constraint

Grails - Rapid Web Application Development for the Java Platform

The Decaffeinated Robot

A Comparison of Open Source Application Development Frameworks for the Enterprise

Kotlin for Android Developers

A Framework for Extensible Languages

Integration of data validation and user interface concerns in a DSL for web applications

Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd.

Grails 1.1. Web Application. Development. Reclaiming Productivity for Faster. Java Web Development. Jon Dickinson PUBLISHING J MUMBAI BIRMINGHAM

Sonatype CLM for Maven. Sonatype CLM for Maven

Lecture 1 Introduction to Android

SimpleFSM - a domain-specific language for SIP communication systems - Part I: Language description

Dominik Helleberg inovex GmbH. Moderne Android Builds mit Gradle

Computer Programming I & II*

The Smalltalk Programming Language. Beatrice Åkerblom

GlassFish v3. Building an ex tensible modular Java EE application server. Jerome Dochez and Ludovic Champenois Sun Microsystems, Inc.

JAAS Java Authentication and Authorization Services

Web Service Testing. SOAP-based Web Services. Software Quality Assurance Telerik Software Academy

Nexus Professional Whitepaper. Repository Management: Stages of Adoption

Monitoring Java Code Using ConGu

Announcements FORTRAN ALGOL COBOL. Simula & Smalltalk. Programming Languages

Android ist anders - Android Dependency Management

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Continuous Integration

Automating Testing and Configuration Data Migration in OTM/GTM Projects using Open Source Tools By Rakesh Raveendran Oracle Consulting

zen Platform technical white paper

To Java SE 8, and Beyond (Plan B)

Examples of Design by Contract in Java

Eclipse. Software Engineering with an Integrated Development Environment (IDE) Markus Scheidgen

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Using EDA Databases: Milkyway & OpenAccess

Introducing Formal Methods. Software Engineering and Formal Methods

Building a Modular Server Platform with OSGi. Dileepa Jayakody Software Engineer SSWSO2 Inc.

Ruby on Rails is a web application framework written in Ruby, a dynamically typed programming language The amazing productivity claims of Rails is

Computational Mathematics with Python

High-Level Language. Building a Modern Computer From First Principles.

Towards a Framework for Generating Tests to Satisfy Complex Code Coverage in Java Pathfinder

soapui Product Comparison

Transcription:

FÕ Ò ŃÔ PŎ ÑŇÒ P ÌM Œ PÑǾ PÒ PÕ Ñ Œ PŘÕ Ñ GContracts Programming by Contract with Groovy Andre Steingress

Andre FÕ Ò ŃÔ PŎ ÑŇÒ P ÌM Œ PSteingress ÑǾ PÒ PÕ Ñ Œ PŘÕ Ñ Independent Software Dev @sternegross, @gcontracts Groovy, Grails, JEE, Spring Portfolio, Android http://blog.andresteingress.com GroovyMag, JavaMagazin (German) 2

FÕÒŃÔPŎÑŇÒPÌ Motivation MŒPÑǾPÒPÕÑŒPŘÕÑ Programming is about assumptions, mental models and knowledge. 3

Motivation FÕ Ò ŃÔ PŎ ÑŇÒ P ÌM Œ PÑǾ PÒ PÕ Ñ Œ PŘÕ Ñ Contracts help to externalize mental models and formalize implicit assumptions. How to formalize assupmtions? Logic is the formal study of correct reasoning Boolean Algebra is the tool of choice 4

FÕÒŃÔPŎÑŇÒPÌ Basic Terms MŒPÑǾPÒPÕÑŒPŘÕÑ Supplier implements a software element (e.g. a class) Client references the software element 5

FÕÒŃÔPŎÑŇÒPÌ Basic Terms MŒPÑǾPÒPÕÑŒPŘÕÑ A contract consists of pre- and postconditions for each public method Precondition expresses the constraints under which a method call will function properly Postcondition expresses properties of the state resultung from a method s execution ContractViolation is thrown if the contract is broken 6

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ Design by Contract (tm) for Groovy Version 1.2.1 Compile-Time Contract Injection Power Assert Support Maven Central Repository pure Java library, no dependencies gcontracts-core-1.2.1.jar 7

FÕÒŃÔPŎÑŇÒPÌ Groovy MŒPÑǾPÒPÕÑŒPŘÕÑ dynamic jvm language power features from Ruby, Smalltalk, Python et. al. reducing Java boilerplate code DSL friendliness with closures seamless integration with Java libraries and classes 8

FÕÒŃÔPŎÑŇÒPÌ Prerequisites MŒPÑǾPÒPÕÑŒPŘÕÑ contracts are specified applying annotations Groovy supports closures def c = { println "hello world" } c.call() Annotation Closures only syntactically supported in Groovy 1.7 officially supported in Groovy 1.8 @RunIf({ jdkversion >= 6 }) 9

FÕÒŃÔPŎÑŇÒPÌ MŒPÑǾPÒPÕÑŒPŘÕÑ Demo 10

FÕÒŃÔPŎÑŇÒPÌ Internals MŒPÑǾPÒPÕÑŒPŘÕÑ Groovy AST Transformation modifies the abstract syntax tree (AST) during compilation runs Uses closures as annotation arguments only syntactically supported in Groovy 1.7 officially supported in Groovy 1.8 Compiles annotation closures to closure classes References compiled closure classes in annotation args Groovy AST Browser shows the outcome! 11

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ 1.2.1 Class Invariants 12

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ 1.2.1 Contract Inheritance import org.gcontracts.annotations.* abstract class Stack { protected internal = [] } @Requires({ item!= null &&!contains(item) }) @Ensures({ last() == item }) def push(def item) { internal << item } //... class StackImpl extends Stack { //... } 13

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ 1.2.1 Contract Inheritance Precondition gets weakened Postcondition gets strengthened 14

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ 1.2.1 Interface Contracts import org.gcontracts.annotations.* interface Stack { @Requires({ item!= null &&!contains(item) }) @Ensures({ last() == item }) def push(def item) @Requires({!isEmpty() }) def pop() } def last() boolean isempty() boolean contains(def item) 15

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ 1.2.1 Labels @Requires({ not_empty:!isempty() }) @Ensures({ not_full:!isfull() descrease_counter: old.count == count - 1 }) 16

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ 1.2.1 Module gcontracts-grails Spring Integration import org.gcontracts.annotations.* import org.springframework.stereotype.* @Component @Invariant({ property?.size() > 0 }) class MySpringBean { protected property } def MySpringBean(def somevalue) { property = somevalue } 17

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ 1.2.1 Module gcontracts-doc GroovyDoc extension 18

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ 1.2.1 Reusing Contracts: Annotation Contracts import org.gcontracts.annotations.meta.* import java.lang.annotation.* @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) @Precondition @AnnotationContract({ it!= null }) public @interface NotNull {} 19

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ 1.2.1 Reusing Contracts: Annotation Contracts class BankAccount { @Requires({ amount > 0.0 }) BigDecimal withdraw(@notnull BigDecimal amount) { if (balance >= amount) { balance -= amount; return amount; } else // Withdrawal not allowed return 0.0; } //... 20

FÕÒŃÔPŎÑŇÒPÌ GContracts MŒPÑǾPÒPÕÑŒPŘÕÑ 1.2.1 More... VM Args to Enable/Disable Contract Checking Domain Model Custom Annotation Processors Cyclic Call Detection Gradle Multi-Build Project... 21

FÕÒŃÔPŎÑŇÒPÌ IDE Support MŒPÑǾPÒPÕÑŒPŘÕÑ IntelliJ Groovy Eclipse Plugin Snapshot for Groovy 1.8 includes support for annotation closures 22

MythBusters FÕ Ò ŃÔ PŎ ÑŇÒ P ÌM Œ PÑǾ PÒ PÕ Ñ Œ PŘÕ Ñ Don t know how to apply contracts in my application! My Methods are too complicated for contracts! I write unit tests, let s go bowling! 23

FÕÒŃÔPŎÑŇÒPÌ Where MŒPÑǾPÒPÕÑŒPŘÕÑ to get it? BSD license Github project (source/binaries) Available in the Central Maven Repo @Grab( group='org.gcontracts', module='gcontracts-core', version='[1.2.1,)' ) Ivy Maven Gradle Lighthouse Issue Tracking Feel free to contribute! Follow @gcontracts 24

FÕÒŃÔPŎÑŇÒPÌ Any Questions? MŒPÑǾPÒPÕÑŒPŘÕÑ 25

FÕÒŃÔPŎÑŇÒPÌ MŒPÑǾPÒPÕÑŒPŘÕÑ Thank you! me@andresteingress.com @sternegross @gcontracts 26