001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.lang.reflect.Constructor;
025: import java.lang.reflect.InvocationTargetException;
026: import java.util.Vector;
027: import java.util.zip.ZipEntry;
028: import java.util.zip.ZipInputStream;
029:
030: @SuppressWarnings({"serial","unused"})
031: public class SerializationStressTest5 extends SerializationStressTest {
032:
033: transient Throwable current;
034:
035: // Use this for retrieving a list of any Throwable Classes that did not get
036: // tested.
037: transient Vector<Class> missedV = new Vector<Class>();
038:
039: transient Class[][] params = new Class[][] {
040: { String.class },
041: { Throwable.class },
042: { Exception.class },
043: { String.class, Exception.class },
044: { String.class, int.class },
045: { String.class, String.class, String.class },
046: { String.class, Error.class },
047: { int.class, boolean.class, boolean.class, int.class,
048: int.class }, {} };
049:
050: transient Object[][] args = new Object[][] {
051: { "message" },
052: { new Throwable() },
053: { new Exception("exception") },
054: { "message", new Exception("exception") },
055: { "message", new Integer(5) },
056: { "message", "message", "message" },
057: { "message", new Error("error") },
058: { new Integer(5), new Boolean(false), new Boolean(false),
059: new Integer(5), new Integer(5) }, {} };
060:
061: public SerializationStressTest5(String name) {
062: super (name);
063: }
064:
065: public void test_writeObject_Throwables() {
066: try {
067: oos.close();
068: } catch (IOException e) {
069: }
070:
071: File javaDir = findJavaDir();
072:
073: Vector<File> classFilesVector = new Vector<File>();
074: if (javaDir != null)
075: findClassFiles(javaDir, classFilesVector);
076: else
077: findClassFilesFromZip(classFilesVector);
078:
079: if (classFilesVector.size() == 0) {
080: fail("No Class Files Found.");
081: }
082:
083: File[] classFilesArray = new File[classFilesVector.size()];
084: classFilesVector.copyInto(classFilesArray);
085:
086: Class[] throwableClasses = findThrowableClasses(classFilesArray);
087: findParam(throwableClasses);
088:
089: // Use this to print out the list of Throwable classes that weren't
090: // tested.
091: /*
092: * System.out.println(); Class[] temp = new Class[missedV.size()];
093: * missedV.copyInto(temp); for (int i = 0; i < temp.length; i++)
094: * System.out.println(i+1 + ": " + temp[i].getName());
095: */
096: }
097:
098: private File[] makeClassPathArray() {
099: String classPath;
100: if (System.getProperty("java.vendor").startsWith("IBM"))
101: classPath = System
102: .getProperty("org.apache.harmony.boot.class.path");
103: else
104: classPath = System.getProperty("sun.boot.class.path");
105: int instanceOfSep = -1;
106: int nextInstance = classPath.indexOf(File.pathSeparatorChar,
107: instanceOfSep + 1);
108: Vector<File> elms = new Vector<File>();
109: while (nextInstance != -1) {
110: elms.add(new File(classPath.substring(instanceOfSep + 1,
111: nextInstance)));
112: instanceOfSep = nextInstance;
113: nextInstance = classPath.indexOf(File.pathSeparatorChar,
114: instanceOfSep + 1);
115: }
116: elms.add(new File(classPath.substring(instanceOfSep + 1)));
117: File[] result = new File[elms.size()];
118: elms.copyInto(result);
119: return result;
120: }
121:
122: private File findJavaDir() {
123: File[] files = makeClassPathArray();
124: for (int i = 0; i < files.length; i++) {
125: if (files[i].isDirectory()) {
126: String[] tempFileNames = files[i].list();
127: for (int j = 0; j < tempFileNames.length; j++) {
128: File tempfile = new File(files[i], tempFileNames[j]);
129: if (tempfile.isDirectory()
130: && tempFileNames[j].equals("java")) {
131: String[] subdirNames = tempfile.list();
132: for (int k = 0; k < subdirNames.length; k++) {
133: File subdir = new File(tempfile,
134: subdirNames[k]);
135: if (subdir.isDirectory()
136: && subdirNames[k].equals("lang")) {
137: return tempfile;
138: }
139: }
140: }
141: }
142: }
143: }
144: return null;
145: }
146:
147: private void findClassFiles(File dir, Vector<File> v) {
148: String[] classFileNames = dir.list();
149: for (int i = 0; i < classFileNames.length; i++) {
150: File file = new File(dir, classFileNames[i]);
151: if (file.isDirectory())
152: findClassFiles(file, v);
153: else if (classFileNames[i].endsWith(".class"))
154: v.add(file);
155: }
156: }
157:
158: private Class[] findThrowableClasses(File[] files) {
159: Class<Throwable> thrClass = Throwable.class;
160: Vector<Class> resultVector = new Vector<Class>();
161: String slash = System.getProperty("file.separator");
162: String begTarget = slash + "java" + slash;
163: String endTarget = ".class";
164: for (int i = 0; i < files.length; i++) {
165: String fileName = files[i].getPath();
166: int instOfBegTarget = fileName.indexOf(begTarget);
167: int instOfEndTarget = fileName.indexOf(endTarget);
168: fileName = fileName.substring(instOfBegTarget + 1,
169: instOfEndTarget);
170: fileName = fileName.replace(slash.charAt(0), '.');
171: try {
172: Class theClass = Class.forName(fileName, false,
173: ClassLoader.getSystemClassLoader());
174: if (thrClass.isAssignableFrom(theClass)) {
175: // java.lang.VirtualMachineError is abstract.
176: // java.io.ObjectStreamException is abstract
177: // java.beans.PropertyVetoException needs a
178: // java.beans.PropertyChangeEvent as a parameter
179: if (!fileName
180: .equals("java.lang.VirtualMachineError")
181: && !fileName
182: .equals("java.io.ObjectStreamException")
183: && !fileName
184: .equals("java.beans.PropertyVetoException"))
185: resultVector.add(theClass);
186: }
187: } catch (ClassNotFoundException e) {
188: fail("ClassNotFoundException : " + fileName);
189: }
190: }
191: Class[] result = new Class[resultVector.size()];
192: resultVector.copyInto(result);
193: return result;
194: }
195:
196: private void initClass(Class thrC, int num) {
197: Constructor[] cons = thrC.getConstructors();
198: for (int i = 0; i < cons.length; i++) {
199: try {
200: Throwable obj = (Throwable) cons[i]
201: .newInstance(args[num]);
202: t_Class(obj, num);
203: break;
204: } catch (IllegalArgumentException e) {
205: // This error should be caught until the correct args is hit.
206: } catch (IllegalAccessException e) {
207: fail("IllegalAccessException while creating instance of: "
208: + thrC.getName());
209: } catch (InstantiationException e) {
210: fail("InstantiationException while creating instance of: "
211: + thrC.getName());
212: } catch (InvocationTargetException e) {
213: fail("InvocationTargetException while creating instance of: "
214: + thrC.getName());
215: }
216: if (i == cons.length - 1) {
217: fail("Failed to create newInstance of: "
218: + thrC.getName());
219: }
220: }
221: }
222:
223: public String getDumpName() {
224: if (current == null) {
225: dumpCount++;
226: return getName();
227: }
228: return getName() + "_" + current.getClass().getName();
229: }
230:
231: private void t_Class(Throwable objToSave, int argsNum) {
232: current = objToSave;
233: Object objLoaded = null;
234: try {
235: if (DEBUG)
236: System.out.println("Obj = " + objToSave);
237: try {
238: objLoaded = dumpAndReload(objToSave);
239: } catch (FileNotFoundException e) {
240: // Must be using xload, ignore missing Throwables
241: System.out.println("Ignoring: "
242: + objToSave.getClass().getName());
243: return;
244: }
245:
246: // Has to have worked
247: boolean equals;
248: equals = objToSave.getClass().equals(objLoaded.getClass());
249: assertTrue(MSG_TEST_FAILED + objToSave, equals);
250: if (argsNum == 0 || (argsNum >= 3 && argsNum <= 7)) {
251: equals = ((Throwable) objToSave).getMessage().equals(
252: ((Throwable) objLoaded).getMessage());
253: assertTrue("Message Test: " + MSG_TEST_FAILED
254: + objToSave, equals);
255: } else {
256: // System.out.println(((Throwable)objToSave).getMessage());
257: equals = ((Throwable) objToSave).getMessage() == null;
258: assertTrue("Null Test 1: (args=" + argsNum + ") "
259: + MSG_TEST_FAILED + objToSave, equals);
260: equals = ((Throwable) objLoaded).getMessage() == null;
261: assertTrue("Null Test 2: (args=" + argsNum + ") "
262: + MSG_TEST_FAILED + objToSave, equals);
263: }
264: } catch (IOException e) {
265: fail("Unexpected IOException in checkIt() : "
266: + e.getMessage());
267: } catch (ClassNotFoundException e) {
268: fail(e.toString() + " - testing "
269: + objToSave.getClass().getName());
270: }
271: }
272:
273: private void findParam(Class[] thrC) {
274: for (int i = 0; i < thrC.length; i++) {
275: Constructor con = null;
276: for (int j = 0; j < params.length; j++) {
277: try {
278: con = thrC[i].getConstructor(params[j]);
279: } catch (NoSuchMethodException e) {
280: // This Error will be caught until the right param is found.
281: }
282:
283: if (con != null) {
284: // If the param was found, initialize the Class
285: initClass(thrC[i], j);
286: break;
287: }
288: // If the param not found then add to missed Vector.
289: if (j == params.length - 1)
290: missedV.add(thrC[i]);
291: }
292: }
293: }
294:
295: private void findClassFilesFromZip(Vector<File> v) {
296: String slash = System.getProperty("file.separator");
297: String javaHome = System.getProperty("java.home");
298: if (!javaHome.endsWith(slash))
299: javaHome += slash;
300:
301: String[] wanted = { "java" + slash + "io",
302: "java" + slash + "lang", "java" + slash + "math",
303: "java" + slash + "net", "java" + slash + "security",
304: "java" + slash + "text", "java" + slash + "util",
305: "java" + slash + "beans", "java" + slash + "rmi",
306: // One or more class files in awt make the VM hang after being
307: // loaded.
308: // "java" + slash + "awt",
309: "java" + slash + "sql",
310: // These are (possibly) all of the throwable classes in awt
311: /*
312: * "java\\awt\\AWTError", "java\\awt\\AWTException",
313: * "java\\awt\\color\\CMMException",
314: * "java\\awt\\color\\ProfileDataException",
315: * "java\\awt\\datatransfer\\MimeTypeParseException",
316: * "java\\awt\\datatransfer\\UnsupportedFlavorException",
317: * "java\\awt\\dnd\\InvalidDnDOperationException",
318: * "java\\awt\\FontFormatException",
319: * "java\\awt\\geom\\IllegalPathStateException",
320: * "java\\awt\\geom\\NoninvertibleTransformException",
321: * "java\\awt\\IllegalComponentStateException",
322: * "java\\awt\\image\\ImagingOpException",
323: * "java\\awt\\image\\RasterFormatException",
324: * "java\\awt\\print\\PrinterAbortException",
325: * "java\\awt\\print\\PrinterException",
326: * "java\\awt\\print\\PrinterIOException"
327: */
328: };
329:
330: File[] files = makeClassPathArray();
331: FileInputStream fis = null;
332: ZipInputStream zis = null;
333: ZipEntry ze = null;
334: for (int i = 0; i < files.length; i++) {
335: String fileName = files[i].getPath();
336: if (files[i].exists() && files[i].isFile()
337: && fileName.endsWith(".jar")
338: || fileName.endsWith(".zip")) {
339: try {
340: fis = new FileInputStream(files[i].getPath());
341: } catch (FileNotFoundException e) {
342: fail("FileNotFoundException trying to open "
343: + files[i].getPath());
344: }
345: zis = new ZipInputStream(fis);
346: while (true) {
347: try {
348: ze = zis.getNextEntry();
349: } catch (IOException e) {
350: fail("IOException while getting next zip entry: "
351: + e);
352: }
353: if (ze == null)
354: break;
355: String zeName = ze.getName();
356: if (zeName.endsWith(".class")) {
357: zeName = zeName.replace('/', slash.charAt(0));
358: for (int j = 0; j < wanted.length; j++) {
359: if (zeName.startsWith(wanted[j])) {
360: // When finding class files from directories the
361: // program saves them as files.
362: // To stay consistent we will turn the ZipEntry
363: // classes into instances of files.
364: File tempF = new File(javaHome + zeName);
365: // Making sure that the same class is not added
366: // twice.
367: boolean duplicate = false;
368: for (int k = 0; k < v.size(); k++) {
369: if (v.get(k).equals(tempF))
370: duplicate = true;
371: }
372: if (!duplicate)
373: v.add(tempF);
374: break;
375: }
376: }
377: }
378: }
379: ;
380: try {
381: zis.close();
382: fis.close();
383: } catch (IOException e) {
384: fail("IOException while trying to close InputStreams: "
385: + e);
386: }
387: }
388: }
389: }
390: }
|