/** Per-test setup */
 @Before
 public void setUpTest() throws Exception {
   txn = createTransaction();
   set = new ScalableHashSet<Object>();
   dataService.setBinding("set", set);
   one = new Int(1);
   dataService.setBinding("one", one);
 }
 private void op(int i) throws Exception {
   if (random.nextInt(5) == 0) {
     DummyTransaction t = txn;
     txn = null;
     t.commit();
     long time = System.currentTimeMillis() - txnStart;
     if (time > maxTxnTime) {
       maxTxnTime = time;
       maxTxnTimeOps = txnOps;
     }
     txnStart = System.currentTimeMillis();
     txnOps = 0;
     createTxn();
   }
   txnOps++;
   int start = id * (objects + objectsBuffer);
   String name = getObjectName(start + random.nextInt(objects));
   switch (random.nextInt(whichOperations.value)) {
     case 0:
       /* Get binding */
       service.getBinding(name, Object.class);
       break;
     case 1:
       /* Set bindings */
       ModifiableObject obj = service.getBinding(name, ModifiableObject.class);
       String name2 = getObjectName(start + random.nextInt(objects));
       ModifiableObject obj2 = service.getBinding(name2, ModifiableObject.class);
       service.setBinding(name, obj2);
       service.setBinding(name2, obj);
       break;
     case 2:
       /* Modify object */
       service.getBinding(name, ModifiableObject.class).incrementNumber(service);
       break;
     case 3:
       /* Create object */
       /*
        * Get the binding first, so the new object is created with
        * proper locality.
        */
       ModifiableObject object = service.getBinding(name, ModifiableObject.class);
       service.removeObject(object);
       object = new ModifiableObject();
       service.setBinding(name, object);
       BigInteger oidBlock = service.createReference(object).getId().shiftRight(32);
       if (block == null || !block.equals(oidBlock)) {
         block = oidBlock;
         System.err.println("Thread " + id + ": create block " + block);
       }
       break;
     default:
       throw new AssertionError();
   }
 }
 /** Stores fields, if they are not null, into bindings. */
 private void endTransaction() throws Exception {
   if (set != null) {
     try {
       dataService.setBinding("set", set);
     } catch (ObjectNotFoundException e) {
     }
   }
   if (one != null) {
     try {
       dataService.setBinding("one", one);
     } catch (ObjectNotFoundException e) {
     }
   }
 }
 @SuppressWarnings("unchecked")
 @Test
 public void testIteratorCollectionNotFound() throws Exception {
   set.add(one);
   Iterator<Object> iter = set.iterator();
   dataService.setBinding("iter", new ManagedSerializable(iter));
   newTransaction();
   DoneRemoving.init();
   dataService.removeObject(set);
   endTransaction();
   DoneRemoving.await(1);
   startTransaction();
   iter = (Iterator<Object>) dataService.getBinding("iter", ManagedSerializable.class).get();
   try {
     iter.next();
     fail("Expected ObjectNotFoundException");
   } catch (ObjectNotFoundException e) {
     System.err.println(e);
   }
   try {
     iter.hasNext();
     fail("Expected ObjectNotFoundException");
   } catch (ObjectNotFoundException e) {
     System.err.println(e);
   }
   try {
     iter.remove();
     fail("Expected an exception");
   } catch (ObjectNotFoundException e) {
     System.err.println(e);
   } catch (IllegalStateException e) {
     System.err.println(e);
   }
 }
 public void testConcurrency() throws Throwable {
   DummyComponentRegistry componentRegistry = new DummyComponentRegistry();
   service = getDataService(props, componentRegistry);
   if (service instanceof ProfileProducer) {
     DummyProfileCoordinator.startProfiling(((ProfileProducer) service));
   }
   DummyTransaction txn = new DummyTransaction();
   txnProxy.setCurrentTransaction(txn);
   service.configure(componentRegistry, txnProxy);
   componentRegistry.setComponent(DataManager.class, service);
   componentRegistry.registerAppContext();
   txn.commit();
   int perThread = objects + objectsBuffer;
   /* Create objects */
   for (int t = 0; t < maxThreads; t++) {
     long startTime = System.currentTimeMillis();
     txn = new DummyTransaction();
     txnProxy.setCurrentTransaction(txn);
     int start = t * perThread;
     BigInteger block = null;
     for (int i = 0; i < perThread; i++) {
       if (startTime + 100 < System.currentTimeMillis()) {
         txn.commit();
         txn = new DummyTransaction();
         txnProxy.setCurrentTransaction(txn);
         if (i > 0) {
           service.getBinding(getObjectName(start), ModifiableObject.class);
         }
       }
       ModifiableObject object = new ModifiableObject();
       if (i == 0) {
         service.manageInNewArea(object);
       }
       service.setBinding(getObjectName(start + i), object);
       BigInteger oidBlock = service.createReference(object).getId().shiftRight(32);
       if (block == null || !block.equals(oidBlock)) {
         block = oidBlock;
         System.err.println("Thread " + t + ": init block " + block);
       }
     }
     txn.commit();
   }
   /* Warm up */
   if (repeat != 1) {
     System.err.println("Warmup:");
     runOperations(1);
   }
   /* Test */
   for (int t = threads; t <= maxThreads; t++) {
     System.err.println("Threads: " + t);
     for (int r = 0; r < repeat; r++) {
       runOperations(t);
     }
   }
 }
 @SuppressWarnings("unchecked")
 @Test
 public void testIterator() throws Exception {
   set.add(null);
   set.add(1);
   set.add(2);
   Iterator iter = set.iterator();
   dataService.setBinding("iter", new ManagedSerializable(iter));
   newTransaction();
   iter = (Iterator) dataService.getBinding("iter", ManagedSerializable.class).get();
   int count = 0;
   while (iter.hasNext()) {
     iter.next();
     count++;
   }
   assertEquals(3, count);
 }
 @SuppressWarnings("unchecked")
 @Test
 public void testIteratorObjectNotFound() throws Exception {
   set.add(one);
   set.add(new Int(2));
   Iterator<Object> iter = set.iterator();
   dataService.setBinding("iter", new ManagedSerializable(iter));
   newTransaction();
   dataService.removeObject(one);
   newTransaction();
   iter = (Iterator<Object>) dataService.getBinding("iter", ManagedSerializable.class).get();
   int count = 0;
   while (iter.hasNext()) {
     try {
       assertEquals(new Int(2), iter.next());
       count++;
     } catch (ObjectNotFoundException e) {
     }
   }
   assertEquals(1, count);
 }
 @SuppressWarnings("unchecked")
 @Test
 public void testIteratorRemove() throws Exception {
   Iterator iter = set.iterator();
   try {
     iter.remove();
     fail("Expected IllegalStateException");
   } catch (IllegalStateException e) {
   }
   set.add(one);
   set.add(new Int(2));
   set.add(new Int(3));
   try {
     iter.remove();
     fail("Expected IllegalStateException");
   } catch (IllegalStateException e) {
   }
   dataService.setBinding("iter", new ManagedSerializable(iter));
   newTransaction();
   iter = (Iterator) dataService.getBinding("iter", ManagedSerializable.class).get();
   while (iter.hasNext()) {
     Object next = iter.next();
     if (one.equals(next)) {
       iter.remove();
       try {
         iter.remove();
         fail("Expected IllegalStateException");
       } catch (IllegalStateException e) {
       }
     }
   }
   newTransaction();
   iter = set.iterator();
   int count = 0;
   while (iter.hasNext()) {
     assertFalse(one.equals(iter.next()));
     count++;
   }
   assertEquals(2, count);
 }
 @SuppressWarnings("unchecked")
 @Test
 public void testIteratorRetainAcrossTransactions() throws Exception {
   set.add(one);
   Iterator<Object> iter = set.iterator();
   dataService.setBinding("iter", new ManagedSerializable(iter));
   newTransaction();
   try {
     iter.hasNext();
     fail("Expected TransactionNotActiveException");
   } catch (TransactionNotActiveException e) {
   }
   try {
     iter.next();
     fail("Expected TransactionNotActiveException");
   } catch (TransactionNotActiveException e) {
   }
   try {
     iter.remove();
     fail("Expected IllegalStateException");
   } catch (IllegalStateException e) {
   }
 }