2 *******************************************************************************
\r
3 * Copyright (C) 2005-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 package com.ibm.icu.dev.test.serializable;
\r
11 import java.io.ByteArrayInputStream;
\r
12 import java.io.ByteArrayOutputStream;
\r
13 import java.io.File;
\r
14 import java.io.FileOutputStream;
\r
15 import java.io.IOException;
\r
16 import java.io.ObjectOutputStream;
\r
17 import java.lang.reflect.Modifier;
\r
18 import java.net.URL;
\r
20 import com.ibm.icu.impl.URLHandler;
\r
25 * TODO To change the template for this generated type comment go to
\r
26 * Window - Preferences - Java - Code Style - Code Templates
\r
28 public class CoverageTest extends CompatibilityTest implements URLHandler.URLVisitor
\r
31 private static Class serializable;
\r
33 public void init() {
\r
35 serializable = Class.forName("java.io.Serializable");
\r
36 } catch (Exception e) {
\r
37 // we're in deep trouble...
\r
38 warnln("Woops! Can't get class info for Serializable.");
\r
42 private Target head = new Target(null);
\r
43 private Target tail = head;
\r
45 private String path;
\r
47 public CoverageTest()
\r
52 public CoverageTest(String path)
\r
57 File dir = new File(path);
\r
59 if (!dir.exists()) {
\r
65 private void writeFile(String className, byte bytes[])
\r
67 File file = new File(path + File.separator + className + ".dat");
\r
68 FileOutputStream stream;
\r
71 stream = new FileOutputStream(file);
\r
73 stream.write(bytes);
\r
75 } catch (Exception e) {
\r
76 System.out.print(" - can't write file!");
\r
80 private void add(String className, int classModifiers, byte bytes[])
\r
82 CoverageTarget newTarget = new CoverageTarget(className, classModifiers, bytes);
\r
84 tail.setNext(newTarget);
\r
88 public class CoverageTarget extends HandlerTarget
\r
90 private byte bytes[];
\r
91 private int modifiers;
\r
93 public CoverageTarget(String className, int classModifiers, byte bytes[])
\r
95 super(className, bytes == null? null : new ByteArrayInputStream(bytes));
\r
98 modifiers = classModifiers;
\r
101 public boolean validate()
\r
103 return super.validate() || Modifier.isAbstract(modifiers);
\r
106 public void execute() throws Exception
\r
108 Class c = Class.forName(name);
\r
110 /*Field uid = */c.getDeclaredField("serialVersionUID");
\r
111 } catch (Exception e) {
\r
112 errln("No serialVersionUID");
\r
115 if (inputStream == null) {
\r
116 params.testCount += 1;
\r
118 if (path != null) {
\r
119 writeFile(name, bytes);
\r
127 public void visit(String str)
\r
129 if(serializable==null){
\r
132 int ix = str.lastIndexOf(".class");
\r
135 String className = "com.ibm.icu" + str.substring(0, ix).replace('/', '.');
\r
137 // Skip things in com.ibm.icu.dev; they're not relevant.
\r
138 if (className.startsWith("com.ibm.icu.dev.")) {
\r
143 Class c = Class.forName(className);
\r
144 int m = c.getModifiers();
\r
146 if (serializable.isAssignableFrom(c)) {
\r
147 if (Modifier.isPublic(m) && !Modifier.isInterface(m)) {
\r
148 SerializableTest.Handler handler = SerializableTest.getHandler(className);
\r
150 if (handler != null) {
\r
151 Object objectsOut[] = handler.getTestObjects();
\r
153 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
\r
154 ObjectOutputStream out = new ObjectOutputStream(byteOut);
\r
157 out.writeObject(objectsOut);
\r
160 } catch (IOException e) {
\r
161 warnln("Error writing test objects: " + e.toString());
\r
165 add(className, m, byteOut.toByteArray());
\r
167 add(className, m, null);
\r
171 } catch (Exception e) {
\r
172 e.printStackTrace();
\r
173 warnln("coverage of " + className + ": " + e.toString());
\r
174 } catch (Throwable e) {
\r
175 e.printStackTrace();
\r
176 warnln("coverage of " + className + ": " + e.toString());
\r
181 protected Target getTargets(String targetName)
\r
184 if (System.getSecurityManager() != null) {
\r
185 // This test won't run under a security manager
\r
187 // TODO: Is the above statement really true?
\r
188 // We probably need to set up the security policy properly
\r
189 // for writing/reading serialized data.
\r
194 if(serializable==null){
\r
197 URL url = getClass().getResource("/com/ibm/icu");
\r
198 URLHandler handler = URLHandler.get(url);
\r
200 handler.guide(this, true, false);
\r
202 return head.getNext();
\r
205 public static void main(String[] args)
\r
207 CoverageTest test = new CoverageTest();
\r