SerializationStressTest.java revision 6d5c5d6c3e64b37d67af1d516b70a3fee38b2796
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.io; 19 20import java.io.ByteArrayInputStream; 21import java.io.ByteArrayOutputStream; 22import java.io.FileInputStream; 23import java.io.FileOutputStream; 24import java.io.IOException; 25import java.io.InputStream; 26import java.io.ObjectInputStream; 27import java.io.ObjectOutputStream; 28import java.io.Serializable; 29import java.security.Permission; 30import java.security.PermissionCollection; 31import java.util.ArrayList; 32import java.util.Arrays; 33import java.util.Calendar; 34import java.util.GregorianCalendar; 35import java.util.HashMap; 36import java.util.HashSet; 37import java.util.Hashtable; 38import java.util.IdentityHashMap; 39import java.util.LinkedHashMap; 40import java.util.LinkedHashSet; 41import java.util.LinkedList; 42import java.util.List; 43import java.util.Map; 44import java.util.PropertyPermission; 45import java.util.Set; 46import java.util.SimpleTimeZone; 47import java.util.SortedMap; 48import java.util.SortedSet; 49import java.util.TimeZone; 50import java.util.TreeMap; 51import java.util.TreeSet; 52 53import dalvik.annotation.TestTargetClass; 54 55/** 56 * Automated Test Suite for class java.io.ObjectOutputStream 57 * 58 */ 59@SuppressWarnings({"serial", "unchecked"}) 60@TestTargetClass(Serializable.class) 61public class SerializationStressTest extends junit.framework.TestCase implements 62 Serializable { 63 64 // protected static final String MODE_XLOAD = "xload"; 65 66 // protected static final String MODE_XDUMP = "xdump"; 67 68 static final String FOO = "foo"; 69 70 static final String MSG_TEST_FAILED = "Failed to write/read/assertion checking: "; 71 72 protected static final boolean DEBUG = false; 73 74 protected static boolean xload = false; 75 76 protected static boolean xdump = false; 77 78 protected static String xFileName = null; 79 80 protected transient int dumpCount = 0; 81 82 protected transient ObjectInputStream ois; 83 84 protected transient ObjectOutputStream oos; 85 86 protected transient ByteArrayOutputStream bao; 87 88 protected void t_MixPrimitivesAndObjects() throws IOException, 89 ClassNotFoundException { 90 int i = 7; 91 String s1 = "string 1"; 92 String s2 = "string 2"; 93 byte[] bytes = { 1, 2, 3 }; 94 95 oos.writeInt(i); 96 oos.writeObject(s1); 97 oos.writeUTF(s2); 98 oos.writeObject(bytes); 99 oos.close(); 100 try { 101 ois = new ObjectInputStream(loadStream()); 102 103 int j = ois.readInt(); 104 assertTrue("Wrong int :" + j, i == j); 105 106 String l1 = (String) ois.readObject(); 107 assertTrue("Wrong obj String :" + l1, s1.equals(l1)); 108 109 String l2 = (String) ois.readUTF(); 110 assertTrue("Wrong UTF String :" + l2, s2.equals(l2)); 111 112 byte[] bytes2 = (byte[]) ois.readObject(); 113 assertTrue("Wrong byte[]", Arrays.equals(bytes, bytes2)); 114 115 } finally { 116 ois.close(); 117 } 118 } 119 120 // ----------------------------------------------------------------------------------- 121 122 static final Map TABLE = new Hashtable(); 123 124 static final Map MAP = new HashMap(); 125 126 static final SortedMap TREE = new TreeMap(); 127 128 static final LinkedHashMap LINKEDMAP = new LinkedHashMap(); 129 130 static final LinkedHashSet LINKEDSET = new LinkedHashSet(); 131 132 static final IdentityHashMap IDENTITYMAP = new IdentityHashMap(); 133 134 static final List ALIST = Arrays.asList(new String[] { "a", "list", "of", 135 "strings" }); 136 137 static final List LIST = new ArrayList(ALIST); 138 139 static final Set SET = new HashSet(Arrays.asList(new String[] { "one", 140 "two", "three" })); 141 142 static final Permission PERM = new PropertyPermission("file.encoding", 143 "write"); 144 145 static final PermissionCollection PERMCOL = PERM.newPermissionCollection(); 146 147 static final SortedSet SORTSET = new TreeSet(Arrays.asList(new String[] { 148 "one", "two", "three" })); 149 150 static final java.text.DateFormat DATEFORM = java.text.DateFormat 151 .getInstance(); 152 153 static final java.text.ChoiceFormat CHOICE = new java.text.ChoiceFormat( 154 "1#one|2#two|3#three"); 155 156 static final java.text.NumberFormat NUMBERFORM = java.text.NumberFormat 157 .getInstance(); 158 159 static final java.text.MessageFormat MESSAGE = new java.text.MessageFormat( 160 "the time: {0,time} and date {0,date}"); 161 162 static final LinkedList LINKEDLIST = new LinkedList(Arrays 163 .asList(new String[] { "a", "linked", "list", "of", "strings" })); 164 165 static final SimpleTimeZone TIME_ZONE = new SimpleTimeZone(3600000, 166 "S-TEST"); 167 168 static final Calendar CALENDAR = new GregorianCalendar(TIME_ZONE); 169 170 static Exception INITIALIZE_EXCEPTION = null; 171 172 static { 173 try { 174 TABLE.put("one", "1"); 175 TABLE.put("two", "2"); 176 TABLE.put("three", "3"); 177 MAP.put("one", "1"); 178 MAP.put("two", "2"); 179 MAP.put("three", "3"); 180 LINKEDMAP.put("one", "1"); 181 LINKEDMAP.put("two", "2"); 182 LINKEDMAP.put("three", "3"); 183 IDENTITYMAP.put("one", "1"); 184 IDENTITYMAP.put("two", "2"); 185 IDENTITYMAP.put("three", "3"); 186 LINKEDSET.add("one"); 187 LINKEDSET.add("two"); 188 LINKEDSET.add("three"); 189 TREE.put("one", "1"); 190 TREE.put("two", "2"); 191 TREE.put("three", "3"); 192 PERMCOL.add(PERM); 193 // To make sure they all use the same Calendar 194 CALENDAR.setTimeZone(new SimpleTimeZone(0, "GMT")); 195 CALENDAR.set(1999, Calendar.JUNE, 23, 15, 47, 13); 196 CALENDAR.set(Calendar.MILLISECOND, 553); 197 DATEFORM.setCalendar(CALENDAR); 198 java.text.DateFormatSymbols symbols = new java.text.DateFormatSymbols(); 199 symbols.setZoneStrings(new String[][] { { "a", "b", "c", "d" }, 200 { "e", "f", "g", "h" } }); 201 ((java.text.SimpleDateFormat) DATEFORM).setDateFormatSymbols(symbols); 202 DATEFORM.setNumberFormat(new java.text.DecimalFormat("#.#;'-'#.#")); 203 DATEFORM.setTimeZone(TimeZone.getTimeZone("EST")); 204 ((java.text.DecimalFormat) NUMBERFORM).applyPattern("#.#;'-'#.#"); 205 MESSAGE.setFormat(0, DATEFORM); 206 MESSAGE.setFormat(1, DATEFORM); 207 } catch (Exception e) { 208 INITIALIZE_EXCEPTION = e; 209 } 210 } 211 212 public SerializationStressTest() { 213 } 214 215 public SerializationStressTest(String name) { 216 super(name); 217 } 218 219 public String getDumpName() { 220 return getName() + dumpCount; 221 } 222 223 protected void dump(Object o) throws IOException, ClassNotFoundException { 224 if (dumpCount > 0) 225 setUp(); 226 // Dump the object 227 try { 228 oos.writeObject(o); 229 } finally { 230 oos.close(); 231 } 232 } 233 234 protected Object dumpAndReload(Object o) throws IOException, 235 ClassNotFoundException { 236 dump(o); 237 return reload(); 238 } 239 240 protected InputStream loadStream() throws IOException { 241 // Choose the load stream 242 if (xload || xdump) { 243 // Load from pre-existing file 244 return new FileInputStream(xFileName + "-" + getDumpName() + ".ser"); 245 } else { 246 // Just load from memory, we dumped to memory 247 return new ByteArrayInputStream(bao.toByteArray()); 248 } 249 } 250 251 protected Object reload() throws IOException, ClassNotFoundException { 252 ois = new ObjectInputStream(loadStream()); 253 dumpCount++; 254 try { 255 return ois.readObject(); 256 } finally { 257 ois.close(); 258 } 259 } 260 261 /** 262 * Sets up the fixture, for example, open a network connection. This method 263 * is called before a test is executed. 264 */ 265 protected void setUp() { 266 if (INITIALIZE_EXCEPTION != null) { 267 throw new ExceptionInInitializerError(INITIALIZE_EXCEPTION); 268 } 269 try { 270 if (xdump) { 271 oos = new ObjectOutputStream(new FileOutputStream(xFileName 272 + "-" + getDumpName() + ".ser")); 273 } else { 274 oos = new ObjectOutputStream(bao = new ByteArrayOutputStream()); 275 } 276 } catch (Exception e) { 277 fail("Exception thrown during setup : " + e.getMessage()); 278 } 279 } 280 281 /** 282 * Tears down the fixture, for example, close a network connection. This 283 * method is called after a test is executed. 284 */ 285 protected void tearDown() { 286 if (oos != null) { 287 try { 288 oos.close(); 289 } catch (Exception e) { 290 } 291 } 292 } 293 294 295} 296