001: /*
002: *******************************************************************************
003: * Copyright (C) 2005-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: *
007: */
008:
009: package com.ibm.icu.dev.test.serializable;
010:
011: import java.io.ByteArrayInputStream;
012: import java.io.ByteArrayOutputStream;
013: import java.io.File;
014: import java.io.FileOutputStream;
015: import java.io.IOException;
016: import java.io.ObjectOutputStream;
017: import java.lang.reflect.Field;
018: import java.lang.reflect.Modifier;
019: import java.net.URL;
020:
021: import com.ibm.icu.impl.URLHandler;
022:
023: /**
024: * @author emader
025: *
026: * TODO To change the template for this generated type comment go to
027: * Window - Preferences - Java - Code Style - Code Templates
028: */
029: public class CoverageTest extends CompatibilityTest implements
030: URLHandler.URLVisitor {
031:
032: private static Class serializable;
033:
034: public void init() {
035: try {
036: serializable = Class.forName("java.io.Serializable");
037: } catch (Exception e) {
038: // we're in deep trouble...
039: warnln("Woops! Can't get class info for Serializable.");
040: }
041: }
042:
043: private Target head = new Target(null);
044: private Target tail = head;
045:
046: private String path;
047:
048: public CoverageTest() {
049: this (null);
050: }
051:
052: public CoverageTest(String path) {
053: this .path = path;
054:
055: if (path != null) {
056: File dir = new File(path);
057:
058: if (!dir.exists()) {
059: dir.mkdirs();
060: }
061: }
062: }
063:
064: private void writeFile(String className, byte bytes[]) {
065: File file = new File(path + File.separator + className + ".dat");
066: FileOutputStream stream;
067:
068: try {
069: stream = new FileOutputStream(file);
070:
071: stream.write(bytes);
072: stream.close();
073: } catch (Exception e) {
074: System.out.print(" - can't write file!");
075: }
076: }
077:
078: private void add(String className, int classModifiers, byte bytes[]) {
079: CoverageTarget newTarget = new CoverageTarget(className,
080: classModifiers, bytes);
081:
082: tail.setNext(newTarget);
083: tail = newTarget;
084: }
085:
086: public class CoverageTarget extends HandlerTarget {
087: private byte bytes[];
088: private int modifiers;
089:
090: public CoverageTarget(String className, int classModifiers,
091: byte bytes[]) {
092: super (className, bytes == null ? null
093: : new ByteArrayInputStream(bytes));
094:
095: this .bytes = bytes;
096: modifiers = classModifiers;
097: }
098:
099: public boolean validate() {
100: return super .validate() || Modifier.isAbstract(modifiers);
101: }
102:
103: public void execute() throws Exception {
104: if (inputStream == null) {
105: params.testCount += 1;
106: } else {
107: Class c = Class.forName(name);
108: try {
109: Field uid = c.getDeclaredField("serialVersionUID");
110: } catch (Exception e) {
111: errln("No serialVersionUID");
112: }
113:
114: if (path != null) {
115: writeFile(name, bytes);
116: }
117:
118: super .execute();
119: }
120: }
121: }
122:
123: public void visit(String str) {
124: if (serializable == null) {
125: return;
126: }
127: int ix = str.lastIndexOf(".class");
128:
129: if (ix >= 0) {
130: String className = "com.ibm.icu"
131: + str.substring(0, ix).replace('/', '.');
132:
133: // Skip things in com.ibm.icu.dev; they're not relevant.
134: if (className.startsWith("com.ibm.icu.dev.")) {
135: return;
136: }
137:
138: try {
139: Class c = Class.forName(className);
140: int m = c.getModifiers();
141:
142: if (serializable.isAssignableFrom(c)) {
143: if (Modifier.isPublic(m)) {
144: SerializableTest.Handler handler = SerializableTest
145: .getHandler(className);
146:
147: if (handler != null) {
148: Object objectsOut[] = handler
149: .getTestObjects();
150: Object objectsIn[];
151: boolean passed = true;
152:
153: ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
154: ObjectOutputStream out = new ObjectOutputStream(
155: byteOut);
156:
157: try {
158: out.writeObject(objectsOut);
159: out.close();
160: byteOut.close();
161: } catch (IOException e) {
162: warnln("Error writing test objects: "
163: + e.toString());
164: return;
165: }
166:
167: add(className, m, byteOut.toByteArray());
168: } else {
169: add(className, m, null);
170: }
171: }
172: }
173: } catch (Exception e) {
174: warnln("coverage of " + className + ": " + e.toString());
175: } catch (Throwable e) {
176: warnln("coverage of " + className + ": " + e.toString());
177: }
178: }
179: }
180:
181: protected Target getTargets(String targetName) {
182:
183: if (System.getSecurityManager() != null) {
184: // This test won't run under a security manager
185: return new CoverageTarget(
186: "Skipped Due To Security Manager",
187: Modifier.ABSTRACT, null);
188: }
189:
190: if (serializable == null) {
191: init();
192: }
193: URL url = getClass().getResource("/com/ibm/icu");
194: URLHandler handler = URLHandler.get(url);
195:
196: handler.guide(this , true, false);
197:
198: return head.getNext();
199: }
200:
201: public static void main(String[] args) {
202: CoverageTest test = new CoverageTest();
203:
204: test.run(args);
205: }
206: }
|