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