/*
* 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 .
*/
package org.alfresco.util.schemacomp;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.alfresco.util.schemacomp.Difference.Where;
import org.alfresco.util.schemacomp.model.AbstractDbObject;
import org.alfresco.util.schemacomp.model.DbObject;
import org.alfresco.util.schemacomp.validator.DbValidator;
import org.hibernate.dialect.Dialect;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
* Tests for the SchemaUtils class.
*
* @author Matt Ward
*/
@RunWith(MockitoJUnitRunner.class)
public class DefaultComparisonUtilsTest
{
private @Mock Results differences;
private DefaultComparisonUtils comparisonUtils;
private DiffContext ctx;
private @Mock Dialect dialect;
@Before
public void setUp()
{
comparisonUtils = new DefaultComparisonUtils();
ctx = new DiffContext(dialect, differences, null, null);
}
@Test
public void compareSimple()
{
comparisonUtils.compareSimple(prop(null), prop(null), ctx);
verify(differences).add(Where.IN_BOTH_NO_DIFFERENCE, prop(null), prop(null));
comparisonUtils.compareSimple(prop("not_null_string"), prop("not_null_string"), ctx);
verify(differences).add(Where.IN_BOTH_NO_DIFFERENCE, prop("not_null_string"), prop("not_null_string"));
comparisonUtils.compareSimple(prop("Not_Null_String"), prop("NOT_NULL_STRING"), ctx);
verify(differences).add(Where.IN_BOTH_NO_DIFFERENCE, prop("Not_Null_String"), prop("NOT_NULL_STRING"));
comparisonUtils.compareSimple(prop("left"), prop("right"), ctx);
verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, prop("left"), prop("right"));
comparisonUtils.compareSimple(prop("left"), prop(null), ctx);
verify(differences).add(Where.ONLY_IN_REFERENCE, prop("left"), prop(null));
comparisonUtils.compareSimple(prop(null), prop("right"), ctx);
verify(differences).add(Where.ONLY_IN_TARGET, prop(null), prop("right"));
}
public DbProperty prop(String propValue)
{
DbObject dbo = new DbObjectWithCollection("dbo", null);
return dbPropForValue(dbo, "someProperty", propValue);
}
@Test
public void compareCollections()
{
DbObject db1 = new DatabaseObject("db1");
DbObject db2 = new DatabaseObject("db2"); // only in left
DbObject db3 = new DatabaseObject("db3"); // only in right
DbObject db4 = new DatabaseObject("db4");
Collection left = new ArrayList();
Collections.addAll(left, db1, db2, db4);
Collection right = new ArrayList();
Collections.addAll(right, db1, db3, db4);
comparisonUtils.compareCollections(left, right, ctx);
// Differences and ommissions are noticed...
verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, new DbProperty(db1), new DbProperty(db1));
verify(differences).add(Where.ONLY_IN_REFERENCE, new DbProperty(db2), null);
verify(differences).add(Where.ONLY_IN_TARGET, null, new DbProperty(db3));
verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, new DbProperty(db4), new DbProperty(db4));
}
@Test
public void compareCollectionsWithMultipleMatches()
{
DbObject db2 = new DatabaseObject("db2");
DbObject db3 = new DatabaseObject("db3");
DbObject db4 = new DatabaseObject("db4");
DbObject db1 = new DatabaseObject("db1", db2, db3);
Collection left = new ArrayList();
Collections.addAll(left, db1, db4);
Collection right = new ArrayList();
Collections.addAll(right, db1, db2, db3);
comparisonUtils.compareCollections(left, right, ctx);
// Differences and ommissions are noticed...
verify(differences).add(Where.ONLY_IN_REFERENCE, new DbProperty(db4), null);
verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, new DbProperty(db1), new DbProperty(db1));
verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, new DbProperty(db1), new DbProperty(db2));
verify(differences).add(Where.IN_BOTH_BUT_DIFFERENCE, new DbProperty(db1), new DbProperty(db3));
}
@Test
public void propertyIsNotComparedWhenValidatorTakesResponsibility()
{
DbObject db1 = new DatabaseObject("db1");
DbProperty db1NameProp = new DbProperty(db1, "name");
DbObject db2 = new DatabaseObject("db2");
DbProperty db2NameProp = new DbProperty(db2, "name");
// Using mock to decouple unit test from actual NameValidator.
DbValidator nameValidator = mock(DbValidator.class);
when(nameValidator.validates("name")).thenReturn(true);
db1.getValidators().add(nameValidator);
comparisonUtils.compareSimple(db1NameProp, db2NameProp, ctx);
verify(differences, never()).add(Where.IN_BOTH_BUT_DIFFERENCE, db1NameProp, db2NameProp);
}
@Test
public void collectionPropertyIsNotComparedWhenValidatorTakesResponsibility()
{
Collection