@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
  public void testCycle() throws JSONObjectAdapterException {
    // Create an object with nesting
    ObjectSchema root = new ObjectSchema();
    root.setName("Root");
    String rootId = new String("root");
    root.setId(rootId);
    // Create a child class
    ObjectSchema child = new ObjectSchema();
    String childId = new String("child");
    child.setName("Child");
    child.setId(childId);
    child.setType(TYPE.OBJECT);
    root.putProperty("childInstance1", child);
    // Add a self reference child
    ObjectSchema rootRef = new ObjectSchema();
    rootRef.setRef(rootId);
    child.putProperty("rootRef", rootRef);
    // Create a grand child

    List<ObjectSchema> list = new ArrayList<ObjectSchema>();
    list.add(root);
    Map<String, ObjectSchema> register =
        PojoGeneratorDriver.registerAllIdentifiedObjectSchemas(list);
    PojoGeneratorDriver.findAndReplaceAllReferencesSchemas(register, list);
  }
  @Test
  public void testInterfaceField() throws JSONObjectAdapterException, ClassNotFoundException {
    // Create an object with nesting
    ObjectSchema inter = new ObjectSchema();
    inter.setName("SomeInterface");
    inter.setType(TYPE.INTERFACE);
    inter.setId("example.org.SomeInterface");

    ObjectSchema interRef = new ObjectSchema();
    interRef.setRef(inter.getId());

    ObjectSchema impl = new ObjectSchema();
    impl.setName("SomeInterfaceImpl");
    impl.setType(TYPE.OBJECT);
    impl.setId("example.org.SomeInterfaceImpl");
    impl.setImplements(new ObjectSchema[] {interRef});

    ObjectSchema root = new ObjectSchema();
    root.setName("Root");
    root.setId(new String("root"));
    root.setType(TYPE.OBJECT);

    // Create a child class
    ObjectSchema child = new ObjectSchema();
    String childId = new String("child");
    child.setName("Child");
    child.setId(childId);
    child.setType(TYPE.OBJECT);
    child.setRef(inter.getId());
    root.putProperty("interfaceField", child);

    List<ObjectSchema> list = new ArrayList<ObjectSchema>();
    list.add(inter);
    list.add(impl);
    list.add(root);
    //		Map<String, ObjectSchema> register =
    // PojoGeneratorDriver.registerAllIdentifiedObjectSchemas(list);
    //		List<ObjectSchema> schemaList =
    // PojoGeneratorDriver.findAndReplaceAllReferencesSchemas(register, list);
    JCodeModel codeModel = new JCodeModel();
    driver.createAllClasses(codeModel, list);
  }
  @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);
  }
  @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 testNestedObjects() throws JSONObjectAdapterException {
    // Create an object with nesting
    ObjectSchema root = new ObjectSchema();
    root.setName("Root");
    root.setId(new String("root"));
    // Create a child class
    ObjectSchema child = new ObjectSchema();
    child.setName("Child");
    child.setType(TYPE.OBJECT);
    root.putProperty("childInstance1", child);
    // Create a grand child
    ObjectSchema grand = new ObjectSchema();
    grand.setName("Grand");
    grand.setType(TYPE.OBJECT);
    String grandId = new String("grand");
    grand.setId(grandId);
    child.putProperty("grandChildInstance1", grand);
    ObjectSchema grandRef = new ObjectSchema();
    grandRef.setRef(grandId);
    child.putProperty("grandChildInstance2", grandRef);
    System.out.println(root.toJSONString(new JSONObjectAdapterImpl()));
    List<ObjectSchema> list = new ArrayList<ObjectSchema>();
    list.add(root);

    // Now before the are replaces this should be a references
    ObjectSchema test = child.getProperties().get("grandChildInstance2");
    assertNotNull(test);
    assertEquals(grandId, test.getRef());

    Map<String, ObjectSchema> register =
        PojoGeneratorDriver.registerAllIdentifiedObjectSchemas(list);
    PojoGeneratorDriver.findAndReplaceAllReferencesSchemas(register, list);
    // Validate that the nest grand child reference is replaced
    test = child.getProperties().get("grandChildInstance2");
    assertNotNull(test);
    assertEquals(null, test.getRef());
    assertEquals(grand, test);
  }