wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ngalla...@apache.org
Subject svn commit: r1022974 [2/2] - in /incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j: compat/tests/ tests/
Date Fri, 15 Oct 2010 15:51:00 GMT
Modified: incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONObjectTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONObjectTest.java?rev=1022974&r1=1022973&r2=1022974&view=diff
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONObjectTest.java
(original)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONObjectTest.java
Fri Oct 15 15:50:59 2010
@@ -6,15 +6,15 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *   http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
- * under the License.    
+ * under the License.
  */
 
 package org.apache.wink.json4j.tests;
@@ -88,7 +88,7 @@ public class JSONObjectTest extends Test
         Exception ex = null;
         // read in a basic JSON file that has all the various types in it.
         try {
-            Reader rdr = new InputStreamReader(new FileInputStream("jsonfiles/utf8_basic.json"),
"UTF-8");
+            Reader rdr = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("utf8_basic.json"),
"UTF-8");
             jObject = new JSONObject(rdr);
             rdr.close();
         } catch (Exception ex1) {
@@ -109,7 +109,7 @@ public class JSONObjectTest extends Test
         // read in a basic JSON file that has all the various types in it.
         // Inputstreams are read as UTF-8 by the underlying parser.
         try {
-            InputStream is = new FileInputStream("jsonfiles/utf8_basic.json");
+            InputStream is = this.getClass().getClassLoader().getResourceAsStream("utf8_basic.json");
             jObject = new JSONObject(is);
             is.close();
         } catch (Exception ex1) {
@@ -527,7 +527,7 @@ public class JSONObjectTest extends Test
         }
         assertTrue(ex == null);
     }
-    
+
 
     public void test_EmptyAppend() throws Exception {
         Exception ex = null;
@@ -554,7 +554,7 @@ public class JSONObjectTest extends Test
         try {
             JSONObject jObject = new JSONObject("{\"long\":1}");
             assertTrue(jObject.getLong("long") == 1);
-            
+
             JSONObject json = new JSONObject("{ Date : 1212790800000 }");
     		assertEquals(1212790800000L, json.getLong("Date"));
         } catch (Exception ex1) {
@@ -973,7 +973,7 @@ public class JSONObjectTest extends Test
         }
         assertTrue(ex == null);
     }
-    
+
     /**
      * Test returning of Integer object if within Integer range.
      */
@@ -993,14 +993,14 @@ public class JSONObjectTest extends Test
             assertEquals( jObject4.opt("int").getClass(), Integer.class);
             assertEquals( jObject5.opt("int").getClass(), Integer.class);
             assertEquals( jObject6.opt("int").getClass(), Integer.class);
-                                          
+
         } catch (Exception ex1) {
             ex = ex1;
             ex.printStackTrace();
         }
         assertTrue(ex == null);
     }
-    
+
     /**
      * Test returning of Long object if value out of Integer range.
      */
@@ -1013,83 +1013,83 @@ public class JSONObjectTest extends Test
             JSONObject jObject = new JSONObject("{\"int\" : " + val1 + "}");
             JSONObject jObject2 = new JSONObject("{\"int\" : " + val2 + "}");
             JSONObject jObject3 = new JSONObject("{\"int\" : 0X" + Long.toHexString(val1.longValue())
+ "}");
-            JSONObject jObject4 = new JSONObject("{\"int\" : 020000000000}");        
+            JSONObject jObject4 = new JSONObject("{\"int\" : 020000000000}");
             assertEquals( jObject.opt("int").getClass(), Long.class);
             assertEquals( jObject2.opt("int").getClass(), Long.class);
             assertEquals( jObject3.opt("int").getClass(), Long.class);
             assertEquals( jObject4.opt("int").getClass(), Long.class);
-                                          
+
         } catch (Exception ex1) {
             ex = ex1;
             ex.printStackTrace();
         }
         assertTrue(ex == null);
     }
-    
+
     public void test_optWithHex() {
     	  Exception ex = null;
 
-          try {       
-            JSONObject jObject3 = new JSONObject("{\"int\" : 0X7f}");               
+          try {
+            JSONObject jObject3 = new JSONObject("{\"int\" : 0X7f}");
             assertEquals( jObject3.opt("int"), 127);
-            jObject3 = new JSONObject("{\"int\" : 0x7f}");               
+            jObject3 = new JSONObject("{\"int\" : 0x7f}");
             assertEquals( jObject3.opt("int"), 127);
-            jObject3 = new JSONObject("{\"int\" : -0x99e}");               
+            jObject3 = new JSONObject("{\"int\" : -0x99e}");
             assertEquals( jObject3.opt("int"), -2462);
-            jObject3 = new JSONObject("{\"int\" : -0X99e}");               
+            jObject3 = new JSONObject("{\"int\" : -0X99e}");
             assertEquals( jObject3.opt("int"), -2462);
-                                            
+
           } catch (Exception ex1) {
               ex = ex1;
               ex.printStackTrace();
           }
           assertTrue(ex == null);
-          
-          try {       
-              JSONObject jObject3 = new JSONObject("{\"int\" : 343g}");                 
                             
+
+          try {
+              JSONObject jObject3 = new JSONObject("{\"int\" : 343g}");
             } catch (Exception ex1) {
                 ex = ex1;
                 ex.printStackTrace();
             }
             assertTrue(ex instanceof JSONException);
-            try {       
-                JSONObject jObject3 = new JSONObject("{\"int\" : 343a}");  
+            try {
+                JSONObject jObject3 = new JSONObject("{\"int\" : 343a}");
             } catch (Exception ex1) {
                   ex = ex1;
                   ex.printStackTrace();
             }
             assertTrue(ex instanceof JSONException);
-          
+
     }
-    
+
     public void test_optNumberReturnsSameException() {
   	  Exception ex = null;
-        // Test to make sure same exception is thrown when hex char is included in 
+        // Test to make sure same exception is thrown when hex char is included in
   	    // normal identifier.
-        try {       
-          JSONObject jObject3 = new JSONObject("{\"int\" : 343h}");               
+        try {
+          JSONObject jObject3 = new JSONObject("{\"int\" : 343h}");
           assertEquals( jObject3.opt("int"), 127);
-         
-                                          
+
+
         } catch (Exception ex1) {
             ex = ex1;
         }
         assertTrue(ex instanceof JSONException);
         Throwable cause = ex.getCause();
         assertTrue(cause == null);
-        
-        try {       
-            JSONObject jObject3 = new JSONObject("{\"int\" : 343a}");               
-            assertEquals( jObject3.opt("int"), 127);                                
+
+        try {
+            JSONObject jObject3 = new JSONObject("{\"int\" : 343a}");
+            assertEquals( jObject3.opt("int"), 127);
           } catch (Exception ex1) {
               ex = ex1;
           }
           assertTrue(ex instanceof JSONException);
           cause = ex.getCause();
           assertTrue(cause == null);
-                
+
     }
-    
+
     public void test_ArrayRetrievalFromJavaArrayInsertion() throws Exception {
         JSONObject json = new JSONObject();
         String[] someArray = { "Hello","World!" };
@@ -1097,10 +1097,10 @@ public class JSONObjectTest extends Test
         JSONArray array = json.getJSONArray("somearray");
         assertEquals(array.get(0), "Hello");
         assertEquals(array.get(1), "World!");
-    } 
+    }
 
     /**************************************************************************/
-    /* The following tests all test failure scenarios due to type mismatching.*/  
+    /* The following tests all test failure scenarios due to type mismatching.*/
     /**************************************************************************/
 
     /**
@@ -1277,7 +1277,7 @@ public class JSONObjectTest extends Test
     }
 
     /**
-     * Test the iterator of the sorted keys.  
+     * Test the iterator of the sorted keys.
      */
     public void test_sortedKeys() {
         HashMap map = new HashMap();
@@ -1335,7 +1335,7 @@ public class JSONObjectTest extends Test
     public void test_utf8_korean() {
         Exception ex = null;
         try {
-            Reader reader = new InputStreamReader(new FileInputStream(new File("jsonfiles/utf8_helloworld_ko.json")),
"UTF-8");
+            Reader reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("utf8_helloworld_ko.json"),
"UTF-8");
             JSONObject jObject = new JSONObject(reader);
             reader.close();
             assertTrue(jObject.getString("greet").equals("\uc548\ub155 \uc138\uc0c1\uc544"));
@@ -1347,14 +1347,14 @@ public class JSONObjectTest extends Test
     }
 
     /**
-     * Verify a UTF 8 file with character codes in the lower part will parse and 
+     * Verify a UTF 8 file with character codes in the lower part will parse and
      * serialize correctly in escaped unicode format (which is valid JSON and easier
      * to debug)
      */
     public void test_utf8_lowerchar() {
         Exception ex = null;
         try {
-            Reader reader = new InputStreamReader(new FileInputStream(new File("jsonfiles/utf8_lowerchar.json")),
"UTF-8");
+            Reader reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("utf8_lowerchar.json"),
"UTF-8");
             JSONObject jObject = new JSONObject(reader);
             reader.close();
             assertTrue(jObject.getString("message").equals("\u00c5\u00c5\u00c5\u00c5"));
@@ -1393,16 +1393,16 @@ public class JSONObjectTest extends Test
             assertTrue(false);
         }
     }
-    
+
     public void test_CStyleCommentWithACommentCharInMiddle() throws Exception {
     	 try {
              JSONObject jObj = new JSONObject("/* * */ { 'test' : 'value' }");
              assertTrue(jObj.has("test"));
              assertTrue(jObj.getString("test").equals("value"));
-             
+
              JSONObject jObj2 = new JSONObject("/* / */ { 'test' : 'value' }");
              assertTrue(jObj2.has("test"));
-             assertTrue(jObj2.getString("test").equals("value")); 
+             assertTrue(jObj2.getString("test").equals("value"));
          } catch (Exception ex) {
              ex.printStackTrace();
              assertTrue(false);
@@ -1419,7 +1419,7 @@ public class JSONObjectTest extends Test
             assertTrue(false);
         }
     }
-    
+
     /**
      * Test special characters in unquoted string key.
      */
@@ -1428,25 +1428,25 @@ public class JSONObjectTest extends Test
             JSONObject jObj = new JSONObject("{test-key:'value'}");
             assertTrue(jObj.has("test-key"));
             assertTrue(jObj.getString("test-key").equals("value"));
-            
+
             jObj = new JSONObject("{test0:'value'}");
             assertTrue(jObj.has("test0"));
             assertTrue(jObj.getString("test0").equals("value"));
-            
+
             jObj = new JSONObject("{test$:'value'}");
             assertTrue(jObj.has("test$"));
             assertTrue(jObj.getString("test$").equals("value"));
-            
+
             jObj = new JSONObject("{test_key:'value'}");
             assertTrue(jObj.has("test_key"));
             assertTrue(jObj.getString("test_key").equals("value"));
-            
+
         } catch (Exception ex) {
             ex.printStackTrace();
             assertTrue(false);
         }
     }
-    
+
     /**
      * Test special characters in unquoted string value.
      */
@@ -1455,15 +1455,15 @@ public class JSONObjectTest extends Test
             JSONObject jObj = new JSONObject("{test:@value}");
             assertTrue(jObj.has("test"));
             assertTrue(jObj.getString("test").equals("@value"));
-            
+
             jObj = new JSONObject("{test:$100}");
             assertTrue(jObj.has("test"));
             assertTrue(jObj.getString("test").equals("$100"));
-            
+
             jObj = new JSONObject("{test:$value}");
             assertTrue(jObj.has("test"));
             assertTrue(jObj.getString("test").equals("$value"));
-            
+
         } catch (Exception ex) {
             ex.printStackTrace();
             assertTrue(false);
@@ -1485,7 +1485,7 @@ public class JSONObjectTest extends Test
         try {
             JSONObject jObj = new JSONObject("// test comment\n{'test':'value'}", true);
         } catch (Exception ex) {
-            ex1 = ex;        
+            ex1 = ex;
         }
 
         assertTrue(ex1 != null);
@@ -1525,5 +1525,5 @@ public class JSONObjectTest extends Test
         }
         assertTrue(ex == null);
     }
-    
+
 }

Modified: incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONParserTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONParserTests.java?rev=1022974&r1=1022973&r2=1022974&view=diff
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONParserTests.java
(original)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONParserTests.java
Fri Oct 15 15:50:59 2010
@@ -6,15 +6,15 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *   http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
- * under the License.    
+ * under the License.
  */
 
 package org.apache.wink.json4j.tests;
@@ -22,17 +22,16 @@ package org.apache.wink.json4j.tests;
 /**
  * Basic junit imports.
  */
+import java.io.InputStream;
+import java.io.StringWriter;
+
 import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import java.io.*;
-import java.util.Iterator;
-import java.util.Set;
 
-/**
- * Import JSON code we're testing
- */
-import org.apache.wink.json4j.*;
-import org.apache.wink.json4j.utils.*;
+import org.apache.wink.json4j.JSON;
+import org.apache.wink.json4j.JSONArray;
+import org.apache.wink.json4j.JSONArtifact;
+import org.apache.wink.json4j.JSONObject;
+import org.apache.wink.json4j.utils.XML;
 
 /**
  * Tests for the basic Java JSON model parser
@@ -40,15 +39,15 @@ import org.apache.wink.json4j.utils.*;
 public class JSONParserTests extends TestCase {
 
     /**
-     * Test a basic transform of an XML file to a JSON string, reparse with generic parser
to validate generic parser, 
+     * Test a basic transform of an XML file to a JSON string, reparse with generic parser
to validate generic parser,
      * then with compact emit for checking.
      */
     public void testJSONGenericObjectParse() {
         Exception ex = null;
-        File file    = new File("xmlfiles/simple.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("simple.xml");
 
         try {
-            String json = XML.toJson(file);
+            String json = XML.toJson(is);
             JSONArtifact jsonA = JSON.parse(json);
 
             assertTrue(jsonA instanceof JSONObject);

Modified: incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/OrderedJSONObjectTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/OrderedJSONObjectTest.java?rev=1022974&r1=1022973&r2=1022974&view=diff
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/OrderedJSONObjectTest.java
(original)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/OrderedJSONObjectTest.java
Fri Oct 15 15:50:59 2010
@@ -6,29 +6,25 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *   http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
- * under the License.    
+ * under the License.
  */
 
 package org.apache.wink.json4j.tests;
 
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import java.io.*;
-import java.util.Iterator;
-import java.util.Set;
-import java.util.HashMap;
+import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Iterator;
 
-import org.apache.wink.json4j.*;
-import org.apache.wink.json4j.utils.*;
+import org.apache.wink.json4j.JSONObject;
+import org.apache.wink.json4j.OrderedJSONObject;
 
 /**
  * Tests for the basic Java OrderedJSONObject model
@@ -181,11 +177,11 @@ public class OrderedJSONObjectTest exten
      */
     public void test_OrderedJsonRead() {
         try {
-            FileInputStream fis = new FileInputStream(new File("jsonfiles/utf8_ordered.json"));
+            InputStream is = this.getClass().getClassLoader().getResourceAsStream("utf8_ordered.json");
             ArrayList orderList = new ArrayList();
 
-            OrderedJSONObject obj = new OrderedJSONObject(fis);
-            fis.close();
+            OrderedJSONObject obj = new OrderedJSONObject(is);
+            is.close();
 
             String jsonStr = obj.write(true);
             Iterator order = obj.getOrder();

Modified: incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/XMLTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/XMLTests.java?rev=1022974&r1=1022973&r2=1022974&view=diff
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/XMLTests.java
(original)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/XMLTests.java
Fri Oct 15 15:50:59 2010
@@ -6,15 +6,15 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *   http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
- * under the License.    
+ * under the License.
  */
 
 package org.apache.wink.json4j.tests;
@@ -22,16 +22,16 @@ package org.apache.wink.json4j.tests;
 /**
  * Basic junit imports.
  */
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+
 import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import java.io.*;
 
-/**
- * Import JSON code we're testing
- */
 import org.apache.wink.json4j.JSONArray;
 import org.apache.wink.json4j.JSONObject;
-import org.apache.wink.json4j.utils.*;
+import org.apache.wink.json4j.utils.XML;
 
 /**
  * Tests for all the basic XML Transform functions.
@@ -43,10 +43,11 @@ public class XMLTests extends TestCase {
      */
     public void testSimpleXMLDocument_AsFileToStringCompact() {
         Exception ex = null;
-        File file    = new File("xmlfiles/simple.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("simple.xml");
 
         try {
-            String JSON = XML.toJson(file);
+            String JSON = XML.toJson(is);
+            is.close();
             System.out.println("JSON compacted text:\n");
             System.out.println(JSON);
         } catch (Exception ex1) {
@@ -60,10 +61,11 @@ public class XMLTests extends TestCase {
      */
     public void testSimpleXMLDocument_AsFileToStringVerbose() {
         Exception ex = null;
-        File file    = new File("xmlfiles/simple.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("simple.xml");
 
         try {
-            String JSON = XML.toJson(file, true);
+            String JSON = XML.toJson(is, true);
+            is.close();
             System.out.println("JSON non-compact text:\n");
             System.out.println(JSON);
         } catch (Exception ex1) {
@@ -78,10 +80,11 @@ public class XMLTests extends TestCase {
      */
     public void testComplexXMLDocument_AsFileToStringCompact() {
         Exception ex = null;
-        File file    = new File("xmlfiles/complex.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("simple.xml");
 
         try {
-            String JSON = XML.toJson(file);
+            String JSON = XML.toJson(is);
+            is.close();
             System.out.println("JSON compacted text:\n");
             System.out.println(JSON);
         } catch (Exception ex1) {
@@ -95,10 +98,11 @@ public class XMLTests extends TestCase {
      */
     public void testComplexXMLDocument_AsFileToStringVerbose() {
         Exception ex = null;
-        File file    = new File("xmlfiles/complex.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("complex.xml");
 
         try {
-            String JSON = XML.toJson(file, true);
+            String JSON = XML.toJson(is, true);
+            is.close();
             System.out.println("JSON non-compact text:\n");
             System.out.println(JSON);
         } catch (Exception ex1) {
@@ -112,10 +116,11 @@ public class XMLTests extends TestCase {
      */
     public void testComplexXMLDocumentWithLongText_AsFileToStringVerbose() {
         Exception ex = null;
-        File file    = new File("xmlfiles/long-text.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("long-text.xml");
 
         try {
-            String JSON = XML.toJson(file, true);
+            String JSON = XML.toJson(is, true);
+            is.close();
             System.out.println("JSON non-compact text with LONG string:\n");
             System.out.println(JSON);
         } catch (Exception ex1) {
@@ -130,12 +135,11 @@ public class XMLTests extends TestCase {
      */
     public void testSimpleXMLDocument_AsStreamToStringCompact() {
         Exception ex = null;
-        File file    = new File("xmlfiles/simple.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("simple.xml");
 
         try {
-            FileInputStream fis = new FileInputStream(file);
-            String JSON = XML.toJson(fis);
-            fis.close();
+            String JSON = XML.toJson(is);
+            is.close();
             System.out.println("JSON compacted text:\n");
             System.out.println(JSON);
         } catch (Exception ex1) {
@@ -151,12 +155,11 @@ public class XMLTests extends TestCase {
      */
     public void testSimpleXMLDocument_AsStreamToStringVerbose() {
         Exception ex = null;
-        File file    = new File("xmlfiles/simple.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("simple.xml");
 
         try {
-            FileInputStream fis = new FileInputStream(file);
-            String JSON = XML.toJson(fis, true);
-            fis.close();
+            String JSON = XML.toJson(is, true);
+            is.close();
             System.out.println("JSON non-compact text:\n");
             System.out.println(JSON);
         } catch (Exception ex1) {
@@ -171,12 +174,11 @@ public class XMLTests extends TestCase {
      */
     public void testComplexXMLDocument_AsStreamToStringCompact() {
         Exception ex = null;
-        File file    = new File("xmlfiles/complex.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("complex.xml");
 
         try {
-            FileInputStream fis = new FileInputStream(file);
-            String JSON = XML.toJson(fis);
-            fis.close();
+            String JSON = XML.toJson(is);
+            is.close();
             System.out.println("JSON compacted text:\n");
             System.out.println(JSON);
         } catch (Exception ex1) {
@@ -190,12 +192,11 @@ public class XMLTests extends TestCase {
      */
     public void testComplexXMLDocument_AsStreamToStringVerbose() {
         Exception ex = null;
-        File file    = new File("xmlfiles/complex.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("complex.xml");
 
         try {
-            FileInputStream fis = new FileInputStream(file);
-            String JSON = XML.toJson(fis, true);
-            fis.close();
+            String JSON = XML.toJson(is, true);
+            is.close();
             System.out.println("JSON non-compact text:\n");
             System.out.println(JSON);
         } catch (Exception ex1) {
@@ -209,17 +210,16 @@ public class XMLTests extends TestCase {
      */
     public void testSimpleXMLDocument_AsStreamToStreamCompact() {
         Exception ex = null;
-        File file    = new File("xmlfiles/simple.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("simple.xml");
         File fileOut = new File("json_output/simple_json_compact.json");
 
         try {
             File parent = fileOut.getParentFile();
             parent.mkdirs();
 
-            FileInputStream fis  = new FileInputStream(file);
             FileOutputStream fos = new FileOutputStream(fileOut);
-            XML.toJson(fis, fos);
-            fis.close();
+            XML.toJson(is, fos);
+            is.close();
             fos.flush();
             fos.close();
         } catch (Exception ex1) {
@@ -233,17 +233,16 @@ public class XMLTests extends TestCase {
      */
     public void testSimpleXMLDocument_AsStreamToStreamVerbose() {
         Exception ex = null;
-        File file    = new File("xmlfiles/simple.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("simple.xml");
         File fileOut = new File("json_output/simple_json_verbose.json");
 
         try {
             File parent = fileOut.getParentFile();
             parent.mkdirs();
 
-            FileInputStream fis  = new FileInputStream(file);
             FileOutputStream fos = new FileOutputStream(fileOut);
-            XML.toJson(fis, fos, true);
-            fis.close();
+            XML.toJson(is, fos, true);
+            is.close();
             fos.flush();
             fos.close();
         } catch (Exception ex1) {
@@ -257,17 +256,16 @@ public class XMLTests extends TestCase {
      */
     public void testComplexXMLDocument_AsStreamToStreamCompact() {
         Exception ex = null;
-        File file    = new File("xmlfiles/complex.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("complex.xml");
         File fileOut = new File("json_output/complex_json_compact.json");
 
         try {
             File parent = fileOut.getParentFile();
             parent.mkdirs();
 
-            FileInputStream fis = new FileInputStream(file);
             FileOutputStream fos = new FileOutputStream(fileOut);
-            XML.toJson(fis, fos);
-            fis.close();
+            XML.toJson(is, fos);
+            is.close();
             fos.flush();
             fos.close();
         } catch (Exception ex1) {
@@ -281,17 +279,16 @@ public class XMLTests extends TestCase {
      */
     public void testComplexXMLDocument_AsStreamToStreamVerbose() {
         Exception ex = null;
-        File file    = new File("xmlfiles/complex.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("complex.xml");
         File fileOut = new File("json_output/complex_json_verbose.json");
 
         try {
             File parent = fileOut.getParentFile();
             parent.mkdirs();
 
-            FileInputStream fis = new FileInputStream(file);
             FileOutputStream fos = new FileOutputStream(fileOut);
-            XML.toJson(fis, fos, true);
-            fis.close();
+            XML.toJson(is, fos, true);
+            is.close();
             fos.flush();
             fos.close();
         } catch (Exception ex1) {
@@ -305,10 +302,10 @@ public class XMLTests extends TestCase {
      */
     public void testSimpleXMLDocument_AsFileToStringCompactFailure() {
         Exception ex = null;
-        File file    = new File("xmlfiles/simple_broken.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("simple_broken.xml");
 
         try {
-            String JSON = XML.toJson(file, true);
+            String JSON = XML.toJson(is, true);
             System.out.println("JSON compacted text:\n");
             System.out.println(JSON);
         } catch (Exception ex1) {
@@ -322,7 +319,7 @@ public class XMLTests extends TestCase {
      */
     public void testAtomFeedConversion1() {
         Exception ex = null;
-        File file    = new File("xmlfiles/atom-xml-entry1");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("atom-xml-entry1");
         File oFile   = new File("json_output/atomentry-xml-json1.json");
 
         try {
@@ -330,10 +327,9 @@ public class XMLTests extends TestCase {
             if (parent != null && !parent.exists()) {
                 parent.mkdirs();
             }
-            FileInputStream fis  = new FileInputStream(file);
             FileOutputStream fos = new FileOutputStream(oFile);
-            XML.toJson(fis, fos, true);
-            fis.close();
+            XML.toJson(is, fos, true);
+            is.close();
             fos.flush();
             fos.close();
         } catch (Exception ex1) {
@@ -348,7 +344,7 @@ public class XMLTests extends TestCase {
      */
     public void testAtomFeedConversion2() {
         Exception ex = null;
-        File file    = new File("xmlfiles/atom-xml-feed1");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("atom-xml-feed1");
         File oFile   = new File("json_output/atomfeed-xml-json2.json");
 
         try {
@@ -356,10 +352,9 @@ public class XMLTests extends TestCase {
             if (parent != null && !parent.exists()) {
                 parent.mkdirs();
             }
-            FileInputStream fis  = new FileInputStream(file);
             FileOutputStream fos = new FileOutputStream(oFile);
-            XML.toJson(fis, fos, true);
-            fis.close();
+            XML.toJson(is, fos, true);
+            is.close();
             fos.flush();
             fos.close();
         } catch (Exception ex1) {
@@ -374,23 +369,22 @@ public class XMLTests extends TestCase {
      */
     public void testLowCharacterUTF8String() {
         Exception ex = null;
-        File file    = new File("xmlfiles/utf8-lowerchar.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("utf8-lowerchar.xml");
         File fileOut = new File("json_output/utf8-lowerchar.json");
 
         try {
             File parent = fileOut.getParentFile();
             parent.mkdirs();
 
-            FileInputStream fis = new FileInputStream(file);
             FileOutputStream fos = new FileOutputStream(fileOut);
-            XML.toJson(fis, fos, false);
-            fis.close();
+            XML.toJson(is, fos, false);
+            is.close();
             fos.flush();
             fos.close();
 
-            fis = new FileInputStream("json_output/utf8-lowerchar.json");
-            JSONObject jObject = new JSONObject(fis);
-            fis.close();
+            is = fileOut.toURL().openStream();// this.getClass().getClassLoader().getResourceAsStream("json_output/utf8-lowerchar.json");
+            JSONObject jObject = new JSONObject(is);
+            is.close();
             String str = (String)jObject.get("hi");
             String expected="\u00C5\u00C5\u00C5\u00C5";
             //Compare this to the string with unicode \u00C5 in it.
@@ -408,23 +402,22 @@ public class XMLTests extends TestCase {
      */
     public void testArrayUTF8String() {
         Exception ex = null;
-        File file    = new File("xmlfiles/utf8-array.xml");
+        InputStream is = this.getClass().getClassLoader().getResourceAsStream("utf8-array.xml");
         File fileOut = new File("json_output/utf8-array.json");
 
         try {
             File parent = fileOut.getParentFile();
             parent.mkdirs();
 
-            FileInputStream fis = new FileInputStream(file);
             FileOutputStream fos = new FileOutputStream(fileOut);
-            XML.toJson(fis, fos, false);
-            fis.close();
+            XML.toJson(is, fos, false);
+            is.close();
             fos.flush();
             fos.close();
 
-            fis = new FileInputStream("json_output/utf8-array.json");
-            JSONObject jObject = new JSONObject(fis);
-            fis.close();
+            is = fileOut.toURL().openStream(); //this.getClass().getClassLoader().getResourceAsStream("utf8-array.json");
+            JSONObject jObject = new JSONObject(is);
+            is.close();
             String expected="\u592a\u548c\u6bbf";
             JSONObject search = (JSONObject)jObject.get("search");
             JSONObject payload = (JSONObject)search.get("payLoad");



Mime
View raw message