]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/ParserSuccessTests.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / ParserSuccessTests.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 java.util.HashMap;
15 import java.util.Map;
16
17 import junit.framework.TestCase;
18
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.simantics.databoard.Datatypes;
23 import org.simantics.databoard.annotations.Length;
24 import org.simantics.databoard.annotations.MIMEType;
25 import org.simantics.databoard.annotations.Optional;
26 import org.simantics.databoard.annotations.Pattern;
27 import org.simantics.databoard.annotations.Range;
28 import org.simantics.databoard.annotations.Union;
29 import org.simantics.databoard.annotations.Unit;
30 import org.simantics.databoard.parser.repository.DataTypeRepository;
31 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
32 import org.simantics.databoard.type.Component;
33 import org.simantics.databoard.type.Datatype;
34 import org.simantics.databoard.type.RecordType;
35
36
37 public class ParserSuccessTests extends TestCase {
38
39         DataTypeRepository repository;
40         Map<String, Datatype> reference;
41         
42         @Before
43         public void setUp() {
44                 repository = new DataTypeRepository();
45                 reference = new HashMap<String, Datatype>();
46         }       
47         
48         public void printRepository() {
49                 for(String name : repository.getTypeNames()) {
50                         Datatype type = repository.get(name);
51                         System.out.println(name + " = " + type + " ("
52                                         + type.getClass() + ")");
53                 }       
54         }
55         
56         @After
57         public void compare() {
58                 for(String name : reference.keySet())
59                         if(!repository.getTypeNames().contains(name))
60                                 fail("Type " + name + " is not in repository.");
61                 for(String name : repository.getTypeNames())
62                         if(!reference.containsKey(name))
63                                 fail("Type " + name + " should not be in the repository.");
64                 for(String name : repository.getTypeNames())
65                         assertEquals("Problem with type " + name + ".",
66                                         reference.get(name), repository.get(name));
67         }               
68
69         @Test
70         public void testBuiltins() throws DataTypeSyntaxError {
71                 repository.addDefinitions(
72                                 "type MyBoolean = Boolean " +
73                                 "type MyByte = Byte " +
74                                 "type MyInteger = Integer " +                           
75                                 "type MyLong = Long " +
76                                 "type MyFloat = Float " +
77                                 "type MyDouble = Double " +
78                                 "type MyString = String "
79                                 );
80                 reference.put("MyBoolean", Datatypes.getDatatypeUnchecked(Boolean.class));
81                 reference.put("MyByte", Datatypes.getDatatypeUnchecked(Byte.class));
82                 reference.put("MyInteger", Datatypes.getDatatypeUnchecked(Integer.class));
83                 reference.put("MyFloat", Datatypes.getDatatypeUnchecked(Float.class));
84                 reference.put("MyDouble", Datatypes.getDatatypeUnchecked(Double.class));
85                 reference.put("MyLong", Datatypes.getDatatypeUnchecked(Long.class));
86                 reference.put("MyString", Datatypes.getDatatypeUnchecked(String.class));
87         }
88         
89         @Test
90         public void testArray() throws DataTypeSyntaxError {
91                 repository.addDefinitions(
92                                 "type Array1 = Integer[] " +
93                                 "type Array2 = String[][] " 
94                                 );
95                 reference.put("Array1", Datatypes.getDatatypeUnchecked(int[].class));           
96                 reference.put("Array2", Datatypes.getDatatypeUnchecked(String[][].class));
97         }
98         
99         static class Optional1 {
100                 @Optional Integer a;
101                 @Optional float[] b;
102         }
103         
104         @Test
105         public void testOptional() throws DataTypeSyntaxError {
106                 repository.addDefinitions(
107                                 "type Optional1 = {" +
108                                 "a : Optional(Integer)," +
109                                 "b : Optional(Float[])" +
110                                 "}"
111                                 );
112                 reference.put("Optional1", Datatypes.getDatatypeUnchecked(Optional1.class));            
113         }
114         
115         static class Record1 {          
116         }
117         
118         static class Record2 {  
119                 String a;
120                 Integer b;
121         }
122         
123         @Test
124         public void testRecord() throws DataTypeSyntaxError {
125                 repository.addDefinitions(
126                                 "type Record1 = {} " +
127                                 "type Record2 = { a : String, b : Integer } " 
128                                 );
129                 reference.put("Record1", Datatypes.getDatatypeUnchecked(Record1.class));                
130                 reference.put("Record2", Datatypes.getDatatypeUnchecked(Record2.class));
131         }
132         
133         public Datatype tuple(Datatype ... types) {
134                 RecordType tuple = new RecordType();
135                 Component[] components = new Component[types.length];
136                 for(int i=0;i<types.length;++i)
137                         components[i] = new Component(Integer.toString(i), types[i]);
138                 tuple.setComponents(components);
139                 return tuple;
140         }
141         
142         @Test
143         public void testTuple() throws DataTypeSyntaxError {
144                 repository.addDefinitions(
145                                 "type Tuple1 = () " +
146                                 "type Tuple2 = (Integer, String) " +
147                                 "type NotTuple = (Double) "                             
148                                 );
149                 reference.put("Tuple1", Datatypes.getDatatypeUnchecked(Record1.class));
150                 reference.put("Tuple2", tuple(
151                                 Datatypes.getDatatypeUnchecked(Integer.class),
152                                 Datatypes.getDatatypeUnchecked(String.class)
153                                 ));
154                 reference.put("NotTuple", Datatypes.getDatatypeUnchecked(Double.class));        
155         }
156         
157         @Union({A.class, /*B.class,*/ C.class})
158         interface Union1 {              
159         }
160         
161         static class A implements Union1 {}
162         static class B implements Union1 { int value; }
163         static class C implements Union1 { float x; int y; }
164         
165         @Test
166         public void testUnion() throws DataTypeSyntaxError {
167                 repository.addDefinitions(
168                                 "type Union1 = | A " +
169                                 "/*             | B Integer */" +
170                                 "             | C { x : Float, y : Integer } " 
171                                 );
172                 reference.put("Union1", Datatypes.getDatatypeUnchecked(Union1.class));
173         }
174         
175         @Test
176         public void testReferences() throws DataTypeSyntaxError {
177                 repository.addDefinitions(
178                                 "type Ref1 = String " +
179                                 "type Ref2 = { a : Ref1, b : Ref3 } " +
180                                 "type Ref3 = Ref4 " + 
181                                 "type Ref4 = Integer "                           
182                                 );
183                 reference.put("Ref1", Datatypes.getDatatypeUnchecked(String.class));
184                 reference.put("Ref2", Datatypes.getDatatypeUnchecked(Record2.class));
185                 reference.put("Ref3", Datatypes.getDatatypeUnchecked(Integer.class));
186                 reference.put("Ref4", Datatypes.getDatatypeUnchecked(Integer.class));           
187         }
188         
189         static class Rec1 {
190                 Rec2[] ref1;
191         }
192         
193         static class Rec2 {
194                 @Optional Rec1 ref2;
195         }
196         
197         @Test
198         public void testRecursion() throws DataTypeSyntaxError {
199                 repository.addDefinitions(
200                                 "type Rec1 = { ref1 : Rec2[] } " + 
201                                 "type Rec2 = { ref2 : Optional(Rec1) } "
202                                 );
203                 reference.put("Rec1", Datatypes.getDatatypeUnchecked(Rec1.class));
204                 reference.put("Rec2", Datatypes.getDatatypeUnchecked(Rec2.class));              
205         }
206         
207         static class BuiltinAnnotations {
208                 @Range("[-9..10]") @Unit("foo") int a;
209                 @Range("[0..3453453453345]") @Unit("foo") long b;
210                 @Range("[-4.3..10]") @Unit("foo") float c;
211                 @Range("[0..10.3]") @Unit("foo") double d;
212                 @Length("0") @MIMEType("foo") @Pattern("\\S{2,9}") String e;
213         }
214         
215         @Test
216         public void testBuiltinAnnotations() throws DataTypeSyntaxError {
217                 repository.addDefinitions(
218                                 "type BuiltinAnnotations = {" +
219                                 "a : Integer(range=[-9..10], unit=\"foo\")," +
220                                 "b : Long(range=[0..3453453453345], unit=\"foo\")," +   
221                                 "c : Float(range=[-4.3..10], unit=\"foo\")," +  
222                                 "d : Double(range=[0..10.3], unit=\"foo\")," +  
223                                 "e : String(length=[0..10], mimeType=\"foo\", pattern=\"\\\\S{2,9}\")" +        
224                                 "}"
225                                 );
226                 reference.put("BuiltinAnnotations", 
227                                 Datatypes.getDatatypeUnchecked(BuiltinAnnotations.class));              
228         }
229         
230         static class ArrayAnnotations {
231                 @Length("[2..]") float[] a;             
232                 @Length("[2..]") double[] b;            
233                 @Length("[1..3]") String[] c;
234                 @Length("2") byte[] d;
235         }
236         
237         @Test
238         public void testArrayAnnotations() throws DataTypeSyntaxError {
239                 repository.addDefinitions(
240                                 "type ArrayAnnotations = {" +
241                                 "a : Float[2..]," +
242                                 "b : Double[..2]," +
243                                 "c : String[1..3]," +
244                                 "d : Byte[2]" +
245                                 "}"
246                                 );
247                 reference.put("ArrayAnnotations", 
248                                 Datatypes.getDatatypeUnchecked(ArrayAnnotations.class));                
249         }
250 }