KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > RegressionTest


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.test;
12
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.PrintStream JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21
22 /**
23  * Check the output of several tests for regressions.
24  */

25 public class RegressionTest {
26
27     PrintStream JavaDoc output;
28     String JavaDoc oldFilename, newFilename, outFilename;
29     public static final String JavaDoc NOTHING_CHANGED_MSG
30         = "All tests unchanged.";
31     /**
32      * Constructor for RegressionTest
33      */

34     public RegressionTest( String JavaDoc oldFilename,
35                             String JavaDoc newFilename,
36                             String JavaDoc outFilename) {
37         this.oldFilename = oldFilename;
38         this.newFilename = newFilename;
39         this.outFilename = outFilename;
40     }
41
42     public static void main(String JavaDoc[] argv) {
43         if (argv.length < 3) {
44             System.err.println("Error: too few arguments");
45             System.err.println("Usage: (progname) oldfile newfile outfile");
46         } else {
47             // ASSERT: The program has at least the correct number of arguments
48
RegressionTest rt = new RegressionTest(argv[0], argv[1], argv[2]);
49             rt.testRegressions();
50         }
51     }
52
53     /**
54      * Test for regressions in the test suite.
55      */

56     public void testRegressions() {
57         // Read the old and new files
58
String JavaDoc oldPass = "";
59         String JavaDoc newPass = "";
60         try {
61             oldPass = readFile(oldFilename);
62             newPass = readFile(newFilename);
63         } catch (Exception JavaDoc e) {
64             System.err.println("Error opening input file");
65             System.err.println(e.getMessage());
66             System.exit(-1);
67         }
68
69         try {
70             output = new PrintStream JavaDoc(
71                         new BufferedOutputStream JavaDoc(
72                             new FileOutputStream JavaDoc(
73                                 new File JavaDoc(outFilename))));
74         } catch (Exception JavaDoc e) {
75             System.err.println("Error opening output file");
76             System.err.println(e.getMessage());
77             System.exit(-1);
78         }
79         // Establish their relationship
80
StringTokenizer JavaDoc oldst = new StringTokenizer JavaDoc(oldPass);
81         StringTokenizer JavaDoc newst = new StringTokenizer JavaDoc(newPass);
82
83         String JavaDoc[] oldTest = nextTest(oldst);
84         String JavaDoc[] newTest = nextTest(newst);
85
86         boolean nothingChanged = true;
87         while (oldTest != null && newTest != null) {
88
89             // Compare the two test names
90
int compareName = oldTest[0].compareTo(newTest[0]);
91             if (compareName == 0) {
92                 int compareStatus = oldTest[1].compareTo(newTest[1]);
93                 if (compareStatus != 0) {
94                     nothingChanged = false;
95                     output.println(testChanged(newTest));
96                 }
97                 oldTest = nextTest(oldst);
98                 newTest = nextTest(newst);
99             } else if (compareName < 0) {
100                 // oldTestName comes first
101
output.println(testNotRun(oldTest));
102                 oldTest = nextTest(oldst);
103                 nothingChanged = false;
104             } else {
105                 // newTestName comes first
106
output.println(testAdded(newTest));
107                 newTest = nextTest(newst);
108                 nothingChanged = false;
109             }
110         }
111         // Make sure all tests are parsed
112
while (oldTest != null) {
113             // oldTestName comes first
114
output.println(testNotRun(oldTest));
115             oldTest = nextTest(oldst);
116             nothingChanged = false;
117         }
118         while (newTest != null) {
119             // newTestName comes first
120
output.println(testAdded(newTest));
121             newTest = nextTest(newst);
122             nothingChanged = false;
123         }
124         // Make sure that there is always some output.
125
if (nothingChanged) {
126             output.println(NOTHING_CHANGED_MSG);
127         }
128         output.close();
129     }
130
131     /**
132      * Get the message for when a test is not run.
133      */

134     static String JavaDoc testNotRun(String JavaDoc[] test) {
135         return "Not run: " + test[0];
136     }
137     
138     /**
139      * Get the message for when a test's status changes.
140      */

141     static String JavaDoc testChanged(String JavaDoc[] test) {
142         return "Changed: " + test[0] + ", " + test[1];
143     }
144     
145     /**
146      * Get the message for when a test is added.
147      */

148     static String JavaDoc testAdded(String JavaDoc[] test) {
149         return "New test: " + test[0] + ", Status: " + test[1];
150     }
151
152     /**
153      * Read the file given by s, and return its contents.
154      */

155     static String JavaDoc readFile(String JavaDoc s) throws IOException JavaDoc {
156         byte[] buf = new byte[8192];
157         FileInputStream JavaDoc r = new FileInputStream JavaDoc(s);
158         ByteArrayOutputStream JavaDoc aStream = new ByteArrayOutputStream JavaDoc();
159         int n;
160         while ((n = r.read(buf)) != -1) {
161             aStream.write(buf, 0, n);
162         }
163         r.close();
164         return aStream.toString();
165     }
166     
167     
168     /**
169      * Returns the next 2 tokens in st, if they exist.
170      * Returns null if they do not.
171      */

172     static String JavaDoc[] nextTest(StringTokenizer JavaDoc st) {
173         String JavaDoc[] test = new String JavaDoc[2];
174         if (st.hasMoreTokens()) {
175             test[0] = st.nextToken();
176         } else {
177             return null;
178         }
179         if (st.hasMoreTokens()) {
180             test[1] = st.nextToken();
181         } else {
182             return null;
183         }
184         return test;
185     }
186 }
187
188
Popular Tags