/******************************************************************************* * Copyright (c) 2010 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.databoard.tests; import java.util.HashMap; import java.util.Map; import junit.framework.TestCase; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.simantics.databoard.Datatypes; import org.simantics.databoard.annotations.Length; import org.simantics.databoard.annotations.MIMEType; import org.simantics.databoard.annotations.Optional; import org.simantics.databoard.annotations.Pattern; import org.simantics.databoard.annotations.Range; import org.simantics.databoard.annotations.Union; import org.simantics.databoard.annotations.Unit; import org.simantics.databoard.parser.repository.DataTypeRepository; import org.simantics.databoard.parser.repository.DataTypeSyntaxError; import org.simantics.databoard.type.Component; import org.simantics.databoard.type.Datatype; import org.simantics.databoard.type.RecordType; public class ParserSuccessTests extends TestCase { DataTypeRepository repository; Map reference; @Before public void setUp() { repository = new DataTypeRepository(); reference = new HashMap(); } public void printRepository() { for(String name : repository.getTypeNames()) { Datatype type = repository.get(name); System.out.println(name + " = " + type + " (" + type.getClass() + ")"); } } @After public void compare() { for(String name : reference.keySet()) if(!repository.getTypeNames().contains(name)) fail("Type " + name + " is not in repository."); for(String name : repository.getTypeNames()) if(!reference.containsKey(name)) fail("Type " + name + " should not be in the repository."); for(String name : repository.getTypeNames()) assertEquals("Problem with type " + name + ".", reference.get(name), repository.get(name)); } @Test public void testBuiltins() throws DataTypeSyntaxError { repository.addDefinitions( "type MyBoolean = Boolean " + "type MyByte = Byte " + "type MyInteger = Integer " + "type MyLong = Long " + "type MyFloat = Float " + "type MyDouble = Double " + "type MyString = String " ); reference.put("MyBoolean", Datatypes.getDatatypeUnchecked(Boolean.class)); reference.put("MyByte", Datatypes.getDatatypeUnchecked(Byte.class)); reference.put("MyInteger", Datatypes.getDatatypeUnchecked(Integer.class)); reference.put("MyFloat", Datatypes.getDatatypeUnchecked(Float.class)); reference.put("MyDouble", Datatypes.getDatatypeUnchecked(Double.class)); reference.put("MyLong", Datatypes.getDatatypeUnchecked(Long.class)); reference.put("MyString", Datatypes.getDatatypeUnchecked(String.class)); } @Test public void testArray() throws DataTypeSyntaxError { repository.addDefinitions( "type Array1 = Integer[] " + "type Array2 = String[][] " ); reference.put("Array1", Datatypes.getDatatypeUnchecked(int[].class)); reference.put("Array2", Datatypes.getDatatypeUnchecked(String[][].class)); } static class Optional1 { @Optional Integer a; @Optional float[] b; } @Test public void testOptional() throws DataTypeSyntaxError { repository.addDefinitions( "type Optional1 = {" + "a : Optional(Integer)," + "b : Optional(Float[])" + "}" ); reference.put("Optional1", Datatypes.getDatatypeUnchecked(Optional1.class)); } static class Record1 { } static class Record2 { String a; Integer b; } @Test public void testRecord() throws DataTypeSyntaxError { repository.addDefinitions( "type Record1 = {} " + "type Record2 = { a : String, b : Integer } " ); reference.put("Record1", Datatypes.getDatatypeUnchecked(Record1.class)); reference.put("Record2", Datatypes.getDatatypeUnchecked(Record2.class)); } public Datatype tuple(Datatype ... types) { RecordType tuple = new RecordType(); Component[] components = new Component[types.length]; for(int i=0;i