public void testAddUniqueConstraint2() throws Exception {
    createTestEntityWithElementCollection();
    addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);

    JavaElementCollectionMapping2_0 elementCollectionMapping =
        (JavaElementCollectionMapping2_0)
            getJavaPersistentType().getAttributes().iterator().next().getMapping();
    CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable();
    collectionTable.addUniqueConstraint(0).addColumnName(0, "FOO");
    collectionTable.addUniqueConstraint(1).addColumnName(0, "BAR");
    collectionTable.addUniqueConstraint(0).addColumnName(0, "BAZ");

    JavaResourceType resourceType =
        (JavaResourceType)
            getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
    JavaResourceField resourceField = resourceType.getFields().iterator().next();
    CollectionTableAnnotation2_0 joinTableAnnotation =
        (CollectionTableAnnotation2_0)
            resourceField.getAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);
    ListIterator<UniqueConstraintAnnotation> uniqueConstraints =
        joinTableAnnotation.getUniqueConstraints().iterator();

    assertEquals("BAZ", uniqueConstraints.next().columnNameAt(0));
    assertEquals("FOO", uniqueConstraints.next().columnNameAt(0));
    assertEquals("BAR", uniqueConstraints.next().columnNameAt(0));
    assertFalse(uniqueConstraints.hasNext());
  }
  public void testgetUniqueConstraintsSize() throws Exception {
    createTestEntityWithElementCollection();
    addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);

    JavaElementCollectionMapping2_0 elementCollectionMapping =
        (JavaElementCollectionMapping2_0)
            getJavaPersistentType().getAttributes().iterator().next().getMapping();
    CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable();

    assertEquals(0, collectionTable.getUniqueConstraintsSize());

    JavaResourceType resourceType =
        (JavaResourceType)
            getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
    JavaResourceField resourceField = resourceType.getFields().iterator().next();
    CollectionTableAnnotation2_0 joinTableAnnotation =
        (CollectionTableAnnotation2_0)
            resourceField.addAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);
    joinTableAnnotation.addUniqueConstraint(0).addColumnName(0, "foo");
    joinTableAnnotation.addUniqueConstraint(1).addColumnName(0, "bar");

    getJpaProject().synchronizeContextModel();

    assertEquals(2, collectionTable.getUniqueConstraintsSize());
  }
  public void testRemoveSpecifiedJoinColumn() throws Exception {
    createTestEntityWithElementCollection();
    addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);

    JavaElementCollectionMapping2_0 elementCollectionMapping =
        (JavaElementCollectionMapping2_0)
            getJavaPersistentType().getAttributes().iterator().next().getMapping();
    CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable();

    JavaResourceType resourceType =
        (JavaResourceType)
            getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
    JavaResourceField resourceField = resourceType.getFields().iterator().next();

    collectionTable.addSpecifiedJoinColumn(0).setSpecifiedName("FOO");
    collectionTable.addSpecifiedJoinColumn(1).setSpecifiedName("BAR");
    collectionTable.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ");

    CollectionTableAnnotation2_0 joinTableResource =
        (CollectionTableAnnotation2_0)
            resourceField.getAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);
    assertEquals(3, joinTableResource.getJoinColumnsSize());

    collectionTable.removeSpecifiedJoinColumn(0);
    assertEquals(2, joinTableResource.getJoinColumnsSize());
    assertEquals("BAR", joinTableResource.joinColumnAt(0).getName());
    assertEquals("BAZ", joinTableResource.joinColumnAt(1).getName());

    collectionTable.removeSpecifiedJoinColumn(0);
    assertEquals(1, joinTableResource.getJoinColumnsSize());
    assertEquals("BAZ", joinTableResource.joinColumnAt(0).getName());

    collectionTable.removeSpecifiedJoinColumn(0);
    assertEquals(0, joinTableResource.getJoinColumnsSize());
  }
  public void testModifySpecifiedCatalog() throws Exception {
    createTestEntityWithElementCollection();
    addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);

    JavaElementCollectionMapping2_0 elementCollectionMapping =
        (JavaElementCollectionMapping2_0)
            getJavaPersistentType().getAttributes().iterator().next().getMapping();
    CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable();

    JavaResourceType resourceType =
        (JavaResourceType)
            getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
    JavaResourceField resourceField = resourceType.getFields().iterator().next();

    CollectionTableAnnotation2_0 resourceCollectionTable =
        (CollectionTableAnnotation2_0)
            resourceField.getAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);

    assertNull(collectionTable.getSpecifiedCatalog());
    assertNull(resourceCollectionTable);

    // set catalog in the context model, verify resource model modified
    collectionTable.setSpecifiedCatalog("foo");
    resourceCollectionTable =
        (CollectionTableAnnotation2_0)
            resourceField.getAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);
    assertEquals("foo", collectionTable.getSpecifiedCatalog());
    assertEquals("foo", resourceCollectionTable.getCatalog());

    // set catalog to null in the context model
    collectionTable.setSpecifiedCatalog(null);
    assertNull(collectionTable.getSpecifiedCatalog());
    assertNull(resourceField.getAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME));
  }
  public void testDefaultName() throws Exception {
    createTestEntityWithValidElementCollection();
    addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);

    JavaElementCollectionMapping2_0 elementCollectionMapping =
        (JavaElementCollectionMapping2_0)
            getJavaPersistentType().getAttributes().iterator().next().getMapping();
    CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable();

    assertEquals(TYPE_NAME + "_projects", collectionTable.getDefaultName());

    JavaResourceType resourceType =
        (JavaResourceType)
            getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
    JavaResourceField resourceField = resourceType.getFields().iterator().next();
    assertNull(resourceField.getAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME));

    // add the collection table annotation, verify default collection table name is the same
    resourceField.addAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);
    assertEquals(TYPE_NAME + "_projects", collectionTable.getDefaultName());
    assertNotNull(resourceField.getAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME));

    // set the entity name, verify default collection table name updates
    getJavaEntity().setSpecifiedName("Foo");
    assertEquals("Foo_projects", collectionTable.getDefaultName());
  }
  public void testUpdateSpecifiedName() throws Exception {
    createTestEntityWithElementCollection();
    addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);

    JavaElementCollectionMapping2_0 elementCollectionMapping =
        (JavaElementCollectionMapping2_0)
            getJavaPersistentType().getAttributes().iterator().next().getMapping();
    CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable();

    JavaResourceType resourceType =
        (JavaResourceType)
            getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
    JavaResourceField resourceField = resourceType.getFields().iterator().next();

    CollectionTableAnnotation2_0 resourceCollectionTable =
        (CollectionTableAnnotation2_0)
            resourceField.getAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);

    assertNull(collectionTable.getSpecifiedName());
    assertNull(resourceCollectionTable);

    // set name in the resource model, verify context model updated
    resourceField.addAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);
    resourceCollectionTable =
        (CollectionTableAnnotation2_0)
            resourceField.getAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);
    resourceCollectionTable.setName("FOO");
    getJpaProject().synchronizeContextModel();
    assertEquals("FOO", collectionTable.getSpecifiedName());
    assertEquals("FOO", resourceCollectionTable.getName());

    // set name to null in the resource model
    resourceCollectionTable.setName(null);
    getJpaProject().synchronizeContextModel();
    assertNull(collectionTable.getSpecifiedName());
    assertNull(resourceCollectionTable.getName());

    resourceCollectionTable.setName("FOO");
    getJpaProject().synchronizeContextModel();
    assertEquals("FOO", collectionTable.getSpecifiedName());
    assertEquals("FOO", resourceCollectionTable.getName());

    resourceField.removeAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);
    getJpaProject().synchronizeContextModel();
    assertNull(collectionTable.getSpecifiedName());
    assertNull(resourceField.getAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME));
  }
  public void testSpecifiedJoinColumnsSize() throws Exception {
    createTestEntityWithElementCollection();
    addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);

    JavaElementCollectionMapping2_0 elementCollectionMapping =
        (JavaElementCollectionMapping2_0)
            getJavaPersistentType().getAttributes().iterator().next().getMapping();
    CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable();

    assertEquals(0, collectionTable.getSpecifiedJoinColumnsSize());

    collectionTable.addSpecifiedJoinColumn(0);
    assertEquals(1, collectionTable.getSpecifiedJoinColumnsSize());

    collectionTable.removeSpecifiedJoinColumn(0);
    assertEquals(0, collectionTable.getSpecifiedJoinColumnsSize());
  }
  public void testUpdateUniqueConstraints() throws Exception {
    createTestEntityWithElementCollection();
    addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);

    JavaElementCollectionMapping2_0 elementCollectionMapping =
        (JavaElementCollectionMapping2_0)
            getJavaPersistentType().getAttributes().iterator().next().getMapping();
    JavaCollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable();
    JavaResourceType resourceType =
        (JavaResourceType)
            getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
    JavaResourceField resourceField = resourceType.getFields().iterator().next();
    CollectionTableAnnotation2_0 joinTableAnnotation =
        (CollectionTableAnnotation2_0)
            resourceField.addAnnotation(CollectionTableAnnotation2_0.ANNOTATION_NAME);

    joinTableAnnotation.addUniqueConstraint(0).addColumnName("FOO");
    joinTableAnnotation.addUniqueConstraint(1).addColumnName("BAR");
    joinTableAnnotation.addUniqueConstraint(2).addColumnName("BAZ");
    getJpaProject().synchronizeContextModel();

    ListIterator<JavaSpecifiedUniqueConstraint> uniqueConstraints =
        collectionTable.getUniqueConstraints().iterator();
    assertEquals("FOO", uniqueConstraints.next().getColumnNames().iterator().next());
    assertEquals("BAR", uniqueConstraints.next().getColumnNames().iterator().next());
    assertEquals("BAZ", uniqueConstraints.next().getColumnNames().iterator().next());
    assertFalse(uniqueConstraints.hasNext());

    joinTableAnnotation.moveUniqueConstraint(2, 0);
    getJpaProject().synchronizeContextModel();
    uniqueConstraints = collectionTable.getUniqueConstraints().iterator();
    assertEquals("BAR", uniqueConstraints.next().getColumnNames().iterator().next());
    assertEquals("BAZ", uniqueConstraints.next().getColumnNames().iterator().next());
    assertEquals("FOO", uniqueConstraints.next().getColumnNames().iterator().next());
    assertFalse(uniqueConstraints.hasNext());

    joinTableAnnotation.moveUniqueConstraint(0, 1);
    getJpaProject().synchronizeContextModel();
    uniqueConstraints = collectionTable.getUniqueConstraints().iterator();
    assertEquals("BAZ", uniqueConstraints.next().getColumnNames().iterator().next());
    assertEquals("BAR", uniqueConstraints.next().getColumnNames().iterator().next());
    assertEquals("FOO", uniqueConstraints.next().getColumnNames().iterator().next());
    assertFalse(uniqueConstraints.hasNext());

    joinTableAnnotation.removeUniqueConstraint(1);
    getJpaProject().synchronizeContextModel();
    uniqueConstraints = collectionTable.getUniqueConstraints().iterator();
    assertEquals("BAZ", uniqueConstraints.next().getColumnNames().iterator().next());
    assertEquals("FOO", uniqueConstraints.next().getColumnNames().iterator().next());
    assertFalse(uniqueConstraints.hasNext());

    joinTableAnnotation.removeUniqueConstraint(1);
    getJpaProject().synchronizeContextModel();
    uniqueConstraints = collectionTable.getUniqueConstraints().iterator();
    assertEquals("BAZ", uniqueConstraints.next().getColumnNames().iterator().next());
    assertFalse(uniqueConstraints.hasNext());

    joinTableAnnotation.removeUniqueConstraint(0);
    getJpaProject().synchronizeContextModel();
    uniqueConstraints = collectionTable.getUniqueConstraints().iterator();
    assertFalse(uniqueConstraints.hasNext());
  }