From 3c6358d518579f10af622b42d310b20a4274f568 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Thu, 6 Oct 2011 04:50:05 +0000 Subject: [PATCH] Fixed json tests. git-svn-id: http://google-refine.googlecode.com/svn/trunk@2275 7d457c2a-affb-35e4-300a-418c747d4874 --- .../refine/tests/importers/ImporterTest.java | 7 +++- .../tests/importers/JsonImporterTests.java | 37 +++++++++++++++++-- .../tests/importers/XmlImporterTests.java | 20 +++++++++- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/main/tests/server/src/com/google/refine/tests/importers/ImporterTest.java b/main/tests/server/src/com/google/refine/tests/importers/ImporterTest.java index 14c78c6d9..d7f1a1326 100644 --- a/main/tests/server/src/com/google/refine/tests/importers/ImporterTest.java +++ b/main/tests/server/src/com/google/refine/tests/importers/ImporterTest.java @@ -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, diff --git a/main/tests/server/src/com/google/refine/tests/importers/JsonImporterTests.java b/main/tests/server/src/com/google/refine/tests/importers/JsonImporterTests.java index 2a5930b89..a3e21d9df 100644 --- a/main/tests/server/src/com/google/refine/tests/importers/JsonImporterTests.java +++ b/main/tests/server/src/com/google/refine/tests/importers/JsonImporterTests.java @@ -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(), "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); } @@ -280,6 +295,18 @@ public class JsonImporterTests extends ImporterTest { sb.append("]"); return sb.toString(); } + + public static JSONObject getOptions(ImportingJob job, TreeImportingParserBase parser) { + JSONObject options = parser.createParserUIInitializationData( + job, new LinkedList(), "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(); @@ -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(); } diff --git a/main/tests/server/src/com/google/refine/tests/importers/XmlImporterTests.java b/main/tests/server/src/com/google/refine/tests/importers/XmlImporterTests.java index e30cb2ec7..1b8f4682e 100644 --- a/main/tests/server/src/com/google/refine/tests/importers/XmlImporterTests.java +++ b/main/tests/server/src/com/google/refine/tests/importers/XmlImporterTests.java @@ -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(), "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(""); @@ -307,7 +325,7 @@ public class XmlImporterTests extends ImporterTest { } try { - parseOneFile(SUT, inputStream); + parseOneFile(SUT, inputStream, getOptions(job, SUT)); } catch (Exception e) { Assert.fail(); }