1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.json.util;
18
19 import java.util.Collections;
20
21 import junit.framework.TestCase;
22 import net.sf.json.JSONArray;
23 import net.sf.json.JSONException;
24 import net.sf.json.JSONFunction;
25
26
27
28
29 public class TestJSONUtils extends TestCase {
30 public static void main( String[] args ) {
31 junit.textui.TestRunner.run( TestJSONUtils.class );
32 }
33
34 public TestJSONUtils( String name ) {
35 super( name );
36 }
37
38 public void testDoubleToString_infinite() {
39 assertEquals( "null", JSONUtils.doubleToString( Double.POSITIVE_INFINITY ) );
40 }
41
42 public void testDoubleToString_nan() {
43 assertEquals( "null", JSONUtils.doubleToString( Double.NaN ) );
44 }
45
46 public void testDoubleToString_trailingZeros() {
47 assertEquals( "200", JSONUtils.doubleToString( 200.00000 ) );
48 }
49
50 public void testGetFunctionParams() {
51 assertEquals( "", JSONUtils.getFunctionParams( "function()" ) );
52 assertEquals( "a", JSONUtils.getFunctionParams( "function(a)" ) );
53 assertEquals( "a,b", JSONUtils.getFunctionParams( "function(a,b)" ) );
54 assertEquals( "", JSONUtils.getFunctionParams( "notAFunction" ) );
55 }
56
57 public void testIsArray() {
58 assertTrue( JSONUtils.isArray( new Object[0] ) );
59 assertTrue( JSONUtils.isArray( new boolean[0] ) );
60 assertTrue( JSONUtils.isArray( new byte[0] ) );
61 assertTrue( JSONUtils.isArray( new char[0] ) );
62 assertTrue( JSONUtils.isArray( new short[0] ) );
63 assertTrue( JSONUtils.isArray( new int[0] ) );
64 assertTrue( JSONUtils.isArray( new long[0] ) );
65 assertTrue( JSONUtils.isArray( new float[0] ) );
66 assertTrue( JSONUtils.isArray( new double[0] ) );
67
68
69 assertTrue( JSONUtils.isArray( new Object[0][0] ) );
70 assertTrue( JSONUtils.isArray( new boolean[0][0] ) );
71 assertTrue( JSONUtils.isArray( new byte[0][0] ) );
72 assertTrue( JSONUtils.isArray( new char[0][0] ) );
73 assertTrue( JSONUtils.isArray( new short[0][0] ) );
74 assertTrue( JSONUtils.isArray( new int[0][0] ) );
75 assertTrue( JSONUtils.isArray( new long[0][0] ) );
76 assertTrue( JSONUtils.isArray( new float[0][0] ) );
77 assertTrue( JSONUtils.isArray( new double[0][0] ) );
78
79
80 assertTrue( JSONUtils.isArray( Collections.EMPTY_SET ) );
81 assertTrue( JSONUtils.isArray( Collections.EMPTY_LIST ) );
82
83
84 assertTrue( JSONUtils.isArray( new JSONArray() ) );
85 }
86
87 public void testIsFunction() {
88 assertTrue( JSONUtils.isFunction( "function(){ return a; }" ) );
89 assertTrue( JSONUtils.isFunction( "function (){ return a; }" ) );
90 assertTrue( JSONUtils.isFunction( "function() { return a; }" ) );
91 assertTrue( JSONUtils.isFunction( "function () { return a; }" ) );
92 assertTrue( JSONUtils.isFunction( "function(a){ return a; }" ) );
93 }
94
95 public void testNumberToString_null() {
96 try{
97 JSONUtils.numberToString( null );
98 fail( "Should have thrown a JSONException" );
99 }catch( JSONException expected ){
100
101 }
102 }
103
104 public void testQuote_emptyString() {
105 assertEquals( "\"\"", JSONUtils.quote( "" ) );
106 }
107
108 public void testQuote_escapeChars() {
109 assertEquals( "\"\\b\\t\\n\\r\\f\"", JSONUtils.quote( "\b\t\n\r\f" ) );
110 }
111
112 public void testQuote_jsonFunction() {
113 JSONFunction jsonFunction = new JSONFunction( "a" );
114 assertEquals( "function(){ a }", JSONUtils.quote( jsonFunction.toString() ) );
115 }
116
117 public void testQuote_nullString() {
118 assertEquals( "\"\"", JSONUtils.quote( null ) );
119 }
120
121 public void testStripQuotes_singleChar_doubleeQuote() {
122 String quoted = "\"";
123 String actual = JSONUtils.stripQuotes( quoted );
124 assertEquals( quoted, actual );
125 }
126
127 public void testStripQuotes_singleChar_singleQuote() {
128 String quoted = "'";
129 String actual = JSONUtils.stripQuotes( quoted );
130 assertEquals( quoted, actual );
131 }
132
133 public void testStripQuotes_twoChars_doubleeQuote() {
134 String quoted = "\"\"";
135 String actual = JSONUtils.stripQuotes( quoted );
136 assertEquals( "", actual );
137 }
138
139 public void testStripQuotes_twoChars_singleQuote() {
140 String quoted = "''";
141 String actual = JSONUtils.stripQuotes( quoted );
142 assertEquals( "", actual );
143 }
144
145 public void testValidity_inifiniteDouble() {
146 try{
147 JSONUtils.testValidity( new Double( Double.POSITIVE_INFINITY ) );
148 fail( "Should have thrown a JSONException" );
149 }catch( JSONException expected ){
150
151 }
152 }
153
154 public void testValidity_inifiniteFloat() {
155 try{
156 JSONUtils.testValidity( new Float( Float.POSITIVE_INFINITY ) );
157 fail( "Should have thrown a JSONException" );
158 }catch( JSONException expected ){
159
160 }
161 }
162
163 public void testValidity_nanDouble() {
164 try{
165 JSONUtils.testValidity( new Double( Double.NaN ) );
166 fail( "Should have thrown a JSONException" );
167 }catch( JSONException expected ){
168
169 }
170 }
171
172 public void testValidity_nanFloat() {
173 try{
174 JSONUtils.testValidity( new Float( Float.NaN ) );
175 fail( "Should have thrown a JSONException" );
176 }catch( JSONException expected ){
177
178 }
179 }
180 }