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());
  }
  public void testMoveSpecifiedJoinColumn() 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();

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

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

    collectionTable.moveSpecifiedJoinColumn(0, 1);
    joinColumns = collectionTable.getSpecifiedJoinColumns().iterator();
    assertEquals("BAZ", joinColumns.next().getName());
    assertEquals("BAR", joinColumns.next().getName());
    assertEquals("FOO", joinColumns.next().getName());

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