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.Arrays;
15 import java.util.HashMap;
17 import junit.framework.TestCase;
19 import org.junit.Test;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.databoard.annotations.Union;
22 import org.simantics.databoard.binding.Binding;
23 import org.simantics.databoard.binding.impl.HashMapBinding;
24 import org.simantics.databoard.binding.impl.OptionalBindingDefault;
25 import org.simantics.databoard.parser.repository.DataValueRepository;
27 public class ValueParserSuccessTests extends TestCase {
29 public Object parse(Binding binding, String text) throws Exception {
30 return new DataValueRepository().translate(text, binding);
34 public void testPrimitives() throws Exception {
35 assertEquals(Boolean.TRUE, parse(Bindings.BOOLEAN, "true"));
36 assertEquals(Boolean.FALSE, parse(Bindings.BOOLEAN, "false"));
38 assertEquals(123, parse(Bindings.INTEGER, "123"));
39 assertEquals(-234234, parse(Bindings.INTEGER, "-234234"));
41 assertEquals(12345678912345L, parse(Bindings.LONG, "12345678912345"));
43 assertEquals(1.2345, parse(Bindings.DOUBLE, "1.2345"));
44 assertEquals(-3.0, parse(Bindings.DOUBLE, "-3"));
45 assertEquals(1e-10, parse(Bindings.DOUBLE, "1e-10"));
47 assertEquals(2.3f, parse(Bindings.FLOAT, "2.3"));
49 assertEquals("Hello World!", parse(Bindings.STRING, "\"Hello World!\""));
50 assertEquals("\n\t\r\\\"", parse(Bindings.STRING, "\"\\n\\t\\r\\\\\\\"\""));
51 assertEquals("This\ntext\nspans\nmultiple\nlines.",
52 parse(Bindings.STRING, "\"\"\"This\ntext\nspans\nmultiple\nlines.\"\"\""));
53 assertEquals("\u0008", parse(Bindings.STRING, "\"\b\""));
56 public void assertArraysEqual(int[] expected, int[] actual) {
57 if(!Arrays.equals(expected, actual))
58 failNotEquals(null, expected, actual);
61 public void testArray() throws Exception {
62 assertArraysEqual(new int[] {1,2,3}, (int[])parse(Bindings.INT_ARRAY, "[1,2,3]"));
65 public void testOptional() throws Exception {
67 (Integer)parse(new OptionalBindingDefault(Bindings.INTEGER), "null"));
68 assertEquals((Integer)1,
69 (Integer)parse(new OptionalBindingDefault(Bindings.INTEGER), "1"));
72 static public class MyRecord {
76 public MyRecord(int x, String y) {
82 public void testRecord() throws Exception {
83 MyRecord record = (MyRecord)parse(Bindings.getBinding(MyRecord.class), "{ y = \"asd\", x = 5 }");
84 assertEquals(5, record.x);
85 assertEquals("asd", record.y);
88 public static class MyRecord2 {
93 public void testRecord2() throws Exception {
94 MyRecord2 record = (MyRecord2)parse(Bindings.getBinding(MyRecord2.class), "{ y = \"asd\", x = 5 }");
95 assertEquals(5, record.x);
96 assertEquals("asd", record.y);
99 public void testTuple() throws Exception {
100 MyRecord record = (MyRecord)parse(Bindings.getBinding(MyRecord.class), "( 5, \"asd\")");
101 assertEquals(5, record.x);
102 assertEquals("asd", record.y);
105 @Union({A.class, C.class})
109 static public class A implements Union1 {}
110 static public class C implements Union1 {
113 public C(float x, int y) {
119 public void testUnion() throws Exception {
120 Union1 union = (Union1)parse(Bindings.getBinding(Union1.class), "C (1.2, 3)");
121 assertTrue(union instanceof C);
122 assertEquals(1.2f, ((C)union).x);
123 assertEquals(3, ((C)union).y);
126 @SuppressWarnings("rawtypes")
127 public void testMap() throws Exception {
128 HashMap map = (HashMap)parse(
130 Bindings.getBinding(String.class),
131 Bindings.getBinding(Integer.class)
133 "map { \"1\" = 1, \"2\" = 2 }");
134 assertEquals(2, map.size());
135 assertEquals(1, map.get("1"));
136 assertEquals(2, map.get("2"));