]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/TestClone.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / TestClone.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 static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertTrue;
16
17 import java.util.Random;
18
19 import org.junit.Test;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.databoard.binding.ArrayBinding;
22 import org.simantics.databoard.binding.Binding;
23 import org.simantics.databoard.binding.MapBinding;
24 import org.simantics.databoard.binding.OptionalBinding;
25 import org.simantics.databoard.binding.RecordBinding;
26 import org.simantics.databoard.binding.UnionBinding;
27 import org.simantics.databoard.binding.VariantBinding;
28 import org.simantics.databoard.binding.util.RandomValue;
29 import org.simantics.databoard.type.Datatype;
30
31 /**
32  * Test Clone is a test case that asserts that {@link Bindings#clone(Object, Binding, Binding)}
33  * returns two separate instances.
34  * 
35  * Also tests that adapt of same binding returns the same instance. 
36  *
37  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
38  */
39 public class TestClone {
40         
41         public @Test void testGenericBinding() throws Exception {               
42                 RandomValue rv = new RandomValue();
43                 rv.refereableRecords = true;
44                 for (int i=0; i<10000; i++) {
45                         rv.random = new Random(i);
46                         rv.random.nextLong();
47                         Datatype type = rv.randomType(0, 3);
48                         Binding binding = Bindings.getMutableBinding(type);
49                         Object o1 = binding.accept(rv);
50                         Object o2 = binding.clone(o1);
51                         System.out.println(i+": "+type);
52                         if (binding.equals(o1, o2)) continue;
53                         assertNotSameInstance(binding, o1, o2);
54                         
55                         Object o3 = Bindings.adapt(o1, binding, binding);
56                         assertTrue( o3 == o1 );
57                 }               
58         }
59         
60         public void assertNotSameInstance(Binding b, Object o1, Object o2) throws Exception {
61                 assertTrue(b.isImmutable() || o1 != o2);
62                 
63                 if (b instanceof RecordBinding) assertNotSameRecord((RecordBinding) b, o1, o2);
64                 if (b instanceof UnionBinding) assertNotSameUnion((UnionBinding) b, o1, o2);
65                 if (b instanceof ArrayBinding) assertNotSameArray((ArrayBinding) b, o1, o2);
66                 if (b instanceof MapBinding) assertNotSameMap((MapBinding) b, o1, o2);
67                 if (b instanceof OptionalBinding) assertNotSameOptional((OptionalBinding) b, o1, o2);
68                 if (b instanceof VariantBinding) assertNotSameVariant((VariantBinding) b, o1, o2);
69         }
70         
71         public void assertNotSameArray(ArrayBinding b, Object o1, Object o2) throws Exception {
72                 int count = b.size(o1);
73                 Binding cb = b.getComponentBinding();
74                 for (int i=0; i<count; i++) {
75                         Object v1 = b.get(o1, i);
76                         Object v2 = b.get(o2, i);
77                         assertNotSameInstance(cb, v1, v2);
78                 }
79         }
80
81         public void assertNotSameMap(MapBinding b, Object o1, Object o2) throws Exception {
82                 int count = b.size(o1);
83                 Binding vb = b.getValueBinding();
84                 Object values1[] = b.getValues(o1);  
85                 Object values2[] = b.getValues(o2);  
86                 for (int i=0; i<count; i++) {
87                         Object v1 = values1[i];
88                         Object v2 = values2[i];
89                         assertNotSameInstance(vb, v1, v2);
90                 }               
91         }
92
93         public void assertNotSameOptional(OptionalBinding b, Object o1, Object o2) throws Exception {
94                 Binding cb = b.getComponentBinding();
95                 Object v1 = b.getValue(o1);
96                 Object v2 = b.getValue(o2);
97                 assertNotSameInstance(cb, v1, v2);
98         }
99
100         public void assertNotSameVariant(VariantBinding b, Object o1, Object o2) throws Exception {
101                 Binding cb = b.getContentBinding(o1);
102                 Object v1 = b.getContent(o1);
103                 Object v2 = b.getContent(o2);
104                 assertNotSameInstance(cb, v1, v2);              
105         }
106
107         public void assertNotSameRecord(RecordBinding b, Object o1, Object o2) throws Exception {
108                 int count = b.getComponentCount();
109                 for (int i=0; i<count; i++) {
110                         Binding cb = b.getComponentBinding(i);
111                         Object v1 = b.getComponent(o1, i);
112                         Object v2 = b.getComponent(o2, i);
113                         assertNotSameInstance(cb, v1, v2);
114                 }       
115         }
116
117         public void assertNotSameUnion(UnionBinding b, Object o1, Object o2) throws Exception {
118                 int tag = b.getTag(o1);
119                 assertEquals(tag, b.getTag(o2));
120                 Binding cb = b.getComponentBinding(tag);
121                 Object v1 = b.getValue(o1);
122                 Object v2 = b.getValue(o2);             
123                 assertNotSameInstance(cb, v1, v2);
124         }
125
126 }
127