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 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 testAddSpecifiedJoinColumn() 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();

    SpecifiedJoinColumn joinColumn = collectionTable.addSpecifiedJoinColumn(0);
    joinColumn.setSpecifiedName("FOO");

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

    assertEquals("FOO", joinTableResource.joinColumnAt(0).getName());

    SpecifiedJoinColumn joinColumn2 = collectionTable.addSpecifiedJoinColumn(0);
    joinColumn2.setSpecifiedName("BAR");

    assertEquals("BAR", joinTableResource.joinColumnAt(0).getName());
    assertEquals("FOO", joinTableResource.joinColumnAt(1).getName());

    SpecifiedJoinColumn joinColumn3 = collectionTable.addSpecifiedJoinColumn(1);
    joinColumn3.setSpecifiedName("BAZ");

    assertEquals("BAR", joinTableResource.joinColumnAt(0).getName());
    assertEquals("BAZ", joinTableResource.joinColumnAt(1).getName());
    assertEquals("FOO", joinTableResource.joinColumnAt(2).getName());

    ListIterator<JavaSpecifiedJoinColumn> joinColumns =
        collectionTable.getSpecifiedJoinColumns().iterator();
    assertEquals(joinColumn2, joinColumns.next());
    assertEquals(joinColumn3, joinColumns.next());
    assertEquals(joinColumn, joinColumns.next());

    joinColumns = collectionTable.getSpecifiedJoinColumns().iterator();
    assertEquals("BAR", joinColumns.next().getName());
    assertEquals("BAZ", joinColumns.next().getName());
    assertEquals("FOO", joinColumns.next().getName());
  }
  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 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());
  }