mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
ALF-10771: DiffContext is now passed around to provide the DB Dialect.
Instead of the Differences being passed around on their own, the DiffContext is able to provide more information -- at present just the Dialect. The dialect can be used to change the way database objects are validated or differenced. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31466 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
package org.alfresco.util.schemacomp.model;
|
||||
|
||||
import org.alfresco.util.schemacomp.ComparisonUtils;
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
import org.alfresco.util.schemacomp.Differences;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
import org.alfresco.util.schemacomp.DefaultComparisonUtils;
|
||||
@@ -154,8 +155,10 @@ public abstract class AbstractDbObject implements DbObject
|
||||
* its diff correctly.
|
||||
*/
|
||||
@Override
|
||||
public void diff(DbObject right, Differences differences, Strength strength)
|
||||
public void diff(DbObject right, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
|
||||
if (name != null && StringUtils.hasText(name))
|
||||
{
|
||||
differences.pushPath(name);
|
||||
@@ -164,10 +167,11 @@ public abstract class AbstractDbObject implements DbObject
|
||||
{
|
||||
differences.pushPath("<" + getClass().getSimpleName() + ">");
|
||||
}
|
||||
comparisonUtils.compareSimple(name, right.getName(), differences, getNameStrength());
|
||||
doDiff(right, differences, strength);
|
||||
comparisonUtils.compareSimple(name, right.getName(), ctx, getNameStrength());
|
||||
doDiff(right, ctx, strength);
|
||||
differences.popPath();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Override this method to provide subclass specific diffing logic.
|
||||
@@ -176,9 +180,10 @@ public abstract class AbstractDbObject implements DbObject
|
||||
* @param differences
|
||||
* @param strength
|
||||
*/
|
||||
protected void doDiff(DbObject right, Differences differences, Strength strength)
|
||||
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* If a ComparisonUtils other than the default is required, then this setter can be used.
|
||||
|
@@ -23,9 +23,11 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
import org.alfresco.util.schemacomp.Differences;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
import org.alfresco.util.schemacomp.Result.Where;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -43,6 +45,8 @@ public class AbstractDbObjectTest
|
||||
{
|
||||
private ConcreteDbObject dbObject;
|
||||
private @Mock Differences differences;
|
||||
private DiffContext ctx;
|
||||
private @Mock Dialect dialect;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
@@ -51,6 +55,7 @@ public class AbstractDbObjectTest
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
dbObject = new ConcreteDbObject("the_object");
|
||||
ctx = new DiffContext(dialect, differences);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -81,7 +86,7 @@ public class AbstractDbObjectTest
|
||||
ConcreteDbObject otherObject = new ConcreteDbObject("the_other_object");
|
||||
dbObject.setNameStrength(Strength.WARN);
|
||||
|
||||
dbObject.diff(otherObject, differences, Strength.ERROR);
|
||||
dbObject.diff(otherObject, ctx, Strength.ERROR);
|
||||
|
||||
InOrder inOrder = inOrder(differences);
|
||||
// The name of the object should be pushed on to the differences path.
|
||||
@@ -89,7 +94,7 @@ public class AbstractDbObjectTest
|
||||
// The name of the object should be diffed
|
||||
inOrder.verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, "the_object", "the_other_object", Strength.WARN);
|
||||
// Then the doDiff() method should be processed
|
||||
inOrder.verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, "left", "right", Strength.ERROR);
|
||||
inOrder.verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, "left", "right");
|
||||
// Later, the path should be popped again
|
||||
inOrder.verify(differences).popPath();
|
||||
}
|
||||
@@ -106,9 +111,10 @@ public class AbstractDbObjectTest
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doDiff(DbObject right, Differences differences, Strength strength)
|
||||
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
|
||||
{
|
||||
differences.add(Where.IN_BOTH_BUT_DIFFERENCE, "left", "right", strength);
|
||||
Differences differences = ctx.getDifferences();
|
||||
differences.add(Where.IN_BOTH_BUT_DIFFERENCE, "left", "right");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.alfresco.util.schemacomp.model;
|
||||
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
import org.alfresco.util.schemacomp.Differences;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
|
||||
@@ -105,10 +106,11 @@ public class Column extends AbstractDbObject
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doDiff(DbObject right, Differences differences, Strength strength)
|
||||
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
Column rightColumn = (Column) right;
|
||||
comparisonUtils.compareSimple(type, rightColumn.type, differences);
|
||||
comparisonUtils.compareSimple(nullable, rightColumn.nullable, differences);
|
||||
comparisonUtils.compareSimple(type, rightColumn.type, ctx);
|
||||
comparisonUtils.compareSimple(nullable, rightColumn.nullable, ctx);
|
||||
}
|
||||
}
|
||||
|
@@ -53,8 +53,8 @@ public class ColumnTest extends DbObjectTestBase<Column>
|
||||
@Override
|
||||
protected void doDiffTests()
|
||||
{
|
||||
inOrder.verify(comparisonUtils).compareSimple(thisColumn.getType(), thatColumn.getType(), differences);
|
||||
inOrder.verify(comparisonUtils).compareSimple(thisColumn.isNullable(), thatColumn.isNullable(), differences);
|
||||
inOrder.verify(comparisonUtils).compareSimple(thisColumn.getType(), thatColumn.getType(), ctx);
|
||||
inOrder.verify(comparisonUtils).compareSimple(thisColumn.isNullable(), thatColumn.isNullable(), ctx);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.alfresco.util.schemacomp.model;
|
||||
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
import org.alfresco.util.schemacomp.Differences;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
|
||||
@@ -56,7 +57,7 @@ public interface DbObject
|
||||
* object passed in to this method.
|
||||
*
|
||||
* @param right The object to compare against.
|
||||
* @param differences A collector of differences.
|
||||
* @param ctx The DiffContext
|
||||
*/
|
||||
void diff(DbObject right, Differences differences, Strength strength);
|
||||
void diff(DbObject right, DiffContext ctx, Strength strength);
|
||||
}
|
||||
|
@@ -24,8 +24,10 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.util.schemacomp.ComparisonUtils;
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
import org.alfresco.util.schemacomp.Differences;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -41,18 +43,21 @@ import org.mockito.runners.MockitoJUnitRunner;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public abstract class DbObjectTestBase<T extends AbstractDbObject>
|
||||
{
|
||||
protected @Mock Dialect dialect;
|
||||
protected @Mock Differences differences;
|
||||
protected DiffContext ctx;
|
||||
protected @Mock ComparisonUtils comparisonUtils;
|
||||
protected InOrder inOrder;
|
||||
protected abstract T getThisObject();
|
||||
protected abstract T getThatObject();
|
||||
|
||||
@Before
|
||||
public void setUpMockito()
|
||||
public void baseSetUp()
|
||||
{
|
||||
// Check that the correct calls happened in the correct order.
|
||||
List<Object> mocks = getMocksUsedInDiff();
|
||||
inOrder = inOrder(mocks.toArray());
|
||||
ctx = new DiffContext(dialect, differences);
|
||||
}
|
||||
|
||||
|
||||
@@ -79,7 +84,7 @@ public abstract class DbObjectTestBase<T extends AbstractDbObject>
|
||||
thatObject.setComparisonUtils(comparisonUtils);
|
||||
|
||||
// Invoke the method under test
|
||||
thisObject.diff(thatObject, differences, Strength.ERROR);
|
||||
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());
|
||||
@@ -88,7 +93,7 @@ public abstract class DbObjectTestBase<T extends AbstractDbObject>
|
||||
inOrder.verify(comparisonUtils).compareSimple(
|
||||
thisObject.getName(),
|
||||
thatObject.getName(),
|
||||
differences,
|
||||
ctx,
|
||||
thisObject.getNameStrength());
|
||||
|
||||
// Then the doDiff() method should be processed...
|
||||
|
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.alfresco.util.schemacomp.model;
|
||||
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
import org.alfresco.util.schemacomp.Differences;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
|
||||
@@ -137,11 +138,12 @@ public class ForeignKey extends AbstractDbObject
|
||||
|
||||
|
||||
@Override
|
||||
protected void doDiff(DbObject right, Differences differences, Strength strength)
|
||||
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
ForeignKey rightFK = (ForeignKey) right;
|
||||
comparisonUtils.compareSimple(localColumn, rightFK.localColumn, differences);
|
||||
comparisonUtils.compareSimple(targetTable, rightFK.targetTable, differences);
|
||||
comparisonUtils.compareSimple(targetColumn, rightFK.targetColumn, differences);
|
||||
comparisonUtils.compareSimple(localColumn, rightFK.localColumn, ctx);
|
||||
comparisonUtils.compareSimple(targetTable, rightFK.targetTable, ctx);
|
||||
comparisonUtils.compareSimple(targetColumn, rightFK.targetColumn, ctx);
|
||||
}
|
||||
}
|
||||
|
@@ -56,8 +56,8 @@ public class ForeignKeyTest extends DbObjectTestBase<ForeignKey>
|
||||
@Override
|
||||
protected void doDiffTests()
|
||||
{
|
||||
inOrder.verify(comparisonUtils).compareSimple(thisFK.getLocalColumn(), thatFK.getLocalColumn(), differences);
|
||||
inOrder.verify(comparisonUtils).compareSimple(thisFK.getTargetTable(), thatFK.getTargetTable(), differences);
|
||||
inOrder.verify(comparisonUtils).compareSimple(thisFK.getTargetColumn(), thatFK.getTargetColumn(), differences);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@@ -20,6 +20,7 @@ package org.alfresco.util.schemacomp.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
import org.alfresco.util.schemacomp.Differences;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
|
||||
@@ -108,9 +109,10 @@ public class Index extends AbstractDbObject
|
||||
|
||||
|
||||
@Override
|
||||
protected void doDiff(DbObject right, Differences differences, Strength strength)
|
||||
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
Index rightIndex = (Index) right;
|
||||
comparisonUtils.compareSimpleCollections(columnNames, rightIndex.columnNames, differences, strength);
|
||||
comparisonUtils.compareSimpleCollections(columnNames, rightIndex.columnNames, ctx, strength);
|
||||
}
|
||||
}
|
||||
|
@@ -60,7 +60,7 @@ public class IndexTest extends DbObjectTestBase<Index>
|
||||
inOrder.verify(comparisonUtils).compareSimpleCollections(
|
||||
thisIndex.getColumnNames(),
|
||||
thatIndex.getColumnNames(),
|
||||
differences,
|
||||
ctx,
|
||||
Strength.ERROR);
|
||||
}
|
||||
|
||||
|
@@ -20,6 +20,7 @@ package org.alfresco.util.schemacomp.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
import org.alfresco.util.schemacomp.Differences;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
|
||||
@@ -85,9 +86,10 @@ public class PrimaryKey extends AbstractDbObject
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doDiff(DbObject right, Differences differences, Strength strength)
|
||||
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
PrimaryKey rightPK = (PrimaryKey) right;
|
||||
comparisonUtils.compareSimpleCollections(columnNames, rightPK.columnNames, differences, strength);
|
||||
comparisonUtils.compareSimpleCollections(columnNames, rightPK.columnNames, ctx, strength);
|
||||
}
|
||||
}
|
||||
|
@@ -59,7 +59,7 @@ public class PrimaryKeyTest extends DbObjectTestBase<PrimaryKey>
|
||||
inOrder.verify(comparisonUtils).compareSimpleCollections(
|
||||
thisPK.getColumnNames(),
|
||||
thatPK.getColumnNames(),
|
||||
differences,
|
||||
ctx,
|
||||
Strength.ERROR);
|
||||
}
|
||||
|
||||
|
@@ -22,6 +22,7 @@ import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
import org.alfresco.util.schemacomp.Differences;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
|
||||
@@ -90,9 +91,10 @@ public class Schema extends AbstractDbObject implements Iterable<DbObject>
|
||||
|
||||
|
||||
@Override
|
||||
protected void doDiff(DbObject right, Differences differences, Strength strength)
|
||||
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
Schema rightSchema = (Schema) right;
|
||||
comparisonUtils.compareCollections(objects, rightSchema.objects, differences);
|
||||
comparisonUtils.compareCollections(objects, rightSchema.objects, ctx);
|
||||
}
|
||||
}
|
||||
|
@@ -57,6 +57,6 @@ public class SchemaTest extends DbObjectTestBase<Schema>
|
||||
{
|
||||
// In addition to the base class functionality, Schema.diff() compares
|
||||
// the DbObjects held in the other schema with its own DbObjects.
|
||||
inOrder.verify(comparisonUtils).compareCollections(left.objects, right.objects, differences);
|
||||
inOrder.verify(comparisonUtils).compareCollections(left.objects, right.objects, ctx);
|
||||
}
|
||||
}
|
||||
|
@@ -22,6 +22,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
import org.alfresco.util.schemacomp.Differences;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
|
||||
@@ -175,12 +176,13 @@ public class Table extends AbstractDbObject
|
||||
|
||||
|
||||
@Override
|
||||
protected void doDiff(DbObject other, Differences differences, Strength strength)
|
||||
{
|
||||
protected void doDiff(DbObject other, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
Table rightTable = (Table) other;
|
||||
comparisonUtils.compareCollections(columns, rightTable.columns, differences);
|
||||
primaryKey.diff(rightTable.primaryKey, differences, strength);
|
||||
comparisonUtils.compareCollections(foreignKeys, rightTable.foreignKeys, differences);
|
||||
comparisonUtils.compareCollections(indexes, rightTable.indexes, differences);
|
||||
comparisonUtils.compareCollections(columns, rightTable.columns, ctx);
|
||||
primaryKey.diff(rightTable.primaryKey, ctx, strength);
|
||||
comparisonUtils.compareCollections(foreignKeys, rightTable.foreignKeys, ctx);
|
||||
comparisonUtils.compareCollections(indexes, rightTable.indexes, ctx);
|
||||
}
|
||||
}
|
||||
|
@@ -74,18 +74,18 @@ public class TableTest extends DbObjectTestBase<Table>
|
||||
public void doDiffTests()
|
||||
{
|
||||
// Check columns
|
||||
inOrder.verify(comparisonUtils).compareCollections(table.getColumns(), otherTable.getColumns(), differences);
|
||||
inOrder.verify(comparisonUtils).compareCollections(table.getColumns(), otherTable.getColumns(), ctx);
|
||||
|
||||
// Check primary key
|
||||
inOrder.verify(primaryKey).diff(otherTable.getPrimaryKey(), differences, Strength.ERROR);
|
||||
inOrder.verify(primaryKey).diff(otherTable.getPrimaryKey(), ctx, Strength.ERROR);
|
||||
|
||||
// Check foreign keys
|
||||
inOrder.verify(comparisonUtils).compareCollections(
|
||||
table.getForeignKeys(), otherTable.getForeignKeys(), differences);
|
||||
table.getForeignKeys(), otherTable.getForeignKeys(), ctx);
|
||||
|
||||
// Check indexes
|
||||
inOrder.verify(comparisonUtils).compareCollections(
|
||||
table.getIndexes(), otherTable.getIndexes(), differences);
|
||||
table.getIndexes(), otherTable.getIndexes(), ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user