001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.util;
016:
017: import java.io.ByteArrayInputStream;
018: import java.io.ByteArrayOutputStream;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * @author gregw
028: *
029: */
030: public class StringMapTest extends TestCase {
031: StringMap m0;
032: StringMap m1;
033: StringMap m5;
034: StringMap m5i;
035:
036: /**
037: * Constructor for StringMapTest.
038: * @param arg0
039: */
040: public StringMapTest(String arg0) {
041: super (arg0);
042: }
043:
044: /*
045: * @see TestCase#setUp()
046: */
047: protected void setUp() throws Exception {
048: super .setUp();
049:
050: m0 = new StringMap();
051: m1 = new StringMap(false);
052: m1.put("abc", "0");
053:
054: m5 = new StringMap(false);
055: m5.put("a", "0");
056: m5.put("ab", "1");
057: m5.put("abc", "2");
058: m5.put("abb", "3");
059: m5.put("bbb", "4");
060:
061: m5i = new StringMap(true);
062: m5i.put(null, "0");
063: m5i.put("ab", "1");
064: m5i.put("abc", "2");
065: m5i.put("abb", "3");
066: m5i.put("bbb", null);
067: }
068:
069: /*
070: * @see TestCase#tearDown()
071: */
072: protected void tearDown() throws Exception {
073: super .tearDown();
074: }
075:
076: public void testSize() {
077: assertEquals(0, m0.size());
078: assertEquals(1, m1.size());
079: assertEquals(5, m5.size());
080: assertEquals(5, m5i.size());
081:
082: m1.remove("abc");
083: m5.remove("abc");
084: m5.put("bbb", "x");
085: m5i.put("ABC", "x");
086: assertEquals(0, m0.size());
087: assertEquals(0, m1.size());
088: assertEquals(4, m5.size());
089: assertEquals(5, m5i.size());
090: }
091:
092: public void testIsEmpty() {
093: assertTrue(m0.isEmpty());
094: assertFalse(m1.isEmpty());
095: assertFalse(m5.isEmpty());
096: assertFalse(m5i.isEmpty());
097: }
098:
099: public void testClear() {
100: m0.clear();
101: m1.clear();
102: m5.clear();
103: m5i.clear();
104: assertTrue(m0.isEmpty());
105: assertTrue(m1.isEmpty());
106: assertTrue(m5.isEmpty());
107: assertTrue(m5i.isEmpty());
108: assertEquals(null, m1.get("abc"));
109: assertEquals(null, m5.get("abc"));
110: assertEquals(null, m5i.get("abc"));
111: }
112:
113: /*
114: * Test for Object put(Object, Object)
115: */
116: public void testPutGet() {
117: assertEquals("2", m5.get("abc"));
118: assertEquals(null, m5.get("aBc"));
119: assertEquals("2", m5i.get("abc"));
120: assertEquals("2", m5i.get("aBc"));
121:
122: m5.put(null, "x");
123: m5.put("aBc", "x");
124: m5i.put("AbC", "x");
125:
126: StringBuffer buffer = new StringBuffer();
127: buffer.append("aBc");
128: assertEquals("2", m5.get("abc"));
129: assertEquals("x", m5.get(buffer));
130: assertEquals("x", m5i.get((Object) "abc"));
131: assertEquals("x", m5i.get("aBc"));
132:
133: assertEquals("x", m5.get(null));
134: assertEquals("0", m5i.get(null));
135:
136: }
137:
138: /*
139: * Test for Map.Entry getEntry(String, int, int)
140: */
141: public void testGetEntryStringintint() {
142: Map.Entry entry;
143:
144: entry = m5.getEntry("xabcyz", 1, 3);
145: assertTrue(entry != null);
146: assertEquals("abc", entry.getKey());
147: assertEquals("2", entry.getValue());
148:
149: entry = m5.getBestEntry("xabcyz".getBytes(), 1, 5);
150: assertTrue(entry != null);
151: assertEquals("abc", entry.getKey());
152: assertEquals("2", entry.getValue());
153:
154: entry = m5.getEntry("xaBcyz", 1, 3);
155: assertTrue(entry == null);
156:
157: entry = m5i.getEntry("xaBcyz", 1, 3);
158: assertTrue(entry != null);
159: assertEquals("abc", entry.getKey());
160: assertEquals("2", entry.getValue());
161: entry.setValue("x");
162: assertEquals("{[c:abc=x]}", entry.toString());
163:
164: entry = m5i.getEntry((String) null, 0, 0);
165: assertTrue(entry != null);
166: assertEquals(null, entry.getKey());
167: assertEquals("0", entry.getValue());
168: entry.setValue("x");
169: assertEquals("[:null=x]", entry.toString());
170:
171: }
172:
173: /*
174: * Test for Map.Entry getEntry(char[], int, int)
175: */
176: public void testGetEntrycharArrayintint() {
177: char[] xabcyz = { 'x', 'a', 'b', 'c', 'y', 'z' };
178: char[] xaBcyz = { 'x', 'a', 'B', 'c', 'y', 'z' };
179: Map.Entry entry;
180:
181: entry = m5.getEntry(xabcyz, 1, 3);
182: assertTrue(entry != null);
183: assertEquals("abc", entry.getKey());
184: assertEquals("2", entry.getValue());
185:
186: entry = m5.getEntry(xaBcyz, 1, 3);
187: assertTrue(entry == null);
188:
189: entry = m5i.getEntry(xaBcyz, 1, 3);
190: assertTrue(entry != null);
191: assertEquals("abc", entry.getKey());
192: assertEquals("2", entry.getValue());
193: }
194:
195: /*
196: * Test for Object remove(Object)
197: */
198: public void testRemove() {
199: m0.remove("abc");
200: m1.remove("abc");
201: m5.remove("aBc");
202: m5.remove("bbb");
203: m5i.remove("aBc");
204: m5i.remove(null);
205:
206: assertEquals(0, m0.size());
207: assertEquals(0, m1.size());
208: assertEquals(4, m5.size());
209: assertEquals(3, m5i.size());
210:
211: assertEquals("2", m5.get("abc"));
212: assertEquals(null, m5.get("bbb"));
213: assertEquals(null, m5i.get("AbC"));
214: assertEquals(null, m5i.get(null));
215: }
216:
217: /*
218: * Test for Set entrySet()
219: */
220: public void testEntrySet() {
221: Set es0 = m0.entrySet();
222: Set es1 = m1.entrySet();
223: Set es5 = m5.entrySet();
224: assertEquals(0, es0.size());
225: assertEquals(1, es1.size());
226: assertEquals(5, es5.size());
227: }
228:
229: /*
230: * Test for boolean containsKey(Object)
231: */
232: public void testContainsKey() {
233: assertTrue(m5.containsKey("abc"));
234: assertTrue(!m5.containsKey("aBc"));
235: assertTrue(m5.containsKey("bbb"));
236: assertTrue(!m5.containsKey("xyz"));
237:
238: assertTrue(m5i.containsKey(null));
239: assertTrue(m5i.containsKey("abc"));
240: assertTrue(m5i.containsKey("aBc"));
241: assertTrue(m5i.containsKey("ABC"));
242: }
243:
244: public void testWriteExternal() throws Exception {
245: ByteArrayOutputStream bout = new ByteArrayOutputStream();
246: ObjectOutputStream oo = new ObjectOutputStream(bout);
247: ObjectInputStream oi;
248:
249: oo.writeObject(m0);
250: oo.writeObject(m1);
251: oo.writeObject(m5);
252: oo.writeObject(m5i);
253:
254: oi = new ObjectInputStream(new ByteArrayInputStream(bout
255: .toByteArray()));
256: m0 = (StringMap) oi.readObject();
257: m1 = (StringMap) oi.readObject();
258: m5 = (StringMap) oi.readObject();
259: m5i = (StringMap) oi.readObject();
260: testSize();
261:
262: oi = new ObjectInputStream(new ByteArrayInputStream(bout
263: .toByteArray()));
264: m0 = (StringMap) oi.readObject();
265: m1 = (StringMap) oi.readObject();
266: m5 = (StringMap) oi.readObject();
267: m5i = (StringMap) oi.readObject();
268: testPutGet();
269:
270: }
271:
272: public void testToString() {
273: assertEquals("{}", m0.toString());
274: assertEquals("{abc=0}", m1.toString());
275: assertTrue(m5.toString().indexOf("abc=2") > 0);
276: }
277:
278: }
|