Search in sources :

Example 11 with Table

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

the class XMLToSchema method startElement.

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    lastText.setLength(0);
    if (qName.equals(XML.EL_SCHEMA)) {
        String name = atts.getValue(XML.ATTR_NAME);
        String dbPrefix = atts.getValue(XML.ATTR_DB_PREFIX);
        int version = Integer.parseInt(atts.getValue(XML.ATTR_VERSION));
        String attrTableColumnOrder = atts.getValue(XML.ATTR_TABLE_COLUMN_ORDER);
        // Should column order be checked for tables?
        boolean compareTableColOrder = attrTableColumnOrder != null ? Boolean.parseBoolean(attrTableColumnOrder) : true;
        schema = new Schema(name, dbPrefix, version, compareTableColOrder);
        stack.push(schema);
    } else if (qName.equals(XML.EL_TABLE)) {
        stack.push(new Table(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_COLUMN)) {
        Column column = new Column(atts.getValue(XML.ATTR_NAME));
        if (atts.getValue(XML.ATTR_ORDER) != null) {
            int order = Integer.parseInt(atts.getValue(XML.ATTR_ORDER));
            column.setOrder(order);
        }
        column.setCompareOrder(schema.isCheckTableColumnOrder());
        stack.push(column);
    } else if (qName.equals(XML.EL_COLUMN_NAME)) {
        if (stack.peek() instanceof PrimaryKey && atts.getValue(XML.ATTR_ORDER) != null) {
            PrimaryKey pk = (PrimaryKey) stack.peek();
            Integer columnOrder = Integer.parseInt(atts.getValue(XML.ATTR_ORDER));
            pk.getColumnOrders().add(columnOrder);
        }
    } else if (qName.equals(XML.EL_PRIMARY_KEY)) {
        stack.push(new PrimaryKey(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_FOREIGN_KEY)) {
        stack.push(new ForeignKey(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_INDEX)) {
        Index index = new Index(atts.getValue(XML.ATTR_NAME));
        boolean unique = Boolean.parseBoolean(atts.getValue(XML.ATTR_UNIQUE));
        index.setUnique(unique);
        stack.push(index);
    } else if (qName.equals(XML.EL_SEQUENCE)) {
        stack.push(new Sequence(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_VALIDATOR)) {
        String className = atts.getValue(XML.ATTR_CLASS);
        DbValidator validator = null;
        try {
            validator = (DbValidator) Class.forName(className).newInstance();
        } catch (Throwable e) {
            throw new RuntimeException("Couldn't create validator, class: " + className, e);
        }
        stack.push(validator);
    } else if (qName.equals(XML.EL_PROPERTY)) {
        String name = atts.getValue(XML.ATTR_NAME);
        stack.push(name);
    }
}
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) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator)

Example 12 with Table

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

the class IndexColumnsValidatorTest method testNonIndex.

@Test
public void testNonIndex() {
    validator.setPattern(Pattern.compile("SYS_NC.+"));
    assertEquals("SYS_NC.+", validator.getPattern().toString());
    try {
        validator.validate(null, new Table("SYS_NC234234"), ctx);
        fail("Validator should faile for non-index object");
    } catch (AlfrescoRuntimeException e) {
    // expected to fail for table object
    }
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) Test(org.junit.Test)

Example 13 with Table

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

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

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

the class DbObjectXMLTransformerTest method transformTableWithoutPrimaryKey.

/**
 * ALF-13979: empty table causes NPE during schema export.
 * @throws IOException
 */
@Test
public void transformTableWithoutPrimaryKey() throws IOException {
    Table table = new Table("my_table");
    assertFalse(table.hasPrimaryKey());
    transformer.output(table);
    BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
    dumpOutput();
    assertHasPreamble(reader);
    skipUntilEnd("  {columns}", reader, true);
    skipUntilEnd("  {foreignkeys}", reader, true);
    skipUntilEnd("  {indexes}", reader, true);
    assertEquals("</table>", reader.readLine());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) 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