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.io.IOException;
16 import junit.framework.TestCase;
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;
30 public class TestReflectionBinding extends TestCase {
32 static public enum TestEnum {
33 Value1, Value2, Value3
36 static public class TestClass {
40 public void testEnumeration() throws SerializationException, BindingException, IOException {
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);
50 Binding b = Bindings.getBindingUnchecked(TestClass.class);
51 RecordBinding rb = (RecordBinding) b;
52 UnionBinding ub = (UnionBinding) rb.componentBindings[0];
53 assertEquals(ub.type(), ut);
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);