use of org.alfresco.util.schemacomp.validator.DbValidator in project alfresco-repository by Alfresco.
the class ValidatingVisitorTest method setUp.
@Before
public void setUp() throws Exception {
refTable = new Table("reference_table");
refIndex = new Index(refTable, "index_name", Arrays.asList("a", "b", "c"));
ctx = new DiffContext(new MySQLInnoDBDialect(), refSchema, targetSchema);
visitor = new ValidatingVisitor(ctx);
validators = new ArrayList<DbValidator>();
validators.add(Mockito.mock(DbValidator.class));
validators.add(Mockito.mock(DbValidator.class));
refIndex.setValidators(validators);
targetTable = new Table("target_table");
targetIndex1 = new Index(targetTable, "index_name", Arrays.asList("a", "b", "c"));
targetIndex2 = new Index(targetTable, "another_index", Arrays.asList("a", "b", "c"));
targetIndex3 = new Index(targetTable, "index_name", Arrays.asList("e", "f"));
comparisonUtils = Mockito.mock(ComparisonUtils.class);
visitor.setComparisonUtils(comparisonUtils);
}
use of org.alfresco.util.schemacomp.validator.DbValidator in project alfresco-repository by Alfresco.
the class XMLToSchemaTest method canReadSchemaXML.
@Test
public void canReadSchemaXML() {
xmlToSchema.parse();
Schema schema = xmlToSchema.getSchema();
assertNotNull("A null Schema object was returned", schema);
assertNull("Schema isn't meant to have a parent", schema.getParent());
assertEquals("alfresco", schema.getName());
assertEquals("myprefix_", schema.getDbPrefix());
assertEquals(325, schema.getVersion());
Iterator<DbObject> objects = schema.iterator();
Table table = (Table) objects.next();
assertSame("Wrong or no parent set on table", schema, table.getParent());
assertEquals("node", table.getName());
assertEquals(3, table.getColumns().size());
assertSame("Wrong or no parent set", table, table.getColumns().get(0).getParent());
assertEquals("id", table.getColumns().get(0).getName());
assertEquals("NUMBER(10)", table.getColumns().get(0).getType());
assertEquals(false, table.getColumns().get(0).isNullable());
assertEquals(1, table.getColumns().get(0).getOrder());
assertEquals(true, table.getColumns().get(0).isAutoIncrement());
assertSame("Wrong or no parent set", table, table.getColumns().get(1).getParent());
assertEquals("nodeRef", table.getColumns().get(1).getName());
assertEquals("VARCHAR2(200)", table.getColumns().get(1).getType());
assertEquals(false, table.getColumns().get(1).isNullable());
assertEquals(3, table.getColumns().get(1).getOrder());
assertEquals(false, table.getColumns().get(1).isAutoIncrement());
assertSame("Wrong or no parent set", table, table.getColumns().get(2).getParent());
assertEquals("name", table.getColumns().get(2).getName());
assertEquals("VARCHAR2(150)", table.getColumns().get(2).getType());
assertEquals(true, table.getColumns().get(2).isNullable());
assertEquals(2, table.getColumns().get(2).getOrder());
assertEquals(false, table.getColumns().get(2).isAutoIncrement());
assertSame("Wrong or no parent set", table, table.getPrimaryKey().getParent());
assertEquals("pk_node", table.getPrimaryKey().getName());
assertEquals(1, table.getPrimaryKey().getColumnNames().size());
assertEquals("id", table.getPrimaryKey().getColumnNames().get(0));
assertEquals(1, table.getPrimaryKey().getColumnOrders().get(0).intValue());
assertEquals(1, table.getForeignKeys().size());
assertSame("Wrong or no parent set", table, table.getForeignKeys().get(0).getParent());
assertEquals("fk_node_noderef", table.getForeignKeys().get(0).getName());
assertEquals("nodeRef", table.getForeignKeys().get(0).getLocalColumn());
assertEquals("node", table.getForeignKeys().get(0).getTargetTable());
assertEquals("nodeRef", table.getForeignKeys().get(0).getTargetColumn());
assertEquals(1, table.getIndexes().size());
Index index = table.getIndexes().get(0);
assertSame("Wrong or no parent set on index", table, index.getParent());
assertEquals("idx_node_by_id", index.getName());
assertEquals(true, index.isUnique());
assertEquals(2, index.getColumnNames().size());
assertEquals("id", index.getColumnNames().get(0));
assertEquals("nodeRef", index.getColumnNames().get(1));
assertEquals(1, index.getValidators().size());
DbValidator validator = index.getValidators().get(0);
assertEquals(NameValidator.class, validator.getClass());
assertEquals(1, validator.getPropertyNames().size());
assertEquals("idx_.+", validator.getProperty("pattern"));
Sequence seq = (Sequence) objects.next();
assertSame("Wrong or no parent set", schema, seq.getParent());
assertEquals("node_seq", seq.getName());
seq = (Sequence) objects.next();
assertSame("Wrong or no parent set", schema, seq.getParent());
assertEquals("person_seq", seq.getName());
seq = (Sequence) objects.next();
assertSame("Wrong or no parent set", schema, seq.getParent());
assertEquals("content_seq", seq.getName());
assertFalse("Should be no more DB objects", objects.hasNext());
}
use of org.alfresco.util.schemacomp.validator.DbValidator in project alfresco-repository by Alfresco.
the class DefaultComparisonUtilsTest method collectionPropertyIsNotComparedWhenValidatorTakesResponsibility.
@Test
public void collectionPropertyIsNotComparedWhenValidatorTakesResponsibility() {
Collection<Object> refCollection = new ArrayList<Object>();
refCollection.add(123);
refCollection.add("both");
DbObject refDbObj = new DbObjectWithCollection("left", refCollection);
DbProperty refCollProp = new DbProperty(refDbObj, "collection");
Collection<Object> targetCollection = new ArrayList<Object>();
targetCollection.add(234);
targetCollection.add("both");
DbObject targetDbObj = new DbObjectWithCollection("right", targetCollection);
DbProperty targetCollProp = new DbProperty(targetDbObj, "collection");
DbValidator validator = mock(DbValidator.class);
when(validator.validates("collection")).thenReturn(true);
refDbObj.getValidators().add(validator);
comparisonUtils.compareSimpleCollections(refCollProp, targetCollProp, ctx);
// No information should be reported...
verify(differences, never()).add(Where.IN_BOTH_NO_DIFFERENCE, dbPropForValue(refDbObj, "collection[1]", "both"), dbPropForValue(targetDbObj, "collection[1]", "both"));
verify(differences, never()).add(Where.ONLY_IN_REFERENCE, dbPropForValue(refDbObj, "collection[0]", 123), dbPropForValue(targetDbObj, "collection", targetCollection));
verify(differences, never()).add(Where.ONLY_IN_TARGET, dbPropForValue(refDbObj, "collection", refCollection), dbPropForValue(targetDbObj, "collection[0]", 234));
}
use of org.alfresco.util.schemacomp.validator.DbValidator in project alfresco-repository by Alfresco.
the class DbObjectXMLTransformerTest method transformSchemaWithColumnOrderCheck.
@Test
public void transformSchemaWithColumnOrderCheck() 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, true);
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=\"true\">", 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());
}
use of org.alfresco.util.schemacomp.validator.DbValidator in project alfresco-repository by Alfresco.
the class XMLToSchema method endElement.
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals(XML.EL_TABLE)) {
Table table = (Table) stack.pop();
schema.add(table);
} else if (qName.equals(XML.EL_COLUMN)) {
Column column = (Column) stack.pop();
Table table = (Table) stack.peek();
column.setParent(table);
table.getColumns().add(column);
} else if (qName.equals(XML.EL_PRIMARY_KEY)) {
PrimaryKey pk = (PrimaryKey) stack.pop();
Table table = (Table) stack.peek();
table.setPrimaryKey(pk);
} else if (qName.equals(XML.EL_FOREIGN_KEY)) {
ForeignKey fk = (ForeignKey) stack.pop();
Table table = (Table) stack.peek();
fk.setParent(table);
table.getForeignKeys().add(fk);
} else if (qName.equals(XML.EL_INDEX)) {
Index index = (Index) stack.pop();
Table table = (Table) stack.peek();
index.setParent(table);
table.getIndexes().add(index);
} else if (qName.equals(XML.EL_SEQUENCE)) {
Sequence seq = (Sequence) stack.pop();
seq.setParent(schema);
schema.add(seq);
} else if (qName.equals(XML.EL_VALIDATOR)) {
DbValidator validator = (DbValidator) stack.pop();
DbObject dbo = (DbObject) stack.peek();
dbo.getValidators().add(validator);
}
// Deal with elements that contain text.
if (lastText.length() != 0) {
if (qName.equals(XML.EL_TYPE)) {
Column column = (Column) stack.peek();
column.setType(lastText.toString());
} else if (qName.equals(XML.EL_NULLABLE)) {
Column column = (Column) stack.peek();
column.setNullable(Boolean.parseBoolean(lastText.toString()));
} else if (qName.equals(XML.EL_AUTOINCREMENT)) {
Column column = (Column) stack.peek();
column.setAutoIncrement(Boolean.parseBoolean(lastText.toString()));
} else if (qName.equals(XML.EL_COLUMN_NAME)) {
if (stack.peek() instanceof PrimaryKey) {
PrimaryKey pk = (PrimaryKey) stack.peek();
pk.getColumnNames().add(lastText.toString());
} else if (stack.peek() instanceof Index) {
Index index = (Index) stack.peek();
index.getColumnNames().add(lastText.toString());
}
} else if (qName.equals(XML.EL_LOCAL_COLUMN)) {
ForeignKey fk = (ForeignKey) stack.peek();
fk.setLocalColumn(lastText.toString());
} else if (qName.equals(XML.EL_TARGET_TABLE)) {
ForeignKey fk = (ForeignKey) stack.peek();
fk.setTargetTable(lastText.toString());
} else if (qName.equals(XML.EL_TARGET_COLUMN)) {
ForeignKey fk = (ForeignKey) stack.peek();
fk.setTargetColumn(lastText.toString());
} else if (qName.equals(XML.EL_PROPERTY)) {
String propValue = lastText.toString();
String propName = (String) stack.pop();
if (stack.peek() instanceof DbValidator) {
@SuppressWarnings("unchecked") DbValidator validator = (DbValidator) stack.peek();
validator.setProperty(propName, propValue);
}
}
}
}
Aggregations