@Test
public void testMultipleIteration() throws IOException
{
// Setup : Store multiple target objects to the database.
TargetObject targetObject1 = ModelFactory.eINSTANCE.createTargetObject();
targetObject1.setSingleAttribute("junit1");
saveObject(targetObject1);
TargetObject targetObject2 = ModelFactory.eINSTANCE.createTargetObject();
targetObject2.setSingleAttribute("junit2");
saveObject(targetObject2);
TargetObject targetObject3 = ModelFactory.eINSTANCE.createTargetObject();
targetObject3.setSingleAttribute("junit3");
saveObject(targetObject3);
// Test: Query the database to return a cursor
ResourceSet resourceSet = createResourceSet();
resourceSet.getLoadOptions().put(Options.OPTION_QUERY_CURSOR, Boolean.TRUE);
Resource resource = resourceSet.getResource(queryURI, true);
// Verify: Make sure we got a cursor back with three elements that match the target objects
HashSet<String> pendingTargets = new HashSet<String>(3);
pendingTargets.add(targetObject1.getSingleAttribute());
pendingTargets.add(targetObject2.getSingleAttribute());
pendingTargets.add(targetObject3.getSingleAttribute());
assertThat(resource, is(notNullValue()));
assertThat(resource.getContents().size(), is(1));
assertThat(resource.getContents().get(0), is(instanceOf(MongoCursor.class)));
MongoCursor cursor = (MongoCursor) resource.getContents().get(0);
Iterator<EObject> iterator = cursor.iterator();
assertThat(iterator, is(notNullValue()));
assertTrue(iterator.hasNext());
TargetObject actualObject = (TargetObject) iterator.next();
pendingTargets.remove(actualObject.getSingleAttribute());
assertTrue(iterator.hasNext());
actualObject = (TargetObject) iterator.next();
pendingTargets.remove(actualObject.getSingleAttribute());
assertTrue(iterator.hasNext());
actualObject = (TargetObject) iterator.next();
pendingTargets.remove(actualObject.getSingleAttribute());
assertFalse(iterator.hasNext());
assertTrue(pendingTargets.isEmpty());
}