Fixed json tests.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2275 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2011-10-06 04:50:05 +00:00
parent 850e147779
commit 3c6358d518
3 changed files with 59 additions and 5 deletions

View File

@ -7,7 +7,10 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.refine.ProjectMetadata;
@ -21,6 +24,7 @@ import com.google.refine.importing.ImportingManager;
import com.google.refine.model.Project;
import com.google.refine.tests.RefineServletStub;
import com.google.refine.tests.RefineTest;
import com.google.refine.util.JSONUtilities;
abstract class ImporterTest extends RefineTest {
//mock dependencies
@ -98,8 +102,9 @@ abstract class ImporterTest extends RefineTest {
project.columnModel.update();
}
protected void parseOneFile(TreeImportingParserBase parser, InputStream inputStream) {
protected void parseOneFile(TreeImportingParserBase parser, InputStream inputStream, JSONObject options) {
ImportColumnGroup rootColumnGroup = new ImportColumnGroup();
Reader reader = new InputStreamReader(inputStream);
parser.parseOneFile(
project,

View File

@ -37,7 +37,10 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
@ -47,8 +50,11 @@ import org.testng.annotations.Test;
import com.google.refine.importers.JsonImporter;
import com.google.refine.importers.JsonImporter.JSONTreeReader;
import com.google.refine.importers.tree.TreeImportingParserBase;
import com.google.refine.importers.tree.TreeReader.Token;
import com.google.refine.importing.ImportingJob;
import com.google.refine.model.Row;
import com.google.refine.util.JSONUtilities;
public class JsonImporterTests extends ImporterTest {
@Override
@ -164,7 +170,16 @@ public class JsonImporterTests extends ImporterTest {
@Test
public void testElementWithMqlReadOutput(){
String mqlOutput = "{\"code\":\"/api/status/ok\",\"result\":[{\"armed_force\":{\"id\":\"/en/wehrmacht\"},\"id\":\"/en/afrika_korps\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/en/sacred_band_of_thebes\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/en/british_army\"},\"id\":\"/en/british_16_air_assault_brigade\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/en/british_army\"},\"id\":\"/en/pathfinder_platoon\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0ch7qgz\"},\"id\":\"/en/sacred_band\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/en/polish_navy\"},\"id\":\"/en/3rd_ship_flotilla\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/m/0c0kxn9\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/m/0c0kxq9\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/m/0c0kxqh\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/m/0c0kxqp\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/m/0c0kxqw\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/m/0c1wxl3\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/m/0c1wxlp\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/m/0ck96kz\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/m/0cm3j23\",\"type\":\"/military/military_unit\"},{\"armed_force\":{\"id\":\"/m/0chtrwn\"},\"id\":\"/m/0cw8hb4\",\"type\":\"/military/military_unit\"}],\"status\":\"200 OK\",\"transaction_id\":\"cache;cache01.p01.sjc1:8101;2010-10-04T15:04:33Z;0007\"}";
RunTest(mqlOutput);
JSONObject options = SUT.createParserUIInitializationData(
job, new LinkedList<JSONObject>(), "text/json");
JSONArray path = new JSONArray();
JSONUtilities.append(path, "__anonymous__");
JSONUtilities.append(path, "result");
JSONUtilities.append(path, "__anonymous__");
JSONUtilities.safePut(options, "recordPath", path);
RunTest(mqlOutput, options);
log(project);
assertProjectCreated(project,3,16);
}
@ -281,6 +296,18 @@ public class JsonImporterTests extends ImporterTest {
return sb.toString();
}
public static JSONObject getOptions(ImportingJob job, TreeImportingParserBase parser) {
JSONObject options = parser.createParserUIInitializationData(
job, new LinkedList<JSONObject>(), "text/json");
JSONArray path = new JSONArray();
JSONUtilities.append(path, "__anonymous__");
JSONUtilities.append(path, "__anonymous__");
JSONUtilities.safePut(options, "recordPath", path);
return options;
}
public static String getSampleWithDuplicateNestedElements(){
StringBuilder sb = new StringBuilder();
sb.append("[");
@ -348,7 +375,11 @@ public class JsonImporterTests extends ImporterTest {
return sb.toString();
}
private void RunTest(String testString){
private void RunTest(String testString) {
RunTest(testString, getOptions(job, SUT));
}
private void RunTest(String testString, JSONObject options) {
try {
inputStream = new ByteArrayInputStream( testString.getBytes( "UTF-8" ) );
} catch (UnsupportedEncodingException e1) {
@ -356,7 +387,7 @@ public class JsonImporterTests extends ImporterTest {
}
try {
parseOneFile(SUT, inputStream);
parseOneFile(SUT, inputStream, options);
} catch (Exception e) {
Assert.fail();
}

View File

@ -36,7 +36,10 @@ package com.google.refine.tests.importers;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
@ -45,7 +48,10 @@ import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.google.refine.importers.XmlImporter;
import com.google.refine.importers.tree.TreeImportingParserBase;
import com.google.refine.importing.ImportingJob;
import com.google.refine.model.Row;
import com.google.refine.util.JSONUtilities;
public class XmlImporterTests extends ImporterTest {
@ -216,6 +222,18 @@ public class XmlImporterTests extends ImporterTest {
return sb.toString();
}
public static JSONObject getOptions(ImportingJob job, TreeImportingParserBase parser) {
JSONObject options = parser.createParserUIInitializationData(
job, new LinkedList<JSONObject>(), "text/json");
JSONArray path = new JSONArray();
JSONUtilities.append(path, "library");
JSONUtilities.append(path, "book");
JSONUtilities.safePut(options, "recordPath", path);
return options;
}
public static String getDeeplyNestedSample(){
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\"?><nest><nest2><library>");
@ -307,7 +325,7 @@ public class XmlImporterTests extends ImporterTest {
}
try {
parseOneFile(SUT, inputStream);
parseOneFile(SUT, inputStream, getOptions(job, SUT));
} catch (Exception e) {
Assert.fail();
}