001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-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.File;
012: import java.io.FileInputStream;
013: import java.io.FileNotFoundException;
014: import java.io.InputStream;
015: import java.io.ObjectInputStream;
016: import java.net.JarURLConnection;
017: import java.net.URL;
018: import java.util.Enumeration;
019: import java.util.MissingResourceException;
020: import java.util.jar.JarEntry;
021: import java.util.jar.JarFile;
022:
023: import com.ibm.icu.dev.test.TestFmwk;
024:
025: //import com.ibm.icu.dev.test.serializable.SerializableTest;
026:
027: /**
028: * @author emader
029: *
030: * TODO To change the template for this generated type comment go to
031: * Window - Preferences - Java - Code Style - Code Templates
032: */
033: public class CompatibilityTest extends TestFmwk {
034: public class FolderTarget extends Target {
035: private Target head = new Target(null);
036: private Target tail = head;
037:
038: public FolderTarget(String name) {
039: super (name);
040: }
041:
042: public void add(String className, InputStream is) {
043: HandlerTarget newTarget = new HandlerTarget(className, is);
044:
045: tail.setNext(newTarget);
046: tail = newTarget;
047: }
048:
049: protected boolean validate() {
050: return true;
051: }
052:
053: protected void execute() throws Exception {
054: params.indentLevel += 1;
055:
056: for (Target target = head.getNext(); target != null; target = target
057: .getNext()) {
058: target.run();
059: }
060:
061: params.indentLevel -= 1;
062: }
063: }
064:
065: public class HandlerTarget extends Target {
066: protected SerializableTest.Handler handler = null;
067: protected InputStream inputStream = null;
068:
069: public HandlerTarget(String name, InputStream is) {
070: super (name);
071: inputStream = is;
072: }
073:
074: protected boolean validate() {
075: handler = SerializableTest.getHandler(name);
076:
077: return handler != null;
078: }
079:
080: protected void execute() throws Exception {
081: if (params.inDocMode()) {
082: // nothing to execute
083: } else if (!params.stack.included) {
084: ++params.invalidCount;
085: } else {
086: params.testCount += 1;
087:
088: try {
089: ObjectInputStream in = new ObjectInputStream(
090: inputStream);
091: Object inputObjects[] = (Object[]) in.readObject();
092: Object testObjects[] = handler.getTestObjects();
093: boolean passed = true;
094:
095: in.close();
096: inputStream.close();
097:
098: // TODO: add equality test...
099: // The commented out code below does that,
100: // but some test objects don't define an equals() method,
101: // and the default method is the same as the "==" operator...
102: for (int i = 0; i < testObjects.length; i += 1) {
103: // if (! inputObjects[i].equals(testObjects[i])) {
104: // errln("Input object " + i + " failed equality test.");
105: // }
106:
107: if (!handler.hasSameBehavior(inputObjects[i],
108: testObjects[i])) {
109: warnln("Input object " + i
110: + " failed behavior test.");
111: }
112: }
113: } catch (MissingResourceException e) {
114: warnln("Could not load the data. " + e.getMessage());
115: } catch (Exception e) {
116: errln("Exception: " + e.toString());
117: }
118: }
119: }
120: }
121:
122: private Target getFileTargets(URL fileURL) {
123: File topDir = new File(fileURL.getPath());
124: File dataDirs[] = topDir.listFiles();
125: FolderTarget target = null;
126:
127: for (int d = 0; d < dataDirs.length; d += 1) {
128: File dataDir = dataDirs[d];
129:
130: if (dataDir.isDirectory()) {
131: FolderTarget newTarget = new FolderTarget(dataDir
132: .getName());
133: File files[] = dataDir.listFiles();
134:
135: newTarget.setNext(target);
136: target = newTarget;
137:
138: for (int i = 0; i < files.length; i += 1) {
139: File file = files[i];
140: String filename = file.getName();
141: int ix = filename.indexOf(".dat");
142:
143: if (ix > 0) {
144: String className = filename.substring(0, ix);
145: InputStream is;
146:
147: try {
148: is = new FileInputStream(file);
149: target.add(className, is);
150: } catch (FileNotFoundException e) {
151: errln("Exception: " + e.toString());
152: }
153:
154: }
155: }
156: }
157: }
158:
159: return target;
160: }
161:
162: private Target getJarTargets(URL jarURL) {
163: String prefix = jarURL.getPath();
164: String currentDir = null;
165: int ix = prefix.indexOf("!/");
166: JarFile jarFile;
167: FolderTarget target = null;
168:
169: if (ix >= 0) {
170: prefix = prefix.substring(ix + 2);
171: }
172:
173: try {
174: JarURLConnection conn = (JarURLConnection) jarURL
175: .openConnection();
176:
177: jarFile = conn.getJarFile();
178:
179: Enumeration entries = jarFile.entries();
180:
181: while (entries.hasMoreElements()) {
182: JarEntry entry = (JarEntry) entries.nextElement();
183: String name = entry.getName();
184:
185: if (name.startsWith(prefix)) {
186: name = name.substring(prefix.length());
187:
188: if (!entry.isDirectory()) {
189: int dx = name.lastIndexOf("/");
190: String dirName = name.substring(1, dx);
191: String filename = name.substring(dx + 1);
192:
193: if (!dirName.equals(currentDir)) {
194: currentDir = dirName;
195:
196: FolderTarget newTarget = new FolderTarget(
197: currentDir);
198:
199: newTarget.setNext(target);
200: target = newTarget;
201: }
202:
203: int xx = filename.indexOf(".dat");
204:
205: if (xx > 0) {
206: String className = filename
207: .substring(0, xx);
208:
209: target.add(className, jarFile
210: .getInputStream(entry));
211: }
212: }
213: }
214: }
215: } catch (Exception e) {
216: errln("jar error: " + e.getMessage());
217: }
218:
219: return target;
220: }
221:
222: protected Target getTargets(String targetName) {
223: URL dataURL = getClass().getResource("data");
224: String protocol = dataURL.getProtocol();
225:
226: if (protocol.equals("jar")) {
227: return getJarTargets(dataURL);
228: } else if (protocol.equals("file")) {
229: return getFileTargets(dataURL);
230: } else {
231: errln("Don't know how to test " + dataURL.getPath());
232: return null;
233: }
234: }
235:
236: public static void main(String[] args) {
237: CompatibilityTest test = new CompatibilityTest();
238:
239: test.run(args);
240: }
241: }
|