Skip to content

Commit

Permalink
Add new EMLValidator constructor to work on String inputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbjones committed Feb 2, 2019
1 parent 64746ce commit 157ce35
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
Binary file modified lib/eml.jar
Binary file not shown.
16 changes: 15 additions & 1 deletion src/main/java/org/ecoinformatics/eml/EMLValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class EMLValidator {
private List<String> errors = null;

/**
* Construct an EMLValidator for use on a given file path.
* Construct an EMLValidator for use on a given File.
* @param file a File containing the EML text to validate
*/
public EMLValidator(File file) {
Expand All @@ -50,6 +50,20 @@ public EMLValidator(File file) {
}
}

/**
* Construct an EMLValidator for use on a text String.
* @param emltext a String containing the EML text to validate
*/
public EMLValidator(String emltext) {
try {
StringReader reader = new StringReader(emltext);
doc = parseDocument(new InputSource(reader));
errors = new ArrayList<String>();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}

/**
* Simple method for command-line validation. The first argument is the
* EML filename.
Expand Down
61 changes: 38 additions & 23 deletions src/test/java/org/ecoinformatics/emltest/EMLValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
package org.ecoinformatics.emltest;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

import junit.framework.Test;
Expand All @@ -36,8 +39,7 @@
/**
* A JUnit test for testing the EMLValidator
*/
public class EMLValidatorTest extends TestCase
{
public class EMLValidatorTest extends TestCase {
private final static String TEST_DIR = "./src/test/resources";
private final static String INVALID_DIR = TEST_DIR + "/invalidEML";

Expand All @@ -46,48 +48,41 @@ public class EMLValidatorTest extends TestCase
*
* @param name the name of the test method
*/
public EMLValidatorTest(String name)
{
public EMLValidatorTest(String name) {
super(name);
}

/** Establish a testing framework by initializing appropriate objects */
public void setUp()
{

public void setUp() {
}

/** Release any objects after tests are complete */
public void tearDown()
{
public void tearDown() {
}

/**
* Create a suite of tests to be run together
*
* @return The test suite
*/
public static Test suite()
{
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new EMLValidatorTest("initialize"));
suite.addTest(new EMLValidatorTest("testValidDocs"));
suite.addTest(new EMLValidatorTest("testInvalidDocs"));
suite.addTest(new EMLValidatorTest("testStringInput"));
return suite;
}

/**
* Check that the testing framework is functioning properly with a trivial
* assertion.
*/
public void initialize()
{
public void initialize() {
assertTrue(true);
}

public void testValidDocs()
{

public void testValidDocs() {
// all valid documents should validate
File testDir = new File(TEST_DIR);
ArrayList fileList = getXmlFiles(testDir);
Expand All @@ -106,7 +101,7 @@ public void testValidDocs()
} catch (Exception e) {
e.printStackTrace(System.err);
fail("Validator exception!\n\n" + e.getClass().getName() +
"(" + e.getMessage() + ")" );
"(" + e.getMessage() + ")" );
}
}
}
Expand Down Expand Up @@ -135,12 +130,34 @@ public void testInvalidDocs() {
} catch (Exception e) {
e.printStackTrace(System.err);
fail("Validator exception!\n\n" + e.getClass().getName() +
"(" + e.getMessage() + ")" );
"(" + e.getMessage() + ")" );
}
}
if (failures != invalidFileCount) {
System.err.println(failures + "/" + invalidFileCount + " failures in directory.");
fail("Error: An error should have been thrown for all invalid files.");
System.err.println(failures + "/" + invalidFileCount + " failures in directory.");
fail("Error: An error should have been thrown for all invalid files.");
}
}

public void testStringInput() {
// document should validate when passed in as a String
File testDir = new File(TEST_DIR);
File testFile = new File(TEST_DIR, "eml-sample.xml");
try {
System.err.println("Validating string input for: " + testFile.getName());
String emltext = new String(Files.readAllBytes(Paths.get(testFile.getAbsolutePath())), StandardCharsets.UTF_8);
EMLValidator validator = new EMLValidator(emltext);
boolean isValid = validator.validate();
if (!isValid) {
for (String e : validator.getErrors()) {
System.err.println(e);
}
fail("Validator: NOT valid: " + testFile.getPath());
}
} catch (Exception e) {
e.printStackTrace(System.err);
fail("Validator exception!\n\n" + e.getClass().getName() +
"(" + e.getMessage() + ")" );
}
}

Expand All @@ -150,8 +167,7 @@ public void testInvalidDocs() {
* @param directory the directory to list
* @return a vector of File objects in the directory
*/
private ArrayList getXmlFiles(File directory)
{
private ArrayList getXmlFiles(File directory) {
String[] files = directory.list();
ArrayList fileList = new ArrayList();

Expand All @@ -164,5 +180,4 @@ private ArrayList getXmlFiles(File directory)
}
return fileList;
}

}

0 comments on commit 157ce35

Please sign in to comment.