@Test
  public void testFindAndReplaceAllReferencesSchemasFull() {
    String referenceId = new String("rootOne");
    ObjectSchema referenced = ObjectSchema.createNewWithId(referenceId);
    HashMap<String, ObjectSchema> registry = new HashMap<String, ObjectSchema>();
    // Add the referenced schema to the register.
    registry.put(referenceId, referenced);
    // Create a third self
    ObjectSchema self = ObjectSchema.createNewWithId(new String("self"));
    ObjectSchema refToSelf = new ObjectSchema();
    refToSelf.setRef(new String(ObjectSchema.SELF_REFERENCE));

    ObjectSchema referenceToOther = new ObjectSchema();
    referenceToOther.setRef(referenceId);

    // Add references in all places
    self.putProperty("one", referenceToOther);
    self.putAdditionalProperty("two", referenceToOther);
    self.setItems(refToSelf);
    self.setAdditionalItems(refToSelf);
    self.setImplements(new ObjectSchema[] {referenceToOther});

    // find and replace
    PojoGeneratorDriver.findAndReplaceAllReferencesSchemas(registry, self);
    // Make sure there are no references
    Iterator<ObjectSchema> it = self.getSubSchemaIterator();
    while (it.hasNext()) {
      ObjectSchema toTest = it.next();
      assertTrue(toTest.getRef() == null);
    }
  }
  @Test
  public void testFindAndReplaceAllReferencesSchemas() {
    // Build up a map with one reference and one not
    // This is not a reference so the replace should just return it.
    String referenceId = new String("rootOne");
    ObjectSchema referenced = ObjectSchema.createNewWithId(referenceId);
    HashMap<String, ObjectSchema> registry = new HashMap<String, ObjectSchema>();
    // Add the referenced schema to the register.
    registry.put(referenceId, referenced);
    // Create a self self reference
    ObjectSchema referenceToOther = new ObjectSchema();
    referenceToOther.setRef(referenceId);

    // Create a third self
    ObjectSchema self = ObjectSchema.createNewWithId(new String("self"));
    ObjectSchema refToSelf = new ObjectSchema();
    refToSelf.setRef(new String(ObjectSchema.SELF_REFERENCE));
    // Now add all three to the a map
    HashMap<String, ObjectSchema> map = new HashMap<String, ObjectSchema>();
    map.put("one", referenced);
    map.put("two", referenceToOther);
    map.put("three", refToSelf);

    Map<String, ObjectSchema> results =
        PojoGeneratorDriver.findAndReplaceAllReferencesSchemas(registry, map, self);
    assertNotNull(results);
    assertEquals(3, results.size());
    assertEquals(referenced, results.get("one"));
    assertEquals(referenced, results.get("two"));
    assertEquals(self, results.get("three"));
  }
 @Test(expected = IllegalArgumentException.class)
 public void testRegisterAllIdentifiedObjectSchemasDuplicate() {
   List<ObjectSchema> list = new ArrayList<ObjectSchema>();
   // Create a duplicate
   list.add(ObjectSchema.createNewWithId("one"));
   list.add(ObjectSchema.createNewWithId("one"));
   // This should fail due to the duplicate
   PojoGeneratorDriver.registerAllIdentifiedObjectSchemas(list);
 }
 @Test(expected = IllegalArgumentException.class)
 public void testRegisterAllIdentifiedObjectSchemasNestedDuplicate() {
   List<ObjectSchema> list = new ArrayList<ObjectSchema>();
   // Create a duplicate
   ObjectSchema root1 = ObjectSchema.createNewWithId("rootOne");
   list.add(root1);
   ObjectSchema root2 = ObjectSchema.createNewWithId("rootTwo");
   list.add(root2);
   // Add a child to each with a duplicate name
   root1.setItems(ObjectSchema.createNewWithId("child"));
   // Add a child to each with a duplicate name
   root2.setItems(ObjectSchema.createNewWithId("child"));
   // This should fail due to the duplicate
   PojoGeneratorDriver.registerAllIdentifiedObjectSchemas(list);
 }
 @Test
 public void testReplaceRefrence() {
   // This is not a reference so the replace should just return it.
   ObjectSchema root1 = ObjectSchema.createNewWithId("rootOne");
   ObjectSchema replaced =
       PojoGeneratorDriver.replaceRefrence(new HashMap<String, ObjectSchema>(), root1, null);
   assertEquals(root1, replaced);
 }
  @Test
  public void testReplaceRefrenceRegistry() {
    // This is not a reference so the replace should just return it.
    String referenceId = "rootOne";
    ObjectSchema referenced = ObjectSchema.createNewWithId(referenceId);
    HashMap<String, ObjectSchema> registry = new HashMap<String, ObjectSchema>();
    // Add the referenced schema to the register.
    registry.put(referenceId, referenced);
    // Create a self self reference
    ObjectSchema referenceToOther = new ObjectSchema();
    referenceToOther.setRef(referenceId);

    // Create a third self
    ObjectSchema self = ObjectSchema.createNewWithId("self");

    ObjectSchema replaced = PojoGeneratorDriver.replaceRefrence(registry, referenceToOther, self);
    // Should be replaced with referenced
    assertEquals(referenced, replaced);
  }
 @Test
 public void testRegisterAllIdentifiedObjectSchemasNested() {
   List<ObjectSchema> list = new ArrayList<ObjectSchema>();
   // Create a duplicate
   ObjectSchema root1 = ObjectSchema.createNewWithId("rootOne");
   list.add(root1);
   ObjectSchema root2 = ObjectSchema.createNewWithId("rootTwo");
   list.add(root2);
   // Add a child to each with a unique name
   root1.setItems(ObjectSchema.createNewWithId("child1"));
   // Add a child to each with a unique name
   root2.setItems(ObjectSchema.createNewWithId("child2"));
   // This should not fail this time.
   Map<String, ObjectSchema> map = PojoGeneratorDriver.registerAllIdentifiedObjectSchemas(list);
   assertNotNull(map);
   assertEquals(4, map.size());
   assertEquals(root1, map.get(new String("rootOne")));
   assertEquals(root2, map.get(new String("rootTwo")));
   assertNotNull(map.get(new String("child1")));
   assertNotNull(map.get(new String("child2")));
 }
  @Test
  public void testReplaceRefrenceSelf() {
    // This is not a reference so the replace should just return it.
    ObjectSchema self = ObjectSchema.createNewWithId("rootOne");
    // Create a self self reference
    ObjectSchema refrenceToSelf = new ObjectSchema();
    refrenceToSelf.setRef(ObjectSchema.SELF_REFERENCE);

    ObjectSchema replaced =
        PojoGeneratorDriver.replaceRefrence(
            new HashMap<String, ObjectSchema>(), refrenceToSelf, self);
    // Should be replaced with self
    assertEquals(self, replaced);
  }
  @Test(expected = IllegalArgumentException.class)
  public void testReplaceRefrenceMissRegistry() {
    // This is not a reference so the replace should just return it.
    String referenceId = new String("rootOne");
    // This time the referenced is not in the register
    HashMap<String, ObjectSchema> registry = new HashMap<String, ObjectSchema>();
    // Create a self self reference
    ObjectSchema referenceToOther = new ObjectSchema();
    referenceToOther.setRef(referenceId);

    // Create a third self
    ObjectSchema self = ObjectSchema.createNewWithId(new String("self"));
    // This should fail since the referenced is not in the register
    ObjectSchema replaced = PojoGeneratorDriver.replaceRefrence(registry, referenceToOther, self);
  }