KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > util > MapCopyTest


1 package org.jboss.cache.util;
2
3 import junit.framework.TestCase;
4
5 import java.io.ByteArrayInputStream JavaDoc;
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.ObjectInputStream JavaDoc;
8 import java.io.ObjectOutputStream JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12 public class MapCopyTest extends TestCase
13 {
14
15    public void testSerializable() throws Exception JavaDoc
16    {
17       HashMap JavaDoc hm = new HashMap JavaDoc();
18       hm.put(null, null);
19       hm.put("y", "z");
20       MapCopy mc = new MapCopy(hm);
21       assertEquals(hm, mc);
22       ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
23       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(os);
24       oos.writeObject(mc);
25       ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(os.toByteArray());
26       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(is);
27       Object JavaDoc o = ois.readObject();
28       assertEquals(hm, o);
29    }
30
31    public void testNull()
32    {
33       HashMap JavaDoc hm = new HashMap JavaDoc();
34       hm.put(null, null);
35       MapCopy mc = new MapCopy(hm);
36       assertEquals(hm, mc);
37       assertEquals(hm.toString(), mc.toString());
38
39       hm.put(null, "x");
40       hm.put("y", null);
41       mc = new MapCopy(hm);
42       mc.toString();
43       assertEquals(true, mc.containsKey("y"));
44    }
45
46    public void testAll()
47    {
48       HashMap JavaDoc hm = new HashMap JavaDoc();
49       hm.put("a", "b");
50       hm.put("b", "c");
51       MapCopy mc = new MapCopy(hm);
52       assertEquals(hm, mc);
53       assertEquals(hm.size(), mc.size());
54       try
55       {
56          mc.clear();
57          fail("read only");
58       }
59       catch (UnsupportedOperationException JavaDoc e)
60       {
61       }
62       HashMap JavaDoc bhm = new HashMap JavaDoc(hm);
63       hm.put("b", "d");
64       assertEquals(bhm, mc);
65       Map.Entry JavaDoc me = (Map.Entry JavaDoc) mc.entrySet().iterator().next();
66       try
67       {
68          me.setValue("arg");
69          fail("read only");
70       }
71       catch (UnsupportedOperationException JavaDoc e)
72       {
73       }
74    }
75
76    public void testModifications()
77    {
78       Map JavaDoc hm = new HashMap JavaDoc();
79       hm.put("a", "b");
80       Map JavaDoc mc = new MapCopy(hm);
81
82       try
83       {
84          mc.put("x", "y");
85          fail("should fail");
86       }
87       catch (UnsupportedOperationException JavaDoc uoe)
88       {
89          // ok
90
}
91
92       try
93       {
94          mc.remove("a");
95          fail("should fail");
96       }
97       catch (UnsupportedOperationException JavaDoc uoe)
98       {
99          // ok
100
}
101
102       try
103       {
104          mc.keySet().iterator().remove();
105          fail("should fail");
106       }
107       catch (UnsupportedOperationException JavaDoc uoe)
108       {
109          // ok
110
}
111
112       try
113       {
114          mc.entrySet().iterator().remove();
115          fail("should fail");
116       }
117       catch (UnsupportedOperationException JavaDoc uoe)
118       {
119          // ok
120
}
121
122       try
123       {
124          mc.values().iterator().remove();
125          fail("should fail");
126       }
127       catch (UnsupportedOperationException JavaDoc uoe)
128       {
129          // ok
130
}
131
132
133    }
134 }
135
Popular Tags