From c7d2d9161321e9aa09bbbf566bce2922d169b9dd Mon Sep 17 00:00:00 2001 From: Douglas Crockford Date: Mon, 16 Jul 2012 09:50:19 -0700 Subject: [PATCH] Remove tests --- tests/SampleResourceBundle_en_US.java | 29 - tests/SampleResourceBundle_fr.java | 28 - tests/TestCDL.java | 351 ---- tests/TestCookie.java | 138 -- tests/TestCookieList.java | 68 - tests/TestHTTP.java | 132 -- tests/TestHTTPTokener.java | 60 - tests/TestJSONArray.java | 1440 -------------- tests/TestJSONException.java | 35 - tests/TestJSONML.java | 782 -------- tests/TestJSONObject.java | 2507 ------------------------- tests/TestJSONStringer.java | 275 --- tests/TestJSONTokener.java | 678 ------- tests/TestJSONWriter.java | 530 ------ tests/TestSuite.java | 21 - tests/TestXML.java | 1320 ------------- tests/TestXMLTokener.java | 473 ----- 17 files changed, 8867 deletions(-) delete mode 100644 tests/SampleResourceBundle_en_US.java delete mode 100644 tests/SampleResourceBundle_fr.java delete mode 100644 tests/TestCDL.java delete mode 100644 tests/TestCookie.java delete mode 100644 tests/TestCookieList.java delete mode 100644 tests/TestHTTP.java delete mode 100644 tests/TestHTTPTokener.java delete mode 100644 tests/TestJSONArray.java delete mode 100644 tests/TestJSONException.java delete mode 100644 tests/TestJSONML.java delete mode 100644 tests/TestJSONObject.java delete mode 100644 tests/TestJSONStringer.java delete mode 100644 tests/TestJSONTokener.java delete mode 100644 tests/TestJSONWriter.java delete mode 100644 tests/TestSuite.java delete mode 100644 tests/TestXML.java delete mode 100644 tests/TestXMLTokener.java diff --git a/tests/SampleResourceBundle_en_US.java b/tests/SampleResourceBundle_en_US.java deleted file mode 100644 index fb25faf..0000000 --- a/tests/SampleResourceBundle_en_US.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * File: SampleResourceBundle_en_US.java - * Author: JSON.org - */ -package org.json.tests; - -import java.util.*; - -/** - * The Class SampleResourceBundle_en_US. - */ -public class SampleResourceBundle_en_US extends ListResourceBundle { - - /* (non-Javadoc) - * @see java.util.ListResourceBundle#getContents() - */ - @Override - public Object[][] getContents() { - return contents; - } - - /** The contents. */ - private Object[][] contents = { - { "ASCII", "American Standard Code for Information Interchange" }, - { "JAVA.desc", "Just Another Vague Acronym" }, - { "JAVA.data", "Sweet language" }, - { "JSON", "JavaScript Object Notation" }, - }; -} \ No newline at end of file diff --git a/tests/SampleResourceBundle_fr.java b/tests/SampleResourceBundle_fr.java deleted file mode 100644 index b5a4dd6..0000000 --- a/tests/SampleResourceBundle_fr.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * File: SampleResourceBundle_fr.java - * Author: JSON.org - */ -package org.json.tests; - -import java.util.*; - -/** - * The Class SampleResourceBundle_fr. - */ -public class SampleResourceBundle_fr extends ListResourceBundle { - - /* (non-Javadoc) - * @see java.util.ListResourceBundle#getContents() - */ - @Override - public Object[][] getContents() { - return contents; - } - - /** The contents. */ - private Object[][] contents = { - { "ASCII", "Number that represent chraracters" }, - { "JAVA", "The language you are running to see this" }, - { "JSON", "What are we testing?" }, - }; -} \ No newline at end of file diff --git a/tests/TestCDL.java b/tests/TestCDL.java deleted file mode 100644 index f3aa468..0000000 --- a/tests/TestCDL.java +++ /dev/null @@ -1,351 +0,0 @@ -/* - * File: TestCDL.java Author: JSON.org - */ -package org.json.tests; - -import org.json.CDL; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Before; - -import junit.framework.TestCase; - -/** - * The Class TestCDL. - */ -public class TestCDL extends TestCase -{ - - /** The string. */ - private String string; - - /** The jsonarray. */ - private JSONArray jsonarray; - - /* - * (non-Javadoc) - * - * @see junit.framework.TestCase#setUp() - */ - @Before - @Override - public void setUp() - { - //@formatter:off - string = - "abc,test,123\n" + - "gg,hh,jj\n" + - "aa,bb,cc\n"; - //@formatter:on - - try - { - jsonarray = new JSONArray(); - JSONObject jo = new JSONObject(); - JSONObject jo2 = new JSONObject(); - jo.put("abc", "gg"); - jo.put("test", "hh"); - jo.put("123", "jj"); - jo2.put("abc", "aa"); - jo2.put("test", "bb"); - jo2.put("123", "cc"); - jsonarray.put(jo); - jsonarray.put(jo2); - } catch (JSONException e) - { - e.printStackTrace(); - } - } - - /** - * Tests the toJsonArray method. - */ - public void testToJsonArray() - { - - try - { - assertEquals(jsonarray.toString(), CDL.toJSONArray(string) - .toString()); - } catch (JSONException e) - { - e.printStackTrace(); - } - } - - /** - * Tests the toJsonArray method using No names. - */ - public static void testToJsonArray_NoNames() - { - - try - { - assertEquals(null, CDL.toJSONArray(new JSONArray(), "abc,123")); - } catch (JSONException e) - { - e.printStackTrace(); - } - } - - /** - * Tests the toJsonArray method using Null names. - */ - public static void testToJsonArray_NullNames() - { - - try - { - assertEquals(null, CDL.toJSONArray(null, "abc,123")); - } catch (JSONException e) - { - e.printStackTrace(); - } - } - - /** - * Tests the toJsonArray method using No data. - */ - public static void testToJsonArray_NoData() - { - - try - { - assertEquals(null, CDL.toJSONArray("abc,123\n")); - } catch (JSONException e) - { - e.printStackTrace(); - } - } - - /** - * Tests the toJsonArray method using weird data. - */ - public void testToJsonArray_WeirdData() - { - //@formatter:off - string = - "abc,test,123\r" + - "gg,hh,\"jj\"\r" + - "aa,\tbb,cc"; - //@formatter:on - try - { - assertEquals(jsonarray.toString(), CDL.toJSONArray(string) - .toString()); - } catch (JSONException e) - { - e.printStackTrace(); - } - } - - /** - * Tests the toJsonArray method using no closing quote. - */ - public void testToJsonArray_NoClosingQuote() - { - //@formatter:off - string = - "abc,test,123\r" + - "gg,hh,jj \r" + - "aa,\"bb ,cc "; - //@formatter:on - try - { - assertEquals(jsonarray.toString(), CDL.toJSONArray(string) - .toString()); - fail("Should have thrown Exception"); - } catch (JSONException e) - { - assertEquals("Missing close quote '\"'. at 35 [character 12 line 5]", e.getMessage()); - } - //@formatter:off - string = - "abc,test,123\r" + - "gg,hh,jj \r" + - "aa,\"bb ,cc \n"; - //@formatter:on - try - { - assertEquals(jsonarray.toString(), CDL.toJSONArray(string) - .toString()); - fail("Should have thrown Exception"); - } catch (JSONException e) - { - assertEquals("Missing close quote '\"'. at 35 [character 0 line 6]", e.getMessage()); - } - //@formatter:off - string = - "abc,test,123\r" + - "gg,hh,jj \r" + - "aa,\"bb ,cc \r"; - //@formatter:on - try - { - assertEquals(jsonarray.toString(), CDL.toJSONArray(string) - .toString()); - fail("Should have thrown Exception"); - } catch (JSONException e) - { - assertEquals( - "Missing close quote '\"'. at 35 [character 12 line 5]", - e.getMessage()); - } - } - - /** - * Tests the toJsonArray method using space after string. - */ - public void testToJsonArray_SpaceAfterString() - { - //@formatter:off - string = - "abc,test,123\r" + - "gg,hh,jj \r" + - "aa,\"bb\" ,cc\r"; - //@formatter:on - try - { - assertEquals(jsonarray.toString(), CDL.toJSONArray(string) - .toString()); - } catch (JSONException e) - { - e.printStackTrace(); - } - } - - /** - * Tests the toJsonArray method using bad character. - */ - public void testToJsonArray_BadCharacter() - { - //@formatter:off - string = - "abc,test,123\r" + - "gg,hh,jj \r" + - "aa,\"bb \"?,cc \r"; - //@formatter:on - try - { - assertEquals(jsonarray.toString(), CDL.toJSONArray(string) - .toString()); - fail("Should have thrown Exception"); - } catch (JSONException e) - { - assertEquals("Bad character '?' (63). at 32 [character 9 line 5]", e.getMessage()); - } - } - - /** - * Tests the toString method. - */ - public void testToString() - { - try - { - assertEquals(string, CDL.toString(jsonarray)); - } catch (JSONException e) - { - e.printStackTrace(); - } - - } - - /** - * Tests the toString method using Bad json array. - */ - public void testToString_BadJsonArray() - { - try - { - jsonarray = new JSONArray(); - assertEquals(null, CDL.toString(jsonarray)); - jsonarray.put("abc"); - assertEquals("", CDL.toString(jsonarray, jsonarray)); - } catch (JSONException e) - { - e.printStackTrace(); - } - - } - - /** - * Tests the toString method using No names. - */ - public void testToString_NoNames() - { - try - { - jsonarray = new JSONArray(); - JSONObject jo = new JSONObject(); - jsonarray.put(jo); - assertEquals(null, CDL.toString(jsonarray)); - - assertEquals(null, CDL.toString(new JSONArray(), jsonarray)); - - JSONArray names = new JSONArray(); - names.put(""); - assertEquals("\n", CDL.toString(names, jsonarray)); - } catch (JSONException e) - { - e.printStackTrace(); - } - } - - /** - * Tests the toString method using Null names. - */ - public void testToString_NullNames() - { - try - { - jsonarray = new JSONArray(); - JSONObject jo = new JSONObject(); - jsonarray.put(jo); - assertEquals(null, CDL.toString(null, jsonarray)); - } catch (JSONException e) - { - e.printStackTrace(); - } - } - - /** - * Tests the toString method using Quotes. - */ - public void testToString_Quotes() - { - - try - { - jsonarray = CDL - .toJSONArray("Comma delimited list test, '\"Strip\"Quotes', 'quote, comma', No quotes, 'Single Quotes', \"Double Quotes\"\n1,'2',\"3\"\n,'It is \"good,\"', \"It works.\"\n\n"); - - string = CDL.toString(jsonarray); - assertEquals( - "\"quote, comma\",\"StripQuotes\",Comma delimited list test\n" - + "3,2,1\n" + "It works.,\"It is good,\",\n", - string); - assertEquals( - "[\n {\n \"quote, comma\": \"3\",\n \"\\\"Strip\\\"Quotes\": \"2\",\n \"Comma delimited list test\": \"1\"\n },\n {\n \"quote, comma\": \"It works.\",\n \"\\\"Strip\\\"Quotes\": \"It is \\\"good,\\\"\",\n \"Comma delimited list test\": \"\"\n }\n]", - jsonarray.toString(1)); - jsonarray = CDL.toJSONArray(string); - assertEquals( - "[\n {\n \"quote, comma\": \"3\",\n \"StripQuotes\": \"2\",\n \"Comma delimited list test\": \"1\"\n },\n {\n \"quote, comma\": \"It works.\",\n \"StripQuotes\": \"It is good,\",\n \"Comma delimited list test\": \"\"\n }\n]", - jsonarray.toString(1)); - } catch (JSONException e) - { - e.printStackTrace(); - } - } - - /** - * Tests the constructor method. - */ - public static void testConstructor() - { - CDL cdl = new CDL(); - assertEquals("CDL", cdl.getClass().getSimpleName()); - } - -} diff --git a/tests/TestCookie.java b/tests/TestCookie.java deleted file mode 100644 index 5ac32f3..0000000 --- a/tests/TestCookie.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * File: TestCookie.java - * Author: JSON.org - */ -package org.json.tests; - -import org.json.Cookie; -import org.json.JSONException; -import org.json.JSONObject; - -import junit.framework.TestCase; - -/** - * The Class TestCookie. - */ -public class TestCookie extends TestCase -{ - - JSONObject jsonobject; - - /** - * Tests the toJsonObject method using random cookie data. - */ - public static void testToJsonObject_RandomCookieData() - { - try - { - JSONObject jsonobject = new JSONObject(); - jsonobject = Cookie - .toJSONObject("f%oo=blah; secure ;expires = April 24, 2002"); - assertEquals("{\n" + " \"expires\": \"April 24, 2002\",\n" - + " \"name\": \"f%oo\",\n" + " \"secure\": true,\n" - + " \"value\": \"blah\"\n" + "}", jsonobject.toString(2)); - assertEquals("f%25oo=blah;expires=April 24, 2002;secure", - Cookie.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the escape and unescape method. - */ - public static void testEscape() - { - StringBuilder testString = new StringBuilder(); - testString.append('h'); - for(int i = 0; i < ' '; i++) - testString.append((char)i); - testString.append('\n'); - testString.append('\t'); - testString.append('\b'); - testString.append('%'); - testString.append('+'); - testString.append('='); - testString.append(';'); - String result = "h%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f%0a%09%08%25%2b%3d%3b"; - assertEquals(result, Cookie.escape(testString.toString())); - } - - /** - * Tests the unescape method. - */ - public static void testUnescape() - { - StringBuilder testString = new StringBuilder(); - testString.append('h'); - for(int i = 0; i < ' '; i++) - testString.append((char)i); - testString.append('\n'); - testString.append('\t'); - testString.append('\b'); - testString.append('%'); - testString.append('+'); - testString.append('%'); - testString.append('0'); - testString.append('\r'); - testString.append(' '); - testString.append(' '); - testString.append('%'); - testString.append('\n'); - testString.append('z'); - testString.append('z'); - testString.append('='); - testString.append(';'); - testString.append('%'); - String result = "h%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f%0a%09%08%25%2b%0\r +%\nzz%3d%3b%"; - assertEquals(testString.toString(), Cookie.unescape(result)); - } - - /** - * Tests the toJsonObject method using value without equals. - */ - public void testToJsonObject_ValueWithoutEquals() - { - try - { - jsonobject = Cookie - .toJSONObject("f%oo=blah; notsecure ;expires = April 24, 2002"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Missing '=' in cookie parameter. at 22 [character 23 line 1]", e.getMessage()); - } - } - - /** - * Tests the toString method. - */ - public static void testToString() - { - try - { - JSONObject jsonobject = new JSONObject(); - jsonobject.put("secure", true); - jsonobject.put("expires", "string1"); - jsonobject.put("domain", "string2"); - jsonobject.put("path", "string3"); - jsonobject.put("name", "foo"); - jsonobject.put("value", "bar"); - assertEquals("foo=bar;expires=string1;domain=string2;path=string3;secure", Cookie.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the constructor method. - */ - public static void testConstructor() - { - Cookie cookie = new Cookie(); - assertEquals("Cookie", cookie.getClass().getSimpleName()); - } - -} \ No newline at end of file diff --git a/tests/TestCookieList.java b/tests/TestCookieList.java deleted file mode 100644 index f54151c..0000000 --- a/tests/TestCookieList.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * File: TestCookieList.java Author: JSON.org - */ -package org.json.tests; - -import org.json.CookieList; -import org.json.JSONException; -import org.json.JSONObject; - -import junit.framework.TestCase; - -/** - * The Class TestCookieList. - */ -public class TestCookieList extends TestCase -{ - - /** The jsonobject. */ - JSONObject jsonobject = new JSONObject(); - - - /** - * Tests the toJsonObject method using random cookie list. - */ - public void testToJsonObject_RandomCookieList() - { - try - { - jsonobject = CookieList - .toJSONObject(" f%oo = b+l=ah ; o;n%40e = t.wo "); - assertEquals("{\n \"o;n@e\": \"t.wo\",\n \"f%oo\": \"b l=ah\"\n}", - jsonobject.toString(2)); - assertEquals("o%3bn@e=t.wo;f%25oo=b l%3dah", - CookieList.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonObject method using null key. - */ - public void testToJsonObject_NullKey() - { - try - { - jsonobject = CookieList - .toJSONObject(" f%oo = b+l=ah ; o;n%40e = t.wo "); - jsonobject.put("abc", JSONObject.NULL); - assertEquals("o%3bn@e=t.wo;f%25oo=b l%3dah", - CookieList.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the constructor method. - */ - public static void testConstructor() - { - CookieList cookielist = new CookieList(); - assertEquals("CookieList", cookielist.getClass().getSimpleName()); - } - -} \ No newline at end of file diff --git a/tests/TestHTTP.java b/tests/TestHTTP.java deleted file mode 100644 index ffa5608..0000000 --- a/tests/TestHTTP.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * File: TestHTTP.java Author: JSON.org - */ -package org.json.tests; - -import org.json.HTTP; -import org.json.JSONObject; - -import junit.framework.TestCase; - -/** - * The Class TestHTTP. - */ -public class TestHTTP extends TestCase -{ - - /** The jsonobject. */ - JSONObject jsonobject = new JSONObject(); - - /** - * Tests the stub method. - */ - public void testToJsonObject_Request() - { - try - { - jsonobject = HTTP - .toJSONObject("GET / HTTP/1.0\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\nAccept-Language: en-us\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; Q312461)\nHost: www.nokko.com\nConnection: keep-alive\nAccept-encoding: gzip, deflate\n"); - assertEquals( - "{\n \"Accept-Language\": \"en-us\",\n \"Request-URI\": \"/\",\n \"Host\": \"www.nokko.com\",\n \"Method\": \"GET\",\n \"Accept-encoding\": \"gzip, deflate\",\n \"User-Agent\": \"Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; Q312461)\",\n \"HTTP-Version\": \"HTTP/1.0\",\n \"Connection\": \"keep-alive\",\n \"Accept\": \"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\"\n}", - jsonobject.toString(2)); - assertEquals( - "GET \"/\" HTTP/1.0\r\n" - + "Accept-Language: en-us\r\n" - + "Host: www.nokko.com\r\n" - + "Accept-encoding: gzip, deflate\r\n" - + "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; Q312461)\r\n" - + "Connection: keep-alive\r\n" - + "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\r\n\r\n", - HTTP.toString(jsonobject)); - - } catch (Exception e) - { - fail(e.toString()); - } - } - - /** - * Tests the toJsonObject method using response. - */ - public void testToJsonObject_Response() - { - try - { - jsonobject = HTTP - .toJSONObject("HTTP/1.1 200 Oki Doki\nDate: Sun, 26 May 2002 17:38:52 GMT\nServer: Apache/1.3.23 (Unix) mod_perl/1.26\nKeep-Alive: timeout=15, max=100\nConnection: Keep-Alive\nTransfer-Encoding: chunked\nContent-Type: text/html\n"); - assertEquals( - "{\n \"Reason-Phrase\": \"Oki Doki\",\n \"Status-Code\": \"200\",\n \"Transfer-Encoding\": \"chunked\",\n \"Date\": \"Sun, 26 May 2002 17:38:52 GMT\",\n \"Keep-Alive\": \"timeout=15, max=100\",\n \"HTTP-Version\": \"HTTP/1.1\",\n \"Content-Type\": \"text/html\",\n \"Connection\": \"Keep-Alive\",\n \"Server\": \"Apache/1.3.23 (Unix) mod_perl/1.26\"\n}", - jsonobject.toString(2)); - assertEquals("HTTP/1.1 200 Oki Doki\r\n" - + "Transfer-Encoding: chunked\r\n" - + "Date: Sun, 26 May 2002 17:38:52 GMT\r\n" - + "Keep-Alive: timeout=15, max=100\r\n" - + "Content-Type: text/html\r\n" - + "Connection: Keep-Alive\r\n" - + "Server: Apache/1.3.23 (Unix) mod_perl/1.26\r\n\r\n", - HTTP.toString(jsonobject)); - } catch (Exception e) - { - fail(e.toString()); - } - } - - /** - * Tests the toString method using null key. - */ - public void testToString_NullKey() - { - try - { - jsonobject = new JSONObject("{\n \"Reason-Phrase\": \"Oki Doki\",\n \"Status-Code\": \"200\",\n \"Transfer-Encoding\": \"chunked\",\n \"Date\": \"Sun, 26 May 2002 17:38:52 GMT\",\n \"Keep-Alive\": \"timeout=15, max=100\",\n \"HTTP-Version\": \"HTTP/1.1\",\n \"Content-Type\": \"text/html\",\n \"Connection\": \"Keep-Alive\",\n \"Server\": \"Apache/1.3.23 (Unix) mod_perl/1.26\"\n}"); - jsonobject.put("testKey", JSONObject.NULL); - assertEquals("HTTP/1.1 200 Oki Doki\r\nDate: Sun, 26 May 2002 17:38:52 GMT\r\nTransfer-Encoding: chunked\r\nKeep-Alive: timeout=15, max=100\r\nConnection: Keep-Alive\r\nContent-Type: text/html\r\nServer: Apache/1.3.23 (Unix) mod_perl/1.26\r\n\r\n", HTTP.toString(jsonobject)); - } catch (Exception e) - { - fail(e.toString()); - } - } - - /** - * Tests the toString method using status code but no reason phrase. - */ - public void testToString_StatusCodeButNoReasonPhrase() - { - try - { - jsonobject = new JSONObject("{\n \"Status-Code\": \"200\",\n \"Transfer-Encoding\": \"chunked\",\n \"Date\": \"Sun, 26 May 2002 17:38:52 GMT\",\n \"Keep-Alive\": \"timeout=15, max=100\",\n \"HTTP-Version\": \"HTTP/1.1\",\n \"Content-Type\": \"text/html\",\n \"Connection\": \"Keep-Alive\",\n \"Server\": \"Apache/1.3.23 (Unix) mod_perl/1.26\"\n}"); - HTTP.toString(jsonobject); - fail("Should have thrown an exception."); - } catch (Exception e) - { - assertEquals("Not enough material for an HTTP header.", e.getMessage()); - } - } - - - - /** - * Tests the toString method using method but no request uri. - */ - public void testToString_MethodButNoRequestUri() - { - try - { - jsonobject = new JSONObject("{\n \"Accept-Language\": \"en-us\",\n \"Host\": \"www.nokko.com\",\n \"Method\": \"GET\",\n \"Accept-encoding\": \"gzip, deflate\",\n \"User-Agent\": \"Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; Q312461)\",\n \"HTTP-Version\": \"HTTP/1.0\",\n \"Connection\": \"keep-alive\",\n \"Accept\": \"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\"\n}"); - HTTP.toString(jsonobject); - fail("Should have thrown an exception."); - } catch (Exception e) - { - assertEquals("Not enough material for an HTTP header.", e.getMessage()); - } - } - - /** - * Tests the constructor method. - */ - public static void testConstructor() - { - HTTP http = new HTTP(); - assertEquals("HTTP", http.getClass().getSimpleName()); - } -} \ No newline at end of file diff --git a/tests/TestHTTPTokener.java b/tests/TestHTTPTokener.java deleted file mode 100644 index 19f0eb4..0000000 --- a/tests/TestHTTPTokener.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * File: TestHTTPTokener.java Author: JSON.org - */ -package org.json.tests; - -import org.json.HTTPTokener; -import org.json.JSONException; - -import junit.framework.TestCase; - -/** - * The Class TestHTTPTokener. - */ -public class TestHTTPTokener extends TestCase -{ - - private HTTPTokener httptokener; - - /** - * Tests the toString method. - */ - public void testNextToken_SimpleString() - { - try - { - httptokener = new HTTPTokener( - "{\n \"Accept-Language\": 'en-us' ," + - "\n \"Host\": 23"); - assertEquals("{", httptokener.nextToken()); - assertEquals("Accept-Language", httptokener.nextToken()); - assertEquals(":", httptokener.nextToken()); - assertEquals("en-us", httptokener.nextToken()); - assertEquals(",", httptokener.nextToken()); - assertEquals("Host", httptokener.nextToken()); - assertEquals(":", httptokener.nextToken()); - assertEquals("23", httptokener.nextToken()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the nextToken method using unterminated string. - */ - public void testNextToken_UnterminatedString() - { - try - { - httptokener = new HTTPTokener( - "'en-us"); - httptokener.nextToken(); - fail("Should have thrown exception"); - } catch (JSONException e) - { - assertEquals("Unterminated string. at 7 [character 8 line 1]", e.getMessage()); - } - } - -} \ No newline at end of file diff --git a/tests/TestJSONArray.java b/tests/TestJSONArray.java deleted file mode 100644 index a8442af..0000000 --- a/tests/TestJSONArray.java +++ /dev/null @@ -1,1440 +0,0 @@ -/* - * File: TestJSONArray.java Author: JSON.org - */ -package org.json.tests; - -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Stack; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.json.JSONString; -import org.junit.Before; - -import junit.framework.TestCase; - -/** - * The Class TestJSONArray. - */ -public class TestJSONArray extends TestCase -{ - - /** The jsonarray. */ - private JSONArray jsonarray; - - /** The string. */ - private String string; - - /* - * (non-Javadoc) - * - * @see junit.framework.TestCase#setUp() - */ - @Override - @Before - public void setUp() - { - jsonarray = new JSONArray(); - string = ""; - } - - /** - * The Class testObject. - */ - public class testObject - { - //Do Nothing - } - - /** - * Tests the jsonArray method using int with leading zeros. - */ - public static void testJsonArray_IntWithLeadingZeros() - { - JSONArray jsonarray; - String string; - - try - { - string = "[001122334455]"; - jsonarray = new JSONArray(string); - assertEquals("[1122334455]", jsonarray.toString()); - } catch (Exception e) - { - fail(e.toString()); - } - } - - /** - * Tests the jsonArray method using scintific notation. - */ - public static void testJsonArray_ScintificNotation() - { - JSONArray jsonarray; - String string; - - try - { - string = "[666e666]"; - jsonarray = new JSONArray(string); - assertEquals("[\"666e666\"]", jsonarray.toString()); - } catch (Exception e) - { - fail(e.toString()); - } - } - - /** - * Tests the jsonArray method using double with leading and trailing zeros. - */ - public static void testJsonArray_DoubleWithLeadingAndTrailingZeros() - { - JSONArray jsonarray; - String string; - - try - { - string = "[00.10]"; - jsonarray = new JSONArray(string); - assertEquals("[0.1]", jsonarray.toString()); - } catch (Exception e) - { - fail(e.toString()); - } - } - - /** - * Tests the constructor method using missing value. - */ - public void testConstructor_MissingValue() - { - try - { - jsonarray = new JSONArray("[\n\r\n\r}"); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("Missing value at 5 [character 0 line 4]", - jsone.getMessage()); - } - } - - /** - * Tests the constructor method using nan. - */ - public void testConstructor_Nan() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(Double.NaN); - jsonarray.toString(); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("JSON does not allow non-finite numbers.", - jsone.getMessage()); - } - } - - /** - * Tests the constructor method using negative infinity. - */ - public void testConstructor_NegativeInfinity() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(Double.NEGATIVE_INFINITY); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("JSON does not allow non-finite numbers.", - jsone.getMessage()); - } - } - - /** - * Tests the constructor method using positive infinity. - */ - public void testConstructor_PositiveInfinity() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(Double.POSITIVE_INFINITY); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("JSON does not allow non-finite numbers.", - jsone.getMessage()); - } - } - - /** - * Tests the put method using positive infinity. - */ - public void testPut_PositiveInfinity() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(Double.POSITIVE_INFINITY); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("JSON does not allow non-finite numbers.", - jsone.getMessage()); - } - } - - /** - * Tests the getDouble method using empty array. - */ - public void testGetDouble_EmptyArray() - { - - try - { - jsonarray.getDouble(0); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("JSONArray[0] not found.", jsone.getMessage()); - } - } - - /** - * Tests the get method using negative index. - */ - public void testGet_NegativeIndex() - { - - try - { - jsonarray.get(-1); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("JSONArray[-1] not found.", jsone.getMessage()); - } - } - - /** - * Tests the put method using nan. - */ - public void testPut_Nan() - { - try - { - jsonarray.put(Double.NaN); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("JSON does not allow non-finite numbers.", - jsone.getMessage()); - } - } - - /** - * Tests the constructor method using object. - */ - public void testConstructor_Object() - { - try - { - jsonarray = new JSONArray(new Object()); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals( - "JSONArray initial value should be a string or collection or array.", - jsone.getMessage()); - } - } - - /** - * Tests the constructor method using bad json. - */ - public void testConstructor_BadJson() - { - - try - { - string = "[)"; - jsonarray = new JSONArray(string); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("Expected a ',' or ']' at 3 [character 4 line 1]", - jsone.getMessage()); - } - } - - /** - * Tests the toString method using locations. - */ - public void testToString_Locations() - { - try - { - string = " [\"San Francisco\", \"New York\", \"Seoul\", \"London\", \"Seattle\", \"Shanghai\"]"; - jsonarray = new JSONArray(string); - assertEquals( - "[\"San Francisco\",\"New York\",\"Seoul\",\"London\",\"Seattle\",\"Shanghai\"]", - jsonarray.toString()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the constructor method using collection. - */ - public void testConstructor_Collection() - { - Collection stringCol = new Stack(); - stringCol.add("string1"); - stringCol.add("string2"); - stringCol.add("string3"); - stringCol.add("string4"); - jsonarray = new JSONArray(stringCol); - assertEquals("[\"string1\",\"string2\",\"string3\",\"string4\"]", - jsonarray.toString()); - } - - /** - * Tests the constructor method using null collection. - */ - public void testConstructor_NullCollection() - { - Collection stringCol = null; - jsonarray = new JSONArray(stringCol); - assertEquals("[]", jsonarray.toString()); - } - - /** - * Tests the constructor method using string array. - */ - public void testConstructor_StringArray() - { - try - { - jsonarray = new JSONArray(new String[] - { - "string1", "string2" - }); - assertEquals("[\"string1\",\"string2\"]", jsonarray.toString()); - - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the opt method. - */ - public void testOpt() - { - try - { - jsonarray = new JSONArray(new String[] - { - "string1", "string2" - }); - assertEquals("string1", jsonarray.opt(0)); - assertEquals("string2", jsonarray.opt(1)); - - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using exception. - */ - public void testToString_Exception() - { - class BadJsonString implements JSONString - { - - /* - * (non-Javadoc) - * - * @see org.json.JSONString#toJSONString() - */ - @Override - public String toJSONString() - { - String[] arString = new String[] - { - "abc" - }; - return arString[1]; - } - - } - - jsonarray = new JSONArray(); - jsonarray.put(new BadJsonString()); - assertEquals(null, jsonarray.toString()); - } - - /** - * Tests the toString method using indents. - */ - public void testToString_Indents() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put(new JSONObject().put("abc", "123")); - jsonarray.put("abc"); - jsonarray.put(new JSONArray().put(new JSONArray()).put( - new JSONArray().put("123").put("abc"))); - assertEquals( - "[\n \"123\",\n {\"abc\": \"123\"},\n \"abc\",\n [\n [],\n [\n \"123\",\n \"abc\"\n ]\n ]\n]", - jsonarray.toString(4)); - assertEquals("[\"123\"]", new JSONArray().put("123").toString(4)); - assertEquals("[]", new JSONArray().toString(4)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the get method using invalid index. - */ - public void testGet_InvalidIndex() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.get(1); - } catch (JSONException e) - { - assertEquals("JSONArray[1] not found.", e.getMessage()); - } - } - - /** - * Tests the get method using valid index. - */ - public void testGet_ValidIndex() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - assertEquals("123", jsonarray.get(0)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the getBoolean method. - */ - public void testGetBoolean() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("true"); - jsonarray.put("false"); - jsonarray.put(true); - jsonarray.put(false); - jsonarray.put("TRUE"); - jsonarray.put("FALSE"); - assertTrue(jsonarray.getBoolean(0)); - assertFalse(jsonarray.getBoolean(1)); - assertTrue(jsonarray.getBoolean(2)); - assertFalse(jsonarray.getBoolean(3)); - assertTrue(jsonarray.getBoolean(4)); - assertFalse(jsonarray.getBoolean(5)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the getBoolean method using non boolean. - */ - public void testGetBoolean_NonBoolean() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.getBoolean(0); - } catch (JSONException e) - { - assertEquals("JSONArray[0] is not a boolean.", e.getMessage()); - } - } - - /** - * Tests the optBoolean method. - */ - public void testOptBoolean() - { - jsonarray = new JSONArray(); - jsonarray.put("true"); - jsonarray.put("false"); - jsonarray.put(true); - jsonarray.put(false); - jsonarray.put("TRUE"); - jsonarray.put("FALSE"); - jsonarray.put("grass"); - assertTrue(jsonarray.optBoolean(0)); - assertFalse(jsonarray.optBoolean(1)); - assertTrue(jsonarray.optBoolean(2)); - assertFalse(jsonarray.optBoolean(3)); - assertTrue(jsonarray.optBoolean(4)); - assertFalse(jsonarray.optBoolean(5)); - assertFalse(jsonarray.optBoolean(6)); - } - - /** - * Tests the getInt method. - */ - public void testGetInt() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put(45); - jsonarray.put(-98); - assertEquals(123, jsonarray.getInt(0)); - assertEquals(-12, jsonarray.getInt(1)); - assertEquals(45, jsonarray.getInt(2)); - assertEquals(-98, jsonarray.getInt(3)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the getInt method using non integer. - */ - public void testGetInt_NonInteger() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("abc"); - jsonarray.getInt(0); - } catch (JSONException e) - { - assertEquals("JSONArray[0] is not a number.", e.getMessage()); - } - } - - /** - * Tests the optInt method. - */ - public void testOptInt() - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put(45); - jsonarray.put(-98); - jsonarray.put("abc"); - assertEquals(123, jsonarray.optInt(0)); - assertEquals(-12, jsonarray.optInt(1)); - assertEquals(45, jsonarray.optInt(2)); - assertEquals(-98, jsonarray.optInt(3)); - assertEquals(0, jsonarray.optInt(4)); - } - - /** - * Tests the getDouble method. - */ - public void testGetDouble() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put(45); - jsonarray.put(-98); - jsonarray.put("123.5"); - jsonarray.put("-12.87"); - jsonarray.put(45.22); - jsonarray.put(-98.18); - assertEquals(123.0, jsonarray.getDouble(0)); - assertEquals(-12.0, jsonarray.getDouble(1)); - assertEquals(45.0, jsonarray.getDouble(2)); - assertEquals(-98.0, jsonarray.getDouble(3)); - assertEquals(123.5, jsonarray.getDouble(4)); - assertEquals(-12.87, jsonarray.getDouble(5)); - assertEquals(45.22, jsonarray.getDouble(6)); - assertEquals(-98.18, jsonarray.getDouble(7)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the getDouble method using non double. - */ - public void testGetDouble_NonDouble() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("abc"); - jsonarray.getDouble(0); - } catch (JSONException e) - { - assertEquals("JSONArray[0] is not a number.", e.getMessage()); - } - } - - /** - * Tests the optDouble method. - */ - public void testOptDouble() - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put(45); - jsonarray.put(-98); - jsonarray.put("123.5"); - jsonarray.put("-12.87"); - try - { - jsonarray.put(45.22); - jsonarray.put(-98.18); - } catch (JSONException e) - { - fail(e.getMessage()); - } - assertEquals(123.0, jsonarray.optDouble(0)); - assertEquals(-12.0, jsonarray.optDouble(1)); - assertEquals(45.0, jsonarray.optDouble(2)); - assertEquals(-98.0, jsonarray.optDouble(3)); - assertEquals(123.5, jsonarray.optDouble(4)); - assertEquals(-12.87, jsonarray.optDouble(5)); - assertEquals(45.22, jsonarray.optDouble(6)); - assertEquals(-98.18, jsonarray.optDouble(7)); - assertEquals(Double.NaN, jsonarray.optDouble(8)); - } - - /** - * Tests the getLong method. - */ - public void testGetLong() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put(45L); - jsonarray.put(-98L); - assertEquals(123, jsonarray.getLong(0)); - assertEquals(-12, jsonarray.getLong(1)); - assertEquals(45, jsonarray.getLong(2)); - assertEquals(-98, jsonarray.getLong(3)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the getLong method using non long. - */ - public void testGetLong_NonLong() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("abc"); - jsonarray.getLong(0); - } catch (JSONException e) - { - assertEquals("JSONArray[0] is not a number.", e.getMessage()); - } - } - - /** - * Tests the optLong method. - */ - public void testOptLong() - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put(45L); - jsonarray.put(-98L); - assertEquals(123, jsonarray.optLong(0)); - assertEquals(-12, jsonarray.optLong(1)); - assertEquals(45, jsonarray.optLong(2)); - assertEquals(-98, jsonarray.optLong(3)); - assertEquals(0, jsonarray.optLong(8)); - } - - /** - * Tests the getString method. - */ - public void testGetString() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put("abc"); - jsonarray.put("123"); - assertEquals("123", jsonarray.getString(0)); - assertEquals("-12", jsonarray.getString(1)); - assertEquals("abc", jsonarray.getString(2)); - assertEquals("123", jsonarray.getString(3)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the getString method using non string. - */ - public void testGetString_NonString() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(123); - jsonarray.getString(0); - } catch (JSONException e) - { - assertEquals("JSONArray[0] not a string.", e.getMessage()); - } - } - - /** - * Tests the optString method. - */ - public void testOptString() - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put("abc"); - jsonarray.put("123"); - assertEquals("123", jsonarray.optString(0)); - assertEquals("-12", jsonarray.optString(1)); - assertEquals("abc", jsonarray.optString(2)); - assertEquals("123", jsonarray.optString(3)); - assertEquals("", jsonarray.optString(4)); - } - - /** - * Tests the optJSONObject method. - */ - public void testOptJSONObject() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(new JSONObject().put("abc", "123")); - assertEquals("{\"abc\":\"123\"}", jsonarray.optJSONObject(0).toString()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the optJSONObject method using non json object. - */ - public void testOptJSONObject_NonJsonObject() - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - assertEquals(null, jsonarray.optJSONObject(0)); - } - - /** - * Tests the optJSONArray method. - */ - public void testOptJSONArray() - { - jsonarray = new JSONArray(); - jsonarray.put(new JSONArray().put("abc")); - assertEquals("[\"abc\"]", jsonarray.optJSONArray(0).toString()); - } - - /** - * Tests the optJSONArray method using non json array. - */ - public void testOptJSONArray_NonJsonArray() - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - assertEquals(null, jsonarray.optJSONArray(0)); - } - - /** - * Tests the isNull method. - */ - public void testIsNull() - { - jsonarray = new JSONArray(); - jsonarray.put(JSONObject.NULL); - jsonarray.put("null"); - assertTrue(jsonarray.isNull(0)); - assertFalse(jsonarray.isNull(1)); - } - - /** - * Tests the writer method. - */ - public void testWriter() - { - try - { - StringWriter sw = new StringWriter(); - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put(45); - jsonarray.put(-98); - jsonarray.put(new JSONArray().put("abc")); - jsonarray.put("-12"); - jsonarray.put("abc"); - jsonarray.put("123"); - jsonarray.put("123"); - jsonarray.put(new JSONObject().put("abc", "123")); - jsonarray.put("abc"); - jsonarray.put("123"); - jsonarray.write(sw); - assertEquals( - "[\"123\",\"-12\",45,-98,[\"abc\"],\"-12\",\"abc\",\"123\",\"123\",{\"abc\":\"123\"},\"abc\",\"123\"]", - sw.toString()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the writer method using bad writer. - */ - public void testWriter_BadWriter() - { - class BadWriter extends Writer - { - - /* - * (non-Javadoc) - * - * @see java.io.Writer#write(char[], int, int) - */ - @Override - public void write(char[] cbuf, int off, int len) throws IOException - { - throw new IOException("Test Message From Bad Writer"); - } - - /* - * (non-Javadoc) - * - * @see java.io.Writer#flush() - */ - @Override - public void flush() throws IOException - { - //Do Nothing - } - - /* - * (non-Javadoc) - * - * @see java.io.Writer#close() - */ - @Override - public void close() throws IOException - { - // Do nothing - } - - } - try - { - BadWriter sw = new BadWriter(); - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put(45); - jsonarray.put(-98); - jsonarray.put(new JSONArray().put("abc")); - jsonarray.put("-12"); - jsonarray.put("abc"); - jsonarray.put("123"); - jsonarray.put("123"); - jsonarray.put(new JSONObject().put("abc", "123")); - jsonarray.put("abc"); - jsonarray.put("123"); - jsonarray.write(sw); - } catch (JSONException e) - { - assertEquals("Test Message From Bad Writer", e.getMessage()); - } - } - - /** - * Tests the put method using object and specific index. - */ - public void testPut_ObjectAndSpecificIndex() - { - try - { - testObject a = new testObject(); - testObject b = new testObject(); - testObject c = new testObject(); - testObject d = new testObject(); - jsonarray = new JSONArray(); - jsonarray.put(0, a); - jsonarray.put(1, b); - assertEquals(a, jsonarray.get(0)); - assertEquals(b, jsonarray.get(1)); - jsonarray.put(0, c); - assertEquals(c, jsonarray.get(0)); - assertEquals(b, jsonarray.get(1)); - jsonarray.put(8, d); - assertEquals(d, jsonarray.get(8)); - assertEquals(JSONObject.NULL, jsonarray.get(2)); - assertEquals(JSONObject.NULL, jsonarray.get(3)); - assertEquals(JSONObject.NULL, jsonarray.get(4)); - assertEquals(JSONObject.NULL, jsonarray.get(5)); - assertEquals(JSONObject.NULL, jsonarray.get(6)); - assertEquals(JSONObject.NULL, jsonarray.get(7)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the put method using object and negative index. - */ - public void testPut_ObjectAndNegativeIndex() - { - try - { - testObject a = new testObject(); - jsonarray = new JSONArray(); - jsonarray.put(-1, a); - fail("Should have thrown exception"); - } catch (JSONException e) - { - assertEquals("JSONArray[-1] not found.", e.getMessage()); - } - } - - /** - * Tests the toJSONObject method. - */ - public void testToJSONObject() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.put("-12"); - jsonarray.put(45); - jsonarray.put(-98); - jsonarray.put(new JSONArray().put("abc")); - jsonarray.put(new JSONObject().put("abc", "123")); - JSONArray names = new JSONArray(new String[] - { - "bdd", "fdsa", "fds", "ewre", "rer", "gfs" - }); - assertEquals( - "{\"gfs\":{\"abc\":\"123\"},\"fdsa\":\"-12\",\"bdd\":\"123\",\"ewre\":-98,\"rer\":[\"abc\"],\"fds\":45}", - jsonarray.toJSONObject(names).toString()); - assertEquals(null, jsonarray.toJSONObject(new JSONArray())); - assertEquals(null, jsonarray.toJSONObject(null)); - assertEquals(null, new JSONArray().toJSONObject(names)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the getJSONObject method. - */ - public void testGetJSONObject() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(new JSONObject().put("abc", "123")); - assertEquals("{\"abc\":\"123\"}", jsonarray.getJSONObject(0).toString()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the getJSONObject method using non json object. - */ - public void testGetJSONObject_NonJsonObject() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.getJSONObject(0); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("JSONArray[0] is not a JSONObject.", e.getMessage()); - } - } - - /** - * Tests the getJSONArray method. - */ - public void testGetJSONArray() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(new JSONArray().put("abc")); - assertEquals("[\"abc\"]", jsonarray.getJSONArray(0).toString()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the getJSONArray method using non json array. - */ - public void testGetJSONArray_NonJsonArray() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("123"); - jsonarray.getJSONArray(0); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("JSONArray[0] is not a JSONArray.", e.getMessage()); - } - } - - /** - * Tests the put method using map. - */ - public void testPut_Map() - { - Map map = new HashMap(); - map.put("abc", "123"); - jsonarray = new JSONArray(); - jsonarray.put(map); - assertEquals("[{\"abc\":\"123\"}]", jsonarray.toString()); - } - - /** - * Tests the constructor method using bad json array. - */ - public void testConstructor_BadJsonArray() - { - try - { - jsonarray = new JSONArray("abc"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("A JSONArray text must start with '[' at 1 [character 2 line 1]", e.getMessage()); - } - } - - /** - * Tests the constructor method. - */ - public void testConstructor() - { - try - { - jsonarray = new JSONArray("[]"); - assertEquals("[]", jsonarray.toString()); - jsonarray = new JSONArray("[\"abc\"]"); - assertEquals("[\"abc\"]", jsonarray.toString()); - jsonarray = new JSONArray("[\"abc\",\"123\"]"); - assertEquals("[\"abc\",\"123\"]", jsonarray.toString()); - jsonarray = new JSONArray("[123,{}]"); - assertEquals("[123,{}]", jsonarray.toString()); - jsonarray = new JSONArray("[123,,{}]"); - assertEquals("[123,null,{}]", jsonarray.toString()); - jsonarray = new JSONArray("[123,,{},]"); - assertEquals("[123,null,{}]", jsonarray.toString()); - jsonarray = new JSONArray("[123,,{};]"); - assertEquals("[123,null,{}]", jsonarray.toString()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the put method using collection. - */ - public void testPut_Collection() - { - Collection stringCol = new Stack(); - stringCol.add("string1"); - stringCol.add("string2"); - stringCol.add("string3"); - stringCol.add("string4"); - jsonarray = new JSONArray(); - jsonarray.put(stringCol); - assertEquals("[[\"string1\",\"string2\",\"string3\",\"string4\"]]", - jsonarray.toString()); - } - - /** - * Tests the put method using boolean and specific index. - */ - public void testPut_BooleanAndSpecificIndex() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(0, true); - jsonarray.put(1, true); - assertEquals(true, jsonarray.get(0)); - assertEquals(true, jsonarray.get(1)); - jsonarray.put(0, false); - assertEquals(false, jsonarray.get(0)); - assertEquals(true, jsonarray.get(1)); - jsonarray.put(8, false); - assertEquals(false, jsonarray.get(8)); - assertEquals(JSONObject.NULL, jsonarray.get(2)); - assertEquals(JSONObject.NULL, jsonarray.get(3)); - assertEquals(JSONObject.NULL, jsonarray.get(4)); - assertEquals(JSONObject.NULL, jsonarray.get(5)); - assertEquals(JSONObject.NULL, jsonarray.get(6)); - assertEquals(JSONObject.NULL, jsonarray.get(7)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the put method using boolean and negative index. - */ - public void testPut_BooleanAndNegativeIndex() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(-1, true); - fail("Should have thrown exception"); - } catch (JSONException e) - { - assertEquals("JSONArray[-1] not found.", e.getMessage()); - } - } - - /** - * Tests the put method using collection and specific index. - */ - public void testPut_CollectionAndSpecificIndex() - { - try - { - Collection a = new Stack(); - a.add("string1"); - a.add("string4"); - Collection b = new Stack(); - b.add("string2"); - b.add("string3"); - Collection c = new Stack(); - c.add("string3"); - c.add("string4"); - Collection d = new Stack(); - d.add("string1"); - d.add("string2"); - jsonarray = new JSONArray(); - jsonarray.put(0, a); - jsonarray.put(1, b); - assertEquals(new JSONArray(a).toString(), jsonarray.get(0).toString()); - assertEquals(new JSONArray(b).toString(), jsonarray.get(1).toString()); - jsonarray.put(0, c); - assertEquals(new JSONArray(c).toString(), jsonarray.get(0).toString()); - assertEquals(new JSONArray(b).toString(), jsonarray.get(1).toString()); - jsonarray.put(8, d); - assertEquals(new JSONArray(d).toString(), jsonarray.get(8).toString()); - assertEquals(JSONObject.NULL, jsonarray.get(2)); - assertEquals(JSONObject.NULL, jsonarray.get(3)); - assertEquals(JSONObject.NULL, jsonarray.get(4)); - assertEquals(JSONObject.NULL, jsonarray.get(5)); - assertEquals(JSONObject.NULL, jsonarray.get(6)); - assertEquals(JSONObject.NULL, jsonarray.get(7)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the put method using collection and negative index. - */ - public void testPut_CollectionAndNegativeIndex() - { - try - { - Collection a = new Stack(); - a.add("string1"); - a.add("string4"); - jsonarray = new JSONArray(); - jsonarray.put(-1, a); - fail("Should have thrown exception"); - } catch (JSONException e) - { - assertEquals("JSONArray[-1] not found.", e.getMessage()); - } - } - - /** - * Tests the put method using double and specific index. - */ - public void testPut_DoubleAndSpecificIndex() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(0, 10.0); - jsonarray.put(1, 30.2); - assertEquals(10.0, jsonarray.get(0)); - assertEquals(30.2, jsonarray.get(1)); - jsonarray.put(0, 52.64); - assertEquals(52.64, jsonarray.get(0)); - assertEquals(30.2, jsonarray.get(1)); - jsonarray.put(8, 14.23); - assertEquals(14.23, jsonarray.get(8)); - assertEquals(JSONObject.NULL, jsonarray.get(2)); - assertEquals(JSONObject.NULL, jsonarray.get(3)); - assertEquals(JSONObject.NULL, jsonarray.get(4)); - assertEquals(JSONObject.NULL, jsonarray.get(5)); - assertEquals(JSONObject.NULL, jsonarray.get(6)); - assertEquals(JSONObject.NULL, jsonarray.get(7)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the put method using double and negative index. - */ - public void testPut_DoubleAndNegativeIndex() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(-1, 30.65); - fail("Should have thrown exception"); - } catch (JSONException e) - { - assertEquals("JSONArray[-1] not found.", e.getMessage()); - } - } - - /** - * Tests the put method using int and specific index. - */ - public void testPut_IntAndSpecificIndex() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(0, 54); - jsonarray.put(1, 82); - assertEquals(54, jsonarray.get(0)); - assertEquals(82, jsonarray.get(1)); - jsonarray.put(0, 36); - assertEquals(36, jsonarray.get(0)); - assertEquals(82, jsonarray.get(1)); - jsonarray.put(8, 67); - assertEquals(67, jsonarray.get(8)); - assertEquals(JSONObject.NULL, jsonarray.get(2)); - assertEquals(JSONObject.NULL, jsonarray.get(3)); - assertEquals(JSONObject.NULL, jsonarray.get(4)); - assertEquals(JSONObject.NULL, jsonarray.get(5)); - assertEquals(JSONObject.NULL, jsonarray.get(6)); - assertEquals(JSONObject.NULL, jsonarray.get(7)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the put method using int and negative index. - */ - public void testPut_IntAndNegativeIndex() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(-1, 3); - fail("Should have thrown exception"); - } catch (JSONException e) - { - assertEquals("JSONArray[-1] not found.", e.getMessage()); - } - } - - /** - * Tests the put method using long and specific index. - */ - public void testPut_LongAndSpecificIndex() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(0, 54L); - jsonarray.put(1, 456789123L); - assertEquals(54L, jsonarray.get(0)); - assertEquals(456789123L, jsonarray.get(1)); - jsonarray.put(0, 72887L); - assertEquals(72887L, jsonarray.get(0)); - assertEquals(456789123L, jsonarray.get(1)); - jsonarray.put(8, 39397L); - assertEquals(39397L, jsonarray.get(8)); - assertEquals(JSONObject.NULL, jsonarray.get(2)); - assertEquals(JSONObject.NULL, jsonarray.get(3)); - assertEquals(JSONObject.NULL, jsonarray.get(4)); - assertEquals(JSONObject.NULL, jsonarray.get(5)); - assertEquals(JSONObject.NULL, jsonarray.get(6)); - assertEquals(JSONObject.NULL, jsonarray.get(7)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the put method using long and negative index. - */ - public void testPut_LongAndNegativeIndex() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(-1, 456486794L); - fail("Should have thrown exception"); - } catch (JSONException e) - { - assertEquals("JSONArray[-1] not found.", e.getMessage()); - } - } - - /** - * Tests the put method using map and specific index. - */ - public void testPut_MapAndSpecificIndex() - { - try - { - Map a = new HashMap(); - a.put("abc", "123"); - Map b = new HashMap(); - b.put("abffc", "1253"); - Map c = new HashMap(); - c.put("addbc", "145623"); - Map d = new HashMap(); - d.put("abffdc", "122623"); - jsonarray = new JSONArray(); - jsonarray.put(0, a); - jsonarray.put(1, b); - assertEquals(new JSONObject(a).toString(), jsonarray.get(0).toString()); - assertEquals(new JSONObject(b).toString(), jsonarray.get(1).toString()); - jsonarray.put(0, c); - assertEquals(new JSONObject(c).toString(), jsonarray.get(0).toString()); - assertEquals(new JSONObject(b).toString(), jsonarray.get(1).toString()); - jsonarray.put(8, d); - assertEquals(new JSONObject(d).toString(), jsonarray.get(8).toString()); - assertEquals(JSONObject.NULL, jsonarray.get(2)); - assertEquals(JSONObject.NULL, jsonarray.get(3)); - assertEquals(JSONObject.NULL, jsonarray.get(4)); - assertEquals(JSONObject.NULL, jsonarray.get(5)); - assertEquals(JSONObject.NULL, jsonarray.get(6)); - assertEquals(JSONObject.NULL, jsonarray.get(7)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the put method using map and negative index. - */ - public void testPut_MapAndNegativeIndex() - { - try - { - Map a = new HashMap(); - a.put("abc", "123"); - jsonarray = new JSONArray(); - jsonarray.put(-1, a); - fail("Should have thrown exception"); - } catch (JSONException e) - { - assertEquals("JSONArray[-1] not found.", e.getMessage()); - } - } - - /** - * Tests the remove method. - */ - public void testRemove() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put(0, 54); - jsonarray.put(1, 456789123); - jsonarray.put(2, 72887); - jsonarray.remove(1); - assertEquals(54, jsonarray.get(0)); - assertEquals(72887, jsonarray.get(1)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - -} \ No newline at end of file diff --git a/tests/TestJSONException.java b/tests/TestJSONException.java deleted file mode 100644 index 8158c2f..0000000 --- a/tests/TestJSONException.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * File: TestJSONException.java Author: JSON.org - */ -package org.json.tests; - -import org.json.JSONException; - -import junit.framework.TestCase; - -/** - * The Class TestJSONException. - */ -public class TestJSONException extends TestCase { - - /** The jsonexception. */ - JSONException jsonexception; - - /** - * Tests the constructor method using string. - */ - public void testConstructor_String() { - jsonexception = new JSONException("test String"); - assertEquals("test String", jsonexception.getMessage()); - } - - /** - * Tests the constructor method using exception. - */ - public void testConstructor_Exception() { - Exception e = new Exception(); - jsonexception = new JSONException(e); - assertEquals(e, jsonexception.getCause()); - } - -} \ No newline at end of file diff --git a/tests/TestJSONML.java b/tests/TestJSONML.java deleted file mode 100644 index 712b368..0000000 --- a/tests/TestJSONML.java +++ /dev/null @@ -1,782 +0,0 @@ -/* - * File: TestJSONML.java Author: JSON.org - */ -package org.json.tests; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONML; -import org.json.JSONObject; -import org.json.XML; - -import junit.framework.TestCase; - -/** - * The Class TestJSONML. - */ -public class TestJSONML extends TestCase -{ - - /** The jsonobject. */ - private JSONObject jsonobject; - - /** The jsonarray. */ - private JSONArray jsonarray; - - /** The string. */ - private String string; - - /** - * Tests the toJsonArray method using open xml tag. - */ - public void testToJsonArray_OpenXmlTag() - { - try { - string = " Basic bread Flour Yeast Water Salt Mix all ingredients together. Knead thoroughly. Cover with a cloth, and leave for one hour in warm room. Knead again. Place in a bread baking tin. Cover with a cloth, and leave for one hour in warm room. Bake in the oven at 180(degrees)C for 30 minutes. "; - - jsonobject = JSONML.toJSONObject(string); - assertEquals( - "{\"cook_time\":\"3 hours\",\"name\":\"bread\",\"tagName\":\"recipe\",\"childNodes\":[{\"tagName\":\"title\",\"childNodes\":[\"Basic bread\"]},{\"amount\":8,\"unit\":\"dL\",\"tagName\":\"ingredient\",\"childNodes\":[\"Flour\"]},{\"amount\":10,\"unit\":\"grams\",\"tagName\":\"ingredient\",\"childNodes\":[\"Yeast\"]},{\"amount\":4,\"unit\":\"dL\",\"tagName\":\"ingredient\",\"state\":\"warm\",\"childNodes\":[\"Water\"]},{\"amount\":1,\"unit\":\"teaspoon\",\"tagName\":\"ingredient\",\"childNodes\":[\"Salt\"]},{\"tagName\":\"instructions\",\"childNodes\":[{\"tagName\":\"step\",\"childNodes\":[\"Mix all ingredients together.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Knead thoroughly.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Cover with a cloth, and leave for one hour in warm room.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Knead again.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Place in a bread baking tin.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Cover with a cloth, and leave for one hour in warm room.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Bake in the oven at 180(degrees)C for 30 minutes.\"]}]}],\"prep_time\":\"5 mins\"}", - jsonobject.toString()); - assertEquals( - "Basic breadFlourYeastWaterSaltMix all ingredients together.Knead thoroughly.Cover with a cloth, and leave for one hour in warm room.Knead again.Place in a bread baking tin.Cover with a cloth, and leave for one hour in warm room.Bake in the oven at 180(degrees)C for 30 minutes.", - JSONML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using xml recipe as json array. - */ - public void testToString_XmlRecipeAsJsonArray() - { - try - { - string = " Basic bread Flour Yeast Water Salt Mix all ingredients together. Knead thoroughly. Cover with a cloth, and leave for one hour in warm room. Knead again. Place in a bread baking tin. Cover with a cloth, and leave for one hour in warm room. Bake in the oven at 180(degrees)C for 30 minutes. "; - - jsonarray = JSONML.toJSONArray(string); - assertEquals( - "[\n \"recipe\",\n {\n \"cook_time\": \"3 hours\",\n \"name\": \"bread\",\n \"prep_time\": \"5 mins\"\n },\n [\n \"title\",\n \"Basic bread\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 8,\n \"unit\": \"dL\"\n },\n \"Flour\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 10,\n \"unit\": \"grams\"\n },\n \"Yeast\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 4,\n \"unit\": \"dL\",\n \"state\": \"warm\"\n },\n \"Water\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 1,\n \"unit\": \"teaspoon\"\n },\n \"Salt\"\n ],\n [\n \"instructions\",\n [\n \"step\",\n \"Mix all ingredients together.\"\n ],\n [\n \"step\",\n \"Knead thoroughly.\"\n ],\n [\n \"step\",\n \"Cover with a cloth, and leave for one hour in warm room.\"\n ],\n [\n \"step\",\n \"Knead again.\"\n ],\n [\n \"step\",\n \"Place in a bread baking tin.\"\n ],\n [\n \"step\",\n \"Cover with a cloth, and leave for one hour in warm room.\"\n ],\n [\n \"step\",\n \"Bake in the oven at 180(degrees)C for 30 minutes.\"\n ]\n ]\n]", - jsonarray.toString(4)); - assertEquals( - "Basic breadFlourYeastWaterSaltMix all ingredients together.Knead thoroughly.Cover with a cloth, and leave for one hour in warm room.Knead again.Place in a bread baking tin.Cover with a cloth, and leave for one hour in warm room.Bake in the oven at 180(degrees)C for 30 minutes.", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJSONObjectHtml method. - */ - public void testToJSONObjectHtml() - { - try - { - string = "

JSONML is a transformation between JSON and XML that preserves ordering of document features.

JSONML can work with JSON arrays or JSON objects.

Three
little
words

"; - jsonobject = JSONML.toJSONObject(string); - assertEquals( - "{\n \"id\": \"demo\",\n \"tagName\": \"div\",\n \"class\": \"JSONML\",\n \"childNodes\": [\n {\n \"tagName\": \"p\",\n \"childNodes\": [\n \"JSONML is a transformation between\",\n {\n \"tagName\": \"b\",\n \"childNodes\": [\"JSON\"]\n },\n \"and\",\n {\n \"tagName\": \"b\",\n \"childNodes\": [\"XML\"]\n },\n \"that preserves ordering of document features.\"\n ]\n },\n {\n \"tagName\": \"p\",\n \"childNodes\": [\"JSONML can work with JSON arrays or JSON objects.\"]\n },\n {\n \"tagName\": \"p\",\n \"childNodes\": [\n \"Three\",\n {\"tagName\": \"br\"},\n \"little\",\n {\"tagName\": \"br\"},\n \"words\"\n ]\n }\n ]\n}", - jsonobject.toString(4)); - assertEquals( - "

JSONML is a transformation betweenJSONandXMLthat preserves ordering of document features.

JSONML can work with JSON arrays or JSON objects.

Three
little
words

", - JSONML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJSONArrayHtml method. - */ - public void testToJSONArrayHtml() - { - try - { - string = "

JSONML is a transformation between JSON and XML that preserves ordering of document features.

JSONML can work with JSON arrays or JSON objects.

Three
little
words

"; - - jsonarray = JSONML.toJSONArray(string); - assertEquals( - "[\n \"div\",\n {\n \"id\": \"demo\",\n \"class\": \"JSONML\"\n },\n [\n \"p\",\n \"JSONML is a transformation between\",\n [\n \"b\",\n \"JSON\"\n ],\n \"and\",\n [\n \"b\",\n \"XML\"\n ],\n \"that preserves ordering of document features.\"\n ],\n [\n \"p\",\n \"JSONML can work with JSON arrays or JSON objects.\"\n ],\n [\n \"p\",\n \"Three\",\n [\"br\"],\n \"little\",\n [\"br\"],\n \"words\"\n ]\n]", - jsonarray.toString(4)); - assertEquals( - "

JSONML is a transformation betweenJSONandXMLthat preserves ordering of document features.

JSONML can work with JSON arrays or JSON objects.

Three
little
words

", - JSONML.toString(jsonarray)); - - string = "{\"xmlns:soap\":\"http://www.w3.org/2003/05/soap-envelope\",\"tagName\":\"soap:Envelope\",\"childNodes\":[{\"tagName\":\"soap:Header\"},{\"tagName\":\"soap:Body\",\"childNodes\":[{\"tagName\":\"ws:listProducts\",\"childNodes\":[{\"tagName\":\"ws:delay\",\"childNodes\":[1]}]}]}],\"xmlns:ws\":\"http://warehouse.acme.com/ws\"}"; - jsonobject = new JSONObject(string); - assertEquals( - "1", - JSONML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonArray method using json information. - */ - public void testToJsonArray_JsonInformation() - { - try - { - string = "First \u0009<content> This is \"content\". 3 JSON does not preserve the sequencing of elements and contents. III T H R E EContent text is an implied structure in XML. JSON does not have implied structure:7everything is explicit.!]]>"; - - - jsonarray = JSONML.toJSONArray(string); - assertEquals( - "[\n \"xml\",\n {\n \"two\": \" \\\"2\\\" \",\n \"one\": 1\n },\n [\"five\"],\n \"First \\t\",\n [\"five\"],\n \"This is \\\"content\\\".\",\n [\n \"three\",\n 3\n ],\n \"JSON does not preserve the sequencing of elements and contents.\",\n [\n \"three\",\n \"III\"\n ],\n [\n \"three\",\n \"T H R E E\"\n ],\n [\"four\"],\n \"Content text is an implied structure in XML.\",\n [\n \"six\",\n {\"content\": 6}\n ],\n \"JSON does not have implied structure:\",\n [\n \"seven\",\n 7\n ],\n \"everything is explicit.\",\n \"CDATA blocks!\"\n]", - jsonarray.toString(4)); - assertEquals( - "First \t<content>This is "content".JSON does not preserve the sequencing of elements and contents.IIIT H R E EContent text is an implied structure in XML.JSON does not have implied structure:everything is explicit.CDATA blocks<are><supported>!", - JSONML.toString(jsonarray)); - - - }catch(JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonArray method using spanish numbers. - */ - public void testToJsonArray_SpanishNumbers() - { - try - { - string = "unodostrestruequatrocinqoseis"; - jsonarray = JSONML.toJSONArray(string); - assertEquals( - "[\n \"xml\",\n {\"do\": 0},\n \"uno\",\n [\n \"a\",\n {\n \"re\": 1,\n \"mi\": 2\n },\n \"dos\",\n [\n \"b\",\n {\"fa\": 3}\n ],\n \"tres\",\n [\n \"c\",\n true\n ],\n \"quatro\"\n ],\n \"cinqo\",\n [\n \"d\",\n \"seis\",\n [\"e\"]\n ]\n]", - jsonarray.toString(4)); - assertEquals( - "unodostresquatrocinqoseis", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonArray method using music notes. - */ - public void testToJsonArray_MusicNotes() - { - try - { - string = "The content of b and The content of cdoremi"; - jsonobject = XML.toJSONObject(string); - assertEquals( - "{\"a\":{\"f\":\"\",\"content\":\"and\",\"d\":[\"do\",\"re\",\"mi\"],\"ichi\":1,\"e\":\"\",\"b\":\"The content of b\",\"c\":{\"content\":\"The content of c\",\"san\":3},\"ni\":2}}", - jsonobject.toString()); - assertEquals( - "anddoremi1The content of bThe content of c32", - XML.toString(jsonobject)); - jsonarray = JSONML.toJSONArray(string); - assertEquals("[\n" + " \"a\",\n" + " {\n" - + " \"ichi\": 1,\n" + " \"ni\": 2\n" + " },\n" - + " [\n" + " \"b\",\n" - + " \"The content of b\"\n" + " ],\n" - + " \"and\",\n" + " [\n" + " \"c\",\n" - + " {\"san\": 3},\n" + " \"The content of c\"\n" - + " ],\n" + " [\n" + " \"d\",\n" - + " \"do\"\n" + " ],\n" + " [\"e\"],\n" - + " [\n" + " \"d\",\n" + " \"re\"\n" - + " ],\n" + " [\"f\"],\n" + " [\n" - + " \"d\",\n" + " \"mi\"\n" + " ]\n" + "]", - jsonarray.toString(4)); - assertEquals( - "The content of bandThe content of cdoremi", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonArray method using table of contents. - */ - public void testToJsonArray_TableOfContents() - { - try - { - string = "Content of the first chapterContent of the second chapter Content of the first subchapter Content of the second subchapterThird Chapter"; - - jsonarray = JSONML.toJSONArray(string); - assertEquals("[\n" + " \"book\",\n" + " [\n" - + " \"chapter\",\n" - + " \"Content of the first chapter\"\n" + " ],\n" - + " [\n" + " \"chapter\",\n" - + " \"Content of the second chapter\",\n" - + " [\n" + " \"chapter\",\n" - + " \"Content of the first subchapter\"\n" - + " ],\n" + " [\n" + " \"chapter\",\n" - + " \"Content of the second subchapter\"\n" - + " ]\n" + " ],\n" + " [\n" - + " \"chapter\",\n" + " \"Third Chapter\"\n" - + " ]\n" + "]", jsonarray.toString(4)); - assertEquals( - "Content of the first chapterContent of the second chapterContent of the first subchapterContent of the second subchapterThird Chapter", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - - } - - - /** - * Tests the toJsonObject method using message xml. - */ - public void testToJsonObject_MessageXml() - { - try - { - string = "111111111111111"; - jsonobject = JSONML.toJSONObject(string); - assertEquals( - "{\"tagName\":\"Root\",\"childNodes\":[{\"tagName\":\"MsgType\",\"childNodes\":[{\"tagName\":\"BatchType\",\"childNodes\":[111111111111111],\"type\":\"string\"}],\"type\":\"node\"}]}", - jsonobject.toString()); - jsonarray = JSONML.toJSONArray(string); - assertEquals( - "[\"Root\",[\"MsgType\",{\"type\":\"node\"},[\"BatchType\",{\"type\":\"string\"},111111111111111]]]", - jsonarray.toString()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonArray method using table mapping. - */ - public void testToJsonArray_TableMapping() - { - try - { - string = " "; - - jsonarray = JSONML.toJSONArray(string); - assertEquals( - "[\n \"mapping\",\n [\"empty\"],\n [\n \"class\",\n {\"name\": \"Customer\"},\n [\n \"field\",\n {\n \"name\": \"ID\",\n \"type\": \"string\"\n },\n [\n \"bind-xml\",\n {\n \"node\": \"attribute\",\n \"name\": \"ID\"\n }\n ]\n ],\n [\n \"field\",\n {\n \"name\": \"FirstName\",\n \"type\": \"FirstName\"\n }\n ],\n [\n \"field\",\n {\n \"name\": \"MI\",\n \"type\": \"MI\"\n }\n ],\n [\n \"field\",\n {\n \"name\": \"LastName\",\n \"type\": \"LastName\"\n }\n ]\n ],\n [\n \"class\",\n {\"name\": \"FirstName\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ],\n [\n \"class\",\n {\"name\": \"MI\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ],\n [\n \"class\",\n {\"name\": \"LastName\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ]\n]", - jsonarray.toString(4)); - assertEquals( - "", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the constructor method. - */ - public static void testConstructor() - { - JSONML jsonml = new JSONML(); - assertEquals("JSONML", jsonml.getClass().getSimpleName()); - } - - /** - * Tests the toJSONArray method using empty closing tag. - */ - public void testToJSONArray_EmptyClosingTag() - { - - try - { - jsonarray = JSONML.toJSONArray(""); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Expected a closing name instead of '>'.", e.getMessage()); - } - } - - /** - * Tests the toJSONArray method using closing tag with question. - */ - public void testToJSONArray_ClosingTagWithQuestion() - { - - try - { - jsonarray = JSONML.toJSONArray(""); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Misshaped close tag at 11 [character 12 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONArray method using meta tag with two dashes. - */ - public void testToJSONArray_MetaTagWithTwoDashes() - { - - try - { - jsonarray = JSONML.toJSONArray(""); - assertEquals( - "[\"abc\",\">\"]", - jsonarray.toString()); - assertEquals( - ">", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJSONArray method using meta tag with one dash. - */ - public void testToJSONArray_MetaTagWithOneDash() - { - - try - { - jsonarray = JSONML.toJSONArray(""); - assertEquals( - "[\"abc\",\"abc-->\"]", - jsonarray.toString()); - assertEquals( - "abc-->", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJSONArray method using meta tag with cdata. - */ - public void testToJSONArray_MetaTagWithCdata() - { - - try - { - jsonarray = JSONML.toJSONArray("]]>"); - assertEquals( - "[\"abc\",\"<\\/abc>\"]", - jsonarray.toString()); - assertEquals( - "<abc></abc>", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJSONArray method using meta tag with bad cdata. - */ - public void testToJSONArray_MetaTagWithBadCdata() - { - try - { - jsonarray = JSONML.toJSONArray("?]>"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Unclosed CDATA at 35 [character 36 line 1]", e.getMessage()); - } - try - { - jsonarray = JSONML.toJSONArray("]?>"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Unclosed CDATA at 35 [character 36 line 1]", e.getMessage()); - } - try - { - jsonarray = JSONML.toJSONArray("]]>"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Expected 'CDATA[' at 12 [character 13 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONArray method using meta tag with cdata only. - */ - public void testToJSONArray_MetaTagWithCdataOnly() - { - - try - { - jsonarray = JSONML.toJSONArray("]]>"); - assertEquals( - "[\"abc\"]", - jsonarray.toString()); - assertEquals( - "", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJSONArray method using meta tag with broken cdata. - */ - public void testToJSONArray_MetaTagWithBrokenCdata() - { - try - { - jsonarray = JSONML.toJSONArray("]]>"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Bad XML at 23 [character 24 line 1]", e.getMessage()); - } - try - { - jsonarray = JSONML.toJSONArray(""); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Expected 'CDATA[' at 9 [character 10 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONArray method using php tag. - */ - public void testToJSONArray_PhpTag() - { - try - { - jsonarray = JSONML.toJSONArray(""); - assertEquals( - "[\"abc\"]", - jsonarray.toString()); - assertEquals( - "", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJSONArray method using misshaped tag. - */ - public void testToJSONArray_MisshapedTag() - { - try - { - jsonarray = JSONML.toJSONArray("<=abcde?>"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Misshaped tag at 7 [character 8 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONObject method using reserved attribute tag name. - */ - public void testToJSONObject_ReservedAttributeTagName() - { - try - { - jsonobject = JSONML.toJSONObject("def"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Reserved attribute. at 12 [character 13 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONObject method using reserved attribute child node. - */ - public void testToJSONObject_ReservedAttributeChildNode() - { - try - { - jsonobject = JSONML.toJSONObject("def"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Reserved attribute. at 14 [character 15 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONObject method using no value attribute. - */ - public void testToJSONObject_NoValueAttribute() - { - try - { - jsonobject = JSONML.toJSONObject("def"); - assertEquals( - "{\"novalue\":\"\",\"tagName\":\"abc\",\"childNodes\":[\"def\"]}", - jsonobject.toString()); - assertEquals( - "def", - JSONML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJSONObject method using no value attribute with equals. - */ - public void testToJSONObject_NoValueAttributeWithEquals() - { - try - { - jsonobject = JSONML.toJSONObject("def"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Missing value at 14 [character 15 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONObject method using empty tag. - */ - public void testToJSONObject_EmptyTag() - { - try - { - jsonobject = JSONML.toJSONObject(""); - assertEquals( - "{\"tagName\":\"abc\"}", - jsonobject.toString()); - assertEquals( - "", - JSONML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJSONArray method using empty tag. - */ - public void testToJSONArray_EmptyTag() - { - try - { - jsonarray = JSONML.toJSONArray(""); - assertEquals( - "[\"abc\"]", - jsonarray.toString()); - assertEquals( - "", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJSONObject method using broken empty tag. - */ - public void testToJSONObject_BrokenEmptyTag() - { - try - { - jsonobject = JSONML.toJSONObject(""); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Misshaped tag at 11 [character 12 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONObject method using misshaped tag. - */ - public void testToJSONObject_MisshapedTag() - { - try - { - jsonobject = JSONML.toJSONObject(""); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Bad XML at 6 [character 7 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONObject method using no name tag. - */ - public void testToJSONObject_NoNameTag() - { - try - { - jsonobject = JSONML.toJSONObject("<>"); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Misshaped tag at 2 [character 3 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONObject method using space. - */ - public void testToJSONObject_Space() - { - try - { - jsonobject = JSONML.toJSONObject(" "); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Bad XML at 3 [character 4 line 1]", e.getMessage()); - } - } - - /** - * Tests the toJSONObject method using space content. - */ - public void testToJSONObject_SpaceContent() - { - try - { - jsonobject = JSONML.toJSONObject(" "); - assertEquals( - "{\"tagName\":\"abc\"}", - jsonobject.toString()); - assertEquals( - "", - JSONML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using json array of json objects. - */ - public void testToString_JsonArrayOfJsonObjects() - { - try - { - jsonarray = new JSONArray(); - jsonarray.put("tagName"); - jsonarray.put(new JSONObject().put("tagName", "myName")); - jsonarray.put(new JSONObject().put("tagName", "otherName")); - assertEquals( - "", - JSONML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using json object of json arrays. - */ - public void testToString_JsonObjectOfJsonArrays() - { - try - { - jsonobject = new JSONObject(); - jsonobject.put("tagName", "MyName"); - jsonobject.put("childNodes", new JSONArray().put("abc").put(new JSONArray().put("def"))); - jsonobject.put("123", new JSONArray("[\"abc\"]")); - assertEquals( - "abc", - JSONML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - -} diff --git a/tests/TestJSONObject.java b/tests/TestJSONObject.java deleted file mode 100644 index 5aad860..0000000 --- a/tests/TestJSONObject.java +++ /dev/null @@ -1,2507 +0,0 @@ -/* - * File: TestJSONObject.java Author: JSON.org - */ -package org.json.tests; - -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Stack; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.json.JSONString; -import org.json.XML; - -import junit.framework.TestCase; - -/** - * The Class TestJSONObject. - */ -public class TestJSONObject extends TestCase -{ - - /** - * The Class GoodJsonString. - */ - public class GoodJsonString implements JSONString - { - - /* - * (non-Javadoc) - * - * @see org.json.JSONString#toJSONString() - */ - @Override - public String toJSONString() - { - return "jsonstring"; - } - - } - - /** - * The Class NullJsonString. - */ - public class NullJsonString implements JSONString - { - - /* - * (non-Javadoc) - * - * @see org.json.JSONString#toJSONString() - */ - @Override - public String toJSONString() - { - return null; - } - - } - - /** - * The Class BadJsonString. - */ - public class BadJsonString implements JSONString - { - - /* - * (non-Javadoc) - * - * @see org.json.JSONString#toJSONString() - */ - @Override - public String toJSONString() - { - String[] arString = new String[] - { - "abc" - }; - return arString[1]; - } - - } - - /** - * The Class ObjectWithPrimatives. - */ - public class ObjectWithPrimatives - { - - /** The i. */ - public int i; - - /** The null string. */ - private String nullString; - - /** The j. */ - private String j; - - /** The k. */ - private double k; - - /** The l. */ - private long l; - - /** The m. */ - public boolean m; - - /** - * Instantiates a new object with primatives. - */ - public ObjectWithPrimatives() - { - i = 3; - j = "3"; - k = 10.03; - l = 5748548957230984584L; - m = true; - nullString = null; - } - - /** - * Gets the i. - * - * @return the i - */ - public int getI() - { - return i; - } - - /** - * Gets the j. - * - * @return the j - */ - public String getJ() - { - return j; - } - - /** - * Gets the k. - * - * @return the k - */ - public double getK() - { - return k; - } - - /** - * Gets the l. - * - * @return the l - */ - public long getL() - { - return l; - } - - /** - * Gets the m. - * - * @return the m - */ - public boolean getM() - { - return m; - } - - /** - * Gets the m. - * - * @param test - * the test - * @return the m - */ - public boolean getM(Boolean test) - { - return m; - } - - /** - * Gets the null string. - * - * @return the null string - */ - public String getNullString() - { - return nullString; - } - - /** - * Gets the zERO. - * - * @return the zERO - */ - public int getZERO() - { - return 0; - } - - /** - * Gets the one. - * - * @return the one - */ - public int getone() - { - return 1; - } - - /** - * Checks if is big. - * - * @return true, if is big - */ - public boolean isBig() - { - return false; - } - - /** - * Checks if is small. - * - * @return true, if is small - */ - @SuppressWarnings("unused") - private boolean isSmall() - { - return true; - } - - } - - /** - * The Class ObjectWithPrimativesExtension. - */ - public class ObjectWithPrimativesExtension extends ObjectWithPrimatives - { - // Same Object - } - - /** The jsonobject. */ - JSONObject jsonobject = new JSONObject(); - - /** The iterator. */ - Iterator iterator; - - /** The jsonarray. */ - JSONArray jsonarray; - - /** The object. */ - Object object; - - /** The string. */ - String string; - - /** The eps. */ - double eps = 2.220446049250313e-16; - - /** - * Tests the null method. - * - * @throws Exception - * the exception - */ - public void testNull() throws Exception - { - jsonobject = new JSONObject("{\"message\":\"null\"}"); - assertFalse(jsonobject.isNull("message")); - assertEquals("null", jsonobject.getString("message")); - - jsonobject = new JSONObject("{\"message\":null}"); - assertTrue(jsonobject.isNull("message")); - } - - /** - * Tests the constructor method using duplicate key. - */ - public void testConstructor_DuplicateKey() - { - try - { - string = "{\"koda\": true, \"koda\": true}"; - jsonobject = new JSONObject(string); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("Duplicate key \"koda\"", jsone.getMessage()); - } - } - - /** - * Tests the constructor method using null key. - */ - public void testConstructor_NullKey() - { - try - { - jsonobject.put(null, "howard"); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("Null key.", jsone.getMessage()); - } - } - - /** - * Tests the getDouble method using invalid key howard. - */ - public void testGetDouble_InvalidKeyHoward() - { - try - { - jsonobject.getDouble("howard"); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("JSONObject[\"howard\"] not found.", - jsone.getMessage()); - } - } - - /** - * Tests the getDouble method using invalid key stooge. - */ - public void testGetDouble_InvalidKeyStooge() - { - jsonobject = new JSONObject(); - try - { - jsonobject.getDouble("stooge"); - fail("expecting JSONException here."); - } catch (JSONException jsone) - { - assertEquals("JSONObject[\"stooge\"] not found.", - jsone.getMessage()); - } - } - - /** - * Tests the isNull method. - */ - public void testIsNull() - { - try - { - jsonobject = new JSONObject(); - object = null; - jsonobject.put("booga", object); - jsonobject.put("wooga", JSONObject.NULL); - assertEquals("{\"wooga\":null}", jsonobject.toString()); - assertTrue(jsonobject.isNull("booga")); - } catch (JSONException e) - { - fail(e.toString()); - } - } - - /** - * Tests the increment method. - */ - public void testIncrement_NewKey() - { - try - { - jsonobject = new JSONObject(); - jsonobject.increment("two"); - jsonobject.increment("two"); - assertEquals("{\"two\":2}", jsonobject.toString()); - assertEquals(2, jsonobject.getInt("two")); - } catch (JSONException e) - { - fail(e.toString()); - } - } - - /** - * Tests the toString method using listof lists. - */ - public void testToString_ListofLists() - { - try - { - string = "{ \"list of lists\" : [ [1, 2, 3], [4, 5, 6], ] }"; - jsonobject = new JSONObject(string); - assertEquals("{\"list of lists\": [\n" + " [\n" + " 1,\n" - + " 2,\n" + " 3\n" + " ],\n" + " [\n" - + " 4,\n" + " 5,\n" + " 6\n" - + " ]\n" + "]}", jsonobject.toString(4)); - } catch (JSONException e) - { - fail(e.toString()); - } - } - - /** - * Tests the toString method using indentation. - */ - public void testToString_Indentation() - { - try - { - string = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }"; - jsonobject = new JSONObject(string); - assertEquals( - "{\"entity\": {\n \"id\": 12336,\n \"averageRating\": null,\n \"ratingCount\": null,\n \"name\": \"IXXXXXXXXXXXXX\",\n \"imageURL\": \"\"\n}}", - jsonobject.toString(2)); - } catch (JSONException e) - { - fail(e.toString()); - } - } - - /** - * Tests the multipleThings method. - */ - public void testMultipleThings() - { - try - { - jsonobject = new JSONObject( - "{foo: [true, false,9876543210, 0.0, 1.00000001, 1.000000000001, 1.00000000000000001," - + " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], " - + " to : null, op : 'Good'," - + "ten:10} postfix comment"); - jsonobject.put("String", "98.6"); - jsonobject.put("JSONObject", new JSONObject()); - jsonobject.put("JSONArray", new JSONArray()); - jsonobject.put("int", 57); - jsonobject.put("double", 123456789012345678901234567890.); - jsonobject.put("true", true); - jsonobject.put("false", false); - jsonobject.put("null", JSONObject.NULL); - jsonobject.put("bool", "true"); - jsonobject.put("zero", -0.0); - jsonobject.put("\\u2028", "\u2028"); - jsonobject.put("\\u2029", "\u2029"); - jsonarray = jsonobject.getJSONArray("foo"); - jsonarray.put(666); - jsonarray.put(2001.99); - jsonarray.put("so \"fine\"."); - jsonarray.put("so ."); - jsonarray.put(true); - jsonarray.put(false); - jsonarray.put(new JSONArray()); - jsonarray.put(new JSONObject()); - jsonobject.put("keys", JSONObject.getNames(jsonobject)); - assertEquals( - "{\n \"to\": null,\n \"ten\": 10,\n \"JSONObject\": {},\n \"JSONArray\": [],\n \"op\": \"Good\",\n \"keys\": [\n \"to\",\n \"ten\",\n \"JSONObject\",\n \"JSONArray\",\n \"op\",\n \"int\",\n \"true\",\n \"foo\",\n \"zero\",\n \"double\",\n \"String\",\n \"false\",\n \"bool\",\n \"\\\\u2028\",\n \"\\\\u2029\",\n \"null\"\n ],\n \"int\": 57,\n \"true\": true,\n \"foo\": [\n true,\n false,\n 9876543210,\n 0,\n 1.00000001,\n 1.000000000001,\n 1,\n 1.0E-17,\n 2,\n 0.1,\n 2.0E100,\n -32,\n [],\n {},\n \"string\",\n 666,\n 2001.99,\n \"so \\\"fine\\\".\",\n \"so .\",\n true,\n false,\n [],\n {}\n ],\n \"zero\": -0,\n \"double\": 1.2345678901234568E29,\n \"String\": \"98.6\",\n \"false\": false,\n \"bool\": \"true\",\n \"\\\\u2028\": \"\\u2028\",\n \"\\\\u2029\": \"\\u2029\",\n \"null\": null\n}", - jsonobject.toString(4)); - assertEquals( - "null10GoodtotenJSONObjectJSONArrayopinttruefoozerodoubleStringfalsebool\\u2028\\u2029null57truetruefalse98765432100.01.000000011.0000000000011.01.0E-172.00.12.0E100-32string6662001.99so "fine".so <fine>.truefalse-0.01.2345678901234568E2998.6falsetrue<\\u2028>\u2028<\\u2029>\u2029null", - XML.toString(jsonobject)); - assertEquals(98.6d, jsonobject.getDouble("String"), eps); - assertTrue(jsonobject.getBoolean("bool")); - assertEquals( - "[true,false,9876543210,0,1.00000001,1.000000000001,1,1.0E-17,2,0.1,2.0E100,-32,[],{},\"string\",666,2001.99,\"so \\\"fine\\\".\",\"so .\",true,false,[],{}]", - jsonobject.getJSONArray("foo").toString()); - assertEquals("Good", jsonobject.getString("op")); - assertEquals(10, jsonobject.getInt("ten")); - assertFalse(jsonobject.optBoolean("oops")); - } catch (JSONException e) - { - fail(e.toString()); - } - } - - /** - * Tests the multipleThings2 method. - */ - public void testMultipleThings2() - { - try - { - jsonobject = new JSONObject( - "{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}"); - assertEquals( - "{\n \"int\": 2147483647,\n \"string\": \"98.6\",\n \"longer\": 9223372036854775807,\n \"double\": \"9223372036854775808\",\n \"long\": 2147483648\n}", - jsonobject.toString(1)); - - // getInt - assertEquals(2147483647, jsonobject.getInt("int")); - assertEquals(-2147483648, jsonobject.getInt("long")); - assertEquals(-1, jsonobject.getInt("longer")); - try - { - jsonobject.getInt("double"); - fail("should fail with - JSONObject[\"double\"] is not an int."); - } catch (JSONException expected) - { - assertEquals("JSONObject[\"double\"] is not an int.", - expected.getMessage()); - } - try - { - jsonobject.getInt("string"); - fail("should fail with - JSONObject[\"string\"] is not an int."); - } catch (JSONException expected) - { - assertEquals("JSONObject[\"string\"] is not an int.", - expected.getMessage()); - } - - // getLong - assertEquals(2147483647, jsonobject.getLong("int")); - assertEquals(2147483648l, jsonobject.getLong("long")); - assertEquals(9223372036854775807l, jsonobject.getLong("longer")); - try - { - jsonobject.getLong("double"); - fail("should fail with - JSONObject[\"double\"] is not a long."); - } catch (JSONException expected) - { - assertEquals("JSONObject[\"double\"] is not a long.", - expected.getMessage()); - } - try - { - jsonobject.getLong("string"); - fail("should fail with - JSONObject[\"string\"] is not a long."); - } catch (JSONException expected) - { - assertEquals("JSONObject[\"string\"] is not a long.", - expected.getMessage()); - } - - // getDouble - assertEquals(2.147483647E9, jsonobject.getDouble("int"), eps); - assertEquals(2.147483648E9, jsonobject.getDouble("long"), eps); - assertEquals(9.223372036854776E18, jsonobject.getDouble("longer"), - eps); - assertEquals(9223372036854775808d, jsonobject.getDouble("double"), - eps); - assertEquals(98.6, jsonobject.getDouble("string"), eps); - - jsonobject.put("good sized", 9223372036854775807L); - assertEquals( - "{\n \"int\": 2147483647,\n \"string\": \"98.6\",\n \"longer\": 9223372036854775807,\n \"good sized\": 9223372036854775807,\n \"double\": \"9223372036854775808\",\n \"long\": 2147483648\n}", - jsonobject.toString(1)); - - jsonarray = new JSONArray( - "[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]"); - assertEquals( - "[\n 2147483647,\n 2147483648,\n 9223372036854775807,\n \"9223372036854775808\"\n]", - jsonarray.toString(1)); - - List expectedKeys = new ArrayList(6); - expectedKeys.add("int"); - expectedKeys.add("string"); - expectedKeys.add("longer"); - expectedKeys.add("good sized"); - expectedKeys.add("double"); - expectedKeys.add("long"); - - iterator = jsonobject.keys(); - while (iterator.hasNext()) - { - string = iterator.next(); - assertTrue(expectedKeys.remove(string)); - } - assertEquals(0, expectedKeys.size()); - } catch (JSONException e) - { - fail(e.toString()); - } - } - - /** - * Tests the put method using collection and map. - */ - public void testPut_CollectionAndMap() - { - try - { - string = "{plist=Apple; AnimalSmells = { pig = piggish; lamb = lambish; worm = wormy; }; AnimalSounds = { pig = oink; lamb = baa; worm = baa; Lisa = \"Why is the worm talking like a lamb?\" } ; AnimalColors = { pig = pink; lamb = black; worm = pink; } } "; - jsonobject = new JSONObject(string); - assertEquals( - "{\"AnimalColors\":{\"worm\":\"pink\",\"lamb\":\"black\",\"pig\":\"pink\"},\"plist\":\"Apple\",\"AnimalSounds\":{\"worm\":\"baa\",\"Lisa\":\"Why is the worm talking like a lamb?\",\"lamb\":\"baa\",\"pig\":\"oink\"},\"AnimalSmells\":{\"worm\":\"wormy\",\"lamb\":\"lambish\",\"pig\":\"piggish\"}}", - jsonobject.toString()); - - Collection collection = null; - Map map = null; - - jsonobject = new JSONObject(map); - jsonarray = new JSONArray(collection); - jsonobject.append("stooge", "Joe DeRita"); - jsonobject.append("stooge", "Shemp"); - jsonobject.accumulate("stooges", "Curly"); - jsonobject.accumulate("stooges", "Larry"); - jsonobject.accumulate("stooges", "Moe"); - jsonobject.accumulate("stoogearray", jsonobject.get("stooges")); - jsonobject.put("map", map); - jsonobject.put("collection", collection); - jsonobject.put("array", jsonarray); - jsonarray.put(map); - jsonarray.put(collection); - assertEquals( - "{\"stooge\":[\"Joe DeRita\",\"Shemp\"],\"map\":{},\"stooges\":[\"Curly\",\"Larry\",\"Moe\"],\"collection\":[],\"stoogearray\":[[\"Curly\",\"Larry\",\"Moe\"]],\"array\":[{},[]]}", - jsonobject.toString()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the accumulate method. - */ - public void testAccumulate() - { - try - { - jsonobject = new JSONObject(); - jsonobject.accumulate("stooge", "Curly"); - jsonobject.accumulate("stooge", "Larry"); - jsonobject.accumulate("stooge", "Moe"); - jsonarray = jsonobject.getJSONArray("stooge"); - jsonarray.put(5, "Shemp"); - assertEquals("{\"stooge\": [\n" + " \"Curly\",\n" - + " \"Larry\",\n" + " \"Moe\",\n" + " null,\n" - + " null,\n" + " \"Shemp\"\n" + "]}", - jsonobject.toString(4)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the write method. - */ - public void testWrite() - { - try - { - jsonobject = new JSONObject(); - jsonobject.accumulate("stooge", "Curly"); - jsonobject.accumulate("stooge", "Larry"); - jsonobject.accumulate("stooge", "Moe"); - jsonarray = jsonobject.getJSONArray("stooge"); - jsonarray.put(5, "Shemp"); - assertEquals( - "{\"stooge\":[\"Curly\",\"Larry\",\"Moe\",null,null,\"Shemp\"]}", - jsonobject.write(new StringWriter()).toString()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using html. - */ - public void testToString_Html() - { - try - { - jsonobject = new JSONObject( - "{script: 'It is not allowed in HTML to send a close script tag in a stringso we insert a backslash before the /'}"); - assertEquals( - "{\"script\":\"It is not allowed in HTML to send a close script tag in a string', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}"); - assertEquals( - "{\n \"quotes\": [\n \"'\",\n \"\\\"\"\n ],\n \"slashes\": \"///\",\n \"ei\": {\"quotes\": \"\\\"'\"},\n \"eo\": {\n \"b\": \"don't\",\n \"a\": \"\\\"quoted\\\"\"\n },\n \"closetag\": \"<\\/script>\",\n \"backslash\": \"\\\\\"\n}", - jsonobject.toString(2)); - assertEquals( - "'"///"'don't"quoted"</script>\\", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonObject method using json information. - */ - public void testToJsonObject_JsonInformation() - { - try - { - string = "First \u0009<content> This is \"content\". 3 JSON does not preserve the sequencing of elements and contents. III T H R E EContent text is an implied structure in XML. JSON does not have implied structure:7everything is explicit.!]]>"; - jsonobject = XML.toJSONObject(string); - assertEquals( - "{\"xml\": {\n \"content\": [\n \"First \\t\",\n \"This is \\\"content\\\".\",\n \"JSON does not preserve the sequencing of elements and contents.\",\n \"Content text is an implied structure in XML.\",\n \"JSON does not have implied structure:\",\n \"everything is explicit.\",\n \"CDATA blocks!\"\n ],\n \"two\": \" \\\"2\\\" \",\n \"seven\": 7,\n \"five\": [\n \"\",\n \"\"\n ],\n \"one\": 1,\n \"three\": [\n 3,\n \"III\",\n \"T H R E E\"\n ],\n \"four\": \"\",\n \"six\": {\"content\": 6}\n}}", - jsonobject.toString(2)); - assertEquals( - "First \t<content>\n" - + "This is "content".\n" - + "JSON does not preserve the sequencing of elements and contents.\n" - + "Content text is an implied structure in XML.\n" - + "JSON does not have implied structure:\n" - + "everything is explicit.\n" - + "CDATA blocks<are><supported>! "2" 713IIIT H R E E6", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using json array of int array. - */ - public void testToString_JsonArrayOfIntArray() - { - try - { - int ar[] = - { 1, 2, 3 }; - jsonarray = new JSONArray(ar); - assertEquals("[1,2,3]", jsonarray.toString()); - assertEquals("123", - XML.toString(ar)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using table mapping. - */ - public void testToString_TableMapping() - { - try - { - string = " "; - jsonobject = XML.toJSONObject(string); - - assertEquals( - "{\"mapping\": {\n \"empty\": \"\",\n \"class\": [\n {\n \"field\": [\n {\n \"bind-xml\": {\n \"node\": \"attribute\",\n \"name\": \"ID\"\n },\n \"name\": \"ID\",\n \"type\": \"string\"\n },\n {\n \"name\": \"FirstName\",\n \"type\": \"FirstName\"\n },\n {\n \"name\": \"MI\",\n \"type\": \"MI\"\n },\n {\n \"name\": \"LastName\",\n \"type\": \"LastName\"\n }\n ],\n \"name\": \"Customer\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"FirstName\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"MI\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"LastName\"\n }\n ]\n}}", - jsonobject.toString(2)); - assertEquals( - "attributeIDIDstringFirstNameFirstNameMIMILastNameLastNameCustomertexttexttextFirstNametexttexttextMItexttexttextLastName", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using book info. - */ - public void testToString_BookInfo() - { - try - { - jsonobject = XML - .toJSONObject("Sample BookThis is chapter 1. It is not very long or interesting.This is chapter 2. Although it is longer than chapter 1, it is not any more interesting."); - assertEquals( - "{\"Book\": {\n \"Chapter\": [\n {\n \"content\": \"This is chapter 1. It is not very long or interesting.\",\n \"id\": 1\n },\n {\n \"content\": \"This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.\",\n \"id\": 2\n }\n ],\n \"Author\": \"Anonymous\",\n \"Title\": \"Sample Book\"\n}}", - jsonobject.toString(2)); - assertEquals( - "This is chapter 1. It is not very long or interesting.1This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.2AnonymousSample Book", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonObject method using table of contents. - */ - public void testToJsonObject_TableOfContents() - { - try - { - string = "Content of the first chapterContent of the second chapter Content of the first subchapter Content of the second subchapterThird Chapter"; - jsonobject = XML.toJSONObject(string); - assertEquals( - "{\"book\": {\"chapter\": [\n \"Content of the first chapter\",\n {\n \"content\": \"Content of the second chapter\",\n \"chapter\": [\n \"Content of the first subchapter\",\n \"Content of the second subchapter\"\n ]\n },\n \"Third Chapter\"\n]}}", - jsonobject.toString(1)); - assertEquals( - "Content of the first chapterContent of the second chapterContent of the first subchapterContent of the second subchapterThird Chapter", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonObject method using simple xml. - */ - public void testToJsonObject_SimpleXml() - { - try - { - string = "122333"; - jsonobject = XML.toJSONObject(string); - assertEquals("{\"xml\": {\n" + " \"a\": [\n" + " \"\",\n" - + " 1,\n" + " 22,\n" + " 333\n" - + " ],\n" + " \"empty\": \"\"\n" + "}}", - jsonobject.toString(4)); - assertEquals("122333", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonObject method using html escapes. - */ - public void testToJsonObject_HtmlEscapes() - { - try - { - jsonobject = XML - .toJSONObject("deluxe&"toot"&toot;Aeksbonusbonus2"); - assertEquals( - "{\"test\": {\n \"w\": [\n \"bonus\",\n \"bonus2\"\n ],\n \"content\": \"deluxe\",\n \"intertag\": \"\",\n \"status\": \"ok\",\n \"blip\": {\n \"content\": \"&\\\"toot\\\"&toot;A\",\n \"sweet\": true\n },\n \"empty\": \"\",\n \"zero\": 0,\n \"x\": \"eks\"\n}}", - jsonobject.toString(2)); - assertEquals( - "bonusbonus2deluxeok&"toot"&toot;&#x41;true0eks", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonObject method using phone book. - */ - public void testToJsonObject_PhoneBook() - { - try - { - jsonobject = XML - .toJSONObject(""); - assertEquals( - "{\"bCard\": {\"bCard\": [\n {\n \"email\": \"khare@mci.net\",\n \"company\": \"MCI\",\n \"lastname\": \"Khare\",\n \"firstname\": \"Rohit\",\n \"homepage\": \"http://pest.w3.org/\"\n },\n {\n \"email\": \"adam@cs.caltech.edu\",\n \"company\": \"Caltech Infospheres Project\",\n \"lastname\": \"Rifkin\",\n \"firstname\": \"Adam\",\n \"homepage\": \"http://www.cs.caltech.edu/~adam/\"\n }\n]}}", - jsonobject.toString(2)); - assertEquals( - "khare@mci.netMCIKhareRohithttp://pest.w3.org/adam@cs.caltech.eduCaltech Infospheres ProjectRifkinAdamhttp://www.cs.caltech.edu/~adam/", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonObject method using customer info. - */ - public void testToJsonObject_CustomerInfo() - { - try - { - jsonobject = XML - .toJSONObject(" Fred fbs0001 Scerbo B "); - assertEquals( - "{\"customer\": {\n \"lastName\": {\"text\": \"Scerbo\"},\n \"MI\": {\"text\": \"B\"},\n \"ID\": \"fbs0001\",\n \"firstName\": {\"text\": \"Fred\"}\n}}", - jsonobject.toString(2)); - assertEquals( - "ScerboBfbs0001Fred", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toJsonObject method using library catalog. - */ - public void testToJsonObject_LibraryCatalog() - { - try - { - jsonobject = XML - .toJSONObject("Repository Address Special Collections LibraryABC UniversityMain Library, 40 Circle DriveOurtown, Pennsylvania17654 USA"); - assertEquals( - "{\"list\":{\"item\":[\"Special Collections Library\",\"ABC University\",\"Main Library, 40 Circle Drive\",\"Ourtown, Pennsylvania\",\"17654 USA\"],\"head\":\"Repository Address\",\"type\":\"simple\"}}", - jsonobject.toString()); - assertEquals( - "Special Collections LibraryABC UniversityMain Library, 40 Circle DriveOurtown, Pennsylvania17654 USARepository Addresssimple", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using xml within xml. - */ - public void testToString_XmlWithinXml() - { - try{ - jsonarray = new JSONArray( - " [\"\", next is an implied null , , ok,] "); - assertEquals("[\"\",\"next is an implied null\",null,\"ok\"]", - jsonarray.toString()); - assertEquals( - "<escape>next is an implied nullnullok", - XML.toString(jsonarray)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using email. - */ - public void testToString_Email() - { - try{ - jsonobject = new JSONObject( - "{Envelope: {Body: {\"ns1:doGoogleSearch\": {oe: \"latin1\", filter: true, q: \"'+search+'\", key: \"GOOGLEKEY\", maxResults: 10, \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\", start: 0, ie: \"latin1\", safeSearch:false, \"xmlns:ns1\": \"urn:GoogleSearch\"}}}}"); - assertEquals( - "{\"Envelope\": {\"Body\": {\"ns1:doGoogleSearch\": {\n \"oe\": \"latin1\",\n \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n \"start\": 0,\n \"q\": \"'+search+'\",\n \"ie\": \"latin1\",\n \"safeSearch\": false,\n \"xmlns:ns1\": \"urn:GoogleSearch\",\n \"maxResults\": 10,\n \"key\": \"GOOGLEKEY\",\n \"filter\": true\n}}}}", - jsonobject.toString(2)); - assertEquals( - "latin1http://schemas.xmlsoap.org/soap/encoding/0'+search+'latin1falseurn:GoogleSearch10GOOGLEKEYtrue", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the toString method using http header. - */ - public void testToString_HttpHeader() - { - try{ - jsonobject = new JSONObject( - "{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}"); - assertEquals( - "{\n \"Request-URI\": \"/\",\n \"nix\": null,\n \"nux\": false,\n \"Method\": \"GET\",\n \"HTTP-Version\": \"HTTP/1.0\",\n \"null\": \"null\"\n}", - jsonobject.toString(2)); - assertTrue(jsonobject.isNull("nix")); - assertTrue(jsonobject.has("nix")); - assertEquals( - "/nullfalseGETHTTP/1.0null", - XML.toString(jsonobject)); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - -} diff --git a/tests/TestXMLTokener.java b/tests/TestXMLTokener.java deleted file mode 100644 index f28af76..0000000 --- a/tests/TestXMLTokener.java +++ /dev/null @@ -1,473 +0,0 @@ -/* - * File: TestXMLTokener.java - * Author: JSON.org - */ -package org.json.tests; - -import org.json.JSONException; -import org.json.XMLTokener; - -import junit.framework.TestCase; - -/** - * The Class TestXMLTokener. - */ -public class TestXMLTokener extends TestCase -{ - - /** The xmltokener. */ - private XMLTokener xmltokener; - - /** - * Tests the nextContent method. - */ - public void testNextContent() - { - try - { - xmltokener = new XMLTokener("< abc>"); - assertEquals('<', xmltokener.nextContent()); - assertEquals("abc>", xmltokener.nextContent()); - assertEquals('<', xmltokener.nextContent()); - assertEquals("de f/>", xmltokener.nextContent()); - assertEquals('<', xmltokener.nextContent()); - assertEquals("/abc>", xmltokener.nextContent()); - assertEquals(null, xmltokener.nextContent()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the nextCdata method. - */ - public void testNextCdata() - { - try - { - xmltokener = new XMLTokener("<[CDATA[]]>"); - assertEquals('<', xmltokener.next('<')); - assertEquals('[', xmltokener.next('[')); - assertEquals("CDATA", xmltokener.nextToken()); - assertEquals('[', xmltokener.next('[')); - assertEquals("", xmltokener.nextCDATA()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the nextCdata method using broken cdata. - */ - public void testNextCdata_BrokenCdata1() - { - try - { - xmltokener = new XMLTokener("<[CDATA[]>"); - assertEquals('<', xmltokener.next('<')); - assertEquals('[', xmltokener.next('[')); - assertEquals("CDATA", xmltokener.nextToken()); - assertEquals('[', xmltokener.next('[')); - xmltokener.nextCDATA(); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Unclosed CDATA at 22 [character 23 line 1]", e.getMessage()); - } - } - - /** - * Tests the nextCdata method using broken cdata. - */ - public void testNextCdata_BrokenCdata2() - { - try - { - xmltokener = new XMLTokener("<[CDATA[]]"); - assertEquals('<', xmltokener.next('<')); - assertEquals('[', xmltokener.next('[')); - assertEquals("CDATA", xmltokener.nextToken()); - assertEquals('[', xmltokener.next('[')); - xmltokener.nextCDATA(); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Unclosed CDATA at 22 [character 23 line 1]", e.getMessage()); - } - } - - /** - * Tests the nextCdata method using broken cdata. - */ - public void testNextCdata_BrokenCdata3() - { - try - { - xmltokener = new XMLTokener("<[CDATA[]]"); - assertEquals('<', xmltokener.next('<')); - assertEquals('[', xmltokener.next('[')); - assertEquals("CDATA", xmltokener.nextToken()); - assertEquals('[', xmltokener.next('[')); - xmltokener.nextCDATA(); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Unclosed CDATA at 22 [character 23 line 1]", e.getMessage()); - } - } - - /** - * Tests the nextCdata method using broken cdata. - */ - public void testNextCdata_BrokenCdata4() - { - try - { - xmltokener = new XMLTokener("<[CDATA["); - assertEquals('<', xmltokener.next('<')); - assertEquals('[', xmltokener.next('[')); - assertEquals("CDATA", xmltokener.nextToken()); - assertEquals('[', xmltokener.next('[')); - xmltokener.nextCDATA(); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Unclosed CDATA at 15 [character 16 line 1]", e.getMessage()); - } - } - - /** - * Tests the nextEntity method using ampersand. - */ - public void testNextEntity_Ampersand() - { - try - { - xmltokener = new XMLTokener("<&>"); - assertEquals('<', xmltokener.next('<')); - assertEquals('&', xmltokener.next('&')); - assertEquals('&', xmltokener.nextEntity('&')); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the nextEntity method using number entity. - */ - public void testNextEntity_NumberEntity() - { - try - { - xmltokener = new XMLTokener("<<>"); - assertEquals('<', xmltokener.next('<')); - assertEquals('&', xmltokener.next('&')); - assertEquals("<", xmltokener.nextEntity('&')); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the nextEntity method using broken entity. - */ - public void testNextEntity_BrokenEntity() - { - try - { - xmltokener = new XMLTokener("< "); - assertEquals('<', xmltokener.next('<')); - assertEquals('&', xmltokener.next('&')); - assertEquals("<", xmltokener.nextEntity('&')); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Missing ';' in XML entity:   at 7 [character 8 line 1]", e.getMessage()); - } - } - - /** - * Tests the nextMeta method using string. - */ - public void testNextMeta_String() - { - try - { - xmltokener = new XMLTokener(""); - assertEquals('<', xmltokener.next('<')); - assertEquals('!', xmltokener.next('!')); - assertEquals(true, xmltokener.nextMeta()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the nextMeta method using open string. - */ - public void testNextMeta_OpenString() - { - try - { - xmltokener = new XMLTokener(""); - assertEquals('<', xmltokener.next('<')); - assertEquals('!', xmltokener.next('!')); - xmltokener.nextMeta(); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Unterminated string at 16 [character 17 line 1]", e.getMessage()); - } - } - - /** - * Tests the nextMeta method using symbols. - */ - public void testNextMeta_Symbols() - { - try - { - xmltokener = new XMLTokener("/=!?>"); - assertEquals('<', xmltokener.next('<')); - assertEquals('!', xmltokener.next('!')); - assertEquals('<', xmltokener.nextMeta()); - assertEquals('>', xmltokener.nextMeta()); - assertEquals('/', xmltokener.nextMeta()); - assertEquals('=', xmltokener.nextMeta()); - assertEquals('!', xmltokener.nextMeta()); - assertEquals('?', xmltokener.nextMeta()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the nextMeta method using misshaped. - */ - public void testNextMeta_Misshaped() - { - try - { - xmltokener = new XMLTokener(""); - assertEquals('<', xmltokener.next('<')); - assertEquals("da", xmltokener.nextToken()); - assertEquals("ta", xmltokener.nextToken()); - assertEquals('>', xmltokener.nextToken()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the nextToken method using tag with bad character. - */ - public void testNextToken_TagWithBadCharacter() - { - try - { - xmltokener = new XMLTokener(""); - assertEquals('<', xmltokener.next('<')); - xmltokener.nextToken(); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Bad character in a name at 4 [character 5 line 1]", e.getMessage()); - } - } - - /** - * Tests the nextToken method using tag with misplaced less than. - */ - public void testNextToken_TagWithMisplacedLessThan() - { - try - { - xmltokener = new XMLTokener("<"); - assertEquals('<', xmltokener.next('<')); - xmltokener.nextToken(); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Misplaced '<' at 2 [character 3 line 1]", e.getMessage()); - } - } - - /** - * Tests the nextToken method using misshaped element. - */ - public void testNextToken_MisshapedElement() - { - try - { - xmltokener = new XMLTokener("<"); - assertEquals('<', xmltokener.next('<')); - xmltokener.nextToken(); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Misshaped element at 2 [character 3 line 1]", e.getMessage()); - } - } - - /** - * Tests the nextToken method using symbols. - */ - public void testNextToken_Symbols() - { - try - { - xmltokener = new XMLTokener("< /=!?"); - assertEquals('<', xmltokener.next('<')); - assertEquals('/', xmltokener.nextToken()); - assertEquals('=', xmltokener.nextToken()); - assertEquals('!', xmltokener.nextToken()); - assertEquals('?', xmltokener.nextToken()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the nextToken method using string. - */ - public void testNextToken_String() - { - try - { - xmltokener = new XMLTokener("<\"abc&123\">"); - assertEquals('<', xmltokener.next('<')); - assertEquals("abc&123", xmltokener.nextToken()); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the nextToken method using no greater than. - */ - public void testNextToken_NoGreaterThan() - { - try - { - xmltokener = new XMLTokener(""); - assertEquals('<', xmltokener.next('<')); - xmltokener.nextToken(); - fail("Should have thrown exception."); - } catch (JSONException e) - { - assertEquals("Unterminated string at 10 [character 11 line 1]", e.getMessage()); - } - } - - /** - * Tests the skipTo method. - */ - public void testSkipTo() - { - try - { - xmltokener = new XMLTokener(""); - assertEquals('<', xmltokener.next('<')); - assertEquals(true, xmltokener.skipPast("c1")); - assertEquals('2', xmltokener.next('2')); - assertEquals(false, xmltokener.skipPast("b1")); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - - /** - * Tests the skipTo method using long parameter. - */ - public void testSkipTo_LongParameter() - { - try - { - xmltokener = new XMLTokener(""); - assertEquals('<', xmltokener.next('<')); - assertEquals(false, xmltokener.skipPast("abcdefghi")); - } catch (JSONException e) - { - fail(e.getMessage()); - } - } - -} \ No newline at end of file