|
@@ -1,208 +1,208 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.GWT.client.localstorage; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.*; |
5 |
|
- |
|
6 |
|
- |
import junit.framework.*; |
7 |
|
- |
|
8 |
|
- |
import java.util.*; |
9 |
|
- |
|
10 |
|
- |
public class LocalStorageTest extends TestCasePlus { |
11 |
|
- |
public static TestSuite suite() { |
12 |
|
- |
return new TestSuite( LocalStorageTest.class ); |
13 |
|
- |
} |
14 |
|
- |
|
15 |
|
- |
public LocalStorageTest( String name ) { |
16 |
|
- |
super( name ); |
17 |
|
- |
} |
18 |
|
- |
|
19 |
|
- |
public static void main( String[] args ) { |
20 |
|
- |
junit.textui.TestRunner.run( suite() ); |
21 |
|
- |
} |
22 |
|
- |
|
23 |
|
- |
@Override |
24 |
|
- |
protected void setUp() |
25 |
|
- |
throws Exception { |
26 |
|
- |
mStorage = new LocalStorageImpl( new Html5LocalStorageTestImpl( 4 ) ); |
27 |
|
- |
} |
28 |
|
- |
|
29 |
|
- |
private LocalStorage mStorage; |
30 |
|
- |
|
31 |
|
- |
private List<String> keySet() { |
32 |
|
- |
List<String> zList = new ArrayList<String>( mStorage.keySet() ); |
33 |
|
- |
Collections.sort( zList ); |
34 |
|
- |
return zList; |
35 |
|
- |
} |
36 |
|
- |
|
37 |
|
- |
private List<Map.Entry<String, String>> entrySet() { |
38 |
|
- |
List<Map.Entry<String, String>> zList = new ArrayList<Map.Entry<String, String>>( mStorage.entrySet() ); |
39 |
|
- |
Collections.sort( zList, new Comparator<Map.Entry<String, String>>() { |
40 |
|
- |
@Override |
41 |
|
- |
public int compare( Map.Entry<String, String> o1, Map.Entry<String, String> o2 ) { |
42 |
|
- |
return o1.getKey().compareTo( o2.getKey() ); |
43 |
|
- |
} |
44 |
|
- |
} ); |
45 |
|
- |
return zList; |
46 |
|
- |
} |
47 |
|
- |
|
48 |
|
- |
public void test_StorageOne() |
49 |
|
- |
throws Exception { |
50 |
|
- |
assertTrue( mStorage.isEmpty() ); |
51 |
|
- |
mStorage.put( "K1", "V1" ); |
52 |
|
- |
assertEquals( 1, mStorage.size() ); |
53 |
|
- |
assertEquals( "[K1]", mStorage.keySet().toString() ); |
54 |
|
- |
assertEquals( "V1", mStorage.get( "K1" ) ); |
55 |
|
- |
assertEquals( "[K1=V1]", mStorage.entrySet().toString() ); |
56 |
|
- |
assertEquals( "V1", mStorage.remove( "K1" ) ); |
57 |
|
- |
assertTrue( mStorage.isEmpty() ); |
58 |
|
- |
|
59 |
|
- |
mStorage.put( "K1", "V1" ); |
60 |
|
- |
assertEquals( 1, mStorage.size() ); |
61 |
|
- |
assertEquals( "V1", mStorage.get( "K1" ) ); |
62 |
|
- |
mStorage.put( "K1", "V1B" ); |
63 |
|
- |
assertEquals( 1, mStorage.size() ); |
64 |
|
- |
assertEquals( "V1B", mStorage.get( "K1" ) ); |
65 |
|
- |
mStorage.clear(); |
66 |
|
- |
assertTrue( mStorage.isEmpty() ); |
67 |
|
- |
} |
68 |
|
- |
|
69 |
|
- |
public void test_StorageMany() |
70 |
|
- |
throws Exception { |
71 |
|
- |
assertTrue( mStorage.isEmpty() ); |
72 |
|
- |
mStorage.put( "K1", "V1" ); |
73 |
|
- |
mStorage.put( "K2", "V2" ); |
74 |
|
- |
mStorage.put( "K3", "V3" ); |
75 |
|
- |
mStorage.put( "K4", "V4" ); |
76 |
|
- |
assertEquals( 4, mStorage.size() ); |
77 |
|
- |
assertEquals( "[K1, K2, K3, K4]", keySet().toString() ); |
78 |
|
- |
assertEquals( "V1", mStorage.get( "K1" ) ); |
79 |
|
- |
assertEquals( "[K1=V1, K2=V2, K3=V3, K4=V4]", entrySet().toString() ); |
80 |
|
- |
assertEquals( "V1", mStorage.remove( "K1" ) ); |
81 |
|
- |
assertEquals( 3, mStorage.size() ); |
82 |
|
- |
assertEquals( "[K2=V2, K3=V3, K4=V4]", entrySet().toString() ); |
83 |
|
- |
assertEquals( "V2", mStorage.put( "K2", null ) ); |
84 |
|
- |
assertEquals( 2, mStorage.size() ); |
85 |
|
- |
assertEquals( "[K3=V3, K4=V4]", entrySet().toString() ); |
86 |
|
- |
mStorage.clear(); |
87 |
|
- |
assertTrue( mStorage.isEmpty() ); |
88 |
|
- |
} |
89 |
|
- |
|
90 |
|
- |
public void test_StorageTooMany() |
91 |
|
- |
throws Exception { |
92 |
|
- |
assertTrue( mStorage.isEmpty() ); |
93 |
|
- |
mStorage.put( "K1", "V1" ); |
94 |
|
- |
mStorage.put( "K2", "V2" ); |
95 |
|
- |
mStorage.put( "K3", "V3" ); |
96 |
|
- |
mStorage.put( "K4", "V4" ); |
97 |
|
- |
assertEquals( 4, mStorage.size() ); |
98 |
|
- |
assertEquals( "[K1=V1, K2=V2, K3=V3, K4=V4]", entrySet().toString() ); |
99 |
|
- |
|
100 |
|
- |
try { |
101 |
|
- |
mStorage.put( "K5", "V5" ); |
102 |
|
- |
fail( "Able to add the 5th entry" ); |
103 |
|
- |
} |
104 |
|
- |
catch ( UnsupportedOperationException expected ) { |
105 |
|
- |
// expected |
106 |
|
- |
} |
107 |
|
- |
assertEquals( 4, mStorage.size() ); |
108 |
|
- |
assertEquals( "[K1=V1, K2=V2, K3=V3, K4=V4]", entrySet().toString() ); |
109 |
|
- |
} |
110 |
|
- |
|
111 |
|
- |
@SuppressWarnings({"RedundantCast", "SuspiciousMethodCalls"}) |
112 |
|
- |
public void test_StorageNullOrEmpties() |
113 |
|
- |
throws Exception { |
114 |
|
- |
assertTrue( mStorage.isEmpty() ); |
115 |
|
- |
try { |
116 |
|
- |
mStorage.get( (Object) null ); |
117 |
|
- |
fail( "Able to get w/ null" ); |
118 |
|
- |
} |
119 |
|
- |
catch ( IllegalArgumentException expected ) { |
120 |
|
- |
// expected |
121 |
|
- |
} |
122 |
|
- |
assertTrue( mStorage.isEmpty() ); |
123 |
|
- |
try { |
124 |
|
- |
mStorage.get( (String) null ); |
125 |
|
- |
fail( "Able to get w/ null" ); |
126 |
|
- |
} |
127 |
|
- |
catch ( IllegalArgumentException expected ) { |
128 |
|
- |
// expected |
129 |
|
- |
} |
130 |
|
- |
assertTrue( mStorage.isEmpty() ); |
131 |
|
- |
try { |
132 |
|
- |
mStorage.get( "" ); |
133 |
|
- |
fail( "Able to get w/ empty" ); |
134 |
|
- |
} |
135 |
|
- |
catch ( IllegalArgumentException expected ) { |
136 |
|
- |
// expected |
137 |
|
- |
} |
138 |
|
- |
assertTrue( mStorage.isEmpty() ); |
139 |
|
- |
try { |
140 |
|
- |
mStorage.put( null, "V0" ); |
141 |
|
- |
fail( "Able to put w/ null" ); |
142 |
|
- |
} |
143 |
|
- |
catch ( IllegalArgumentException expected ) { |
144 |
|
- |
// expected |
145 |
|
- |
} |
146 |
|
- |
assertTrue( mStorage.isEmpty() ); |
147 |
|
- |
try { |
148 |
|
- |
mStorage.put( "", "V0" ); |
149 |
|
- |
fail( "Able to put w/ empty" ); |
150 |
|
- |
} |
151 |
|
- |
catch ( IllegalArgumentException expected ) { |
152 |
|
- |
// expected |
153 |
|
- |
} |
154 |
|
- |
assertTrue( mStorage.isEmpty() ); |
155 |
|
- |
try { |
156 |
|
- |
mStorage.remove( null ); |
157 |
|
- |
fail( "Able to remove w/ null" ); |
158 |
|
- |
} |
159 |
|
- |
catch ( IllegalArgumentException expected ) { |
160 |
|
- |
// expected |
161 |
|
- |
} |
162 |
|
- |
assertTrue( mStorage.isEmpty() ); |
163 |
|
- |
try { |
164 |
|
- |
mStorage.remove( "" ); |
165 |
|
- |
fail( "Able to remove w/ empty" ); |
166 |
|
- |
} |
167 |
|
- |
catch ( IllegalArgumentException expected ) { |
168 |
|
- |
// expected |
169 |
|
- |
} |
170 |
|
- |
assertTrue( mStorage.isEmpty() ); |
171 |
|
- |
} |
172 |
|
- |
|
173 |
|
- |
public void test_StorageTrimmed() |
174 |
|
- |
throws Exception { |
175 |
|
- |
assertTrue( mStorage.isEmpty() ); |
176 |
|
- |
mStorage.put( " K1 ", "V1" ); |
177 |
|
- |
assertEquals( 1, mStorage.size() ); |
178 |
|
- |
assertEquals( "[K1]", mStorage.keySet().toString() ); |
179 |
|
- |
assertEquals( "V1", mStorage.get( " K1 " ) ); |
180 |
|
- |
assertEquals( "[K1=V1]", mStorage.entrySet().toString() ); |
181 |
|
- |
assertEquals( "V1", mStorage.remove( " K1 " ) ); |
182 |
|
- |
assertTrue( mStorage.isEmpty() ); |
183 |
|
- |
try { |
184 |
|
- |
mStorage.get( " " ); |
185 |
|
- |
fail( "Able to get w/ empty" ); |
186 |
|
- |
} |
187 |
|
- |
catch ( IllegalArgumentException expected ) { |
188 |
|
- |
// expected |
189 |
|
- |
} |
190 |
|
- |
assertTrue( mStorage.isEmpty() ); |
191 |
|
- |
try { |
192 |
|
- |
mStorage.put( " ", "V0" ); |
193 |
|
- |
fail( "Able to put w/ empty" ); |
194 |
|
- |
} |
195 |
|
- |
catch ( IllegalArgumentException expected ) { |
196 |
|
- |
// expected |
197 |
|
- |
} |
198 |
|
- |
assertTrue( mStorage.isEmpty() ); |
199 |
|
- |
try { |
200 |
|
- |
mStorage.remove( " " ); |
201 |
|
- |
fail( "Able to remove w/ empty" ); |
202 |
|
- |
} |
203 |
|
- |
catch ( IllegalArgumentException expected ) { |
204 |
|
- |
// expected |
205 |
|
- |
} |
206 |
|
- |
assertTrue( mStorage.isEmpty() ); |
207 |
|
- |
} |
208 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.GWT.client.localstorage; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.*; |
|
5 |
+ |
|
|
6 |
+ |
import junit.framework.*; |
|
7 |
+ |
|
|
8 |
+ |
import java.util.*; |
|
9 |
+ |
|
|
10 |
+ |
public class LocalStorageTest extends TestCasePlus { |
|
11 |
+ |
public static TestSuite suite() { |
|
12 |
+ |
return new TestSuite( LocalStorageTest.class ); |
|
13 |
+ |
} |
|
14 |
+ |
|
|
15 |
+ |
public LocalStorageTest( String name ) { |
|
16 |
+ |
super( name ); |
|
17 |
+ |
} |
|
18 |
+ |
|
|
19 |
+ |
public static void main( String[] args ) { |
|
20 |
+ |
junit.textui.TestRunner.run( suite() ); |
|
21 |
+ |
} |
|
22 |
+ |
|
|
23 |
+ |
@Override |
|
24 |
+ |
protected void setUp() |
|
25 |
+ |
throws Exception { |
|
26 |
+ |
mStorage = new LocalStorageImpl( new Html5LocalStorageTestImpl( 4 ) ); |
|
27 |
+ |
} |
|
28 |
+ |
|
|
29 |
+ |
private LocalStorage mStorage; |
|
30 |
+ |
|
|
31 |
+ |
private List<String> keySet() { |
|
32 |
+ |
List<String> zList = new ArrayList<String>( mStorage.keySet() ); |
|
33 |
+ |
Collections.sort( zList ); |
|
34 |
+ |
return zList; |
|
35 |
+ |
} |
|
36 |
+ |
|
|
37 |
+ |
private List<Map.Entry<String, String>> entrySet() { |
|
38 |
+ |
List<Map.Entry<String, String>> zList = new ArrayList<Map.Entry<String, String>>( mStorage.entrySet() ); |
|
39 |
+ |
Collections.sort( zList, new Comparator<Map.Entry<String, String>>() { |
|
40 |
+ |
@Override |
|
41 |
+ |
public int compare( Map.Entry<String, String> o1, Map.Entry<String, String> o2 ) { |
|
42 |
+ |
return o1.getKey().compareTo( o2.getKey() ); |
|
43 |
+ |
} |
|
44 |
+ |
} ); |
|
45 |
+ |
return zList; |
|
46 |
+ |
} |
|
47 |
+ |
|
|
48 |
+ |
public void test_StorageOne() |
|
49 |
+ |
throws Exception { |
|
50 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
51 |
+ |
mStorage.put( "K1", "V1" ); |
|
52 |
+ |
assertEquals( 1, mStorage.size() ); |
|
53 |
+ |
assertEquals( "[K1]", mStorage.keySet().toString() ); |
|
54 |
+ |
assertEquals( "V1", mStorage.get( "K1" ) ); |
|
55 |
+ |
assertEquals( "[K1=V1]", mStorage.entrySet().toString() ); |
|
56 |
+ |
assertEquals( "V1", mStorage.remove( "K1" ) ); |
|
57 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
58 |
+ |
|
|
59 |
+ |
mStorage.put( "K1", "V1" ); |
|
60 |
+ |
assertEquals( 1, mStorage.size() ); |
|
61 |
+ |
assertEquals( "V1", mStorage.get( "K1" ) ); |
|
62 |
+ |
mStorage.put( "K1", "V1B" ); |
|
63 |
+ |
assertEquals( 1, mStorage.size() ); |
|
64 |
+ |
assertEquals( "V1B", mStorage.get( "K1" ) ); |
|
65 |
+ |
mStorage.clear(); |
|
66 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
67 |
+ |
} |
|
68 |
+ |
|
|
69 |
+ |
public void test_StorageMany() |
|
70 |
+ |
throws Exception { |
|
71 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
72 |
+ |
mStorage.put( "K1", "V1" ); |
|
73 |
+ |
mStorage.put( "K2", "V2" ); |
|
74 |
+ |
mStorage.put( "K3", "V3" ); |
|
75 |
+ |
mStorage.put( "K4", "V4" ); |
|
76 |
+ |
assertEquals( 4, mStorage.size() ); |
|
77 |
+ |
assertEquals( "[K1, K2, K3, K4]", keySet().toString() ); |
|
78 |
+ |
assertEquals( "V1", mStorage.get( "K1" ) ); |
|
79 |
+ |
assertEquals( "[K1=V1, K2=V2, K3=V3, K4=V4]", entrySet().toString() ); |
|
80 |
+ |
assertEquals( "V1", mStorage.remove( "K1" ) ); |
|
81 |
+ |
assertEquals( 3, mStorage.size() ); |
|
82 |
+ |
assertEquals( "[K2=V2, K3=V3, K4=V4]", entrySet().toString() ); |
|
83 |
+ |
assertEquals( "V2", mStorage.put( "K2", null ) ); |
|
84 |
+ |
assertEquals( 2, mStorage.size() ); |
|
85 |
+ |
assertEquals( "[K3=V3, K4=V4]", entrySet().toString() ); |
|
86 |
+ |
mStorage.clear(); |
|
87 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
88 |
+ |
} |
|
89 |
+ |
|
|
90 |
+ |
public void test_StorageTooMany() |
|
91 |
+ |
throws Exception { |
|
92 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
93 |
+ |
mStorage.put( "K1", "V1" ); |
|
94 |
+ |
mStorage.put( "K2", "V2" ); |
|
95 |
+ |
mStorage.put( "K3", "V3" ); |
|
96 |
+ |
mStorage.put( "K4", "V4" ); |
|
97 |
+ |
assertEquals( 4, mStorage.size() ); |
|
98 |
+ |
assertEquals( "[K1=V1, K2=V2, K3=V3, K4=V4]", entrySet().toString() ); |
|
99 |
+ |
|
|
100 |
+ |
try { |
|
101 |
+ |
mStorage.put( "K5", "V5" ); |
|
102 |
+ |
fail( "Able to add the 5th entry" ); |
|
103 |
+ |
} |
|
104 |
+ |
catch ( UnsupportedOperationException expected ) { |
|
105 |
+ |
// expected |
|
106 |
+ |
} |
|
107 |
+ |
assertEquals( 4, mStorage.size() ); |
|
108 |
+ |
assertEquals( "[K1=V1, K2=V2, K3=V3, K4=V4]", entrySet().toString() ); |
|
109 |
+ |
} |
|
110 |
+ |
|
|
111 |
+ |
@SuppressWarnings({"RedundantCast", "SuspiciousMethodCalls"}) |
|
112 |
+ |
public void test_StorageNullOrEmpties() |
|
113 |
+ |
throws Exception { |
|
114 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
115 |
+ |
try { |
|
116 |
+ |
mStorage.get( (Object) null ); |
|
117 |
+ |
fail( "Able to get w/ null" ); |
|
118 |
+ |
} |
|
119 |
+ |
catch ( IllegalArgumentException expected ) { |
|
120 |
+ |
// expected |
|
121 |
+ |
} |
|
122 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
123 |
+ |
try { |
|
124 |
+ |
mStorage.get( (String) null ); |
|
125 |
+ |
fail( "Able to get w/ null" ); |
|
126 |
+ |
} |
|
127 |
+ |
catch ( IllegalArgumentException expected ) { |
|
128 |
+ |
// expected |
|
129 |
+ |
} |
|
130 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
131 |
+ |
try { |
|
132 |
+ |
mStorage.get( "" ); |
|
133 |
+ |
fail( "Able to get w/ empty" ); |
|
134 |
+ |
} |
|
135 |
+ |
catch ( IllegalArgumentException expected ) { |
|
136 |
+ |
// expected |
|
137 |
+ |
} |
|
138 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
139 |
+ |
try { |
|
140 |
+ |
mStorage.put( null, "V0" ); |
|
141 |
+ |
fail( "Able to put w/ null" ); |
|
142 |
+ |
} |
|
143 |
+ |
catch ( IllegalArgumentException expected ) { |
|
144 |
+ |
// expected |
|
145 |
+ |
} |
|
146 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
147 |
+ |
try { |
|
148 |
+ |
mStorage.put( "", "V0" ); |
|
149 |
+ |
fail( "Able to put w/ empty" ); |
|
150 |
+ |
} |
|
151 |
+ |
catch ( IllegalArgumentException expected ) { |
|
152 |
+ |
// expected |
|
153 |
+ |
} |
|
154 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
155 |
+ |
try { |
|
156 |
+ |
mStorage.remove( null ); |
|
157 |
+ |
fail( "Able to remove w/ null" ); |
|
158 |
+ |
} |
|
159 |
+ |
catch ( IllegalArgumentException expected ) { |
|
160 |
+ |
// expected |
|
161 |
+ |
} |
|
162 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
163 |
+ |
try { |
|
164 |
+ |
mStorage.remove( "" ); |
|
165 |
+ |
fail( "Able to remove w/ empty" ); |
|
166 |
+ |
} |
|
167 |
+ |
catch ( IllegalArgumentException expected ) { |
|
168 |
+ |
// expected |
|
169 |
+ |
} |
|
170 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
171 |
+ |
} |
|
172 |
+ |
|
|
173 |
+ |
public void test_StorageTrimmed() |
|
174 |
+ |
throws Exception { |
|
175 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
176 |
+ |
mStorage.put( " K1 ", "V1" ); |
|
177 |
+ |
assertEquals( 1, mStorage.size() ); |
|
178 |
+ |
assertEquals( "[K1]", mStorage.keySet().toString() ); |
|
179 |
+ |
assertEquals( "V1", mStorage.get( " K1 " ) ); |
|
180 |
+ |
assertEquals( "[K1=V1]", mStorage.entrySet().toString() ); |
|
181 |
+ |
assertEquals( "V1", mStorage.remove( " K1 " ) ); |
|
182 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
183 |
+ |
try { |
|
184 |
+ |
mStorage.get( " " ); |
|
185 |
+ |
fail( "Able to get w/ empty" ); |
|
186 |
+ |
} |
|
187 |
+ |
catch ( IllegalArgumentException expected ) { |
|
188 |
+ |
// expected |
|
189 |
+ |
} |
|
190 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
191 |
+ |
try { |
|
192 |
+ |
mStorage.put( " ", "V0" ); |
|
193 |
+ |
fail( "Able to put w/ empty" ); |
|
194 |
+ |
} |
|
195 |
+ |
catch ( IllegalArgumentException expected ) { |
|
196 |
+ |
// expected |
|
197 |
+ |
} |
|
198 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
199 |
+ |
try { |
|
200 |
+ |
mStorage.remove( " " ); |
|
201 |
+ |
fail( "Able to remove w/ empty" ); |
|
202 |
+ |
} |
|
203 |
+ |
catch ( IllegalArgumentException expected ) { |
|
204 |
+ |
// expected |
|
205 |
+ |
} |
|
206 |
+ |
assertTrue( mStorage.isEmpty() ); |
|
207 |
+ |
} |
|
208 |
+ |
} |