]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/TestValidator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / TestValidator.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.tests;
13
14 import junit.framework.TestCase;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.databoard.annotations.Length;
18 import org.simantics.databoard.annotations.Optional;
19 import org.simantics.databoard.annotations.Pattern;
20 import org.simantics.databoard.annotations.Range;
21 import org.simantics.databoard.binding.Binding;
22 import org.simantics.databoard.binding.error.BindingConstructionException;
23 import org.simantics.databoard.binding.error.BindingException;
24
25 public class TestValidator extends TestCase {
26
27         public enum Test { Key1, Key2 }
28         
29         public static class Luokka {
30                 public @Range("[0..100]") Integer percent = 0;
31                 public @Optional Integer optionalValue;
32                 public Boolean booleanValue = true;
33                 public @Length("7") @Range("[1..39]") int[] lottoNumbers = new int[] {1,2,3,4,5,6,7};
34                 public @Length("10") @Pattern("0x\\d{8}") String hexNumber = "0x11223344"; 
35         }
36         
37         public static Binding b = Bindings.getBindingUnchecked(Luokka.class);
38         public Luokka l;
39         
40         public void setUp() throws Exception {
41                 l = new Luokka();
42         }
43         
44         public void testNumberType() throws Exception {
45                 
46                 l.percent = 100; 
47                 b.assertInstaceIsValid( l );
48                 
49                 l.percent = 0;
50                 b.assertInstaceIsValid( l );
51                 
52                 try {
53                         l.percent = -1;
54                         b.assertInstaceIsValid( -1 );
55                         fail();
56                 } catch ( BindingException e ) {}
57                 
58                 try {
59                         l.percent = 101;
60                         b.assertInstaceIsValid( 101 );
61                         fail();
62                 } catch ( BindingException e ) {}
63
64                 try {
65                         b.assertInstaceIsValid( new Integer(5) );
66                         fail();
67                 } catch ( BindingException e ) {}               
68                 
69         }
70         
71         public void testOptionalType() throws BindingException {
72                 l.optionalValue = 5;
73                 b.assertInstaceIsValid( l );
74                 
75                 try {
76                         l.percent = null;
77                         b.assertInstaceIsValid( l );
78                         fail();
79                 } catch ( BindingException e ) {}                               
80         }
81         
82         public void testBooleanType() throws BindingException {
83                 b.assertInstaceIsValid( l );
84                 
85                 try {
86                         b.assertInstaceIsValid( new Integer(5) );
87                         fail();
88                 } catch ( BindingException e ) {}                               
89                 
90         }
91         
92         public void testStringType() throws BindingException {
93                 b.assertInstaceIsValid( l );
94                 
95                 try {
96                         // not according to pattern
97                         l.hexNumber = "Hello World";
98                         b.assertInstaceIsValid( l );
99                         fail();
100                 } catch ( BindingException e ) {}
101                 
102                 try {
103                         // Too short
104                         l.hexNumber = "0x100";
105                         b.assertInstaceIsValid( l );
106                         fail();
107                 } catch ( BindingException e ) {}                               
108                 
109         }
110         
111         public void testRecordType() throws BindingException {
112                 b.assertInstaceIsValid( l );
113                 // Already thoroughly tested by all other tests
114         }
115         
116         public void testUnionType() throws BindingException, BindingConstructionException {
117                 Binding eb = Bindings.getBinding( Test.class );
118                 eb.assertInstaceIsValid(Test.Key1);
119                 eb.assertInstaceIsValid(Test.Key2);
120                 try {
121                         b.assertInstaceIsValid( new Integer(5) );
122                         fail();
123                 } catch ( BindingException e ) {}                               
124                 
125         }
126         
127         public void testArrayType() throws BindingException {
128                 b.assertInstaceIsValid( l );
129                                 
130                 try {
131                         // Too many numbers
132                         l.lottoNumbers = new int[] {1,2,3,4,5,6,7, 10, 10, 10, 10};
133                         b.assertInstaceIsValid( l );
134                         fail();
135                 } catch ( BindingException e ) {}                               
136
137                 try {
138                         // Not enough numbers
139                         l.lottoNumbers = new int[] {1, 2, 3};
140                         b.assertInstaceIsValid( l );
141                         fail();
142                 } catch ( BindingException e ) {}                               
143                                 
144                 try {
145                         // not with in 1..39
146                         l.lottoNumbers = new int[] {100,-2,3,4,5,6,7};
147                         b.assertInstaceIsValid( l );
148                         fail();
149                 } catch ( BindingException e ) {}
150
151         }
152         
153 }
154