1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.tests;
14 import static org.junit.Assert.assertEquals;
\r
15 import static org.junit.Assert.assertFalse;
\r
16 import static org.junit.Assert.assertNull;
\r
17 import static org.junit.Assert.assertTrue;
\r
18 import gnu.trove.map.hash.TObjectIntHashMap;
\r
20 import java.io.File;
\r
21 import java.net.InetAddress;
\r
22 import java.util.HashMap;
\r
23 import java.util.LinkedList;
\r
24 import java.util.List;
\r
25 import java.util.Map;
\r
26 import java.util.Random;
\r
27 import java.util.TreeMap;
\r
28 import java.util.TreeSet;
\r
30 import org.junit.Before;
\r
31 import org.junit.Test;
\r
32 import org.simantics.databoard.Accessors;
\r
33 import org.simantics.databoard.Bindings;
\r
34 import org.simantics.databoard.Datatypes;
\r
35 import org.simantics.databoard.accessor.Accessor;
\r
36 import org.simantics.databoard.accessor.ArrayAccessor;
\r
37 import org.simantics.databoard.accessor.BooleanAccessor;
\r
38 import org.simantics.databoard.accessor.ByteAccessor;
\r
39 import org.simantics.databoard.accessor.DoubleAccessor;
\r
40 import org.simantics.databoard.accessor.FloatAccessor;
\r
41 import org.simantics.databoard.accessor.IntegerAccessor;
\r
42 import org.simantics.databoard.accessor.LongAccessor;
\r
43 import org.simantics.databoard.accessor.MapAccessor;
\r
44 import org.simantics.databoard.accessor.OptionalAccessor;
\r
45 import org.simantics.databoard.accessor.RecordAccessor;
\r
46 import org.simantics.databoard.accessor.StringAccessor;
\r
47 import org.simantics.databoard.accessor.UnionAccessor;
\r
48 import org.simantics.databoard.accessor.VariantAccessor;
\r
49 import org.simantics.databoard.accessor.error.AccessorException;
\r
50 import org.simantics.databoard.accessor.event.Event;
\r
51 import org.simantics.databoard.accessor.event.InvalidatedEvent;
\r
52 import org.simantics.databoard.accessor.file.FileVariantAccessor;
\r
53 import org.simantics.databoard.accessor.impl.ChangeSet;
\r
54 import org.simantics.databoard.accessor.impl.CompositeRecord;
\r
55 import org.simantics.databoard.accessor.impl.DirectoryMap;
\r
56 import org.simantics.databoard.accessor.impl.EventCollection;
\r
57 import org.simantics.databoard.accessor.interestset.InterestSet;
\r
58 import org.simantics.databoard.accessor.wire.WireClient;
\r
59 import org.simantics.databoard.accessor.wire.WireServer;
\r
60 import org.simantics.databoard.adapter.AdaptException;
\r
61 import org.simantics.databoard.binding.ArrayBinding;
\r
62 import org.simantics.databoard.binding.Binding;
\r
63 import org.simantics.databoard.binding.BooleanBinding;
\r
64 import org.simantics.databoard.binding.ByteBinding;
\r
65 import org.simantics.databoard.binding.DoubleBinding;
\r
66 import org.simantics.databoard.binding.FloatBinding;
\r
67 import org.simantics.databoard.binding.IntegerBinding;
\r
68 import org.simantics.databoard.binding.LongBinding;
\r
69 import org.simantics.databoard.binding.MapBinding;
\r
70 import org.simantics.databoard.binding.OptionalBinding;
\r
71 import org.simantics.databoard.binding.RecordBinding;
\r
72 import org.simantics.databoard.binding.StringBinding;
\r
73 import org.simantics.databoard.binding.UnionBinding;
\r
74 import org.simantics.databoard.binding.VariantBinding;
\r
75 import org.simantics.databoard.binding.factory.BindingScheme;
\r
76 import org.simantics.databoard.binding.factory.MutableBindingFactory;
\r
77 import org.simantics.databoard.binding.impl.ObjectArrayBinding;
\r
78 import org.simantics.databoard.binding.mutable.MutableVariant;
\r
79 import org.simantics.databoard.binding.util.RandomValue;
\r
80 import org.simantics.databoard.method.Client;
\r
81 import org.simantics.databoard.method.Server;
\r
82 import org.simantics.databoard.serialization.Serializer;
\r
83 import org.simantics.databoard.type.Datatype;
\r
84 import org.simantics.databoard.type.UnionType;
\r
85 import org.simantics.databoard.util.binary.BinaryMemory;
\r
98 * TODO Test {@link InvalidatedEvent}
100 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
102 public class TestAccessor {
105 Map<Datatype, Binding> repository;
\r
106 BindingScheme scheme;
\r
107 /////////////////////////////////////
110 public @Before void init() {
\r
111 rv = new RandomValue();
\r
112 repository = new HashMap<Datatype, Binding>();
\r
113 scheme = new MutableBindingFactory( repository );
\r
116 public static File createTmpDir()
\r
118 String tmp = System.getenv("tmp");
\r
119 if (tmp==null) tmp = "c:/temp";
\r
120 Random r = new Random();
\r
121 String randomName = "tmp-"+(r.nextInt(10000)+10000);
\r
122 File tmpDir = new File(tmp+"/"+randomName);
\r
123 Boolean ok = tmpDir.mkdirs();
\r
128 public boolean isKeyShortEnough(Binding binding, Object value) throws AdaptException {
\r
129 String key = (String) Bindings.adapt(value, binding, Bindings.STR_VARIANT);
\r
130 return key.length()<=200;
\r
133 public @Test void testWireAccessor() throws Exception {
\r
134 System.out.println("Wire accessor test");
\r
139 for (int i=0; i<10000; i++) {
\r
140 Bindings.defaultBindingRepository.clear();
\r
141 Bindings.bindingRepository.clear();
\r
142 Bindings.serializerRepository.clear();
\r
143 repository.clear();
\r
144 System.out.println(i+": ");
\r
145 rv = new RandomValue(i);
\r
146 rv.refereableRecords = false;
\r
147 Datatype type = rv.randomType(0, 3);
\r
148 Binding binding = scheme.getBindingUnchecked(type);
\r
149 Object instance = binding.accept(rv);
\r
150 // System.out.println(binding.printValue(instance, true));
\r
151 Accessor accessor = Accessors.getAccessor(binding, instance);
\r
153 WireServer wireServer = new WireServer(accessor);
\r
154 Server server = new Server(0, wireServer.getMethodInterface());
\r
155 WireClient wireClient = new WireClient();
\r
156 Client client = new Client(InetAddress.getByName("localhost"), server.getPort(), wireClient.getClientMethodInterface());
\r
157 wireClient.setServerMethodInterface(client.getConnection().getRemoteMethodInterface());
\r
158 Accessor remoteAccessor = (Accessor) wireClient.getAccessor(null);
\r
159 testAccessor( remoteAccessor, false );
\r
161 wireClient.close();
\r
168 public @Test void testBinaryAccessor() throws Exception {
\r
169 System.out.println("Test Binary Memory:");
\r
171 Datatype type = Datatypes.VARIANT;
\r
172 Binding binding = scheme.getBindingUnchecked( type );
\r
173 Serializer s = binding.serializer();
\r
174 TObjectIntHashMap<Object> identities = new TObjectIntHashMap<Object>();
\r
176 for (int i=0; i<10000; i++) {
\r
177 Bindings.defaultBindingRepository.clear();
\r
178 Bindings.bindingRepository.clear();
\r
179 Bindings.serializerRepository.clear();
\r
180 repository.clear();
\r
181 rv = new RandomValue(i);
\r
182 rv.getRandom().nextLong();
\r
183 rv.refereableRecords = false;
\r
184 System.out.println(i+": ");
\r
185 BinaryMemory ram = new BinaryMemory(0);
\r
186 Object instance = binding.accept(rv);
\r
188 binding.assertInstaceIsValid(instance);
\r
189 // System.out.println(instance);
\r
191 identities.clear();
\r
192 int size = s.getSize(instance, identities);
\r
194 ram.setLength(size);
\r
196 identities.clear();
\r
197 s.serialize(ram, identities, instance);
\r
198 identities.clear();
\r
201 Object instance2 = s.deserialize(ram);
\r
202 assertTrue( binding.equals(instance, instance2) );
\r
203 binding.assertInstaceIsValid(instance2);
\r
205 VariantAccessor a = (VariantAccessor) Accessors.getAccessor(ram, type);
\r
214 public @Test void testJavaAccessor() throws Exception {
\r
215 System.out.println("Test Java Objects:");
\r
216 for (int i=0; i<10000; i++) {
\r
217 Bindings.defaultBindingRepository.clear();
\r
218 Bindings.bindingRepository.clear();
\r
219 Bindings.serializerRepository.clear();
\r
220 repository.clear();
\r
221 System.out.println(i+": ");
\r
222 rv = new RandomValue(i);
\r
223 rv.refereableRecords = false;
\r
224 Datatype type = rv.randomType(0, 3);
\r
225 Binding binding = scheme.getBindingUnchecked(type);
\r
226 Object instance = binding.accept(rv);
\r
227 // System.out.println(binding.printValue(instance, true));
\r
228 Accessor accessor = Accessors.getAccessor(binding, instance);
\r
229 testAccessor(accessor);
\r
234 * This test tests composite record by adding 10 fields, and running the
\r
235 * composition as a record accessor
\r
237 public @Test void testCompositeRecord() throws Exception {
\r
238 System.out.println("Test composite accessor");
\r
239 for (int i=0; i<100; i++) {
\r
240 CompositeRecord record = new CompositeRecord();
\r
241 for (int j=1; j<=10; j++) {
\r
243 Binding binding = Bindings.MUTABLE_VARIANT;
\r
244 rv = new RandomValue(i*543+j*23);
\r
245 rv.getRandom().nextLong();
\r
246 rv.refereableRecords = false;
\r
247 MutableVariant instance = (MutableVariant) binding.accept(rv);
\r
248 String fieldName = "Field"+j;
\r
249 Accessor fa = Accessors.getAccessor(instance.getBinding(), instance.getValue());
\r
250 record.addField(fieldName, fa);
\r
252 System.out.println(i);
\r
254 testAccessor( record );
\r
259 public @Test void testFolderMap() throws Exception {
\r
260 // 1000 tests proves failure
\r
261 for (int i=0; i<100; i++) {
\r
262 Bindings.defaultBindingRepository.clear();
\r
263 Bindings.bindingRepository.clear();
\r
264 Bindings.serializerRepository.clear();
\r
265 repository.clear();
\r
267 System.out.println(i+": ");
\r
268 File dir = createTmpDir();
\r
269 DirectoryMap map = Accessors.openDirectory(dir);
\r
270 Binding keyBinding = Bindings.STR_VARIANT;
\r
272 System.out.println("Test Folder Map: "+dir);
\r
275 for (int j=0; j<10; j++) {
\r
276 rv = new RandomValue(i*231231243+j*213);
\r
277 rv.refereableRecords = false;
\r
282 key = (String) keyBinding.accept(rv);
\r
283 } while (key.length() > 240);
\r
286 Datatype valueType = rv.randomType(0, 2);
\r
287 Binding valueBinding = scheme.getBindingUnchecked(valueType);
\r
288 Object value = valueBinding.accept(rv);
\r
290 System.out.println(key);
\r
291 MutableVariant vv = new MutableVariant(valueBinding, value);
\r
292 map.put(Bindings.STR_VARIANT, key, Bindings.MUTABLE_VARIANT, vv);
\r
299 // Remove all files in the folder
\r
302 System.out.println( dir.listFiles().length + " files.");
\r
310 public @Test void testBinaryFile() throws Exception {
311 File tmpFile = File.createTempFile("TestAccessor", ".dat");
312 System.out.println("Test Binary File: "+tmpFile);
313 tmpFile.deleteOnExit();
314 FileVariantAccessor fa = Accessors.createFile(tmpFile);
315 Datatype type = Datatypes.VARIANT;
316 Binding binding = scheme.getBindingUnchecked( type );
318 for (int i=0; i<10000; i++) {
319 Bindings.bindingRepository.clear();
\r
320 Bindings.serializerRepository.clear();
\r
321 Bindings.defaultBindingRepository.clear();
\r
322 repository.clear();
\r
324 System.out.println(i+": ");
325 rv = new RandomValue(i);
326 rv.getRandom().nextLong();
327 rv.refereableRecords = false;
329 Object instance = binding.accept(rv);
330 System.out.print(i+": "+ binding.printValueDefinition(instance, true));
331 fa.setValue(binding, instance);
339 /////////////////////////////////////
341 public void testAccessor(Accessor a) throws Exception {
\r
342 testAccessor(a, true);
\r
345 public void testAccessor(Accessor a, boolean runTypeSpecific) throws Exception {
347 Datatype type = a.type();
348 Binding binding = scheme.getBindingUnchecked(type);
349 InterestSet is = InterestSet.newInterestSet(type, true, true, true);
350 boolean mutable = !binding.isImmutable();
352 // Original value and test value
353 Object origValue = a.getValue(binding);
354 binding.assertInstaceIsValid(origValue);
355 Object testValue = binding.accept(rv);
\r
357 // Test Accessor#getValue(binding, instance);
\r
358 Binding mutableBinding = Bindings.getMutableBinding(type);
\r
359 Object mutableInstance = mutableBinding.createDefault();
\r
360 a.getValue(mutableBinding, mutableInstance);
\r
361 assertTrue( Bindings.equals(binding, origValue, mutableBinding, mutableInstance) );
\r
363 boolean same = binding.equals(origValue, testValue);
364 // Create a reference value and an accessor
365 Object refValue = binding.clone(origValue);
366 Accessor ra = Accessors.getAccessor(binding, refValue);
370 // Write test, without listeners
371 ra.setValue(binding, testValue);
372 Object refClone = ra.getValue(binding);
373 assertTrue( binding.equals(testValue, refClone) );
374 ra.setValue(binding, binding.clone(origValue) );
375 refClone = ra.getValue(binding);
376 assertTrue( binding.equals(origValue, refClone) );
378 // Write test, with listeners
379 ChangeSet cs = new ChangeSet(); // Collects Transformation form origValue to testValue
380 a.addListener(cs, is, null, null);
381 a.setValue(binding, testValue);
382 // There must be events in change set
383 if (!same) assertFalse(cs.isEmpty());
385 // Verify the accessor now contains the test value
386 Object x = a.getValue(binding);
387 assertTrue( binding.equals(testValue, x ) );
389 // Convert reference value to testValue
390 LinkedList<Event> rollback = new LinkedList<Event>();
391 List<Event> events = cs.getAndClearEvents(); // Transformation form origValue to testValue
392 ra.apply(events, rollback); // Apply transformation from origValue to testValue, and gather rollback, a transformation from testValue to origValue
393 x = ra.getValue(binding);
394 assertTrue( binding.equals(x, testValue) );
396 // Revert reference value to origValue using rollback
397 a.removeListener(cs);
\r
398 a.apply(rollback, null); // Apply transformation from testValue to origValue
399 x = a.getValue(binding);
400 assertTrue( binding.equals( x, origValue ) );
403 // Ensure the accessor has the correct value
404 Object x = a.getValue(binding);
\r
405 assertTrue( binding.equals( x, origValue) );
\r
407 if (runTypeSpecific) {
\r
408 // Type specific tests
409 if (a instanceof ArrayAccessor) testArrayAccessor((ArrayAccessor) a);
410 if (a instanceof RecordAccessor) testRecordAccessor((RecordAccessor) a);
411 if (a instanceof MapAccessor) testMapAccessor((MapAccessor) a);
412 if (a instanceof BooleanAccessor) testBooleanAccessor((BooleanAccessor) a);
413 if (a instanceof ByteAccessor) testByteAccessor((ByteAccessor) a);
414 if (a instanceof DoubleAccessor) testDoubleAccessor((DoubleAccessor) a);
415 if (a instanceof FloatAccessor) testFloatAccessor((FloatAccessor) a);
416 if (a instanceof IntegerAccessor) testIntegerAccessor((IntegerAccessor) a);
417 if (a instanceof LongAccessor) testLongAccessor((LongAccessor) a);
418 if (a instanceof OptionalAccessor) testOptionalAccessor((OptionalAccessor) a);
419 if (a instanceof StringAccessor) testStringAccessor((StringAccessor) a);
420 if (a instanceof UnionAccessor) testUnionAccessor((UnionAccessor) a);
421 if (a instanceof VariantAccessor) testVariantAccessor((VariantAccessor) a);
423 // Ensure the accessor has the correct value
424 a.setValue(binding, origValue);
425 x = a.getValue(binding);
\r
426 assertTrue( binding.equals( x, origValue) );
\r
430 public void testMapAccessor(MapAccessor a) throws Exception {
431 Datatype type = a.type();
432 MapBinding binding = (MapBinding) scheme.getBindingUnchecked(type);
433 Binding kb = binding.getKeyBinding();
434 Binding vb = binding.getValueBinding();
435 InterestSet is = InterestSet.newInterestSet(type, true, true, false);
436 boolean mutable = !binding.isImmutable();
439 // getAll(Binding, Binding, Object[], Object[])
440 Object oks[] = new Object[ len ];
441 Object ovs[] = new Object[ len ];
442 a.getAll(kb, vb, oks, ovs);
444 // getAll(Binding, Binding, Map<Object, Object>)
445 TreeMap<Object, Object> om = new TreeMap<Object, Object>(kb);
446 a.getAll(kb, vb, om);
448 // getKeys(Binding), getValues(Binding)
449 Object oks2[] = a.getKeys(kb);
450 Object ovs2[] = a.getValues(vb);
452 // assert the results are the same for the getAll()s and getKeys and getValues
453 for (int i=0; i<len; i++) {
454 assertTrue( om.containsKey(oks[i]) );
455 assertTrue( om.containsKey(oks2[i]) );
456 Object v = om.get(oks[i]);
457 assertTrue( vb.equals( v, ovs[i] ) );
458 assertTrue( vb.equals( v, ovs2[i] ) );
460 assertTrue( vb.equals( v, ovs[i] ) );
461 assertTrue( vb.equals( v, ovs2[i] ) );
464 // Test Get entries
\r
468 Object keys[] = new Object[ len ];
\r
469 Object values[] = new Object[ len ];
\r
470 int c = a.getEntries(kb, a.getFirstKey(kb), true, a.getLastKey(kb), true,
\r
471 new ObjectArrayBinding(kb), keys,
\r
472 new ObjectArrayBinding(vb), values, -1);
\r
474 assertEquals(len, c);
\r
475 for (int i=0; i<len; i++) {
\r
476 assertTrue( kb.equals( oks[i], keys[i] ) );
\r
477 assertTrue( vb.equals( ovs[i], values[i] ) );
\r
480 // Exclude first and last
\r
481 keys = new Object[ len-2 ];
\r
482 values = new Object[ len-2 ];
\r
483 c = a.getEntries(kb, a.getFirstKey(kb), false, a.getLastKey(kb), false,
\r
484 new ObjectArrayBinding(kb), keys,
\r
485 new ObjectArrayBinding(vb), values, -1);
\r
486 assertEquals(len-2, c);
\r
487 for (int i=1; i<len-1; i++) {
\r
488 assertTrue( kb.equals( oks[i], keys[i-1] ) );
\r
489 assertTrue( vb.equals( ovs[i], values[i-1] ) );
\r
493 c = a.getEntries(kb, a.getFirstKey(kb), true, a.getLastKey(kb), false,
\r
494 new ObjectArrayBinding(kb), keys,
\r
495 new ObjectArrayBinding(vb), values, 1);
\r
496 assertEquals(1, c);
\r
497 assertTrue( kb.equals( oks[0], keys[0] ) );
\r
498 assertTrue( vb.equals( ovs[0], values[0] ) );
\r
500 catch (AccessorException e) {
\r
501 assertEquals( "Not implemented", e.getMessage() );
\r
506 // Create test values
507 TreeSet<Object> keys = new TreeSet<Object>(kb);
508 for (int i=0; i<10; i++) {
\r
510 if (kb.type().equals(Datatypes.VARIANT)) {
\r
512 key = kb.accept(rv);
\r
513 } while( !kb.type().equals(Datatypes.VARIANT) || !isKeyShortEnough(kb, key) );
\r
515 key = kb.accept(rv);
\r
519 int testValueCount = keys.size();
520 Object tks[] = keys.toArray( new Object[testValueCount] );
521 Object tvs[] = new Object[ testValueCount ];
522 for (int i=0; i<testValueCount; i++) tvs[i] = vb.accept(rv);
525 // getFirstKey(Binding)
526 Object fk = a.getFirstKey(kb);
527 assertTrue( kb.equals(fk, oks[0]) );
529 // getLastKey(Binding)
530 Object lk = a.getLastKey(kb);
\r
531 assertTrue( kb.equals(lk, oks[len-1]) );
533 // getLowerKey(Binding, Object)
534 Object k = a.getLowerKey(kb, fk);
536 k = a.getLowerKey(kb, lk);
537 assertTrue( kb.equals(k, oks[len-2]) );
539 // getFloorKey(Binding, Object)
540 k = a.getFloorKey(kb, fk);
541 assertTrue( kb.equals(k, oks[0]) );
542 k = a.getFloorKey(kb, lk);
543 assertTrue( kb.equals(k, oks[len-1]) );
545 // getCeilingKey(Binding, Object)
546 k = a.getCeilingKey(kb, lk);
547 assertTrue( kb.equals(k, oks[len-1]) );
548 k = a.getCeilingKey(kb, fk);
549 assertTrue( kb.equals(k, oks[0]) );
551 // getHigherKey(Binding, Object)
552 k = a.getHigherKey(kb, lk);
\r
554 k = a.getHigherKey(kb, fk);
555 assertTrue( kb.equals(k, oks[1]) );
559 // putAll(Binding, Binding, Object[], Object[])
561 int size = a.size();
\r
562 assertEquals( 0, size );
563 a.putAll(kb, vb, oks, ovs);
\r
565 assertEquals( len, size );
568 // putAll(Binding, Binding, Map<Object, Object>)
570 assertEquals( 0, a.size() );
571 a.putAll(kb, vb, om);
572 assertEquals( len, a.size() );
576 for (int i=0; i<len; i++) {
577 assertTrue( a.containsKey(kb, oks[i]) );
578 assertTrue( a.containsValue(vb, ovs[i]) );
585 for (int i=0; i<testValueCount; i++) {
586 if (!a.containsKey(kb, tks[i])) count++;
587 a.put(kb, tks[i], vb, tvs[i]);
589 assertEquals(count, a.size());
590 for (int i=0; i<testValueCount; i++) {
591 Object o = a.get(kb, tks[i], vb);
592 assertTrue( vb.equals(tvs[i], o) );
594 // remove(Binding, Object)
595 for (int i=0; i<testValueCount; i++) {
596 a.remove(kb, tks[i]);
598 for (int i=0; i<len; i++) {
599 a.remove(kb, oks[i]);
601 a.putAll(kb, vb, oks, ovs);
605 int common = Math.min(len, testValueCount);
607 for (int i=0; i<common; i++) {
608 a.put(kb, oks[i], vb, tvs[i]);
609 Object x = a.get(kb, oks[i], vb);
610 assertTrue( vb.equals( tvs[i], x) );
612 for (int i=0; i<common; i++) {
613 a.put(kb, oks[i], vb, ovs[i]);
614 Object x = a.get(kb, oks[i], vb);
615 assertTrue( vb.equals( ovs[i], x ) );
618 //// Test sub-accessors with recursion
619 for (int i=0; i<len; i++) {
620 Accessor sa = a.getValueAccessor(kb, oks[i]);
625 public void testArrayAccessor(ArrayAccessor a) throws Exception {
626 Datatype type = a.type();
627 ArrayBinding binding = (ArrayBinding) scheme.getBindingUnchecked(type);
628 Binding cb = binding.getComponentBinding();
629 InterestSet is = InterestSet.newInterestSet(type, true, true, false);
630 boolean mutable = !binding.isImmutable();
633 Object ovs[] = new Object[ len ];
636 //// Create test values
637 int testValueCount = rv.getRandom().nextInt(10)+10;
638 Object tvs[] = new Object[ testValueCount ];
639 Object tvs2[] = new Object[ testValueCount ];
640 for (int i=0; i<testValueCount; i++) {
641 tvs[i] = cb.accept(rv);
642 tvs2[i] = cb.accept(rv);
648 //// Add in values to end
651 for (int i=0; i<testValueCount; i++) {
652 Object o1 = a.get(i + len, cb);
654 assertTrue( cb.equals(o1, o2) );
657 // Test get to existing instance
\r
659 Object o1 = cb.createDefault();
\r
660 for (int i=0; i<testValueCount; i++) {
\r
661 a.get(i + len, cb, o1);
\r
662 Object o2 = tvs[i];
\r
663 assertTrue( cb.equals(o1, o2) );
\r
668 Object all[] = new Object[ testValueCount + len ];
670 for (int i=0; i<testValueCount; i++) {
671 Object o1 = all[i+len];
673 assertTrue( cb.equals(o1, o2) );
675 a.remove(len, testValueCount);
676 for (int i=0; i<len; i++) {
678 Object o2 = a.get(i, cb);
679 assertTrue( cb.equals(o1, o2) );
681 assertEquals(len, a.size());
684 //// Add in values to the beginning
685 a.addAll(0, cb, tvs);
686 for (int i=0; i<testValueCount; i++) {
687 Object o1 = a.get(i, cb);
689 assertTrue( cb.equals(o1, o2) );
693 for (int i=0; i<testValueCount; i++) {
696 assertTrue( cb.equals(o1, o2) );
698 a.remove(0, testValueCount);
699 assertEquals(len, a.size());
702 int oldSize = a.size();
\r
703 a.setSize( oldSize + 10);
\r
704 assertEquals( oldSize+10, a.size() );
\r
705 a.setSize( oldSize );
706 assertEquals( oldSize, a.size() );
\r
709 int common = Math.min(len, testValueCount);
710 for (int i=0; i<common; i++)
\r
711 a.set(i, cb, tvs[i]);
\r
713 for (int i=0; i<common; i++) assertTrue( cb.equals( tvs[i], a.get(i, cb)) );
714 for (int i=0; i<common; i++) a.set(i, cb, ovs[i]);
715 for (int i=0; i<common; i++) assertTrue( cb.equals( ovs[i], a.get(i, cb)) );
717 //// Test sub-accessors with recursion
718 for (int i=0; i<len; i++) {
719 Accessor sa = a.getAccessor(i);
725 public void testRecordAccessor(RecordAccessor a) throws Exception {
726 Datatype type = a.type();
727 RecordBinding binding = (RecordBinding) scheme.getBindingUnchecked(type);
728 Binding cbs[] = binding.getComponentBindings();
729 InterestSet is = InterestSet.newInterestSet(type, true, true, false);
732 Object ovs[] = new Object[ len ];
733 for (int i=0; i<len; i++) ovs[i] = a.getFieldValue(i, cbs[i]);
735 //// Create test values
736 Object tvs[] = new Object[ len ];
737 Object tvs2[] = new Object[ len ];
738 for (int i=0; i<len; i++) {
739 tvs[i] = cbs[i].accept(rv);
740 tvs2[i] = cbs[i].accept(rv);
743 //// Test setValue()
\r
745 for (int i=0; i<len; i++)
\r
746 a.setFieldValue(i, cbs[i], tvs[i]);
747 for (int i=0; i<len; i++) assertTrue( cbs[i].equals( tvs[i], a.getFieldValue(i, cbs[i])) );
748 for (int i=0; i<len; i++) a.setFieldValue(i, cbs[i], ovs[i]);
749 for (int i=0; i<len; i++) assertTrue( cbs[i].equals( ovs[i], a.getFieldValue(i, cbs[i])) );
\r
751 //// Test sub-accessors with recursion
752 for (int i=0; i<len; i++) {
753 Accessor sa = a.getFieldAccessor(i);
759 public void testOptionalAccessor(OptionalAccessor a) throws Exception {
760 Datatype type = a.type();
761 OptionalBinding binding = (OptionalBinding) scheme.getBindingUnchecked(type);
762 Binding cb = binding.getComponentBinding();
763 InterestSet is = InterestSet.newInterestSet(type, true, true, true);
765 // Remember original value
766 boolean hadValue = a.hasValue();
767 Object ov = hadValue ? a.getComponentValue(cb) : null;
768 Object tv = cb.accept(rv);
770 a.setComponentValue(cb, tv);
771 assertTrue( cb.equals(a.getComponentValue(cb), tv) );
772 assertEquals( true, a.hasValue());
775 assertEquals( false, a.hasValue());
777 // Test Invalidated Event
780 a.setComponentValue(cb, tv);
781 Accessor sa = a.getComponentAccessor();
782 EventCollection ec = new EventCollection();
783 a.addListener(ec, is, null, null);
785 a.removeListener(ec);
786 boolean invalidatedEventOk = false;
787 for (Event e : ec.getAndClearEvents()) {
788 invalidatedEventOk |= (e instanceof InvalidatedEvent) && (e.reference !=null);
791 assertTrue( invalidatedEventOk );
796 a.setComponentValue(cb, ov);
802 public void testUnionAccessor(UnionAccessor a) throws Exception {
803 UnionType type = a.type();
804 UnionBinding binding = (UnionBinding) scheme.getBindingUnchecked(type);
805 Binding cbs[] = binding.getComponentBindings();
806 InterestSet is = InterestSet.newInterestSet(type, true, true, true);
809 // Get original values
811 Object ov = a.getComponentValue(cbs[ot]);
813 //// Create test values
814 Object tvs[] = new Object[ len ];
815 Object tvs2[] = new Object[ len ];
816 for (int i=0; i<len; i++) {
817 tvs[i] = cbs[i].accept(rv);
818 tvs2[i] = cbs[i].accept(rv);
822 for (int i=0; i<len; i++) {
823 a.setComponentValue(i, cbs[i], tvs[i]);
824 assertEquals( i, a.getTag() );
825 assertTrue( Bindings.equals(cbs[i], a.getComponentValue(cbs[i]), cbs[i], tvs[i]) );
828 // Test Invalidated Event
829 if (type.getComponentCount()>1)
832 a.setComponentValue(i, cbs[i], tvs[i]);
833 Accessor sa = a.getComponentAccessor();
834 EventCollection ec = new EventCollection();
835 a.addListener(ec, is, null, null);
837 a.setComponentValue(i, cbs[i], tvs[i]);
838 boolean invalidatedEventOk = false;
839 List<Event> events = ec.getAndClearEvents();
840 for (Event e : events) {
841 invalidatedEventOk |= (e instanceof InvalidatedEvent) && (e.reference !=null);
843 a.removeListener(ec);
845 assertTrue( invalidatedEventOk );
848 a.setComponentValue(ot, cbs[ot], ov);
850 //// Test sub-accessors with recursion
851 Accessor sa = a.getComponentAccessor();
856 public void testVariantAccessor(VariantAccessor a) throws Exception {
857 Datatype type = a.type();
858 VariantBinding binding = (VariantBinding) scheme.getBindingUnchecked(type);
859 InterestSet is = InterestSet.newInterestSet(type, true, true, true);
861 // Remember the original value
862 Binding ob = scheme.getBindingUnchecked( type );
863 Object ov = a.getValue(ob);
864 ob.assertInstaceIsValid(ov);
865 boolean mutable = !binding.isImmutable();
868 // Create test values
869 int testValueCount = 10;
870 Binding tvb[] = new Binding[testValueCount];
871 Object tvs[] = new Object[testValueCount];
872 for (int i=0; i<testValueCount; i++) {
873 tvb[i] = scheme.getBindingUnchecked( rv.randomType(0, 2) );
874 tvs[i] = tvb[i].accept(rv);
878 for (int i=0; i<testValueCount; i++) {
879 a.setContentValue(tvb[i], tvs[i]);
880 Object o = a.getContentValue(tvb[i]);
881 assertTrue( tvb[i].equals(o, tvs[i]) );
884 // Test Invalidated Event
888 Datatype ct = tvb[0].type();
889 a.setContentValue(tvb[0], tvs[0]);
890 EventCollection ec = new EventCollection();
891 a.addListener(ec, is, null, null);
892 Accessor sa = a.getContentAccessor();
893 a.setContentValue(tvb[1], tvs[1]);
894 a.removeListener(ec);
896 // Assigned same type
897 if (ct.getClass().equals(tb.type().getClass())) {
900 boolean invalidatedEventOk = false;
901 List<Event> events = ec.getAndClearEvents();
902 for (Event e : events) {
903 invalidatedEventOk |= (e instanceof InvalidatedEvent) && (e.reference !=null);
905 assertTrue( invalidatedEventOk );
910 //// Test sub-accessors with recursion
911 Accessor sa = a.getContentAccessor();
918 public void testByteAccessor(ByteAccessor a) throws Exception {
919 byte ov = a.getValue();
920 ByteBinding b = Bindings.BYTE;
921 for (int i=0; i<10; i++) {
922 Byte r = (Byte) rv.visit(b);
924 assertTrue( r == a.getValue() );
929 public void testDoubleAccessor(DoubleAccessor a) throws Exception {
930 double ov = a.getValue();
931 DoubleBinding b = Bindings.DOUBLE;
932 for (int i=0; i<10; i++) {
933 Double r = (Double) rv.visit(b);
935 assertTrue( r == a.getValue() );
940 public void testFloatAccessor(FloatAccessor a) throws Exception {
941 float ov = a.getValue();
942 FloatBinding b = Bindings.FLOAT;
943 for (int i=0; i<10; i++) {
944 Float r = (Float) rv.visit(b);
946 assertTrue( r == a.getValue() );
951 public void testIntegerAccessor(IntegerAccessor a) throws Exception {
952 int ov = a.getValue();
953 IntegerBinding b = Bindings.INTEGER;
954 for (int i=0; i<10; i++) {
955 Integer r = (Integer) rv.visit(b);
957 assertTrue( r == a.getValue() );
962 public void testLongAccessor(LongAccessor a) throws Exception {
963 long ov = a.getValue();
964 LongBinding b = Bindings.LONG;
965 for (int i=0; i<10; i++) {
966 Long r = (Long) rv.visit(b);
968 assertTrue( r == a.getValue() );
973 public void testStringAccessor(StringAccessor a) throws Exception {
974 String ov = a.getValue();
975 StringBinding b = Bindings.STRING;
976 for (int i=0; i<10; i++) {
977 String r = (String) rv.visit(b);
979 assertEquals( r, a.getValue() );
984 public void testBooleanAccessor(BooleanAccessor a) throws Exception {
985 Boolean ov = a.getValue();
986 BooleanBinding b = Bindings.BOOLEAN;
987 for (int i=0; i<10; i++) {
988 Boolean r = (Boolean) rv.visit(b);
990 assertEquals( r, a.getValue() );