ALF-7260: unit tests, bug fixes.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31455 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2011-10-25 08:28:13 +00:00
parent b9c32d6aa5
commit 4ef52e4e72
27 changed files with 1428 additions and 187 deletions

View File

@@ -18,8 +18,10 @@
*/
package org.alfresco.util.schemacomp.model;
import org.alfresco.util.schemacomp.ComparisonUtils;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.SchemaUtils;
import org.alfresco.util.schemacomp.Result.Strength;
import org.alfresco.util.schemacomp.DefaultComparisonUtils;
import org.springframework.util.StringUtils;
/**
@@ -30,7 +32,10 @@ import org.springframework.util.StringUtils;
public abstract class AbstractDbObject implements DbObject
{
private String name;
/** How differences in the name field should be reported */
private Strength nameStrength = Strength.ERROR;
protected ComparisonUtils comparisonUtils = new DefaultComparisonUtils();
/**
* Default constructor
*/
@@ -64,6 +69,22 @@ public abstract class AbstractDbObject implements DbObject
this.name = name;
}
/**
* @return the nameStrength
*/
public Strength getNameStrength()
{
return this.nameStrength;
}
/**
* @param nameStrength the nameStrength to set
*/
public void setNameStrength(Strength nameStrength)
{
this.nameStrength = nameStrength;
}
@Override
public boolean sameAs(DbObject other)
{
@@ -133,7 +154,7 @@ public abstract class AbstractDbObject implements DbObject
* its diff correctly.
*/
@Override
public void diff(DbObject right, Differences differences)
public void diff(DbObject right, Differences differences, Strength strength)
{
if (name != null && StringUtils.hasText(name))
{
@@ -143,19 +164,30 @@ public abstract class AbstractDbObject implements DbObject
{
differences.pushPath("<" + getClass().getSimpleName() + ">");
}
SchemaUtils.compareSimple(name, right.getName(), differences);
doDiff(right, differences);
comparisonUtils.compareSimple(name, right.getName(), differences, getNameStrength());
doDiff(right, differences, strength);
differences.popPath();
}
/**
* Override this method to provide subclass specific diffing logic.
*
* @param right
* @param differences
* @param strength
*/
protected void doDiff(DbObject right, Differences differences)
protected void doDiff(DbObject right, Differences differences, Strength strength)
{
}
/**
* If a ComparisonUtils other than the default is required, then this setter can be used.
* Useful for testing, where a mock can be injected.
*
* @param comparisonUtils the comparisonUtils to set
*/
public void setComparisonUtils(ComparisonUtils comparisonUtils)
{
this.comparisonUtils = comparisonUtils;
}
}

View File

@@ -0,0 +1,114 @@
/*
* 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.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.inOrder;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Result.Strength;
import org.alfresco.util.schemacomp.Result.Where;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
* Tests for the AbstractDbObject base class.
*
* @author Matt Ward
*/
@RunWith(MockitoJUnitRunner.class)
public class AbstractDbObjectTest
{
private ConcreteDbObject dbObject;
private @Mock Differences differences;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception
{
dbObject = new ConcreteDbObject("the_object");
}
@Test
public void defaultNameStrength()
{
assertEquals(Strength.ERROR, dbObject.getNameStrength());
}
@Test
public void sameAs()
{
dbObject.setName(null);
assertFalse("Not the same.", dbObject.sameAs(null));
assertFalse("Not the same.", dbObject.sameAs(new ConcreteDbObject("other_obj_name")));
assertTrue("The very same", dbObject.sameAs(dbObject));
dbObject.setName("the_name");
assertFalse("Not the same.", dbObject.sameAs(null));
assertFalse("Not the same.", dbObject.sameAs(new ConcreteDbObject("different_name")));
assertTrue("Logically the same object.", dbObject.sameAs(new ConcreteDbObject("the_name")));
assertTrue("The very same object with non-null name", dbObject.sameAs(dbObject));
}
@Test
public void diff()
{
ConcreteDbObject otherObject = new ConcreteDbObject("the_other_object");
dbObject.setNameStrength(Strength.WARN);
dbObject.diff(otherObject, differences, 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);
// Then the doDiff() method should be processed
inOrder.verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, "left", "right", Strength.ERROR);
// Later, the path should be popped again
inOrder.verify(differences).popPath();
}
/**
* Concrete DbObject for testing the AbstractDbObject base class.
*/
private static class ConcreteDbObject extends AbstractDbObject
{
public ConcreteDbObject(String name)
{
super(name);
}
@Override
protected void doDiff(DbObject right, Differences differences, Strength strength)
{
differences.add(Where.IN_BOTH_BUT_DIFFERENCE, "left", "right", strength);
}
}
}

View File

@@ -19,7 +19,7 @@
package org.alfresco.util.schemacomp.model;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.SchemaUtils;
import org.alfresco.util.schemacomp.Result.Strength;
/**
* Represents a column in a database table.
@@ -105,10 +105,10 @@ public class Column extends AbstractDbObject
}
@Override
protected void doDiff(DbObject right, Differences differences)
protected void doDiff(DbObject right, Differences differences, Strength strength)
{
Column rightColumn = (Column) right;
SchemaUtils.compareSimple(type, rightColumn.type, differences);
SchemaUtils.compareSimple(nullable, rightColumn.nullable, differences);
comparisonUtils.compareSimple(type, rightColumn.type, differences);
comparisonUtils.compareSimple(nullable, rightColumn.nullable, differences);
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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.model;
import org.junit.Before;
/**
* Tests for the Column class.
* @author Matt Ward
*/
public class ColumnTest extends DbObjectTestBase<Column>
{
private Column thisColumn;
private Column thatColumn;
@Before
public void setUp() throws Exception
{
thisColumn = new Column("this_column", "VARCHAR2(100)", false);
thatColumn = new Column("this_column", "NUMBER(10)", true);
}
@Override
protected Column getThisObject()
{
return thisColumn;
}
@Override
protected Column getThatObject()
{
return thatColumn;
}
@Override
protected void doDiffTests()
{
inOrder.verify(comparisonUtils).compareSimple(thisColumn.getType(), thatColumn.getType(), differences);
inOrder.verify(comparisonUtils).compareSimple(thisColumn.isNullable(), thatColumn.isNullable(), differences);
}
}

View File

@@ -19,6 +19,7 @@
package org.alfresco.util.schemacomp.model;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Result.Strength;
/**
* All database objects to be modelled for schema comparisons must implement this interface, examples
@@ -31,6 +32,11 @@ public interface DbObject
/**
* Are the two <code>DbObject</code>s logically the same? For example two Index objects may have
* different names, but are the same index as they both index the same columns for the same table.
* <p>
* If two objects a and b have the same logical identity, it does not mean that <code>a.equals(b) == true</code>.
* The two objects may well have differences and will be flagged as such by the schema comparison tool. When
* <code>a.sameAs(b) == true</code> it makes it easier to show the differences as related, i.e. a and b are
* different rather than, a is only in the 'left' tree and b is only in the 'right' tree.
*
* @param other
* @return
@@ -52,5 +58,5 @@ public interface DbObject
* @param right The object to compare against.
* @param differences A collector of differences.
*/
void diff(DbObject right, Differences differences);
void diff(DbObject right, Differences differences, Strength strength);
}

View File

@@ -0,0 +1,102 @@
/*
* 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.model;
import static org.mockito.Mockito.inOrder;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.util.schemacomp.ComparisonUtils;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Result.Strength;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
* Abstract base class for tests for AbstractDbObject subclasses.
*
* @author Matt Ward
*/
@RunWith(MockitoJUnitRunner.class)
public abstract class DbObjectTestBase<T extends AbstractDbObject>
{
protected @Mock Differences differences;
protected @Mock ComparisonUtils comparisonUtils;
protected InOrder inOrder;
protected abstract T getThisObject();
protected abstract T getThatObject();
@Before
public void setUpMockito()
{
// Check that the correct calls happened in the correct order.
List<Object> mocks = getMocksUsedInDiff();
inOrder = inOrder(mocks.toArray());
}
/**
* Override to add additional mocks to the InOrder call verification.
*
* @return List<Object>
*/
protected List<Object> getMocksUsedInDiff()
{
List<Object> objects = new ArrayList<Object>();
objects.add(differences);
objects.add(comparisonUtils);
return objects;
}
@Test
public void canDiffObjects()
{
AbstractDbObject thisObject = getThisObject();
thisObject.setComparisonUtils(comparisonUtils);
AbstractDbObject thatObject = getThatObject();
thatObject.setComparisonUtils(comparisonUtils);
// Invoke the method under test
thisObject.diff(thatObject, differences, 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(),
differences,
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,7 +19,7 @@
package org.alfresco.util.schemacomp.model;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.SchemaUtils;
import org.alfresco.util.schemacomp.Result.Strength;
/**
@@ -137,11 +137,11 @@ public class ForeignKey extends AbstractDbObject
@Override
protected void doDiff(DbObject right, Differences differences)
protected void doDiff(DbObject right, Differences differences, Strength strength)
{
ForeignKey rightFK = (ForeignKey) right;
SchemaUtils.compareSimple(localColumn, rightFK.localColumn, differences);
SchemaUtils.compareSimple(targetTable, rightFK.targetTable, differences);
SchemaUtils.compareSimple(targetColumn, rightFK.targetColumn, differences);
comparisonUtils.compareSimple(localColumn, rightFK.localColumn, differences);
comparisonUtils.compareSimple(targetTable, rightFK.targetTable, differences);
comparisonUtils.compareSimple(targetColumn, rightFK.targetColumn, differences);
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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.model;
import org.junit.Before;
/**
* Tests for the ForeignKey class.
*
* @author Matt Ward
*/
public class ForeignKeyTest extends DbObjectTestBase<ForeignKey>
{
private ForeignKey thisFK, thatFK;
@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");
}
@Override
protected ForeignKey getThisObject()
{
return thisFK;
}
@Override
protected ForeignKey getThatObject()
{
return thatFK;
}
@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);
}
}

View File

@@ -21,7 +21,7 @@ package org.alfresco.util.schemacomp.model;
import java.util.List;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.SchemaUtils;
import org.alfresco.util.schemacomp.Result.Strength;
/**
* Represents an index on a table.
@@ -40,6 +40,7 @@ public class Index extends AbstractDbObject
{
super(name);
this.columnNames = columnNames;
setNameStrength(Strength.WARN);
}
/**
@@ -105,10 +106,11 @@ public class Index extends AbstractDbObject
return false;
}
@Override
protected void doDiff(DbObject right, Differences differences)
protected void doDiff(DbObject right, Differences differences, Strength strength)
{
Index rightIndex = (Index) right;
SchemaUtils.compareSimpleCollections(columnNames, rightIndex.columnNames, differences);
comparisonUtils.compareSimpleCollections(columnNames, rightIndex.columnNames, differences, strength);
}
}

View File

@@ -0,0 +1,93 @@
/*
* 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.model;
import java.util.Arrays;
import org.alfresco.util.schemacomp.Result.Strength;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Tests for the Index class.
* @author Matt Ward
*/
public class IndexTest extends DbObjectTestBase<Index>
{
private Index thisIndex;
private Index thatIndex;
@Before
public void setUp()
{
thisIndex = new Index("this_index", Arrays.asList("id", "name", "age"));
thatIndex = new Index("that_index", Arrays.asList("a", "b"));
}
@Override
protected Index getThisObject()
{
return thisIndex;
}
@Override
protected Index getThatObject()
{
return thatIndex;
}
@Override
protected void doDiffTests()
{
inOrder.verify(comparisonUtils).compareSimpleCollections(
thisIndex.getColumnNames(),
thatIndex.getColumnNames(),
differences,
Strength.ERROR);
}
@Test
public void differentNamesResultInWarningsNotErrors()
{
assertEquals("Name differences should be reported as warnings.", Strength.WARN, thisIndex.getNameStrength());
}
@Test
public void sameAs()
{
assertTrue("Indexes should be logically the same.",
thisIndex.sameAs(new Index("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"))));
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"))));
assertFalse("Indexes should be identified different (different name and column order)",
thisIndex.sameAs(new Index("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"))));
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.model;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* Test suite for all the model tests.
*
* @author Matt Ward
*/
@RunWith(Suite.class)
@Suite.SuiteClasses(
{
AbstractDbObjectTest.class,
ColumnTest.class,
ForeignKeyTest.class,
IndexTest.class,
PrimaryKeyTest.class,
SchemaTest.class,
SequenceTest.class,
TableTest.class
})
public class ModelTestSuite
{
// Suite defined by annotation above.
}

View File

@@ -21,7 +21,7 @@ package org.alfresco.util.schemacomp.model;
import java.util.List;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.SchemaUtils;
import org.alfresco.util.schemacomp.Result.Strength;
/**
* Primary key on a table.
@@ -33,6 +33,17 @@ public class PrimaryKey extends AbstractDbObject
private List<String> columnNames;
/**
* Constructor
* @param name
* @param columnNames
*/
public PrimaryKey(String name, List<String> columnNames)
{
super(name);
this.columnNames = columnNames;
}
/**
* @return the columnNames
*/
@@ -74,9 +85,9 @@ public class PrimaryKey extends AbstractDbObject
}
@Override
protected void doDiff(DbObject right, Differences differences)
protected void doDiff(DbObject right, Differences differences, Strength strength)
{
PrimaryKey rightPK = (PrimaryKey) right;
SchemaUtils.compareSimpleCollections(columnNames, rightPK.columnNames, differences);
comparisonUtils.compareSimpleCollections(columnNames, rightPK.columnNames, differences, strength);
}
}

View File

@@ -0,0 +1,66 @@
/*
* 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.model;
import java.util.Arrays;
import org.alfresco.util.schemacomp.Result.Strength;
import org.junit.Before;
/**
* Tests for the PrimaryKey class.
*
* @author Matt Ward
*/
public class PrimaryKeyTest extends DbObjectTestBase<PrimaryKey>
{
private PrimaryKey thisPK;
private PrimaryKey thatPK;
@Before
public void setUp()
{
thisPK = new PrimaryKey("this_pk", Arrays.asList("id", "name", "age"));
thatPK = new PrimaryKey("that_pk", Arrays.asList("a", "b"));
}
@Override
protected PrimaryKey getThisObject()
{
return thisPK;
}
@Override
protected PrimaryKey getThatObject()
{
return thatPK;
}
@Override
protected void doDiffTests()
{
inOrder.verify(comparisonUtils).compareSimpleCollections(
thisPK.getColumnNames(),
thatPK.getColumnNames(),
differences,
Strength.ERROR);
}
}

View File

@@ -23,7 +23,7 @@ import java.util.Iterator;
import java.util.List;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.SchemaUtils;
import org.alfresco.util.schemacomp.Result.Strength;
/**
* Instances of this class represent a database schema.
@@ -32,7 +32,7 @@ import org.alfresco.util.schemacomp.SchemaUtils;
*/
public class Schema extends AbstractDbObject implements Iterable<DbObject>
{
private final List<DbObject> objects = new ArrayList<DbObject>();
protected final List<DbObject> objects = new ArrayList<DbObject>();
/**
* Construct a schema with the given name.
@@ -90,9 +90,9 @@ public class Schema extends AbstractDbObject implements Iterable<DbObject>
@Override
protected void doDiff(DbObject right, Differences differences)
protected void doDiff(DbObject right, Differences differences, Strength strength)
{
Schema rightSchema = (Schema) right;
SchemaUtils.compareCollections(objects, rightSchema.objects, differences);
comparisonUtils.compareCollections(objects, rightSchema.objects, differences);
}
}

View File

@@ -0,0 +1,62 @@
/*
* 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.model;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
/**
* Tests for the Schema class.
*
* @author Matt Ward
*/
@RunWith(MockitoJUnitRunner.class)
public class SchemaTest extends DbObjectTestBase<Schema>
{
private Schema left;
private Schema right;
@Before
public void setUp()
{
left = new Schema("left_schema");
right = new Schema("right_schema");
}
@Override
protected Schema getThisObject()
{
return left;
}
@Override
protected Schema getThatObject()
{
return right;
}
@Override
protected void doDiffTests()
{
// 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);
}
}

View File

@@ -0,0 +1,36 @@
/*
* 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.model;
import org.junit.Ignore;
import org.junit.Test;
/**
* Tests for the Sequence class.
* @author Matt Ward
*/
public class SequenceTest
{
@Ignore
@Test
public void noTestsRequired()
{
// No functionality over and above AbstractDbObject at present.
}
}

View File

@@ -23,7 +23,7 @@ import java.util.Collection;
import java.util.List;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.SchemaUtils;
import org.alfresco.util.schemacomp.Result.Strength;
/**
* Instances of this class represent a database table.
@@ -165,24 +165,22 @@ public class Table extends AbstractDbObject
if (other.indexes != null) return false;
}
else if (!this.indexes.equals(other.indexes)) return false;
// TODO: this is difficult, equals probably should include this, but diffs shouldn't -
// decide what to do about this.
// if (this.primaryKey == null)
// {
// if (other.primaryKey != null) return false;
// }
// else if (!this.primaryKey.equals(other.primaryKey)) return false;
if (this.primaryKey == null)
{
if (other.primaryKey != null) return false;
}
else if (!this.primaryKey.equals(other.primaryKey)) return false;
return true;
}
@Override
protected void doDiff(DbObject other, Differences differences)
protected void doDiff(DbObject other, Differences differences, Strength strength)
{
Table rightTable = (Table) other;
SchemaUtils.compareCollections(columns, rightTable.columns, differences);
primaryKey.diff(rightTable.primaryKey, differences);
SchemaUtils.compareCollections(foreignKeys, rightTable.foreignKeys, differences);
SchemaUtils.compareCollections(indexes, rightTable.indexes, differences);
comparisonUtils.compareCollections(columns, rightTable.columns, differences);
primaryKey.diff(rightTable.primaryKey, differences, strength);
comparisonUtils.compareCollections(foreignKeys, rightTable.foreignKeys, differences);
comparisonUtils.compareCollections(indexes, rightTable.indexes, differences);
}
}

View File

@@ -0,0 +1,102 @@
/*
* 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.model;
import static java.util.Arrays.asList;
import java.util.Collection;
import java.util.List;
import org.alfresco.util.schemacomp.Result.Strength;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
* Tests for the Table class.
*
* @author Matt Ward
*/
@RunWith(MockitoJUnitRunner.class)
public class TableTest extends DbObjectTestBase<Table>
{
private Table table;
private Table otherTable;
private Collection<Column> columns;
private @Mock PrimaryKey primaryKey;
private Collection<ForeignKey> foreignKeys;
private Collection<Index> indexes;
@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);
}
@Override
protected List<Object> getMocksUsedInDiff()
{
List<Object> mocks = super.getMocksUsedInDiff();
mocks.add(primaryKey);
return mocks;
}
public void doDiffTests()
{
// Check columns
inOrder.verify(comparisonUtils).compareCollections(table.getColumns(), otherTable.getColumns(), differences);
// Check primary key
inOrder.verify(primaryKey).diff(otherTable.getPrimaryKey(), differences, Strength.ERROR);
// Check foreign keys
inOrder.verify(comparisonUtils).compareCollections(
table.getForeignKeys(), otherTable.getForeignKeys(), differences);
// Check indexes
inOrder.verify(comparisonUtils).compareCollections(
table.getIndexes(), otherTable.getIndexes(), differences);
}
@Override
protected Table getThisObject()
{
return table;
}
@Override
protected Table getThatObject()
{
return otherTable;
}
}