Search in sources :

Example 16 with Table

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

the class DbObjectXMLTransformerTest method transformObjectWithValidators.

@Test
public void transformObjectWithValidators() 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 table = new Table(null, "my_table", columns, pk, fks, indexes);
    NameValidator nameValidator = new NameValidator();
    nameValidator.setPattern(Pattern.compile("match_me_if_you_can"));
    List<DbValidator> validators = new ArrayList<DbValidator>();
    validators.add(nameValidator);
    table.setValidators(validators);
    transformer.output(table);
    BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
    dumpOutput();
    assertHasPreamble(reader);
    assertEquals("<table name=\"my_table\">", reader.readLine());
    assertEquals("  <validators>", reader.readLine());
    assertEquals("    <validator class=\"org.alfresco.util.schemacomp.validator.NameValidator\">", reader.readLine());
    assertEquals("      <properties>", reader.readLine());
    assertEquals("        <property name=\"pattern\">match_me_if_you_can</property>", reader.readLine());
    assertEquals("      </properties>", reader.readLine());
    assertEquals("    </validator>", reader.readLine());
    assertEquals("  </validators>", reader.readLine());
    assertEquals("  <columns>", reader.readLine());
    skipUntilEnd("       {column}", reader);
    skipUntilEnd("       {column}", reader);
    assertEquals("  </columns>", reader.readLine());
    skipUntilEnd("  {primarykey}", reader);
    assertEquals("  <foreignkeys>", reader.readLine());
    skipUntilEnd("       {foreignkey}", reader);
    skipUntilEnd("       {foreignkey}", reader);
    assertEquals("  </foreignkeys>", reader.readLine());
    assertEquals("  <indexes>", reader.readLine());
    skipUntilEnd("       {index}", reader);
    skipUntilEnd("       {index}", reader);
    assertEquals("  </indexes>", reader.readLine());
    assertEquals("</table>", reader.readLine());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) NameValidator(org.alfresco.util.schemacomp.validator.NameValidator) ArrayList(java.util.ArrayList) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) 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)

Example 17 with Table

use of org.alfresco.util.schemacomp.model.Table 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 18 with Table

use of org.alfresco.util.schemacomp.model.Table 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)

Example 19 with Table

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

the class DbObjectXMLTransformerTest method transformTable.

@Test
public void transformTable() 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 table = new Table(null, "my_table", columns, pk, fks, indexes);
    transformer.output(table);
    BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
    dumpOutput();
    assertHasPreamble(reader);
    assertEquals("<table name=\"my_table\">", reader.readLine());
    assertEquals("  <columns>", reader.readLine());
    skipUntilEnd("       {column}", reader);
    skipUntilEnd("       {column}", reader);
    assertEquals("  </columns>", reader.readLine());
    skipUntilEnd("  {primarykey}", reader);
    assertEquals("  <foreignkeys>", reader.readLine());
    skipUntilEnd("       {foreignkey}", reader);
    skipUntilEnd("       {foreignkey}", reader);
    assertEquals("  </foreignkeys>", reader.readLine());
    assertEquals("  <indexes>", reader.readLine());
    skipUntilEnd("       {index}", reader);
    skipUntilEnd("       {index}", reader);
    assertEquals("  </indexes>", reader.readLine());
    assertEquals("</table>", reader.readLine());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Column(org.alfresco.util.schemacomp.model.Column) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey) Test(org.junit.Test)

Aggregations

Table (org.alfresco.util.schemacomp.model.Table)19 Test (org.junit.Test)13 PrimaryKey (org.alfresco.util.schemacomp.model.PrimaryKey)11 Schema (org.alfresco.util.schemacomp.model.Schema)11 Index (org.alfresco.util.schemacomp.model.Index)10 DbValidator (org.alfresco.util.schemacomp.validator.DbValidator)8 Column (org.alfresco.util.schemacomp.model.Column)7 ForeignKey (org.alfresco.util.schemacomp.model.ForeignKey)7 Sequence (org.alfresco.util.schemacomp.model.Sequence)7 BufferedReader (java.io.BufferedReader)5 StringReader (java.io.StringReader)5 DbObject (org.alfresco.util.schemacomp.model.DbObject)5 ArrayList (java.util.ArrayList)2 NameValidator (org.alfresco.util.schemacomp.validator.NameValidator)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 MySQLInnoDBDialect (org.alfresco.repo.domain.dialect.MySQLInnoDBDialect)1 Before (org.junit.Before)1