Skip to content

Commit

Permalink
Merge pull request #153 from dickschoeller/151-more-test-coverage
Browse files Browse the repository at this point in the history
Issue #151 - cover some more code
  • Loading branch information
dickschoeller committed Jan 24, 2017
2 parents 1126c2c + 2c1caf4 commit 2a1bec9
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ public LocalDate estimateFromBirth(final LocalDate localDate) {
* @return the adjusted date
*/
private LocalDate parentDateIncrement(final LocalDate date) {
if (date == null) {
return null;
}
final int years = typicals.ageAtMarriage()
+ typicals.gapBetweenChildren();
return plusYearsAdjustToBegin(date, years);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
package org.schoellerfamily.gedbrowser.analytics.test;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.joda.time.LocalDate;
import org.junit.Before;
import org.junit.Test;
import org.schoellerfamily.gedbrowser.analytics.BirthDateFromParentsEstimator;
import org.schoellerfamily.gedbrowser.datamodel.Family;
import org.schoellerfamily.gedbrowser.datamodel.Person;
import org.schoellerfamily.gedbrowser.datamodel.util.GedObjectBuilder;

/**
* @author Dick Schoeller
*/
public class BirthDateFromParentsEstimatorTest {
/** */
private transient GedObjectBuilder builder;
/** */
private transient Person person2;
/** */
private transient Person person3;
/** */
private transient Family family1;
/** */
private transient BirthDateFromParentsEstimator estimator;

/** */
@Before
public void setUp() {
builder = new GedObjectBuilder();
final Person person1 = builder.createPerson1();
person2 = builder.createPerson2();
person3 = builder.createPerson3();

family1 = builder.createFamily1();

builder.addChildToFamily(family1, person1);
builder.addHusbandToFamily(family1, person2);
builder.addWifeToFamily(family1, person3);
estimator = new BirthDateFromParentsEstimator(person1);
}

/** */
@Test
public void testFromBirthWithOnlyMarriage() {
builder.createFamilyEvent(family1, "Marriage", "10 MAY 1960");
assertNull("Should be null because no dates available to use",
estimator.estimateFromBirth(null));
}

/** */
@Test
public void testFromBirthWithOnlyFatherBirth() {
builder.createPersonEvent(person2, "Birth", "1 JAN 1935");
final LocalDate expected = new LocalDate(1962, 1, 1);
assertMatch(expected, estimator.estimateFromBirth(null));
}

/** */
@Test
public void testFromBirthWithOnlyFather() {
final Person child1 = builder.createPerson1();
final Person father = builder.createPerson3();
final Family family = builder.createFamily1();
builder.addChildToFamily(family, child1);
builder.addHusbandToFamily(family, father);
builder.createPersonEvent(father, "Birth", "1 JAN 1935");
final BirthDateFromParentsEstimator e =
new BirthDateFromParentsEstimator(child1);
final LocalDate expected = new LocalDate(1962, 1, 1);
assertMatch(expected, e.estimateFromBirth(null));
}

/** */
@Test
public void testFromBirthWithOnlyMotherBirth() {
builder.createPersonEvent(person3, "Birth", "1 JAN 1939");
final LocalDate expected = new LocalDate(1966, 1, 1);
assertMatch(expected, estimator.estimateFromBirth(null));
}

/** */
@Test
public void testFromBirthWithOnlyMother() {
final Person child1 = builder.createPerson1();
final Person mother = builder.createPerson4();
final Family family = builder.createFamily1();
builder.createFamilyEvent(family, "Marriage", "10 MAY 1960");
builder.addChildToFamily(family, child1);
builder.addWifeToFamily(family, mother);
builder.createPersonEvent(mother, "Birth", "1 JAN 1939");
final BirthDateFromParentsEstimator e =
new BirthDateFromParentsEstimator(child1);
final LocalDate expected = new LocalDate(1966, 1, 1);
assertMatch(expected, e.estimateFromBirth(null));
}

/** */
@Test
public void testFromBirthFamilyNoParents() {
final Person child1 = builder.createPerson1();
final Family family = builder.createFamily1();
builder.addChildToFamily(family, child1);
final BirthDateFromParentsEstimator e =
new BirthDateFromParentsEstimator(child1);
assertNull("Should not get a date without parents",
e.estimateFromBirth(null));
}

/** */
@Test
public void testFromBirthWithBothParentsBirth() {
builder.createPersonEvent(person2, "Birth", "1 JAN 1935");
builder.createPersonEvent(person3, "Birth", "1 JAN 1939");
final LocalDate expected = new LocalDate(1962, 1, 1);
assertMatch(expected, estimator.estimateFromBirth(null));
}

/** */
@Test
public void testFromBirthWithPreviousDate() {
builder.createPersonEvent(person2, "Birth", "1 JAN 1935");
builder.createPersonEvent(person3, "Birth", "1 JAN 1939");
final LocalDate expected = new LocalDate(1966, 10, 1);
assertMatch(expected, estimator.estimateFromBirth(expected));
}

/** */
@Test
public void testFromMarriageWithOnlyMarriage() {
builder.createFamilyEvent(family1, "Marriage", "10 MAY 1960");
final LocalDate expected = new LocalDate(1962, 5, 1);
assertMatch(expected, estimator.estimateFromMarriage(null));
}

/** */
@Test
public void testFromMarriageWithOlderSibling() {
final Person child1 = builder.createPerson1();
final Person child2 = builder.createPerson2();
final Person father = builder.createPerson3();
final Person mother = builder.createPerson4();
final Family family = builder.createFamily1();
builder.createFamilyEvent(family, "Marriage", "10 MAY 1960");
builder.addChildToFamily(family, child1);
builder.addChildToFamily(family, child2);
builder.addHusbandToFamily(family, father);
builder.addWifeToFamily(family, mother);
final BirthDateFromParentsEstimator e =
new BirthDateFromParentsEstimator(child2);
final LocalDate expected = new LocalDate(1964, 5, 1);
assertMatch(expected, e.estimateFromMarriage(null));
}

/** */
@Test
public void testFromMarriageWithYoungerSibling() {
final Person child1 = builder.createPerson1();
final Person child2 = builder.createPerson2();
final Person father = builder.createPerson3();
final Person mother = builder.createPerson4();
final Family family = builder.createFamily1();
builder.createFamilyEvent(family, "Marriage", "10 MAY 1960");
builder.addChildToFamily(family, child1);
builder.addChildToFamily(family, child2);
builder.addHusbandToFamily(family, father);
builder.addWifeToFamily(family, mother);
final BirthDateFromParentsEstimator e =
new BirthDateFromParentsEstimator(child1);
final LocalDate expected = new LocalDate(1962, 5, 1);
assertMatch(expected, e.estimateFromMarriage(null));
}

/** */
@Test
public void testFromMarriageWithPreviousDate() {
builder.createFamilyEvent(family1, "Marriage", "10 MAY 1960");
final LocalDate expected = new LocalDate(1965, 5, 1);
assertMatch(expected, estimator.estimateFromMarriage(expected));
}

/** */
@Test
public void testFromMarriageWithOnlyFatherBirth() {
builder.createPersonEvent(person2, "Birth", "1 JAN 1935");
assertNull("Should be null because no dates available to use",
estimator.estimateFromMarriage(null));
}

/** */
@Test
public void testFromMarriageWithOnlyMotherBirth() {
builder.createPersonEvent(person3, "Birth", "1 JAN 1939");
assertNull("Should be null because no dates available to use",
estimator.estimateFromMarriage(null));
}

/** */
@Test
public void testFromMarriageWithBothParentsBirth() {
builder.createPersonEvent(person2, "Birth", "1 JAN 1935");
builder.createPersonEvent(person3, "Birth", "1 JAN 1939");
assertNull("Should be null because no dates available to use",
estimator.estimateFromMarriage(null));
}

/** */
@Test
public void testFromMarriageFamilyNoParents() {
final Person child1 = builder.createPerson1();
final Family family = builder.createFamily1();
builder.addChildToFamily(family, child1);
builder.createFamilyEvent(family, "Marriage", "10 MAY 1960");
final BirthDateFromParentsEstimator e =
new BirthDateFromParentsEstimator(child1);
final LocalDate expected = new LocalDate(1962, 5, 1);
assertMatch(expected, e.estimateFromMarriage(null));
}

/**
* @param expected expected date
* @param actual actual date
*/
private void assertMatch(final LocalDate expected, final LocalDate actual) {
assertTrue(mismatchString(expected, actual),
expected.isEqual(actual));
}

/**
* @param expected expected date
* @param actual actual date
* @return string describing the mismatch
*/
private String mismatchString(final LocalDate expected,
final LocalDate actual) {
return "Don't match! expected: " + expected + ", actual: " + actual;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public final class RootFinder implements FinderStrategy {
*/
@Override
public GedObject find(final GedObject owner, final String str) {
if (!(owner instanceof Root)) {
return null;
}
final Root root = (Root) owner;
return root.getObjects().get(str);
}
Expand All @@ -38,9 +35,6 @@ public GedObject find(final GedObject owner, final String str) {
*/
@Override
public void insert(final GedObject owner, final GedObject gob) {
if (!(owner instanceof Root)) {
return;
}
final Root root = (Root) owner;
root.insert(gob.getString(), gob);
}
Expand All @@ -59,9 +53,6 @@ public <T extends GedObject> T find(
*/
@Override
public String getFilename(final GedObject owner) {
if (!(owner instanceof Root)) {
return null;
}
final Root root = (Root) owner;
return root.getTheFilename();
}
Expand All @@ -71,9 +62,6 @@ public String getFilename(final GedObject owner) {
*/
@Override
public String getDbName(final GedObject owner) {
if (!(owner instanceof Root)) {
return null;
}
final Root root = (Root) owner;
return root.getTheDbName();
}
Expand All @@ -84,9 +72,6 @@ public String getDbName(final GedObject owner) {
@Override
public Collection<Person> findBySurname(final GedObject owner,
final String surname) {
if (!(owner instanceof Root)) {
return null;
}
final Root root = (Root) owner;
final Map<String, GedObject> objectMap = root.getObjects();
final Collection<GedObject> objects = objectMap.values();
Expand Down Expand Up @@ -143,9 +128,6 @@ public int compare(final Person arg0,
@Override
public Collection<String> findBySurnamesBeginWith(final GedObject owner,
final String beginsWith) {
if (!(owner instanceof Root)) {
return null;
}
final Root root = (Root) owner;
final Map<String, GedObject> objectMap = root.getObjects();
final Collection<GedObject> objects = objectMap.values();
Expand All @@ -167,9 +149,6 @@ public Collection<String> findBySurnamesBeginWith(final GedObject owner,
*/
@Override
public Collection<String> findSurnameInitialLetters(final GedObject owner) {
if (!(owner instanceof Root)) {
return null;
}
final Root root = (Root) owner;
final Map<String, GedObject> objectMap = root.getObjects();
final Collection<GedObject> objects = objectMap.values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public void setUp() {

builder.addHusbandToFamily(family6, person6);
builder.addWifeToFamily(family6, person7);

final Person person8 = builder.createPerson("I8", "Same/Name/");
builder.createPersonEvent(person8, "Birth", "1 JAN 1950");
final Person person9 = builder.createPerson("I9", "Same/Name/");
builder.createPersonEvent(person9, "Birth", "1 JAN 1940");
final Person person10 = builder.createPerson("I10", "Same/Name/");
builder.createPersonEvent(person10, "Birth", "1 JAN 1950");
final Person person11 = builder.createPerson("I11", "Different/Name/");
builder.createPersonEvent(person11, "Birth", "1 JAN 1930");
}

/** */
Expand Down Expand Up @@ -394,7 +403,7 @@ public void testPersonFindGedObjectString() {
@Test
public void testLetterFinder() {
final String[] expected = {
"?", "R", "S"
"?", "N", "R", "S"
};
final Collection<String> letters = person6.findSurnameInitialLetters();
int i = 0;
Expand Down Expand Up @@ -446,6 +455,18 @@ public void testBeginsWithFound() {
}
}

/** */
@Test
public void testOrder() {
// Note I10 sorts ahead of I8.
final String[] expected = {"I11", "I9", "I10", "I8"};
final Collection<Person> persons = person6.findBySurname("Name");
int i = 0;
for (final Person person : persons) {
assertEquals("ID out of order", expected[i++], person.getString());
}
}

/** */
@Test
public void testBeginsWithNotFound() {
Expand Down

0 comments on commit 2a1bec9

Please sign in to comment.