2 *******************************************************************************
\r
3 * Copyright (C) 1996-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.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
28 public class CompatibilityTest extends TestFmwk
\r
30 public class FolderTarget extends Target
\r
32 private Target head = new Target(null);
\r
33 private Target tail = head;
\r
35 public FolderTarget(String name)
\r
40 public void add(String className, InputStream is)
\r
42 HandlerTarget newTarget = new HandlerTarget(className, is);
\r
44 tail.setNext(newTarget);
\r
48 protected boolean validate()
\r
53 protected void execute() throws Exception
\r
55 params.indentLevel += 1;
\r
57 for (Target target = head.getNext(); target != null; target = target.getNext())
\r
62 params.indentLevel -= 1;
\r
66 public class HandlerTarget extends Target
\r
68 protected SerializableTest.Handler handler = null;
\r
69 protected InputStream inputStream = null;
\r
71 public HandlerTarget(String name, InputStream is)
\r
77 protected boolean validate()
\r
79 handler = SerializableTest.getHandler(name);
\r
81 return handler != null;
\r
84 protected void execute() throws Exception
\r
86 if (params.inDocMode()) {
\r
87 // nothing to execute
\r
88 } else if (!params.stack.included) {
\r
89 ++params.invalidCount;
\r
91 params.testCount += 1;
\r
94 ObjectInputStream in = new ObjectInputStream(inputStream);
\r
95 Object inputObjects[] = (Object[]) in.readObject();
\r
96 Object testObjects[] = handler.getTestObjects();
\r
99 inputStream.close();
\r
101 // TODO: add equality test...
\r
102 // The commented out code below does that,
\r
103 // but some test objects don't define an equals() method,
\r
104 // and the default method is the same as the "==" operator...
\r
105 for (int i = 0; i < testObjects.length; i += 1) {
\r
106 // if (! inputObjects[i].equals(testObjects[i])) {
\r
107 // errln("Input object " + i + " failed equality test.");
\r
110 if (! handler.hasSameBehavior(inputObjects[i], testObjects[i])) {
\r
111 warnln("Input object " + i + " failed behavior test.");
\r
114 }catch (MissingResourceException e){
\r
115 warnln("Could not load the data. "+e.getMessage());
\r
116 } catch (Exception e) {
\r
117 e.printStackTrace();
\r
118 errln("Exception: " + e.toString());
\r
125 private static final String[][] SKIP_CASES = {
\r
126 {"ICU_3.8.1", "com.ibm.icu.text.PluralFormat.dat"},
\r
127 {"ICU_3.8.1", "com.ibm.icu.text.PluralRules.dat"},
\r
128 {"ICU_3.6", "com.ibm.icu.text.RuleBasedNumberFormat.dat"},
\r
129 {"ICU_3.8.1", "com.ibm.icu.text.RuleBasedNumberFormat.dat"},
\r
130 {"ICU_4.0", "com.ibm.icu.text.RuleBasedNumberFormat.dat"},
\r
131 {"ICU_4.2.1", "com.ibm.icu.text.CurrencyPluralInfo.dat"},
\r
134 private Target getFileTargets(URL fileURL)
\r
136 File topDir = new File(fileURL.getPath());
\r
137 File dataDirs[] = topDir.listFiles();
\r
138 FolderTarget target = null;
\r
140 for (int d = 0; d < dataDirs.length; d += 1) {
\r
141 File dataDir = dataDirs[d];
\r
143 if (dataDir.isDirectory()) {
\r
144 FolderTarget newTarget = new FolderTarget(dataDir.getName());
\r
145 File files[] = dataDir.listFiles();
\r
147 newTarget.setNext(target);
\r
148 target = newTarget;
\r
150 String dataDirName = dataDir.getName();
\r
153 for (int i = 0; i < files.length; i += 1) {
\r
154 File file = files[i];
\r
155 String filename = file.getName();
\r
156 int ix = filename.indexOf(".dat");
\r
159 String className = filename.substring(0, ix);
\r
161 // Skip some cases which do not work well
\r
162 for (int j = 0; j < SKIP_CASES.length; j++) {
\r
163 if (dataDirName.equals(SKIP_CASES[j][0]) && filename.equals(SKIP_CASES[j][1])) {
\r
164 logln("Skipping test case - " + dataDirName + "/" + className);
\r
165 continue element_loop;
\r
172 is = new FileInputStream(file);
\r
173 target.add(className, is);
\r
174 } catch (FileNotFoundException e) {
\r
175 errln("Exception: " + e.toString());
\r
186 private Target getJarTargets(URL jarURL)
\r
188 String prefix = jarURL.getPath();
\r
189 String currentDir = null;
\r
190 int ix = prefix.indexOf("!/");
\r
192 FolderTarget target = null;
\r
195 prefix = prefix.substring(ix + 2);
\r
199 JarURLConnection conn = (JarURLConnection) jarURL.openConnection();
\r
201 jarFile = conn.getJarFile();
\r
203 Enumeration entries = jarFile.entries();
\r
206 while (entries.hasMoreElements()) {
\r
207 JarEntry entry = (JarEntry)entries.nextElement();
\r
208 String name = entry.getName();
\r
210 if (name.startsWith(prefix)) {
\r
211 name = name.substring(prefix.length());
\r
213 if (! entry.isDirectory()) {
\r
214 int dx = name.lastIndexOf("/");
\r
215 String dirName = name.substring(1, dx);
\r
216 String filename = name.substring(dx + 1);
\r
218 if (! dirName.equals(currentDir)) {
\r
219 currentDir = dirName;
\r
221 FolderTarget newTarget = new FolderTarget(currentDir);
\r
223 newTarget.setNext(target);
\r
224 target = newTarget;
\r
227 int xx = filename.indexOf(".dat");
\r
230 String className = filename.substring(0, xx);
\r
232 // Skip some cases which do not work well
\r
233 for (int i = 0; i < SKIP_CASES.length; i++) {
\r
234 if (dirName.equals(SKIP_CASES[i][0]) && filename.equals(SKIP_CASES[i][1])) {
\r
235 logln("Skipping test case - " + dirName + "/" + className);
\r
236 continue element_loop;
\r
240 target.add(className, jarFile.getInputStream(entry));
\r
245 } catch (Exception e) {
\r
246 errln("jar error: " + e.getMessage());
\r
252 protected Target getTargets(String targetName)
\r
254 URL dataURL = getClass().getResource("data");
\r
255 String protocol = dataURL.getProtocol();
\r
257 if (protocol.equals("jar")) {
\r
258 return getJarTargets(dataURL);
\r
259 } else if (protocol.equals("file")) {
\r
260 return getFileTargets(dataURL);
\r
262 errln("Don't know how to test " + dataURL.getPath());
\r
267 public static void main(String[] args)
\r
269 CompatibilityTest test = new CompatibilityTest();
\r