ALF-7260: schema comparator

* replace path/push/pop mechanism as it doesn't work well at reporting where differences/validation errors occur.
* add getParent() to DbObject - so that a path-style identifier can be deduced for a DbObject when needed
* add DbProperty to specify a specific DbObject's property and value -- acts as a schema location pointer
* refactored Result code (need difference result and validation error result)





git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31527 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2011-10-27 18:07:06 +00:00
parent 385003c6c9
commit 723fe98cf2
37 changed files with 1022 additions and 613 deletions

View File

@@ -18,17 +18,12 @@
*/
package org.alfresco.util.schemacomp.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.alfresco.util.schemacomp.ComparisonUtils;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Result.Strength;
import org.alfresco.util.schemacomp.DbProperty;
import org.alfresco.util.schemacomp.DefaultComparisonUtils;
import org.springframework.util.StringUtils;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Result.Strength;
import org.alfresco.util.schemacomp.Results;
/**
* Useful base class for many, if not all the {@link DbObject} implementations.
@@ -37,25 +32,22 @@ import org.springframework.util.StringUtils;
*/
public abstract class AbstractDbObject implements DbObject
{
private DbObject parent;
private String name;
/** How differences in the name field should be reported */
private Strength nameStrength = Strength.ERROR;
protected ComparisonUtils comparisonUtils = new DefaultComparisonUtils();
/**
* Default constructor
*/
public AbstractDbObject()
{
}
/**
* Instantiate, giving the object a name.
* Instantiate, giving the object a parent and a name.
*
* @param parent
* @param name
*/
public AbstractDbObject(String name)
public AbstractDbObject(DbObject parent, String name)
{
this.parent = parent;
this.name = name;
}
@@ -152,8 +144,8 @@ public abstract class AbstractDbObject implements DbObject
}
/**
* Provides an implementation of {@link DbObject#diff(DbObject, Differences)}. The template
* method {@link #doDiff(DbObject, Differences)} provides the subclass specific diffing logic,
* Provides an implementation of {@link DbObject#diff(DbObject, Results)}. The template
* method {@link #doDiff(DbObject, Results)} provides the subclass specific diffing logic,
* whilst this method handles the workflow required in most cases: set the path's prefix that will be
* used to explain where differences occur; compare the name fields of the two objects; delegate to the
* subclass specific diffing (if any); remove the last path addition ready for the next object to perform
@@ -161,23 +153,30 @@ public abstract class AbstractDbObject implements DbObject
*/
@Override
public void diff(DbObject right, DiffContext ctx, Strength strength)
{
Differences differences = ctx.getDifferences();
{
DbProperty leftNameProp = new DbProperty(this, "name");
DbProperty rightNameProp = new DbProperty(right, "name");
comparisonUtils.compareSimple(leftNameProp, rightNameProp, ctx, getNameStrength());
if (name != null && StringUtils.hasText(name))
{
differences.pushPath(name);
}
else
{
differences.pushPath("<" + getClass().getSimpleName() + ">");
}
comparisonUtils.compareSimple(name, right.getName(), ctx, getNameStrength());
doDiff(right, ctx, strength);
differences.popPath();
}
@Override
public DbObject getParent()
{
return parent;
}
@Override
public void setParent(DbObject parent)
{
this.parent = parent;
}
/**
* Override this method to provide subclass specific diffing logic.
*

View File

@@ -26,10 +26,11 @@ import static org.mockito.Mockito.inOrder;
import java.util.ArrayList;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DbProperty;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Difference.Where;
import org.alfresco.util.schemacomp.Result.Strength;
import org.alfresco.util.schemacomp.Result.Where;
import org.alfresco.util.schemacomp.Results;
import org.alfresco.util.schemacomp.ValidationResult;
import org.hibernate.dialect.Dialect;
import org.junit.Before;
@@ -48,7 +49,7 @@ import org.mockito.runners.MockitoJUnitRunner;
public class AbstractDbObjectTest
{
private ConcreteDbObject dbObject;
private @Mock Differences differences;
private @Mock Results differences;
private DiffContext ctx;
private @Mock Dialect dialect;
@@ -93,37 +94,52 @@ public class AbstractDbObjectTest
dbObject.diff(otherObject, ctx, Strength.ERROR);
InOrder inOrder = inOrder(differences);
// The name of the object should be pushed on to the differences path.
inOrder.verify(differences).pushPath("the_object");
// The name of the object should be diffed
inOrder.verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, "the_object", "the_other_object", Strength.WARN);
inOrder.verify(differences).add(
Where.IN_BOTH_BUT_DIFFERENCE,
new DbProperty(dbObject, "name"),
new DbProperty(otherObject, "name"),
Strength.WARN);
// Then the doDiff() method should be processed
inOrder.verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, "left", "right");
// Later, the path should be popped again
inOrder.verify(differences).popPath();
inOrder.verify(differences).add(
Where.IN_BOTH_BUT_DIFFERENCE,
new DbProperty(dbObject, "someProp"),
new DbProperty(otherObject, "someProp"));
}
/**
* Concrete DbObject for testing the AbstractDbObject base class.
*/
private static class ConcreteDbObject extends AbstractDbObject
public static class ConcreteDbObject extends AbstractDbObject
{
private String someProp = "property value";
public ConcreteDbObject(String name)
{
super(name);
super(null, name);
}
@Override
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
{
Differences differences = ctx.getDifferences();
differences.add(Where.IN_BOTH_BUT_DIFFERENCE, "left", "right");
Results differences = ctx.getDifferences();
differences.add(
Where.IN_BOTH_BUT_DIFFERENCE,
new DbProperty(this, "someProp"),
new DbProperty(right, "someProp"));
}
@Override
public void accept(DbObjectVisitor visitor)
{
}
public String getSomeProp()
{
return this.someProp;
}
}
}

View File

@@ -19,8 +19,9 @@
package org.alfresco.util.schemacomp.model;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DbProperty;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Results;
import org.alfresco.util.schemacomp.Result.Strength;
/**
@@ -37,13 +38,14 @@ public class Column extends AbstractDbObject
/**
* Construct a Column.
*
* @table the parent table
* @param name
* @param type
* @param nullable
*/
public Column(String name, String type, boolean nullable)
public Column(Table table, String name, String type, boolean nullable)
{
super(name);
super(table, name);
this.type = type;
this.nullable = nullable;
}
@@ -109,10 +111,16 @@ public class Column extends AbstractDbObject
@Override
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
{
Differences differences = ctx.getDifferences();
Column rightColumn = (Column) right;
comparisonUtils.compareSimple(type, rightColumn.type, ctx);
comparisonUtils.compareSimple(nullable, rightColumn.nullable, ctx);
Results differences = ctx.getDifferences();
DbProperty thisTypeProp = new DbProperty(this, "type");
DbProperty thisNullableProp = new DbProperty(this, "nullable");
Column thatColumn = (Column) right;
DbProperty thatTypeProp = new DbProperty(thatColumn, "type");
DbProperty thatNullableProp = new DbProperty(thatColumn, "nullable");
comparisonUtils.compareSimple(thisTypeProp, thatTypeProp, ctx);
comparisonUtils.compareSimple(thisNullableProp, thatNullableProp, ctx);
}
@Override

View File

@@ -19,6 +19,7 @@
package org.alfresco.util.schemacomp.model;
import org.alfresco.util.schemacomp.DbProperty;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.*;
@@ -36,8 +37,8 @@ public class ColumnTest extends DbObjectTestBase<Column>
@Before
public void setUp() throws Exception
{
thisColumn = new Column("this_column", "VARCHAR2(100)", false);
thatColumn = new Column("this_column", "NUMBER(10)", true);
thisColumn = new Column(null, "this_column", "VARCHAR2(100)", false);
thatColumn = new Column(null, "that_column", "NUMBER(10)", true);
}
@Override
@@ -55,8 +56,14 @@ public class ColumnTest extends DbObjectTestBase<Column>
@Override
protected void doDiffTests()
{
inOrder.verify(comparisonUtils).compareSimple(thisColumn.getType(), thatColumn.getType(), ctx);
inOrder.verify(comparisonUtils).compareSimple(thisColumn.isNullable(), thatColumn.isNullable(), ctx);
DbProperty thisTypeProp = new DbProperty(thisColumn, "type");
DbProperty thatTypeProp = new DbProperty(thatColumn, "type");
inOrder.verify(comparisonUtils).compareSimple(thisTypeProp, thatTypeProp, ctx);
DbProperty thisNullableProp = new DbProperty(thisColumn, "nullable");
DbProperty thatNullableProp = new DbProperty(thatColumn, "nullable");
inOrder.verify(comparisonUtils).compareSimple(thisNullableProp, thatNullableProp, ctx);
}
@Test

View File

@@ -20,7 +20,7 @@ package org.alfresco.util.schemacomp.model;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Results;
import org.alfresco.util.schemacomp.Result.Strength;
/**
@@ -54,7 +54,7 @@ public interface DbObject
/**
* Generate a report of differences between this object ('left') and another object ('right').
* Differences between the left and right objects under inspection are captured in the {@link Differences}
* Differences between the left and right objects under inspection are captured in the {@link Results}
* object passed in to this method.
*
* @param right The object to compare against.
@@ -70,4 +70,20 @@ public interface DbObject
* @param visitor
*/
void accept(DbObjectVisitor visitor);
/**
* Get the parent object for which this object is a child. If this is the root object
* then null should be returned.
*
* @return Parent reference or null
*/
DbObject getParent();
/**
* Sets the parent object.
*
* @see #getParent()
* @param parent
*/
void setParent(DbObject parent);
}

View File

@@ -25,8 +25,9 @@ import java.util.List;
import org.alfresco.util.schemacomp.ComparisonUtils;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DbProperty;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Results;
import org.alfresco.util.schemacomp.Result.Strength;
import org.alfresco.util.schemacomp.ValidationResult;
import org.hibernate.dialect.Dialect;
@@ -46,7 +47,7 @@ import org.mockito.runners.MockitoJUnitRunner;
public abstract class DbObjectTestBase<T extends AbstractDbObject>
{
protected @Mock Dialect dialect;
protected @Mock Differences differences;
protected @Mock Results differences;
protected DiffContext ctx;
protected @Mock ComparisonUtils comparisonUtils;
protected InOrder inOrder;
@@ -89,21 +90,15 @@ public abstract class DbObjectTestBase<T extends AbstractDbObject>
// Invoke the method under test
thisObject.diff(thatObject, ctx, Strength.ERROR);
// The name of the object should be pushed on to the differences path.
inOrder.verify(differences).pushPath(thisObject.getName());
// The name of the object should be diffed
inOrder.verify(comparisonUtils).compareSimple(
thisObject.getName(),
thatObject.getName(),
new DbProperty(thisObject, "name"),
new DbProperty(thatObject, "name"),
ctx,
thisObject.getNameStrength());
// Then the doDiff() method should be processed...
doDiffTests();
// Later, the path should be popped again
inOrder.verify(differences).popPath();
}
protected abstract void doDiffTests();

View File

@@ -19,8 +19,9 @@
package org.alfresco.util.schemacomp.model;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DbProperty;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Results;
import org.alfresco.util.schemacomp.Result.Strength;
@@ -40,14 +41,15 @@ public class ForeignKey extends AbstractDbObject
/**
* Constructor.
*
* @param table the parent table
* @param fkName
* @param localColumn
* @param targetTable
* @param targetColumn
*/
public ForeignKey(String fkName, String localColumn, String targetTable, String targetColumn)
public ForeignKey(Table table, String fkName, String localColumn, String targetTable, String targetColumn)
{
super(fkName);
super(table, fkName);
this.localColumn = localColumn;
this.targetTable = targetTable;
this.targetColumn = targetColumn;
@@ -141,11 +143,19 @@ public class ForeignKey extends AbstractDbObject
@Override
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
{
Differences differences = ctx.getDifferences();
ForeignKey rightFK = (ForeignKey) right;
comparisonUtils.compareSimple(localColumn, rightFK.localColumn, ctx);
comparisonUtils.compareSimple(targetTable, rightFK.targetTable, ctx);
comparisonUtils.compareSimple(targetColumn, rightFK.targetColumn, ctx);
ForeignKey thatFK = (ForeignKey) right;
comparisonUtils.compareSimple(
new DbProperty(this, "localColumn"),
new DbProperty(thatFK, "localColumn"),
ctx);
comparisonUtils.compareSimple(
new DbProperty(this, "targetTable"),
new DbProperty(thatFK, "targetTable"),
ctx);
comparisonUtils.compareSimple(
new DbProperty(this, "targetColumn"),
new DbProperty(thatFK, "targetColumn"),
ctx);
}
@Override

View File

@@ -21,6 +21,7 @@ package org.alfresco.util.schemacomp.model;
import static org.mockito.Mockito.verify;
import org.alfresco.util.schemacomp.DbProperty;
import org.junit.Before;
import org.junit.Test;
@@ -37,8 +38,8 @@ public class ForeignKeyTest extends DbObjectTestBase<ForeignKey>
@Before
public void setUp() throws Exception
{
thisFK = new ForeignKey("this_fk", "local_col", "target_table", "target_col");
thatFK = new ForeignKey("that_fk", "local_col", "target_table", "target_col");
thisFK = new ForeignKey(null, "this_fk", "local_col", "target_table", "target_col");
thatFK = new ForeignKey(null, "that_fk", "local_col", "target_table", "target_col");
}
@@ -59,9 +60,18 @@ public class ForeignKeyTest extends DbObjectTestBase<ForeignKey>
@Override
protected void doDiffTests()
{
inOrder.verify(comparisonUtils).compareSimple(thisFK.getLocalColumn(), thatFK.getLocalColumn(), ctx);
inOrder.verify(comparisonUtils).compareSimple(thisFK.getTargetTable(), thatFK.getTargetTable(), ctx);
inOrder.verify(comparisonUtils).compareSimple(thisFK.getTargetColumn(), thatFK.getTargetColumn(), ctx);
inOrder.verify(comparisonUtils).compareSimple(
new DbProperty(thisFK, "localColumn"),
new DbProperty(thatFK, "localColumn"),
ctx);
inOrder.verify(comparisonUtils).compareSimple(
new DbProperty(thisFK, "targetTable"),
new DbProperty(thatFK, "targetTable"),
ctx);
inOrder.verify(comparisonUtils).compareSimple(
new DbProperty(thisFK, "targetColumn"),
new DbProperty(thatFK, "targetColumn"),
ctx);
}
@Test

View File

@@ -21,8 +21,8 @@ package org.alfresco.util.schemacomp.model;
import java.util.List;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DbProperty;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Result.Strength;
/**
@@ -36,11 +36,12 @@ public class Index extends AbstractDbObject
/**
* @param table the parent table
* @param columnNames
*/
public Index(String name, List<String> columnNames)
public Index(Table table, String name, List<String> columnNames)
{
super(name);
super(table, name);
this.columnNames = columnNames;
setNameStrength(Strength.WARN);
}
@@ -112,9 +113,12 @@ public class Index extends AbstractDbObject
@Override
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
{
Differences differences = ctx.getDifferences();
Index rightIndex = (Index) right;
comparisonUtils.compareSimpleCollections(columnNames, rightIndex.columnNames, ctx, strength);
comparisonUtils.compareSimpleCollections(
new DbProperty(this, "columnNames"),
new DbProperty(rightIndex, "columnNames"),
ctx,
strength);
}

View File

@@ -20,6 +20,7 @@ package org.alfresco.util.schemacomp.model;
import java.util.Arrays;
import org.alfresco.util.schemacomp.DbProperty;
import org.alfresco.util.schemacomp.Result.Strength;
import org.junit.Before;
import org.junit.Test;
@@ -39,8 +40,8 @@ public class IndexTest extends DbObjectTestBase<Index>
@Before
public void setUp()
{
thisIndex = new Index("this_index", Arrays.asList("id", "name", "age"));
thatIndex = new Index("that_index", Arrays.asList("a", "b"));
thisIndex = new Index(null, "this_index", Arrays.asList("id", "name", "age"));
thatIndex = new Index(null, "that_index", Arrays.asList("a", "b"));
}
@Override
@@ -59,8 +60,8 @@ public class IndexTest extends DbObjectTestBase<Index>
protected void doDiffTests()
{
inOrder.verify(comparisonUtils).compareSimpleCollections(
thisIndex.getColumnNames(),
thatIndex.getColumnNames(),
new DbProperty(thisIndex, "columnNames"),
new DbProperty(thatIndex, "columnNames"),
ctx,
Strength.ERROR);
}
@@ -77,19 +78,19 @@ public class IndexTest extends DbObjectTestBase<Index>
public void sameAs()
{
assertTrue("Indexes should be logically the same.",
thisIndex.sameAs(new Index("this_index", Arrays.asList("id", "name", "age"))));
thisIndex.sameAs(new Index(null, "this_index", Arrays.asList("id", "name", "age"))));
assertTrue("Indexes should be logically the same, despite different names (as same column order)",
thisIndex.sameAs(new Index("different_name", Arrays.asList("id", "name", "age"))));
thisIndex.sameAs(new Index(null, "different_name", Arrays.asList("id", "name", "age"))));
assertTrue("Indexes should be identified as the same despite different column order (as same name).",
thisIndex.sameAs(new Index("this_index", Arrays.asList("name", "id", "age"))));
thisIndex.sameAs(new Index(null, "this_index", Arrays.asList("name", "id", "age"))));
assertFalse("Indexes should be identified different (different name and column order)",
thisIndex.sameAs(new Index("different_name", Arrays.asList("name", "id", "age"))));
thisIndex.sameAs(new Index(null, "different_name", Arrays.asList("name", "id", "age"))));
assertFalse("Indexes should be identified different (different name & different columns)",
thisIndex.sameAs(new Index("different_name", Arrays.asList("node_ref", "url"))));
thisIndex.sameAs(new Index(null, "different_name", Arrays.asList("node_ref", "url"))));
}

View File

@@ -21,8 +21,8 @@ package org.alfresco.util.schemacomp.model;
import java.util.List;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DbProperty;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Result.Strength;
/**
@@ -37,12 +37,13 @@ public class PrimaryKey extends AbstractDbObject
/**
* Constructor
* @param table the parent table
* @param name
* @param columnNames
*/
public PrimaryKey(String name, List<String> columnNames)
public PrimaryKey(Table table, String name, List<String> columnNames)
{
super(name);
super(table, name);
this.columnNames = columnNames;
}
@@ -89,9 +90,12 @@ public class PrimaryKey extends AbstractDbObject
@Override
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
{
Differences differences = ctx.getDifferences();
PrimaryKey rightPK = (PrimaryKey) right;
comparisonUtils.compareSimpleCollections(columnNames, rightPK.columnNames, ctx, strength);
comparisonUtils.compareSimpleCollections(
new DbProperty(this, "columnNames"),
new DbProperty(rightPK, "columnNames"),
ctx,
strength);
}
@Override

View File

@@ -22,6 +22,7 @@ import static org.mockito.Mockito.verify;
import java.util.Arrays;
import org.alfresco.util.schemacomp.DbProperty;
import org.alfresco.util.schemacomp.Result.Strength;
import org.junit.Before;
import org.junit.Test;
@@ -40,8 +41,8 @@ public class PrimaryKeyTest extends DbObjectTestBase<PrimaryKey>
@Before
public void setUp()
{
thisPK = new PrimaryKey("this_pk", Arrays.asList("id", "name", "age"));
thatPK = new PrimaryKey("that_pk", Arrays.asList("a", "b"));
thisPK = new PrimaryKey(null, "this_pk", Arrays.asList("id", "name", "age"));
thatPK = new PrimaryKey(null, "that_pk", Arrays.asList("a", "b"));
}
@Override
@@ -60,8 +61,8 @@ public class PrimaryKeyTest extends DbObjectTestBase<PrimaryKey>
protected void doDiffTests()
{
inOrder.verify(comparisonUtils).compareSimpleCollections(
thisPK.getColumnNames(),
thatPK.getColumnNames(),
new DbProperty(thisPK, "columnNames"),
new DbProperty(thatPK, "columnNames"),
ctx,
Strength.ERROR);
}

View File

@@ -24,7 +24,7 @@ import java.util.List;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Results;
import org.alfresco.util.schemacomp.Result.Strength;
/**
@@ -43,11 +43,18 @@ public class Schema extends AbstractDbObject implements Iterable<DbObject>
*/
public Schema(String name)
{
super(name);
super(null, name);
}
/**
* Add an object to this schema - this method will set this schema
* as the object's parent.
*
* @param dbObject
*/
public void add(DbObject dbObject)
{
dbObject.setParent(this);
objects.add(dbObject);
}
@@ -94,7 +101,6 @@ public class Schema extends AbstractDbObject implements Iterable<DbObject>
@Override
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
{
Differences differences = ctx.getDifferences();
Schema rightSchema = (Schema) right;
comparisonUtils.compareCollections(objects, rightSchema.objects, ctx);
}

View File

@@ -28,9 +28,9 @@ import org.alfresco.util.schemacomp.DbObjectVisitor;
*/
public class Sequence extends AbstractDbObject
{
public Sequence(String name)
public Sequence(DbObject parent, String name)
{
super(name);
super(parent, name);
}
@Override

View File

@@ -21,7 +21,6 @@ package org.alfresco.util.schemacomp.model;
import static org.mockito.Mockito.verify;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
/**
@@ -36,8 +35,8 @@ public class SequenceTest extends DbObjectTestBase<Sequence>
@Before
public void setUp()
{
thisSequence = new Sequence("this_sequence");
thatSequence = new Sequence("that_sequence");
thisSequence = new Sequence(null, "this_sequence");
thatSequence = new Sequence(null, "that_sequence");
}
@Test

View File

@@ -24,7 +24,6 @@ import java.util.List;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Result.Strength;
/**
@@ -34,28 +33,29 @@ import org.alfresco.util.schemacomp.Result.Strength;
*/
public class Table extends AbstractDbObject
{
private List<Column> columns = new ArrayList<Column>();
private final List<Column> columns = new ArrayList<Column>();
private PrimaryKey primaryKey;
private List<ForeignKey> foreignKeys = new ArrayList<ForeignKey>();
private List<Index> indexes = new ArrayList<Index>();
private final List<ForeignKey> foreignKeys = new ArrayList<ForeignKey>();
private final List<Index> indexes = new ArrayList<Index>();
public Table(String name, Collection<Column> columns, PrimaryKey primaryKey,
public Table(Schema parentSchema, String name, Collection<Column> columns, PrimaryKey primaryKey,
Collection<ForeignKey> foreignKeys, Collection<Index> indexes)
{
super(name);
super(parentSchema, name);
if (columns != null)
{
this.columns.addAll(columns);
setColumns(columns);
}
primaryKey.setParent(this);
this.primaryKey = primaryKey;
if (foreignKeys != null)
{
this.foreignKeys.addAll(foreignKeys);
setForeignKeys(foreignKeys);
}
if (indexes != null)
{
this.indexes.addAll(indexes);
setIndexes(indexes);
}
}
@@ -72,9 +72,15 @@ public class Table extends AbstractDbObject
/**
* @param columns the columns to set
*/
public void setColumns(List<Column> columns)
public void setColumns(Collection<Column> columns)
{
this.columns = columns;
this.columns.clear();
this.columns.addAll(columns);
for (Column column : columns)
{
column.setParent(this);
}
}
@@ -92,6 +98,7 @@ public class Table extends AbstractDbObject
*/
public void setPrimaryKey(PrimaryKey primaryKey)
{
primaryKey.setParent(this);
this.primaryKey = primaryKey;
}
@@ -108,9 +115,15 @@ public class Table extends AbstractDbObject
/**
* @param foreignKeys the foreignKeys to set
*/
public void setForeignKeys(List<ForeignKey> foreignKeys)
public void setForeignKeys(Collection<ForeignKey> foreignKeys)
{
this.foreignKeys = foreignKeys;
this.foreignKeys.clear();
this.foreignKeys.addAll(foreignKeys);
for (ForeignKey fk : foreignKeys)
{
fk.setParent(this);
}
}
@@ -126,9 +139,15 @@ public class Table extends AbstractDbObject
/**
* @param indexes the indexes to set
*/
public void setIndexes(List<Index> indexes)
public void setIndexes(Collection<Index> indexes)
{
this.indexes = indexes;
this.indexes.clear();
this.indexes.addAll(indexes);
for (Index index : indexes)
{
index.setParent(this);
}
}
@@ -179,7 +198,6 @@ public class Table extends AbstractDbObject
@Override
protected void doDiff(DbObject other, DiffContext ctx, Strength strength)
{
Differences differences = ctx.getDifferences();
Table rightTable = (Table) other;
comparisonUtils.compareCollections(columns, rightTable.columns, ctx);
primaryKey.diff(rightTable.primaryKey, ctx, strength);

View File

@@ -19,17 +19,12 @@
package org.alfresco.util.schemacomp.model;
import static java.util.Arrays.asList;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.alfresco.util.schemacomp.Result.Strength;
import org.apache.poi.ss.formula.functions.Columns;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -56,26 +51,11 @@ public class TableTest extends DbObjectTestBase<Table>
@Before
public void setUp() throws Exception
{
/*columns = asList(
new Column("columnA", "VARCHAR2(50)", false),
new Column("columnB", "VARCHAR2(100)", false),
new Column("columnC", "VARCHAR2(200)", true));
foreignKeys = asList(new ForeignKey("fk", "localcolumn", "targettable", "targetcolumn"));
indexes = asList(new Index("an_index", asList("columnA", "columnC")));
table = new Table("the_table", columns, primaryKey, foreignKeys, indexes);
otherTable = new Table("the_other_table", columns, primaryKey, foreignKeys, indexes);*/
columns = listOfMocks(Column.class, 3);
foreignKeys = listOfMocks(ForeignKey.class, 1);
indexes = listOfMocks(Index.class, 1);
table = new Table("the_table", columns, primaryKey, foreignKeys, indexes);
otherTable = new Table("the_other_table", columns, primaryKey, foreignKeys, indexes);
table = new Table(null, "the_table", columns, primaryKey, foreignKeys, indexes);
otherTable = new Table(null, "the_other_table", columns, primaryKey, foreignKeys, indexes);
}