2 *******************************************************************************
\r
3 * Copyright (C) 2005-2010, 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
19 import java.util.Enumeration;
\r
21 import com.ibm.icu.impl.URLHandler;
\r
26 * TODO To change the template for this generated type comment go to
\r
27 * Window - Preferences - Java - Code Style - Code Templates
\r
29 public class CoverageTest extends CompatibilityTest implements URLHandler.URLVisitor
\r
32 private static Class serializable;
\r
34 public void init() {
\r
36 serializable = Class.forName("java.io.Serializable");
\r
37 } catch (Exception e) {
\r
38 // we're in deep trouble...
\r
39 warnln("Woops! Can't get class info for Serializable.");
\r
43 private Target head = new Target(null);
\r
44 private Target tail = head;
\r
46 private String path;
\r
48 public CoverageTest()
\r
53 public CoverageTest(String path)
\r
58 File dir = new File(path);
\r
60 if (!dir.exists()) {
\r
66 private void writeFile(String className, byte bytes[])
\r
68 File file = new File(path + File.separator + className + ".dat");
\r
69 FileOutputStream stream;
\r
72 stream = new FileOutputStream(file);
\r
74 stream.write(bytes);
\r
76 } catch (Exception e) {
\r
77 System.out.print(" - can't write file!");
\r
81 private void add(String className, int classModifiers, byte bytes[])
\r
83 CoverageTarget newTarget = new CoverageTarget(className, classModifiers, bytes);
\r
85 tail.setNext(newTarget);
\r
89 public class CoverageTarget extends HandlerTarget
\r
91 private byte bytes[];
\r
92 private int modifiers;
\r
94 public CoverageTarget(String className, int classModifiers, byte bytes[])
\r
96 super(className, bytes == null? null : new ByteArrayInputStream(bytes));
\r
99 modifiers = classModifiers;
\r
102 public boolean validate()
\r
104 return super.validate() || Modifier.isAbstract(modifiers);
\r
107 public void execute() throws Exception
\r
109 Class c = Class.forName(name);
\r
111 /*Field uid = */c.getDeclaredField("serialVersionUID");
\r
112 } catch (Exception e) {
\r
113 errln("No serialVersionUID");
\r
116 if (inputStream == null) {
\r
117 params.testCount += 1;
\r
119 if (path != null) {
\r
120 writeFile(name, bytes);
\r
128 public void visit(String str)
\r
130 if(serializable==null){
\r
133 int ix = str.lastIndexOf(".class");
\r
136 String className = "com.ibm.icu" + str.substring(0, ix).replace('/', '.');
\r
138 // Skip things in com.ibm.icu.dev; they're not relevant.
\r
139 if (className.startsWith("com.ibm.icu.dev.")) {
\r
144 Class c = Class.forName(className);
\r
145 int m = c.getModifiers();
\r
147 if (!c.isEnum() && serializable.isAssignableFrom(c)) {
\r
148 if (Modifier.isPublic(m) && !Modifier.isInterface(m)) {
\r
149 SerializableTest.Handler handler = SerializableTest.getHandler(className);
\r
151 if (handler != null) {
\r
152 Object objectsOut[] = handler.getTestObjects();
\r
154 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
\r
155 ObjectOutputStream out = new ObjectOutputStream(byteOut);
\r
158 out.writeObject(objectsOut);
\r
161 } catch (IOException e) {
\r
162 warnln("Error writing test objects: " + e.toString());
\r
166 add(className, m, byteOut.toByteArray());
\r
168 add(className, m, null);
\r
172 } catch (Exception e) {
\r
173 e.printStackTrace();
\r
174 warnln("coverage of " + className + ": " + e.toString());
\r
175 } catch (Throwable e) {
\r
176 e.printStackTrace();
\r
177 warnln("coverage of " + className + ": " + e.toString());
\r
182 protected Target getTargets(String targetName)
\r
185 if (System.getSecurityManager() != null) {
\r
186 // This test won't run under a security manager
\r
188 // TODO: Is the above statement really true?
\r
189 // We probably need to set up the security policy properly
\r
190 // for writing/reading serialized data.
\r
195 if(serializable==null){
\r
200 Enumeration<URL> urlEnum = getClass().getClassLoader().getResources("com/ibm/icu");
\r
201 while (urlEnum.hasMoreElements()) {
\r
202 URL url = urlEnum.nextElement();
\r
203 URLHandler handler = URLHandler.get(url);
\r
204 handler.guide(this, true, false);
\r
206 } catch (IOException e) {
\r
207 throw new RuntimeException(e);
\r
210 return head.getNext();
\r
213 public static void main(String[] args)
\r
215 CoverageTest test = new CoverageTest();
\r