KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > util > TestHashMap


1 package se.anatom.ejbca.util;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.util.HashMap JavaDoc;
5
6 import junit.framework.TestCase;
7
8 import org.apache.commons.lang.StringUtils;
9 import org.ejbca.util.Base64GetHashMap;
10 import org.ejbca.util.Base64PutHashMap;
11
12 /** Tests Base64 HashMap XML encoding and decoding
13  *
14  * @author tomasg
15  * @version $Id: TestHashMap.java,v 1.5.6.1 2007/06/25 14:40:38 anatom Exp $
16  */

17 public class TestHashMap extends TestCase {
18     //private static final Logger log = Logger.getLogger(TestHashMap.class);
19

20     public TestHashMap(String JavaDoc name) {
21         super(name);
22     }
23
24     protected void setUp() throws Exception JavaDoc {
25     }
26
27     protected void tearDown() throws Exception JavaDoc {
28     }
29
30     public void test01HashMapNormal() throws Exception JavaDoc {
31         HashMap JavaDoc a = new HashMap JavaDoc();
32         a.put("foo0", Boolean.valueOf(false));
33         a.put("foo1", "fooString");
34         a.put("foo2", new Integer JavaDoc(2));
35         a.put("foo3", Boolean.valueOf(true));
36         
37         // Write to XML
38
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
39         java.beans.XMLEncoder JavaDoc encoder = new java.beans.XMLEncoder JavaDoc(baos);
40         encoder.writeObject(a);
41         encoder.close();
42         String JavaDoc data = baos.toString("UTF8");
43         //log.error(data);
44

45         java.beans.XMLDecoder JavaDoc decoder = new java.beans.XMLDecoder JavaDoc(new java.io.ByteArrayInputStream JavaDoc(data.getBytes("UTF8")));
46         HashMap JavaDoc b = (HashMap JavaDoc) decoder.readObject();
47         decoder.close();
48         assertEquals(((Boolean JavaDoc)b.get("foo0")).booleanValue(),false);
49         assertEquals(((Boolean JavaDoc)b.get("foo3")).booleanValue(),true);
50         assertEquals(((String JavaDoc)b.get("foo1")),"fooString");
51         assertEquals(((Integer JavaDoc)b.get("foo2")).intValue(),2);
52
53     }
54     
55     public void test01HashMapStrangeChars() throws Exception JavaDoc {
56         HashMap JavaDoc a = new HashMap JavaDoc();
57         a.put("foo0", Boolean.valueOf(false));
58         a.put("foo1", "\0001\0002fooString");
59         a.put("foo2", new Integer JavaDoc(2));
60         a.put("foo3", Boolean.valueOf(true));
61         
62         // Write to XML
63
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
64         java.beans.XMLEncoder JavaDoc encoder = new java.beans.XMLEncoder JavaDoc(baos);
65         encoder.writeObject(a);
66         encoder.close();
67         String JavaDoc data = baos.toString("UTF8");
68         //log.error(data);
69

70         try {
71             java.beans.XMLDecoder JavaDoc decoder = new java.beans.XMLDecoder JavaDoc(new java.io.ByteArrayInputStream JavaDoc(data.getBytes("UTF8")));
72             HashMap JavaDoc b = (HashMap JavaDoc) decoder.readObject();
73             decoder.close();
74             assertEquals(((Boolean JavaDoc)b.get("foo0")).booleanValue(),false);
75         // We can get two different errors, I don't know if it is different java versions or what...
76
// The important thing is that we do expect an error to occur here
77
} catch (ClassCastException JavaDoc e) {
78             return;
79         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
80             return;
81         }
82         String JavaDoc javaver = System.getProperty("java.version");
83         //log.error(javaver);
84
if (StringUtils.contains(javaver, "1.6")) {
85             // In java 1.6 the above does work because it encodes the special characters
86
// <string><char code="#0"/>1<char code="#0"/>2fooString</string>
87
assertTrue(true);
88         } else {
89             // In java 1.5 the above does not work, because it will insert bad xml-characters
90
// so the test will fail if we got here.
91
assertTrue(false);
92         }
93     }
94     public void test01HashMapStrangeCharsSafe() throws Exception JavaDoc {
95         HashMap JavaDoc h = new HashMap JavaDoc();
96         h.put("foo0", Boolean.valueOf(false));
97         h.put("foo1", "\0001\0002fooString");
98         h.put("foo2", new Integer JavaDoc(2));
99         h.put("foo3", Boolean.valueOf(true));
100         h.put("foo4", "");
101         HashMap JavaDoc a = new Base64PutHashMap();
102         a.putAll(h);
103         
104         // Write to XML
105
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
106         java.beans.XMLEncoder JavaDoc encoder = new java.beans.XMLEncoder JavaDoc(baos);
107         encoder.writeObject(a);
108         encoder.close();
109         String JavaDoc data = baos.toString("UTF8");
110         //log.error(data);
111

112         try {
113             java.beans.XMLDecoder JavaDoc decoder = new java.beans.XMLDecoder JavaDoc(new java.io.ByteArrayInputStream JavaDoc(data.getBytes("UTF8")));
114             HashMap JavaDoc b = (HashMap JavaDoc) decoder.readObject();
115             decoder.close();
116             HashMap JavaDoc c = new Base64GetHashMap(b);
117             assertEquals(((Boolean JavaDoc)c.get("foo0")).booleanValue(),false);
118             assertEquals(((Boolean JavaDoc)c.get("foo3")).booleanValue(),true);
119             assertEquals(((String JavaDoc)c.get("foo1")),"\0001\0002fooString");
120             assertEquals(((String JavaDoc)c.get("foo4")),"");
121             assertEquals(((Integer JavaDoc)c.get("foo2")).intValue(),2);
122             
123         } catch (ClassCastException JavaDoc e) {
124             assertTrue(false);
125         }
126     }
127     public void test01HashMapNormalCharsSafe() throws Exception JavaDoc {
128         HashMap JavaDoc h = new HashMap JavaDoc();
129         h.put("foo0", Boolean.valueOf(false));
130         h.put("foo1", "fooString");
131         h.put("foo2", new Integer JavaDoc(2));
132         h.put("foo3", Boolean.valueOf(true));
133         h.put("foo4", "");
134         HashMap JavaDoc a = new Base64PutHashMap();
135         a.putAll(h);
136         
137         // Write to XML
138
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
139         java.beans.XMLEncoder JavaDoc encoder = new java.beans.XMLEncoder JavaDoc(baos);
140         encoder.writeObject(a);
141         encoder.close();
142         String JavaDoc data = baos.toString("UTF8");
143         //log.error(data);
144

145         try {
146             java.beans.XMLDecoder JavaDoc decoder = new java.beans.XMLDecoder JavaDoc(new java.io.ByteArrayInputStream JavaDoc(data.getBytes("UTF8")));
147             HashMap JavaDoc b = (HashMap JavaDoc) decoder.readObject();
148             decoder.close();
149             HashMap JavaDoc c = new Base64GetHashMap(b);
150             assertEquals(((Boolean JavaDoc)c.get("foo0")).booleanValue(),false);
151             assertEquals(((Boolean JavaDoc)c.get("foo3")).booleanValue(),true);
152             assertEquals(((String JavaDoc)c.get("foo4")),"");
153             assertEquals(((String JavaDoc)c.get("foo1")),"fooString");
154             assertEquals(((Integer JavaDoc)c.get("foo2")).intValue(),2);
155             
156         } catch (ClassCastException JavaDoc e) {
157             assertTrue(false);
158         }
159     }
160 }
161
Popular Tags