2 *******************************************************************************
\r
3 * Copyright (C) 1996-2008, 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.File;
\r
12 import java.io.FileInputStream;
\r
13 import java.io.FileNotFoundException;
\r
14 import java.io.InputStream;
\r
15 import java.io.ObjectInputStream;
\r
16 import java.net.JarURLConnection;
\r
17 import java.net.URL;
\r
18 import java.util.Enumeration;
\r
19 import java.util.MissingResourceException;
\r
20 import java.util.jar.JarEntry;
\r
21 import java.util.jar.JarFile;
\r
23 import com.ibm.icu.dev.test.TestFmwk;
\r
24 //import com.ibm.icu.dev.test.serializable.SerializableTest;
\r
29 public class CompatibilityTest extends TestFmwk
\r
31 public class FolderTarget extends Target
\r
33 private Target head = new Target(null);
\r
34 private Target tail = head;
\r
36 public FolderTarget(String name)
\r
41 public void add(String className, InputStream is)
\r
43 HandlerTarget newTarget = new HandlerTarget(className, is);
\r
45 tail.setNext(newTarget);
\r
49 protected boolean validate()
\r
54 protected void execute() throws Exception
\r
56 params.indentLevel += 1;
\r
58 for (Target target = head.getNext(); target != null; target = target.getNext())
\r
63 params.indentLevel -= 1;
\r
67 public class HandlerTarget extends Target
\r
69 protected SerializableTest.Handler handler = null;
\r
70 protected InputStream inputStream = null;
\r
72 public HandlerTarget(String name, InputStream is)
\r
78 protected boolean validate()
\r
80 handler = SerializableTest.getHandler(name);
\r
82 return handler != null;
\r
85 protected void execute() throws Exception
\r
87 if (params.inDocMode()) {
\r
88 // nothing to execute
\r
89 } else if (!params.stack.included) {
\r
90 ++params.invalidCount;
\r
92 params.testCount += 1;
\r
95 ObjectInputStream in = new ObjectInputStream(inputStream);
\r
96 Object inputObjects[] = (Object[]) in.readObject();
\r
97 Object testObjects[] = handler.getTestObjects();
\r
100 inputStream.close();
\r
102 // TODO: add equality test...
\r
103 // The commented out code below does that,
\r
104 // but some test objects don't define an equals() method,
\r
105 // and the default method is the same as the "==" operator...
\r
106 for (int i = 0; i < testObjects.length; i += 1) {
\r
107 // if (! inputObjects[i].equals(testObjects[i])) {
\r
108 // errln("Input object " + i + " failed equality test.");
\r
111 if (! handler.hasSameBehavior(inputObjects[i], testObjects[i])) {
\r
112 warnln("Input object " + i + " failed behavior test.");
\r
115 }catch (MissingResourceException e){
\r
116 warnln("Could not load the data. "+e.getMessage());
\r
117 } catch (Exception e) {
\r
118 e.printStackTrace();
\r
119 errln("Exception: " + e.toString());
\r
126 private static final String[][] SKIP_CASES = {
\r
127 {"ICU_3.8.1", "com.ibm.icu.text.PluralFormat.dat"},
\r
128 {"ICU_3.8.1", "com.ibm.icu.text.PluralRules.dat"},
\r
129 // The case below actually works OK with 4.2M1 on JRE5, but not on JRE1.4.2.
\r
130 // It looks the use of anonymous inner class is the root cause. If we want
\r
131 // to fix the root cause, it will likely break the backward compatibility.
\r
132 // For now, we're skipping this case even it works OK on JRE5. See ticket#6550.
\r
133 {"ICU_4.0", "com.ibm.icu.text.PluralRules.dat"},
\r
134 {"ICU_3.6", "com.ibm.icu.text.RuleBasedNumberFormat.dat"},
\r
135 {"ICU_3.8.1", "com.ibm.icu.text.RuleBasedNumberFormat.dat"},
\r
136 {"ICU_4.0", "com.ibm.icu.text.RuleBasedNumberFormat.dat"},
\r
139 private Target getFileTargets(URL fileURL)
\r
141 File topDir = new File(fileURL.getPath());
\r
142 File dataDirs[] = topDir.listFiles();
\r
143 FolderTarget target = null;
\r
145 for (int d = 0; d < dataDirs.length; d += 1) {
\r
146 File dataDir = dataDirs[d];
\r
148 if (dataDir.isDirectory()) {
\r
149 FolderTarget newTarget = new FolderTarget(dataDir.getName());
\r
150 File files[] = dataDir.listFiles();
\r
152 newTarget.setNext(target);
\r
153 target = newTarget;
\r
155 String dataDirName = dataDir.getName();
\r
158 for (int i = 0; i < files.length; i += 1) {
\r
159 File file = files[i];
\r
160 String filename = file.getName();
\r
161 int ix = filename.indexOf(".dat");
\r
164 String className = filename.substring(0, ix);
\r
166 // Skip some cases which do not work well
\r
167 for (int j = 0; j < SKIP_CASES.length; j++) {
\r
168 if (dataDirName.equals(SKIP_CASES[j][0]) && filename.equals(SKIP_CASES[j][1])) {
\r
169 logln("Skipping test case - " + dataDirName + "/" + className);
\r
170 continue element_loop;
\r
177 is = new FileInputStream(file);
\r
178 target.add(className, is);
\r
179 } catch (FileNotFoundException e) {
\r
180 errln("Exception: " + e.toString());
\r
191 private Target getJarTargets(URL jarURL)
\r
193 String prefix = jarURL.getPath();
\r
194 String currentDir = null;
\r
195 int ix = prefix.indexOf("!/");
\r
197 FolderTarget target = null;
\r
200 prefix = prefix.substring(ix + 2);
\r
204 JarURLConnection conn = (JarURLConnection) jarURL.openConnection();
\r
206 jarFile = conn.getJarFile();
\r
208 Enumeration entries = jarFile.entries();
\r
211 while (entries.hasMoreElements()) {
\r
212 JarEntry entry = (JarEntry)entries.nextElement();
\r
213 String name = entry.getName();
\r
215 if (name.startsWith(prefix)) {
\r
216 name = name.substring(prefix.length());
\r
218 if (! entry.isDirectory()) {
\r
219 int dx = name.lastIndexOf("/");
\r
220 String dirName = name.substring(1, dx);
\r
221 String filename = name.substring(dx + 1);
\r
223 if (! dirName.equals(currentDir)) {
\r
224 currentDir = dirName;
\r
226 FolderTarget newTarget = new FolderTarget(currentDir);
\r
228 newTarget.setNext(target);
\r
229 target = newTarget;
\r
232 int xx = filename.indexOf(".dat");
\r
235 String className = filename.substring(0, xx);
\r
237 // Skip some cases which do not work well
\r
238 for (int i = 0; i < SKIP_CASES.length; i++) {
\r
239 if (dirName.equals(SKIP_CASES[i][0]) && filename.equals(SKIP_CASES[i][1])) {
\r
240 logln("Skipping test case - " + dirName + "/" + className);
\r
241 continue element_loop;
\r
245 target.add(className, jarFile.getInputStream(entry));
\r
250 } catch (Exception e) {
\r
251 errln("jar error: " + e.getMessage());
\r
257 protected Target getTargets(String targetName)
\r
259 URL dataURL = getClass().getResource("data");
\r
260 String protocol = dataURL.getProtocol();
\r
262 if (protocol.equals("jar")) {
\r
263 return getJarTargets(dataURL);
\r
264 } else if (protocol.equals("file")) {
\r
265 return getFileTargets(dataURL);
\r
267 errln("Don't know how to test " + dataURL.getPath());
\r
272 public static void main(String[] args)
\r
274 CompatibilityTest test = new CompatibilityTest();
\r