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:
@@ -38,8 +38,7 @@ public interface ComparisonUtils
|
||||
DbObject findSameObjectAs(Collection<? extends DbObject> objects, final DbObject objToFind);
|
||||
|
||||
void compareSimpleCollections(Collection<? extends Object> leftCollection,
|
||||
Collection<? extends Object> rightCollection, Differences differences,
|
||||
Strength strength);
|
||||
Collection<? extends Object> rightCollection, DiffContext ctx, Strength strength);
|
||||
|
||||
/**
|
||||
* Compare collections, reporting differences using the default {@link Result.Strength}
|
||||
@@ -47,7 +46,7 @@ public interface ComparisonUtils
|
||||
* @see #compareCollections(Collection, Collection, Differences, Strength)
|
||||
*/
|
||||
void compareCollections(Collection<? extends DbObject> leftCollection,
|
||||
Collection<? extends DbObject> rightCollection, Differences differences);
|
||||
Collection<? extends DbObject> rightCollection, DiffContext ctx);
|
||||
|
||||
/**
|
||||
* Compare collections of {@link DbObject}s using their {@link DbObject#diff(DbObject, Differences)} method.
|
||||
@@ -59,7 +58,7 @@ public interface ComparisonUtils
|
||||
* @param strength
|
||||
*/
|
||||
void compareCollections(Collection<? extends DbObject> leftCollection,
|
||||
Collection<? extends DbObject> rightCollection, Differences differences,
|
||||
Collection<? extends DbObject> rightCollection, DiffContext ctx,
|
||||
Strength strength);
|
||||
|
||||
/**
|
||||
@@ -67,7 +66,7 @@ public interface ComparisonUtils
|
||||
*
|
||||
* @see #compareSimple(Object, Object, Differences, Strength)
|
||||
*/
|
||||
void compareSimple(Object left, Object right, Differences differences);
|
||||
void compareSimple(Object left, Object right, DiffContext ctx);
|
||||
|
||||
/**
|
||||
* Compare two 'simple' (i.e. non-{@link DbObject} objects) using their {@link Object#equals(Object)} method
|
||||
@@ -78,6 +77,6 @@ public interface ComparisonUtils
|
||||
* @param differences
|
||||
* @param strength
|
||||
*/
|
||||
void compareSimple(Object left, Object right, Differences differences, Strength strength);
|
||||
void compareSimple(Object left, Object right, DiffContext ctx, Strength strength);
|
||||
|
||||
}
|
@@ -55,8 +55,9 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
|
||||
@Override
|
||||
public void compareSimpleCollections(Collection<? extends Object> leftCollection,
|
||||
Collection<? extends Object> rightCollection, Differences differences, Strength strength)
|
||||
Collection<? extends Object> rightCollection, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
for (Object leftObj : leftCollection)
|
||||
{
|
||||
if (rightCollection.contains(leftObj))
|
||||
@@ -92,9 +93,9 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
*/
|
||||
@Override
|
||||
public void compareCollections(Collection<? extends DbObject> leftCollection,
|
||||
Collection<? extends DbObject> rightCollection, Differences differences)
|
||||
Collection<? extends DbObject> rightCollection, DiffContext ctx)
|
||||
{
|
||||
compareCollections(leftCollection, rightCollection, differences, null);
|
||||
compareCollections(leftCollection, rightCollection, ctx, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,8 +109,9 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
*/
|
||||
@Override
|
||||
public void compareCollections(Collection<? extends DbObject> leftCollection,
|
||||
Collection<? extends DbObject> rightCollection, Differences differences, Strength strength)
|
||||
Collection<? extends DbObject> rightCollection, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
for (DbObject leftObj : leftCollection)
|
||||
{
|
||||
DbObject rightObj = findSameObjectAs(rightCollection, leftObj);
|
||||
@@ -118,7 +120,7 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
{
|
||||
// There is an equivalent object in the right hand collection as
|
||||
// in the left.
|
||||
leftObj.diff(rightObj, differences, strength);
|
||||
leftObj.diff(rightObj, ctx, strength);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -144,9 +146,9 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
* @see #compareSimple(Object, Object, Differences, Strength)
|
||||
*/
|
||||
@Override
|
||||
public void compareSimple(Object left, Object right, Differences differences)
|
||||
public void compareSimple(Object left, Object right, DiffContext ctx)
|
||||
{
|
||||
compareSimple(left, right, differences, null);
|
||||
compareSimple(left, right, ctx, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,7 +161,7 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
* @param strength
|
||||
*/
|
||||
@Override
|
||||
public void compareSimple(Object left, Object right, Differences differences, Strength strength)
|
||||
public void compareSimple(Object left, Object right, DiffContext ctx, Strength strength)
|
||||
{
|
||||
|
||||
Where where = null;
|
||||
@@ -192,6 +194,6 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
}
|
||||
}
|
||||
|
||||
differences.add(where, left, right, strength);
|
||||
ctx.getDifferences().add(where, left, right, strength);
|
||||
}
|
||||
}
|
||||
|
@@ -33,6 +33,7 @@ import java.util.List;
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
import org.alfresco.util.schemacomp.Result.Where;
|
||||
import org.alfresco.util.schemacomp.model.DbObject;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -49,29 +50,32 @@ public class DefaultComparisonUtilsTest
|
||||
{
|
||||
private @Mock Differences differences;
|
||||
private DefaultComparisonUtils comparisonUtils;
|
||||
private DiffContext ctx;
|
||||
private @Mock Dialect dialect;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
comparisonUtils = new DefaultComparisonUtils();
|
||||
ctx = new DiffContext(dialect, differences);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareSimple()
|
||||
{
|
||||
comparisonUtils.compareSimple(null, null, differences, Strength.ERROR);
|
||||
comparisonUtils.compareSimple(null, null, ctx, Strength.ERROR);
|
||||
verify(differences).add(Where.IN_BOTH_NO_DIFFERENCE, null, null, Strength.ERROR);
|
||||
|
||||
comparisonUtils.compareSimple("not_null_string", "not_null_string", differences, Strength.ERROR);
|
||||
comparisonUtils.compareSimple("not_null_string", "not_null_string", ctx, Strength.ERROR);
|
||||
verify(differences).add(Where.IN_BOTH_NO_DIFFERENCE, "not_null_string", "not_null_string", Strength.ERROR);
|
||||
|
||||
comparisonUtils.compareSimple("left", "right", differences, Strength.ERROR);
|
||||
comparisonUtils.compareSimple("left", "right", ctx, Strength.ERROR);
|
||||
verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, "left", "right", Strength.ERROR);
|
||||
|
||||
comparisonUtils.compareSimple("left", null, differences, Strength.ERROR);
|
||||
comparisonUtils.compareSimple("left", null, ctx, Strength.ERROR);
|
||||
verify(differences).add(Where.ONLY_IN_LEFT, "left", null, Strength.ERROR);
|
||||
|
||||
comparisonUtils.compareSimple(null, "right", differences, Strength.ERROR);
|
||||
comparisonUtils.compareSimple(null, "right", ctx, Strength.ERROR);
|
||||
verify(differences).add(Where.ONLY_IN_RIGHT, null, "right", Strength.ERROR);
|
||||
}
|
||||
|
||||
@@ -94,11 +98,11 @@ public class DefaultComparisonUtilsTest
|
||||
Collection<DbObject> right = new ArrayList<DbObject>();
|
||||
Collections.addAll(right, db1, db3, db4);
|
||||
|
||||
comparisonUtils.compareCollections(left, right, differences, Strength.ERROR);
|
||||
comparisonUtils.compareCollections(left, right, ctx, Strength.ERROR);
|
||||
|
||||
// Objects in both are asked for their differences
|
||||
verify(db1).diff(db1, differences, Strength.ERROR);
|
||||
verify(db4).diff(db4, differences, Strength.ERROR);
|
||||
verify(db1).diff(db1, ctx, Strength.ERROR);
|
||||
verify(db4).diff(db4, ctx, Strength.ERROR);
|
||||
|
||||
// Objects in only one collections are marked as such
|
||||
verify(differences).add(Where.ONLY_IN_LEFT, db2, null, Strength.ERROR);
|
||||
@@ -130,7 +134,7 @@ public class DefaultComparisonUtilsTest
|
||||
rightCollection.add("both");
|
||||
rightCollection.add("one more right only");
|
||||
|
||||
comparisonUtils.compareSimpleCollections(leftCollection, rightCollection, differences, Strength.WARN);
|
||||
comparisonUtils.compareSimpleCollections(leftCollection, rightCollection, ctx, Strength.WARN);
|
||||
|
||||
verify(differences).add(Where.IN_BOTH_NO_DIFFERENCE, 123, 123, Strength.WARN);
|
||||
verify(differences).add(Where.IN_BOTH_NO_DIFFERENCE, "both", "both", Strength.WARN);
|
||||
|
61
source/java/org/alfresco/util/schemacomp/DiffContext.java
Normal file
61
source/java/org/alfresco/util/schemacomp/DiffContext.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2011 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.util.schemacomp;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
|
||||
/**
|
||||
* A context made available to schema differencing and validation operations. It supplies information
|
||||
* about the {@link Dialect database dialect} that should be used when validating database properties
|
||||
* and the {@link Differences} object that should be populated with schema differences and validation errors.
|
||||
*
|
||||
* @author Matt Ward
|
||||
*/
|
||||
public class DiffContext
|
||||
{
|
||||
private final Dialect dialect;
|
||||
private final Differences differences;
|
||||
|
||||
|
||||
/**
|
||||
* @param dialect
|
||||
* @param differences
|
||||
*/
|
||||
public DiffContext(Dialect dialect, Differences differences)
|
||||
{
|
||||
this.dialect = dialect;
|
||||
this.differences = differences;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the dialect
|
||||
*/
|
||||
public Dialect getDialect()
|
||||
{
|
||||
return this.dialect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the differences
|
||||
*/
|
||||
public Differences getDifferences()
|
||||
{
|
||||
return this.differences;
|
||||
}
|
||||
}
|
@@ -69,6 +69,13 @@ public class Differences implements Iterable<Result>
|
||||
Result result = new Result(where, left, right, path.getCurrentPath(), strength);
|
||||
items.add(result);
|
||||
}
|
||||
|
||||
|
||||
public void add(Where where, Object left, Object right)
|
||||
{
|
||||
add(where, left, right, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Obtain an iterator for the top-level items held in this schema - since this is a hierarchical model,
|
||||
@@ -126,7 +133,6 @@ public class Differences implements Iterable<Result>
|
||||
private void makeCurrentPath()
|
||||
{
|
||||
current = StringUtils.join(components, ".");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -20,6 +20,7 @@ package org.alfresco.util.schemacomp;
|
||||
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
import org.alfresco.util.schemacomp.model.Schema;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
|
||||
/**
|
||||
* Compares two abstract database schemas, a 'left' schema and a 'right' schema. The left schema is the primary
|
||||
@@ -31,7 +32,7 @@ public class SchemaComparator
|
||||
{
|
||||
private final Schema leftSchema;
|
||||
private final Schema rightSchema;
|
||||
private final Differences differences = new Differences();
|
||||
private final DiffContext ctx;
|
||||
|
||||
/**
|
||||
* Construct a comparator to compare schemas left and right.
|
||||
@@ -39,17 +40,18 @@ public class SchemaComparator
|
||||
* @param left
|
||||
* @param right
|
||||
*/
|
||||
public SchemaComparator(Schema left, Schema right)
|
||||
public SchemaComparator(Schema left, Schema right, Dialect dialect)
|
||||
{
|
||||
this.leftSchema = left;
|
||||
this.rightSchema = right;
|
||||
this.ctx = new DiffContext(dialect, new Differences());
|
||||
}
|
||||
|
||||
|
||||
public void compare()
|
||||
{
|
||||
// Check the left schema against the right schema and record any differences.
|
||||
leftSchema.diff(rightSchema, differences, Strength.ERROR);
|
||||
leftSchema.diff(rightSchema, ctx, Strength.ERROR);
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +60,6 @@ public class SchemaComparator
|
||||
*/
|
||||
public Differences getDifferences()
|
||||
{
|
||||
return this.differences;
|
||||
return this.ctx.getDifferences();
|
||||
}
|
||||
}
|
||||
|
@@ -38,6 +38,8 @@ import org.alfresco.util.schemacomp.model.PrimaryKey;
|
||||
import org.alfresco.util.schemacomp.model.Schema;
|
||||
import org.alfresco.util.schemacomp.model.Table;
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.MySQL5InnoDBDialect;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -51,12 +53,14 @@ public class SchemaComparatorTest
|
||||
private SchemaComparator comparator;
|
||||
private Schema left;
|
||||
private Schema right;
|
||||
private Dialect dialect;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
{
|
||||
left = new Schema("left_schema");
|
||||
right = new Schema("right_schema");
|
||||
dialect = new MySQL5InnoDBDialect();
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +84,7 @@ public class SchemaComparatorTest
|
||||
right.add(table("table_in_right"));
|
||||
|
||||
|
||||
comparator = new SchemaComparator(left, right);
|
||||
comparator = new SchemaComparator(left, right, dialect);
|
||||
comparator.compare();
|
||||
|
||||
dumpDiffs(comparator.getDifferences(), true);
|
||||
@@ -144,7 +148,7 @@ public class SchemaComparatorTest
|
||||
indexes("sys_random_idx_name id")));
|
||||
|
||||
|
||||
comparator = new SchemaComparator(left, right);
|
||||
comparator = new SchemaComparator(left, right, dialect);
|
||||
comparator.compare();
|
||||
|
||||
dumpDiffs(comparator.getDifferences(), true);
|
||||
|
@@ -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