Skip to content

Commit

Permalink
Simplify tests so they don't write files.
Browse files Browse the repository at this point in the history
  • Loading branch information
szeke committed Dec 26, 2013
1 parent 84f9322 commit bed97eb
Showing 1 changed file with 46 additions and 38 deletions.
84 changes: 46 additions & 38 deletions src/test/java/edu/isi/karma/rdf/TestJSONRDFGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@

package edu.isi.karma.rdf;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.junit.After;
import org.junit.AfterClass;
Expand All @@ -41,12 +40,12 @@

/**
* @author dipsy
*
*
*/
public class TestJSONRDFGenerator {

JSONRDFGenerator rdfGen;

/**
* @throws java.lang.Exception
*/
Expand All @@ -68,12 +67,14 @@ public static void tearDownAfterClass() throws Exception {
public void setUp() throws Exception {
rdfGen = JSONRDFGenerator.getInstance();

//Add the models in
// Add the models in
R2RMLMappingIdentifier modelIdentifier = new R2RMLMappingIdentifier(
"people-model", new File(getTestDataFolder() + "/people-model.ttl").toURI().toURL());
"people-model", new File(getTestDataFolder()
+ "/people-model.ttl").toURI().toURL());
rdfGen.addModel(modelIdentifier);
modelIdentifier = new R2RMLMappingIdentifier(
"cs548-events-model", new File(getTestDataFolder() + "/cs548-events-model.ttl").toURI().toURL());
modelIdentifier = new R2RMLMappingIdentifier("cs548-events-model",
new File(getTestDataFolder() + "/cs548-events-model.ttl")
.toURI().toURL());
rdfGen.addModel(modelIdentifier);
}

Expand All @@ -85,59 +86,66 @@ public void tearDown() throws Exception {
}

/**
* Test method for {@link edu.isi.karma.rdf.JSONRDFGenerator#generateRDF(java.lang.String, java.lang.String, boolean, java.io.PrintWriter)}.
* Test method for
* {@link edu.isi.karma.rdf.JSONRDFGenerator#generateRDF(java.lang.String, java.lang.String, boolean, java.io.PrintWriter)}
* .
*/
@Test
public void testGenerateRDF1() {
try {

String filename = getTestDataFolder() + "/people.json";
System.out.println("Load json file: " + filename);
String jsonData = EncodingDetector.getString(new File(filename), "utf-8");

String outputFilePath = getTestDataFolder() + "/people.rdf";
System.out.println("Generate RDF: " + outputFilePath);
OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(outputFilePath), "UTF-8");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);

String jsonData = EncodingDetector.getString(new File(filename),
"utf-8");

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

rdfGen.generateRDF("people-model", jsonData, true, pw);
String readRDF = EncodingDetector.getString(new File(outputFilePath), "utf-8");
assertNotEquals(readRDF.length(), 0);
} catch(Exception e) {
String rdf = sw.toString();

assertNotEquals(rdf.length(), 0);
String[] lines = rdf.split("\n");
assertEquals(102, lines.length);
} catch (Exception e) {
fail("Execption: " + e.getMessage());
}
}

/**
* Test method for {@link edu.isi.karma.rdf.JSONRDFGenerator#generateRDF(java.lang.String, java.lang.String, boolean, java.io.PrintWriter)}.
* Test method for
* {@link edu.isi.karma.rdf.JSONRDFGenerator#generateRDF(java.lang.String, java.lang.String, boolean, java.io.PrintWriter)}
* .
*/
@Test
public void testGenerateRDF2() {
try {

String filename = getTestDataFolder() + "/cs548-events.json";
System.out.println("Load json file: " + filename);
String jsonData = EncodingDetector.getString(new File(filename), "utf-8");

String outputFilePath = getTestDataFolder() + "/cs548-events.rdf";
System.out.println("Generate RDF: " + outputFilePath);
OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(outputFilePath), "UTF-8");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);

String jsonData = EncodingDetector.getString(new File(filename),
"utf-8");

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

rdfGen.generateRDF("cs548-events-model", jsonData, true, pw);
String readRDF = EncodingDetector.getString(new File(outputFilePath), "utf-8");
assertNotEquals(readRDF.length(), 0);
} catch(Exception e) {
String rdf = sw.toString();

assertNotEquals(rdf.length(), 0);
String[] lines = rdf.split("\n");
assertEquals(234, lines.length);
} catch (Exception e) {
fail("Execption: " + e.getMessage());
}
}

private String getRootFolder() {
return getClass().getClassLoader().getResource(".").getPath() + "/../../";
return getClass().getClassLoader().getResource(".").getPath()
+ "/../../";
}

private String getTestDataFolder() {
return getRootFolder() + "src/test/karma-data";
}
Expand Down

0 comments on commit bed97eb

Please sign in to comment.