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

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

    ListIterator<JavaSpecifiedUniqueConstraint> uniqueConstraints =
        collectionTable.getUniqueConstraints().iterator();
    assertFalse(uniqueConstraints.hasNext());

    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(0).addColumnName(0, "bar");
    getJpaProject().synchronizeContextModel();

    uniqueConstraints = collectionTable.getUniqueConstraints().iterator();
    assertTrue(uniqueConstraints.hasNext());
    assertEquals("bar", uniqueConstraints.next().getColumnNames().iterator().next());
    assertEquals("foo", uniqueConstraints.next().getColumnNames().iterator().next());
    assertFalse(uniqueConstraints.hasNext());
  }
  public void testRemoveUniqueConstraint() throws Exception {
    createTestEntityWithElementCollection();
    addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);

    JavaElementCollectionMapping2_0 elementCollectionMapping =
        (JavaElementCollectionMapping2_0)
            getJavaPersistentType().getAttributes().iterator().next().getMapping();
    JavaCollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable();
    collectionTable.addUniqueConstraint(0).addColumnName(0, "FOO");
    collectionTable.addUniqueConstraint(1).addColumnName(0, "BAR");
    collectionTable.addUniqueConstraint(2).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);

    assertEquals(3, joinTableAnnotation.getUniqueConstraintsSize());

    collectionTable.removeUniqueConstraint(1);

    ListIterator<UniqueConstraintAnnotation> uniqueConstraintAnnotations =
        joinTableAnnotation.getUniqueConstraints().iterator();
    assertEquals("FOO", uniqueConstraintAnnotations.next().columnNameAt(0));
    assertEquals("BAZ", uniqueConstraintAnnotations.next().columnNameAt(0));
    assertFalse(uniqueConstraintAnnotations.hasNext());

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

    collectionTable.removeUniqueConstraint(1);
    uniqueConstraintAnnotations = joinTableAnnotation.getUniqueConstraints().iterator();
    assertEquals("FOO", uniqueConstraintAnnotations.next().columnNameAt(0));
    assertFalse(uniqueConstraintAnnotations.hasNext());

    uniqueConstraints = collectionTable.getUniqueConstraints().iterator();
    assertEquals("FOO", uniqueConstraints.next().getColumnNames().iterator().next());
    assertFalse(uniqueConstraints.hasNext());

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