Search in sources :

Example 16 with Schema

use of org.alfresco.util.schemacomp.model.Schema in project alfresco-repository by Alfresco.

the class SchemaComparatorTest method indexColumnOrderingComparedCorrectly.

@Test
public void indexColumnOrderingComparedCorrectly() {
    reference = new Schema("schema", "alf_", 590, true);
    target = new Schema("schema", "alf_", 590, true);
    // Reference schema's database objects.
    reference.add(new Table(reference, "table_name", columns("id NUMBER(10)", "nodeRef VARCHAR2(200)", "name VARCHAR2(150)"), pk("pk", "id"), fkeys(), indexes("index_name id nodeRef")));
    // Target schema's database objects - note different order of index columns.
    target.add(new Table(target, "table_name", columns("id NUMBER(10)", "nodeRef VARCHAR2(200)", "name VARCHAR2(150)"), pk("pk", "id"), fkeys(), indexes("index_name nodeRef id")));
    comparator = new SchemaComparator(reference, target, dialect);
    comparator.validateAndCompare();
    // See stdout for diagnostics dump...
    dumpDiffs(comparator.getComparisonResults(), false);
    dumpValidation(comparator.getComparisonResults());
    Results results = comparator.getComparisonResults();
    Iterator<Result> it = results.iterator();
    Difference diff = (Difference) it.next();
    assertEquals(Where.IN_BOTH_BUT_DIFFERENCE, diff.getWhere());
    assertEquals("schema.table_name.index_name.columnNames[0]", diff.getLeft().getPath());
    assertEquals("schema.table_name.index_name.columnNames[0]", diff.getRight().getPath());
    assertEquals("columnNames[0]", diff.getLeft().getPropertyName());
    assertEquals("id", diff.getLeft().getPropertyValue());
    assertEquals("columnNames[0]", diff.getRight().getPropertyName());
    assertEquals("nodeRef", diff.getRight().getPropertyValue());
    diff = (Difference) it.next();
    assertEquals(Where.IN_BOTH_BUT_DIFFERENCE, diff.getWhere());
    assertEquals("schema.table_name.index_name.columnNames[1]", diff.getLeft().getPath());
    assertEquals("schema.table_name.index_name.columnNames[1]", diff.getRight().getPath());
    assertEquals("columnNames[1]", diff.getLeft().getPropertyName());
    assertEquals("nodeRef", diff.getLeft().getPropertyValue());
    assertEquals("columnNames[1]", diff.getRight().getPropertyName());
    assertEquals("id", diff.getRight().getPropertyValue());
    assertFalse("There should be no more differences", it.hasNext());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Schema(org.alfresco.util.schemacomp.model.Schema) Test(org.junit.Test)

Example 17 with Schema

use of org.alfresco.util.schemacomp.model.Schema in project alfresco-repository by Alfresco.

the class SchemaComparatorTest method columnOrderingComparedCorrectlyWhenEnabled.

@Test
public void columnOrderingComparedCorrectlyWhenEnabled() {
    reference = new Schema("schema", "alf_", 590, true);
    target = new Schema("schema", "alf_", 590, true);
    // Reference schema's database objects.
    reference.add(new Table(reference, "table_name", columns("id NUMBER(10)", "nodeRef VARCHAR2(200)", "name VARCHAR2(150)"), new PrimaryKey(null, "my_pk_name", Arrays.asList("id", "nodeRef"), Arrays.asList(1, 2)), fkeys(), indexes()));
    // Target schema's database objects - note different column order
    target.add(new Table(target, "table_name", columns("id NUMBER(10)", "name VARCHAR2(150)", "nodeRef VARCHAR2(200)"), new PrimaryKey(null, "my_pk_name", Arrays.asList("id", "nodeRef"), Arrays.asList(1, 2)), fkeys(), indexes()));
    comparator = new SchemaComparator(reference, target, dialect);
    comparator.validateAndCompare();
    // See stdout for diagnostics dump...
    dumpDiffs(comparator.getComparisonResults(), false);
    dumpValidation(comparator.getComparisonResults());
    Results results = comparator.getComparisonResults();
    Iterator<Result> it = results.iterator();
    Difference diff = (Difference) it.next();
    assertEquals(Where.IN_BOTH_BUT_DIFFERENCE, diff.getWhere());
    assertEquals("schema.table_name.nodeRef.order", diff.getLeft().getPath());
    assertEquals("order", diff.getLeft().getPropertyName());
    assertEquals(2, diff.getLeft().getPropertyValue());
    assertEquals("schema.table_name.nodeRef.order", diff.getRight().getPath());
    assertEquals("order", diff.getRight().getPropertyName());
    assertEquals(3, diff.getRight().getPropertyValue());
    diff = (Difference) it.next();
    assertEquals(Where.IN_BOTH_BUT_DIFFERENCE, diff.getWhere());
    assertEquals("schema.table_name.name.order", diff.getLeft().getPath());
    assertEquals("order", diff.getLeft().getPropertyName());
    assertEquals(3, diff.getLeft().getPropertyValue());
    assertEquals("schema.table_name.name.order", diff.getRight().getPath());
    assertEquals("order", diff.getRight().getPropertyName());
    assertEquals(2, diff.getRight().getPropertyValue());
    assertFalse("There should be no more differences", it.hasNext());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Schema(org.alfresco.util.schemacomp.model.Schema) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Test(org.junit.Test)

Example 18 with Schema

use of org.alfresco.util.schemacomp.model.Schema in project alfresco-repository by Alfresco.

the class SchemaToXMLTest method canTransformSchemaToXML.

@Test
public void canTransformSchemaToXML() throws IOException {
    Writer writer = new StringWriter();
    StreamResult out = new StreamResult(writer);
    Schema schema = new Schema("alfresco", "my-prefix", 501, true);
    schema.add(table("node", columns("id NUMBER(10)", "nodeRef VARCHAR2(200)", "name VARCHAR2(150)"), pk("pk_node", "id"), fkeys(fk("fk_node_noderef", "nodeRef", "node", "nodeRef")), indexes("idx_node_by_id id nodeRef")));
    schema.add(sequence("node_seq"));
    schema.add(sequence("content_seq"));
    SchemaToXML transformer = new SchemaToXML(schema, out);
    transformer.execute();
    System.out.println(writer.toString());
    // Check the first couple of lines, details tests of the actual content
    // are performed by DbObjectXMLTransformerTest
    BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
    String firstLine = reader.readLine();
    assertTrue(firstLine.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    String xsd = "xmlns=\"http://www.alfresco.org/repo/db-schema\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xsi:schemaLocation=\"http://www.alfresco.org/repo/db-schema db-schema.xsd\"";
    assertTrue(firstLine.endsWith("<schema " + xsd + " name=\"alfresco\" dbprefix=\"my-prefix\" version=\"501\" tablecolumnorder=\"true\">"));
    assertEquals("  <validators>", reader.readLine());
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Schema(org.alfresco.util.schemacomp.model.Schema) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 19 with Schema

use of org.alfresco.util.schemacomp.model.Schema in project alfresco-repository by Alfresco.

the class SchemaComparatorTest method systemGeneratedPrimaryKeyAndIndex.

/**
 * Tests index of primary key validation, problem found when comparing DB2 schemas (now EOLed) which has
 * system generated indexes for primary keys, but might still be useful as a test.
 */
@Test
public void systemGeneratedPrimaryKeyAndIndex() {
    reference = new Schema("schema", "alf_", 9012, false);
    target = new Schema("schema", "alf_", 9012, false);
    NameValidator validator = new NameValidator();
    validator.setProperty("pattern", "SQL[0-9]+");
    final List<DbValidator> validators = new ArrayList<DbValidator>();
    validators.add(new NameValidator());
    reference.add(new Table(reference, "ALF_ACL_CHANGE_SET", columns(false, "id NUMBER(10)", "nodeRef VARCHAR2(200)", "name VARCHAR2(150)"), new PrimaryKey(null, "SQL120116153559440", Arrays.asList("id", "nodeRef"), Arrays.asList(1, 2)), fkeys(), indexes("SQL120116153559441 [unique] ID_", "fooX ID_")));
    // Target schema's database objects
    target.add(new Table(target, "ALF_ACL_CHANGE_SET", columns(false, "id NUMBER(10)", "name VARCHAR2(150)", "nodeRef VARCHAR2(200)"), new PrimaryKey(null, "SQL120116153559442", Arrays.asList("id", "nodeRef"), Arrays.asList(1, 2)), fkeys(), indexes("SQL120116153559443 [unique] ID_", "fooX ID_")));
    reference.add(new Table(reference, "ALF_LOCK_RESOURCE", columns(false, "id NUMBER(10)", "nodeRef VARCHAR2(200)", "name VARCHAR2(150)"), new PrimaryKey(null, "SQL120116153554310", Arrays.asList("ID", "int"), Arrays.asList(1, 2)), fkeys(), indexes("SQL120116153616440 [unique] ID_")));
    target.add(new Table(reference, "ALF_LOCK_RESOURCE", columns(false, "id NUMBER(10)", "nodeRef VARCHAR2(200)", "name VARCHAR2(150)"), new PrimaryKey(null, "SQL120116153554313", Arrays.asList("ID", "int"), Arrays.asList(1, 2)), fkeys(), indexes("SQL120116153616444 [unique] ID_")));
    /**
     * Now plug in the pattern validator
     */
    DbObjectVisitor visitor = new DbObjectVisitor() {

        @Override
        public void visit(DbObject dbObject) {
            if (dbObject instanceof Index) {
                dbObject.setValidators(validators);
            }
            if (dbObject instanceof PrimaryKey) {
                dbObject.setValidators(validators);
            }
        }
    };
    reference.accept(visitor);
    target.accept(visitor);
    comparator = new SchemaComparator(reference, target, dialect);
    comparator.validateAndCompare();
    // See stdout for diagnostics dump...
    dumpDiffs(comparator.getComparisonResults(), false);
    dumpValidation(comparator.getComparisonResults());
    Results results = comparator.getComparisonResults();
    // There are no logical differences
    assertEquals(0, results.size());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) DbObject(org.alfresco.util.schemacomp.model.DbObject) NameValidator(org.alfresco.util.schemacomp.validator.NameValidator) Schema(org.alfresco.util.schemacomp.model.Schema) ArrayList(java.util.ArrayList) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator) Test(org.junit.Test)

Example 20 with Schema

use of org.alfresco.util.schemacomp.model.Schema in project alfresco-repository by Alfresco.

the class DbObjectXMLTransformerTest method transformSchemaNoColumnOrderCheck.

@Test
public void transformSchemaNoColumnOrderCheck() throws IOException {
    Collection<Column> columns = columns("one VARCHAR2(100)", "two NUMBER(10)");
    PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id"), Arrays.asList(1));
    Collection<ForeignKey> fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc"));
    Collection<Index> indexes = indexes("index_one col1 col2", "index_two col3 col4");
    Table tableOne = new Table(null, "table_one", columns, pk, fks, indexes);
    Table tableTwo = new Table(null, "table_two", columns, pk, fks, indexes);
    Schema schema = new Schema("my_schema", "alf_", 132, false);
    schema.add(tableOne);
    schema.add(tableTwo);
    schema.add(new Sequence(null, "sequence_one"));
    schema.add(new Sequence(null, "sequence_two"));
    schema.add(new Sequence(null, "sequence_three"));
    schema.setValidators(new ArrayList<DbValidator>());
    transformer.output(schema);
    BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
    dumpOutput();
    assertHasPreamble(reader);
    assertEquals("<schema " + "xmlns=\"http://www.alfresco.org/repo/db-schema\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xsi:schemaLocation=\"http://www.alfresco.org/repo/db-schema db-schema.xsd\" " + "name=\"my_schema\" dbprefix=\"alf_\" version=\"132\" tablecolumnorder=\"false\">", reader.readLine());
    assertEquals("  <objects>", reader.readLine());
    skipUntilEnd("       {table}", reader);
    skipUntilEnd("       {table}", reader);
    skipUntilEnd("       {sequence}", reader, true);
    skipUntilEnd("       {sequence}", reader, true);
    skipUntilEnd("       {sequence}", reader, true);
    assertEquals("  </objects>", reader.readLine());
    assertEquals("</schema>", reader.readLine());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Schema(org.alfresco.util.schemacomp.model.Schema) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) Sequence(org.alfresco.util.schemacomp.model.Sequence) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey) Column(org.alfresco.util.schemacomp.model.Column) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator) Test(org.junit.Test)

Aggregations

Schema (org.alfresco.util.schemacomp.model.Schema)20 Table (org.alfresco.util.schemacomp.model.Table)11 Test (org.junit.Test)9 PrimaryKey (org.alfresco.util.schemacomp.model.PrimaryKey)7 Index (org.alfresco.util.schemacomp.model.Index)6 Sequence (org.alfresco.util.schemacomp.model.Sequence)5 DbValidator (org.alfresco.util.schemacomp.validator.DbValidator)5 Column (org.alfresco.util.schemacomp.model.Column)4 DbObject (org.alfresco.util.schemacomp.model.DbObject)4 BufferedReader (java.io.BufferedReader)3 StringReader (java.io.StringReader)3 ForeignKey (org.alfresco.util.schemacomp.model.ForeignKey)3 StringWriter (java.io.StringWriter)2 StreamResult (javax.xml.transform.stream.StreamResult)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1