1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.tests;
14 import java.util.HashMap;
17 import junit.framework.TestCase;
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;
37 public class ParserSuccessTests extends TestCase {
39 DataTypeRepository repository;
40 Map<String, Datatype> reference;
44 repository = new DataTypeRepository();
45 reference = new HashMap<String, Datatype>();
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() + ")");
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));
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 "
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));
90 public void testArray() throws DataTypeSyntaxError {
91 repository.addDefinitions(
92 "type Array1 = Integer[] " +
93 "type Array2 = String[][] "
95 reference.put("Array1", Datatypes.getDatatypeUnchecked(int[].class));
96 reference.put("Array2", Datatypes.getDatatypeUnchecked(String[][].class));
99 static class Optional1 {
105 public void testOptional() throws DataTypeSyntaxError {
106 repository.addDefinitions(
107 "type Optional1 = {" +
108 "a : Optional(Integer)," +
109 "b : Optional(Float[])" +
112 reference.put("Optional1", Datatypes.getDatatypeUnchecked(Optional1.class));
115 static class Record1 {
118 static class Record2 {
124 public void testRecord() throws DataTypeSyntaxError {
125 repository.addDefinitions(
126 "type Record1 = {} " +
127 "type Record2 = { a : String, b : Integer } "
129 reference.put("Record1", Datatypes.getDatatypeUnchecked(Record1.class));
130 reference.put("Record2", Datatypes.getDatatypeUnchecked(Record2.class));
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);
143 public void testTuple() throws DataTypeSyntaxError {
144 repository.addDefinitions(
145 "type Tuple1 = () " +
146 "type Tuple2 = (Integer, String) " +
147 "type NotTuple = (Double) "
149 reference.put("Tuple1", Datatypes.getDatatypeUnchecked(Record1.class));
150 reference.put("Tuple2", tuple(
151 Datatypes.getDatatypeUnchecked(Integer.class),
152 Datatypes.getDatatypeUnchecked(String.class)
154 reference.put("NotTuple", Datatypes.getDatatypeUnchecked(Double.class));
157 @Union({A.class, /*B.class,*/ C.class})
161 static class A implements Union1 {}
162 static class B implements Union1 { int value; }
163 static class C implements Union1 { float x; int y; }
166 public void testUnion() throws DataTypeSyntaxError {
167 repository.addDefinitions(
168 "type Union1 = | A " +
169 "/* | B Integer */" +
170 " | C { x : Float, y : Integer } "
172 reference.put("Union1", Datatypes.getDatatypeUnchecked(Union1.class));
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 "
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));
198 public void testRecursion() throws DataTypeSyntaxError {
199 repository.addDefinitions(
200 "type Rec1 = { ref1 : Rec2[] } " +
201 "type Rec2 = { ref2 : Optional(Rec1) } "
203 reference.put("Rec1", Datatypes.getDatatypeUnchecked(Rec1.class));
204 reference.put("Rec2", Datatypes.getDatatypeUnchecked(Rec2.class));
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;
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}\")" +
226 reference.put("BuiltinAnnotations",
227 Datatypes.getDatatypeUnchecked(BuiltinAnnotations.class));
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;
238 public void testArrayAnnotations() throws DataTypeSyntaxError {
239 repository.addDefinitions(
240 "type ArrayAnnotations = {" +
243 "c : String[1..3]," +
247 reference.put("ArrayAnnotations",
248 Datatypes.getDatatypeUnchecked(ArrayAnnotations.class));