Annotations and Input Validation in Java - SHIP Validatorii.uib.no/~dagh/validatorflyer.pdf ·...
Transcript of Annotations and Input Validation in Java - SHIP Validatorii.uib.no/~dagh/validatorflyer.pdf ·...
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Annotations and Input Validation in JavaSHIP Validator
Dag Hovland. Based on joint work with Khalid A. Mughal andFederico Mancini.
July 17, 2009
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Premises
Annotations
Property-TestsThe Validation Process
Dynamic Typing
Handling null Values
Cross-Tests
Boolean Composition of TestsComposed Property-TestsComposed Cross-Tests
Error Messages
Related Work
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Information as Properties
I Java is Object-Oriented — everything is an object
I Information available in no-arg methods: properties
I Input is represented as properties of an object
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Goals
I A framework that should be easy to add onto existing projects
I It should be easy to use and create tests
I Tests should be reusable and composable
I Composition of tests should be easy and powerful
I No code inside methods in the class representing input
I No references to code by string names
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Annotations
I From Java 5.0
I Standardizing comments
I A kind of interface
I On classes, methods, fields, variables
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
The Validation Process
Scenario: International Money Transfer
IBAN
BIC BICCODE
Account
Clearing-code AB1232342
Amount € 10000 . 10 c
Pay international bill
Insert title here file:///scratch/workspace/Validation/tex/form.html
1 of 1 02/06/09 21:34
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
The Validation Process
Configuration by Proximity
@ValidateBICpublic String getBIC() { return BIC; }
@IntRange(min=0,max=10000)public Integer getAmountEuro() { return amountEuro; }
@IntRange(min=0,max=99)
public Integer getAmountCents() { return amountCents; }
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
The Validation Process
Using the Framework
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
The Validation Process
@Validationpublic @interface IntRange {int min();int max();public static class Testerimplements IPropertyTester<IntRange, Integer> {public boolean runTest(IntRange r, Integer v) {return(v >= r.min() && v <= r.max());
}}
}
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
The Validation Process
Interface for Property-Tests
public interface IPropertyTester <A extends Annotation,I>{public boolean runTest(A an, I o)throws ValidationException;
}
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Input Validation as Dynamic Typing
I validate(o) similar to down-casting in Java
I Why cannot Java’s type hierarchy be used?
I String and Integer are final
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
@Required and @NotRequired
I But how should tests handle null values?
I They should, by default, lead to a crash
I However, null is also used to signal special cases
I @Required specifies that a property should not be null —almost standard
I @NotRequired causes no other tests to be run, if the value isnull
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Cross-Tests over Multiple Properties
I Property-Tests test a single property
I Cross-Tests test several properties at once
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Scenario: International Money Transfer
IBAN
BIC BICCODE
Account
Clearing-code AB1232342
Amount € 10000 . 10 c
Pay international bill
Insert title here file:///scratch/workspace/Validation/tex/form.html
1 of 1 02/06/09 21:34
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Using Cross-Tests
@ExactlyOneNull
@NotRequired
public String getIBAN() { return IBAN; }
@ExactlyOneNull
@AllOrNoneNull
@NotRequired
public String getAccount() { return account; }
@AllOrNoneNull
@NotRequired
public String getClearingcode() { return clearingcode; }
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Declaring a Cross-Test
@CrossValidationpublic @interface AllOrNoneNull {public static class Testerimplements ICrossTester<AllOrNoneNull,String>{public boolean runTest(AllOrNoneNull c,
ArrayList<String> v)throws ValidationException {...
}}
}
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
public interface ICrossTester <A extends Annotation, V> {public boolean runTest(A a, ArrayList<V> v)throws ValidationException;
}
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Composed Property-TestsComposed Cross-Tests
Basic and Composed Tests
I Basic Tests contain an inner class implementing a test
I Composed Tests are annotated with property-annotations
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Composed Property-TestsComposed Cross-Tests
Property-Tests
@ValidateBICpublic String getBIC() { return BIC; }
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Composed Property-TestsComposed Cross-Tests
Example of Composed Property-Test
@Validation@BoolTest(BoolType.AND)@PatMatch("\\w8|\\w11")@AdditionalTest
@interface ValidateBIC{}
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Composed Property-TestsComposed Cross-Tests
Boolean Composition of Tests
I Annotation @BoolTest
I public enum BoolType{OR, AND, ALLFALSE}
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Composed Property-TestsComposed Cross-Tests
Types of Tests
Basic Tests Composed Tests
Property-Tests @IntRange @ValidateBICCross-Tests @AllOrNoneNull @AmountCheck
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Composed Property-TestsComposed Cross-Tests
Composed Cross-Tests
@IntRange(min=0,max=10000)@AmountCheckpublic Integer getAmountEuro() { return amountEuro; }
@IntRange(min=0,max=99)@AmountCheck
public Integer getAmountCents() { return amountCents; }
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Composed Property-TestsComposed Cross-Tests
Declaring Composed Cross-Tests
// MaxAmount declaration@CrossValidation@BoolTest(BoolType.OR)@OneLessThan(1)@AllLessThan(10000)public @interface MaxAmount {}
//AmountCheck declaration@CrossValidation@BoolTest(BoolType.AND)@SumMin(1)@MaxAmountpublic @interface AmountCheck {}
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Composed Property-TestsComposed Cross-Tests
@AmountCheck
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
Custom Error Message
@Validation@BoolTest(BoolType.AND)@PatMatch("\\w8|\\w11")@AdditionalTest@ValErr(message="Invalid BIC")
public @interface ValidateBIC{}
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
The value "BICCODE" returned by method "getBIC" has notpassed the following property-test:+Test: @ValidateBIC()(Error Message: Invalid BIC) because:|-+Test: @PatMatch(value=\w8|\w11)
================================================The following cross-tests have failed:+Test: @AmountCheck() because:|-+Test: @MaxAmount() because:|-+Test: @OneLessThan(value=1)|-+Test: @AllLessThan(value=10000)
+Test: @AllOrNoneNull()+Test: @ExactlyOneNull() because:|-+Test: @ExactlyNNull(value=1)
================================================
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java
OverviewPremises
AnnotationsProperty-Tests
Dynamic TypingHandling null Values
Cross-TestsBoolean Composition of Tests
Error MessagesRelated Work
I Heimdall
I Struts 2
I Java Specification Request (JSR) 303: Bean Validation
I Hibernate
I Hookom and Holmgren
http://shipvalidator.sourceforge.net
Dag Hovland. Based on joint work with Khalid A. Mughal and Federico Mancini.Annotations and Input Validation in Java