Skip to content

Commit

Permalink
Expand check on header reading
Browse files Browse the repository at this point in the history
This won't change coverage, but it will help assess whether
the parsing of the header, which has special characteristics,
is proceeding as expected.
  • Loading branch information
dickschoeller committed Jun 24, 2017
1 parent 3a9c8e6 commit 3cf954f
Showing 1 changed file with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.schoellerfamily.gedbrowser.datamodel.Date;
import org.schoellerfamily.gedbrowser.datamodel.GedObject;
import org.schoellerfamily.gedbrowser.datamodel.Head;
import org.schoellerfamily.gedbrowser.datamodel.Person;
import org.schoellerfamily.gedbrowser.datamodel.Root;
import org.schoellerfamily.gedbrowser.datamodel.navigator.PersonNavigator;
Expand Down Expand Up @@ -55,6 +59,78 @@ public void testFactoryGedFile() throws IOException {
logger.info(spouses.get(0));
}

/**
* Test GedLine with an array input.
*
* @throws IOException never.
*/
@Test
public void testFactoryGedFileHead() throws IOException {
final AbstractGedLine top = readFileTestSource();
final Root root = g2g.create(top);
final Head head = (Head) root.find("Header");
final List<String> headsCheckResults = checkHeads(head);
assertEquals(
"Problems in head:\n" + formatResult(headsCheckResults),
0, headsCheckResults.size());
}

/**
* @param head the header to check
* @return list of problem descriptions
*/
private List<String> checkHeads(final Head head) {
final List<String> errors = new ArrayList<>();
if (head == null) {
errors.add("No header found");
return errors;
}
final int expectedSize = 6;
final List<GedObject> attributes = head.getAttributes();
if (attributes.size() < expectedSize) {
errors.add("Wrong number of attributes: expected 6, found "
+ attributes.size());
}
final String[] expectedAttributes = {
"Source",
"Submittor",
"GEDCOM",
"Destination",
"Date",
"Character Set"
};
for (int i = 0; i < expectedSize; i++) {
final GedObject attribute = attributes.get(i);
final String expectedType = expectedAttributes[i];
final String actualType = attribute.getString();
if ("Date".equals(expectedType)) {
if (!attribute.getClass().equals(Date.class)) {
errors.add("attribute[" + i + "], expected type "
+ expectedType + ", actual type " + actualType);
}
} else {
if (!expectedType.equals(actualType)) {
errors.add("attribute[" + i + "], expected type "
+ expectedType + ", actual type " + actualType);
}
}
}
return errors;
}

/**
* @param checkResults array
* @return a nicely build up string
*/
@SuppressWarnings("PMD.UseStringBufferForStringAppends")
private String formatResult(final List<String> checkResults) {
String output = "";
for (final String result : checkResults) {
output += " " + result + "\n";
}
return output;
}

/**
* Read data for tests available to prepare data for tests.
*
Expand Down

0 comments on commit 3cf954f

Please sign in to comment.