]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/TestReflectionBinding.java
Update SCL-editor annotations asynchronously
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / TestReflectionBinding.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.io.IOException;
15
16 import junit.framework.TestCase;
17
18 import org.simantics.databoard.Bindings;
19 import org.simantics.databoard.Datatypes;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.binding.RecordBinding;
22 import org.simantics.databoard.binding.UnionBinding;
23 import org.simantics.databoard.binding.error.BindingException;
24 import org.simantics.databoard.serialization.SerializationException;
25 import org.simantics.databoard.serialization.Serializer;
26 import org.simantics.databoard.type.Datatype;
27 import org.simantics.databoard.type.RecordType;
28 import org.simantics.databoard.type.UnionType;
29
30 public class TestReflectionBinding extends TestCase {
31     
32     static public enum TestEnum {
33         Value1, Value2, Value3
34     }
35     
36     static public class TestClass {
37         public TestEnum e0;
38     }
39     
40     public void testEnumeration() throws SerializationException, BindingException, IOException {
41         // Test DataType
42         Datatype dt = Datatypes.getDatatypeUnchecked(TestClass.class);
43         RecordType rt = (RecordType) dt;
44         UnionType ut = (UnionType) rt.getComponentType(0);        
45         assertEquals("Value1", ut.components[0].name);
46         assertEquals("Value2", ut.components[1].name);
47         assertEquals("Value3", ut.components[2].name);        
48
49         // Test Binding
50         Binding b = Bindings.getBindingUnchecked(TestClass.class);
51         RecordBinding rb = (RecordBinding) b;
52         UnionBinding ub = (UnionBinding) rb.componentBindings[0];
53         assertEquals(ub.type(), ut);
54         
55         // TODO Test serialization
56         TestClass tc = new TestClass();
57         tc.e0 = TestEnum.Value3;        
58         Serializer s = b.serializer();
59         byte[] data = s.serialize(tc);
60         assertEquals(1, data.length);
61         tc = (TestClass) s.deserialize(data);
62         assertEquals(TestEnum.Value3, tc.e0);
63     }
64
65 }
66