1 package se.anatom.ejbca.util; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.util.HashMap ; 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 17 public class TestHashMap extends TestCase { 18 20 public TestHashMap(String name) { 21 super(name); 22 } 23 24 protected void setUp() throws Exception { 25 } 26 27 protected void tearDown() throws Exception { 28 } 29 30 public void test01HashMapNormal() throws Exception { 31 HashMap a = new HashMap (); 32 a.put("foo0", Boolean.valueOf(false)); 33 a.put("foo1", "fooString"); 34 a.put("foo2", new Integer (2)); 35 a.put("foo3", Boolean.valueOf(true)); 36 37 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 39 java.beans.XMLEncoder encoder = new java.beans.XMLEncoder (baos); 40 encoder.writeObject(a); 41 encoder.close(); 42 String data = baos.toString("UTF8"); 43 45 java.beans.XMLDecoder decoder = new java.beans.XMLDecoder (new java.io.ByteArrayInputStream (data.getBytes("UTF8"))); 46 HashMap b = (HashMap ) decoder.readObject(); 47 decoder.close(); 48 assertEquals(((Boolean )b.get("foo0")).booleanValue(),false); 49 assertEquals(((Boolean )b.get("foo3")).booleanValue(),true); 50 assertEquals(((String )b.get("foo1")),"fooString"); 51 assertEquals(((Integer )b.get("foo2")).intValue(),2); 52 53 } 54 55 public void test01HashMapStrangeChars() throws Exception { 56 HashMap a = new HashMap (); 57 a.put("foo0", Boolean.valueOf(false)); 58 a.put("foo1", "\0001\0002fooString"); 59 a.put("foo2", new Integer (2)); 60 a.put("foo3", Boolean.valueOf(true)); 61 62 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 64 java.beans.XMLEncoder encoder = new java.beans.XMLEncoder (baos); 65 encoder.writeObject(a); 66 encoder.close(); 67 String data = baos.toString("UTF8"); 68 70 try { 71 java.beans.XMLDecoder decoder = new java.beans.XMLDecoder (new java.io.ByteArrayInputStream (data.getBytes("UTF8"))); 72 HashMap b = (HashMap ) decoder.readObject(); 73 decoder.close(); 74 assertEquals(((Boolean )b.get("foo0")).booleanValue(),false); 75 } catch (ClassCastException e) { 78 return; 79 } catch (ArrayIndexOutOfBoundsException e) { 80 return; 81 } 82 String javaver = System.getProperty("java.version"); 83 if (StringUtils.contains(javaver, "1.6")) { 85 assertTrue(true); 88 } else { 89 assertTrue(false); 92 } 93 } 94 public void test01HashMapStrangeCharsSafe() throws Exception { 95 HashMap h = new HashMap (); 96 h.put("foo0", Boolean.valueOf(false)); 97 h.put("foo1", "\0001\0002fooString"); 98 h.put("foo2", new Integer (2)); 99 h.put("foo3", Boolean.valueOf(true)); 100 h.put("foo4", ""); 101 HashMap a = new Base64PutHashMap(); 102 a.putAll(h); 103 104 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 106 java.beans.XMLEncoder encoder = new java.beans.XMLEncoder (baos); 107 encoder.writeObject(a); 108 encoder.close(); 109 String data = baos.toString("UTF8"); 110 112 try { 113 java.beans.XMLDecoder decoder = new java.beans.XMLDecoder (new java.io.ByteArrayInputStream (data.getBytes("UTF8"))); 114 HashMap b = (HashMap ) decoder.readObject(); 115 decoder.close(); 116 HashMap c = new Base64GetHashMap(b); 117 assertEquals(((Boolean )c.get("foo0")).booleanValue(),false); 118 assertEquals(((Boolean )c.get("foo3")).booleanValue(),true); 119 assertEquals(((String )c.get("foo1")),"\0001\0002fooString"); 120 assertEquals(((String )c.get("foo4")),""); 121 assertEquals(((Integer )c.get("foo2")).intValue(),2); 122 123 } catch (ClassCastException e) { 124 assertTrue(false); 125 } 126 } 127 public void test01HashMapNormalCharsSafe() throws Exception { 128 HashMap h = new HashMap (); 129 h.put("foo0", Boolean.valueOf(false)); 130 h.put("foo1", "fooString"); 131 h.put("foo2", new Integer (2)); 132 h.put("foo3", Boolean.valueOf(true)); 133 h.put("foo4", ""); 134 HashMap a = new Base64PutHashMap(); 135 a.putAll(h); 136 137 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 139 java.beans.XMLEncoder encoder = new java.beans.XMLEncoder (baos); 140 encoder.writeObject(a); 141 encoder.close(); 142 String data = baos.toString("UTF8"); 143 145 try { 146 java.beans.XMLDecoder decoder = new java.beans.XMLDecoder (new java.io.ByteArrayInputStream (data.getBytes("UTF8"))); 147 HashMap b = (HashMap ) decoder.readObject(); 148 decoder.close(); 149 HashMap c = new Base64GetHashMap(b); 150 assertEquals(((Boolean )c.get("foo0")).booleanValue(),false); 151 assertEquals(((Boolean )c.get("foo3")).booleanValue(),true); 152 assertEquals(((String )c.get("foo4")),""); 153 assertEquals(((String )c.get("foo1")),"fooString"); 154 assertEquals(((Integer )c.get("foo2")).intValue(),2); 155 156 } catch (ClassCastException e) { 157 assertTrue(false); 158 } 159 } 160 } 161 | Popular Tags |