Debian Codegraph @

src/test/java/org/astrogrid/samp/SampUtilsTest.java

raw
1 package org.astrogrid.samp;
2
3 import java.net.InetAddress;
4 import java.net.UnknownHostException;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashMap;
8 import java.util.LinkedHashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Properties;
12 import java.util.Random;
13
14 import junit.framework.TestCase;
15
16 public class SampUtilsTest extends TestCase {
17
18 private final Random random_ = new Random( 232323L );
19
20 public void testCodecs() {
21 for ( int k = 0; k < 100; k++ ) {
22 boolean b = random_.nextBoolean();
23 int i = random_.nextInt();
24 long l = random_.nextLong();
25 double f = random_.nextDouble();
26 assertEquals( b, SampUtils
27 .decodeBoolean( SampUtils.encodeBoolean( b ) ) );
28 assertEquals( i, SampUtils
29 .decodeInt( SampUtils.encodeInt( i ) ) );
30 assertEquals( l, SampUtils
31 .decodeLong( SampUtils.encodeLong( l ) ) );
32 assertEquals( f, SampUtils
33 .decodeFloat( SampUtils.encodeFloat( f ) ), 0.0 );
34 }
35 }
36
37 public void testUriCodecs() {
38 assertEquals( "abc", SampUtils.uriEncode( "abc" ) );
39 assertEquals( "abc", SampUtils.uriDecode( "abc" ) );
40 assertEquals( "a b+c+d", SampUtils.uriDecode( "a%20b+c%2Bd" ) );
41 String[] txts = {
42 "abc",
43 "http://localhost/file",
44 "a b c * / % ++--%1",
45 };
46 for ( int i = 0; i < txts.length; i++ ) {
47 String txt = txts[ i ];
48 assertEquals( txt,
49 SampUtils.uriDecode( SampUtils.uriEncode( txt ) ) );
50 assertEquals( txt,
51 SampUtils.uriDecode(
52 SampUtils.uriDecode(
53 SampUtils.uriEncode(
54 SampUtils.uriEncode( txt ) ) ) ) );
55 }
56 }
57
58 public void testChars() {
59 assertTrue( SampUtils.isStringChar( 'x' ) );
60 assertTrue( SampUtils.isStringChar( 'X' ) );
61 assertTrue( SampUtils.isStringChar( (char) 0x09 ) );
62 assertTrue( SampUtils.isStringChar( (char) 0x0a ) );
63 assertTrue( SampUtils.isStringChar( (char) 0x0d ) );
64
65 assertTrue( ! SampUtils.isStringChar( (char) 0 ) );
66 assertTrue( ! SampUtils.isStringChar( (char) 0x0e ) );
67 assertTrue( ! SampUtils.isStringChar( '\b' ) );
68 assertTrue( ! SampUtils.isStringChar( (char) 0xffef ) );
69 }
70
71 public void testChecks() {
72 goodObject( "a" );
73 goodObject( "abc\t\r\n" );
74 badObject( "abc\b" );
75 badObject( new Integer( 23 ) );
76 badObject( new Double( Math.E ) );
77 badObject( null );
78 badObject( Arrays.asList( new Object[] { "AA", null, } ) );
79 List abclist = Arrays.asList( new Object[] { "a", "bb", "ccc" } );
80 goodObject( abclist );
81 goodObject( new ArrayList() );
82 goodObject( new HashMap() );
83 Map xmap = new HashMap();
84 xmap.put( "xx", abclist );
85 goodObject( xmap );
86 xmap.put( abclist, "xx" );
87 badObject( xmap );
88 }
89
90 public void testHostname() throws UnknownHostException {
91 String hprop = SampUtils.LOCALHOST_PROP;
92 Properties sysprops = System.getProperties();
93 String prop = sysprops.getProperty( hprop );
94 sysprops.remove( hprop );
95 assertEquals( "127.0.0.1", SampUtils.getLocalhost() );
96 sysprops.setProperty( hprop, "host-with-the-most" );
97 assertEquals( "host-with-the-most", SampUtils.getLocalhost() );
98 sysprops.setProperty( hprop, "[hostname]" );
99 assertEquals( InetAddress.getLocalHost().getCanonicalHostName(),
100 SampUtils.getLocalhost() );
101 if ( prop == null ) {
102 sysprops.remove( hprop );
103 }
104 else {
105 sysprops.setProperty( hprop, prop );
106 }
107 }
108
109 public void testJson() {
110 Map m = new LinkedHashMap();
111 m.put( "one", "1" );
112 m.put( "two", "2" );
113 m.put( "list", Arrays.asList( new String[] { "A", "B", "C" } ) );
114 String js =
115 "{\"one\":\"1\",\"two\":\"2\",\"list\":[\"A\",\"B\",\"C\"]}";
116 assertEquals( nows( js ), nows( SampUtils.toJson( m, false ) ) );
117 assertEquals( nows( js ), nows( SampUtils.toJson( m, true ) ) );
118 assertEquals( nows( js ),
119 nows( SampUtils.toJson( SampUtils.fromJson( js ),
120 true ) ) );
121 assertTrue( SampUtils.toJson( m, false ).indexOf( '\n' ) < 0 );
122 assertTrue( SampUtils.toJson( m, true ).indexOf( '\n' ) >= 0 );
123 assertEquals( new HashMap(), SampUtils.fromJson("{}") );
124 assertEquals( new HashMap(), SampUtils.fromJson("{ }") );
125 assertEquals( new ArrayList(), SampUtils.fromJson("[]") );
126 assertEquals( new ArrayList(), SampUtils.fromJson("[ ]") );
127 }
128
129 private static String nows( String txt ) {
130 return txt.replaceAll( "\\s+", "" );
131 }
132
133 private void goodObject( Object obj ) {
134 SampUtils.checkObject( obj );
135 SampUtils.checkList( Arrays.asList( new Object[] { "a", obj, "c" } ) );
136 Map map = new HashMap();
137 map.put( "k1", "v1" );
138 map.put( "key", obj );
139 map.put( "k2", "v2" );
140 SampUtils.checkMap( map );
141 }
142
143 private void badObject( Object obj ) {
144 try {
145 SampUtils.checkObject( obj );
146 fail( "Object should be bad: " + obj );
147 }
148 catch ( DataException e ) {
149 return;
150 }
151 try {
152 SampUtils.checkList( Arrays.asList( new Object[]
153 { "a", obj, "c" } ) );
154 fail( "Object should be bad: " + obj );
155 }
156 catch ( DataException e ) {
157 return;
158 }
159 }
160 }