1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author Andres Almiray <aalmiray@users.sourceforge.net>
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        // two dimensions
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        // collections
80        assertTrue( JSONUtils.isArray( Collections.EMPTY_SET ) );
81        assertTrue( JSONUtils.isArray( Collections.EMPTY_LIST ) );
82  
83        // jsonArray
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          // ok
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          // ok
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          // ok
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          // ok
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          // ok
178       }
179    }
180 }