Skip to content

Commit

Permalink
fix null comparisons #36
Browse files Browse the repository at this point in the history
Adding helper class for null comparisons prior to actual comparator and
updated in DateTimeComparator, EntityComparator and
ChildEntityComparator
  • Loading branch information
costea32 committed Nov 7, 2016
1 parent d77fbc8 commit 653cf27
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import org.testmonkeys.jentitytest.comparison.ComparisonContext;
import org.testmonkeys.jentitytest.comparison.MultiResultComparator;
import org.testmonkeys.jentitytest.comparison.result.ComparisonResult;
import org.testmonkeys.jentitytest.comparison.result.FailedComparisonResult;
import org.testmonkeys.jentitytest.comparison.util.NullComparison;
import org.testmonkeys.jentitytest.comparison.util.NullComparisonResult;
import org.testmonkeys.jentitytest.framework.JEntityTestException;

import java.beans.PropertyDescriptor;
Expand All @@ -14,6 +15,7 @@

public class ChildEntityComparator extends MultiResultComparator {

private final NullComparison nullComparisonHelper = new NullComparison();
private List<ComparisonResult> comparisonResults;

public ChildEntityComparator() {
Expand All @@ -23,15 +25,11 @@ public ChildEntityComparator() {
@Override
protected List<ComparisonResult> computeComparison(PropertyDescriptor property, Object actualValue, Object expectedValue, ComparisonContext context) throws JEntityTestException {

if (actualValue == null && expectedValue == null)
return new ArrayList<>();
if (actualValue != null && expectedValue == null || actualValue == null) {
List<ComparisonResult> results = new ArrayList<>();
ComparisonResult result = new FailedComparisonResult(context,
actualValue == null ? "null" : "not null",
expectedValue == null ? "null" : "not null");
results.add(result);
return results;
NullComparisonResult nullComparisonResult = nullComparisonHelper.compareOnNulls(actualValue, expectedValue, context);
if (!nullComparisonResult.isPassed() || nullComparisonResult.isStopComparison()) {
List<ComparisonResult> comparisonResults = new ArrayList<>();
comparisonResults.add(nullComparisonResult);
return comparisonResults;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.testmonkeys.jentitytest.comparison.ComparisonContext;
import org.testmonkeys.jentitytest.comparison.ComparisonModel;
import org.testmonkeys.jentitytest.comparison.result.ComparisonResult;
import org.testmonkeys.jentitytest.comparison.util.NullComparison;
import org.testmonkeys.jentitytest.comparison.util.NullComparisonResult;
import org.testmonkeys.jentitytest.framework.JEntityTestException;

import java.beans.PropertyDescriptor;
Expand All @@ -15,28 +17,30 @@
public class EntityComparator {

private final EntityComparisonDictionary comparisonDictionary = EntityComparisonDictionary.getInstance();
private final NullComparison nullComparisonHelper = new NullComparison();

public List<ComparisonResult> compare(Object actual, Object expected) throws JEntityTestException {
ComparisonModel comparisonModel = comparisonDictionary.getComparisonModel(expected.getClass());
return compare(actual, expected, comparisonModel);
}

public List<ComparisonResult> compare(Object actual, Object expected, ComparisonContext context) throws JEntityTestException {
ComparisonModel comparisonModel = comparisonDictionary.getComparisonModel(expected.getClass());
return compare(actual, expected, comparisonModel, context);
return compare(actual, expected, null);
}

public List<ComparisonResult> compare(Object actual, Object expected, ComparisonModel model) throws JEntityTestException {
return compare(actual, expected, model, null);
}

public List<ComparisonResult> compare(Object actual, Object expected, ComparisonModel model, ComparisonContext context) throws JEntityTestException {
public List<ComparisonResult> compare(Object actual, Object expected, ComparisonContext context) throws JEntityTestException {
List<ComparisonResult> comparisonResults = new LinkedList<>();
if (context == null) {
context = new ComparisonContext();
context.setParentName("Entity");
context.setActualObj(actual);
}

NullComparisonResult nullComparisonResult = nullComparisonHelper.compareOnNulls(actual, expected, context);
if (!nullComparisonResult.isPassed() || nullComparisonResult.isStopComparison()) {
comparisonResults.add(nullComparisonResult);
return comparisonResults;
}

ComparisonModel model = comparisonDictionary.getComparisonModel(expected.getClass());


for (PropertyDescriptor propertyDescriptor : model.getComparableProperties()) {
Comparator comparator = model.getComparator(propertyDescriptor);
ComparisonContext comparisonContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.testmonkeys.jentitytest.comparison.ComparisonContext;
import org.testmonkeys.jentitytest.comparison.SingleResultComparator;
import org.testmonkeys.jentitytest.comparison.result.ComparisonResult;
import org.testmonkeys.jentitytest.comparison.util.NullComparison;
import org.testmonkeys.jentitytest.comparison.util.NullComparisonResult;
import org.testmonkeys.jentitytest.framework.JEntityTestException;

import java.beans.PropertyDescriptor;
Expand All @@ -11,16 +13,18 @@

public class DateTimeComparator extends SingleResultComparator {

private final NullComparison nullComparisonHelper = new NullComparison();

private int delta = 0;
private ChronoUnit unit = ChronoUnit.NANOS;

@Override
protected ComparisonResult computeSingleComparison(PropertyDescriptor property, Object a, Object e, ComparisonContext context) {
ComparisonResult result = new ComparisonResult(false, context, a, e);

if (a == null && e == null) {
result.setPassed(true);
return result;
NullComparisonResult nullComparisonResult = nullComparisonHelper.compareOnNulls(a, e, context);
if (!nullComparisonResult.isPassed() || nullComparisonResult.isStopComparison()) {
return nullComparisonResult;
}

if (!(a instanceof Temporal) && !(e instanceof Temporal))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.testmonkeys.jentitytest.comparison.util;

import org.testmonkeys.jentitytest.comparison.ComparisonContext;

public class NullComparison {

public NullComparisonResult compareOnNulls(Object actual, Object expected, ComparisonContext context) {
NullComparisonResult result = new NullComparisonResult(true, context, actual, expected);

if (actual == null && expected == null) {
result.setStopComparison(true);
return result;
}

if (actual != null && expected != null)
return result;

result.setPassed(false);
result.setExpected(expected == null ? "null" : "not null");
result.setActual(actual == null ? "null" : "not null");
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.testmonkeys.jentitytest.comparison.util;

import org.testmonkeys.jentitytest.comparison.ComparisonContext;
import org.testmonkeys.jentitytest.comparison.result.ComparisonResult;

public class NullComparisonResult extends ComparisonResult {

private boolean stopComparison;

public NullComparisonResult(boolean passed, ComparisonContext context, Object actual, Object expected) {
super(passed, context, actual, expected);
}

public boolean isStopComparison() {
return stopComparison;
}

public void setStopComparison(boolean stopComparison) {
this.stopComparison = stopComparison;
}
}

0 comments on commit 653cf27

Please sign in to comment.