KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > recording > RecordingTest


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2003 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

20  
21 package net.innig.macker.recording;
22
23 import net.innig.macker.Macker;
24 import net.innig.macker.rule.*;
25 import net.innig.macker.event.*;
26 import net.innig.macker.structure.*;
27
28 import java.io.File JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.BufferedWriter JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.io.StringWriter JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.*;
35
36 import junit.framework.Test;
37 import junit.framework.TestSuite;
38 import junit.framework.TestResult;
39 import junit.framework.AssertionFailedError;
40
41 import org.jdom.*;
42 import org.jdom.input.SAXBuilder;
43 import org.jdom.output.XMLOutputter;
44
45
46 public final class RecordingTest
47     implements Test
48     {
49     public static final String JavaDoc
50         TEST_DIR_PROP = "Macker.RecordingTest.testDir",
51         BUILD_DIR_PROP = "Macker.RecordingTest.buildDir";
52     
53     public static TestSuite suite()
54         {
55         String JavaDoc testDirS = System.getProperty(TEST_DIR_PROP);
56         String JavaDoc buildDirS = System.getProperty(BUILD_DIR_PROP);
57         if(testDirS == null)
58             throw new IllegalStateException JavaDoc("System property " + TEST_DIR_PROP + " not set");
59         if(buildDirS == null)
60             throw new IllegalStateException JavaDoc("System property " + BUILD_DIR_PROP + " not set");
61         
62         File JavaDoc testDir = new File JavaDoc(testDirS);
63         File JavaDoc buildDir = new File JavaDoc(buildDirS);
64         if(!testDir.isDirectory())
65             throw new IllegalArgumentException JavaDoc(testDir + " is not a directory");
66         if(!buildDir.isDirectory())
67             throw new IllegalArgumentException JavaDoc(buildDir + " is not a directory");
68         
69         File JavaDoc[] testFiles = testDir.listFiles();
70         
71         Arrays.sort( // most recent first
72
testFiles,
73             new Comparator()
74                 {
75                 public int compare(Object JavaDoc f1, Object JavaDoc f2)
76                     {
77                     long diff = ((File JavaDoc) f2).lastModified() - ((File JavaDoc) f1).lastModified();
78                     return diff > 0 ? 1 : -1;
79                     }
80                 });
81         
82         TestSuite suite = new TestSuite("Rules File Tests");
83         for(int f = 0; f < testFiles.length; f++)
84             if(testFiles[f].getName().endsWith(".xml"))
85                 suite.addTest(new RecordingTest(testFiles[f], buildDir));
86                 
87         return suite;
88         }
89     
90     public RecordingTest(File JavaDoc testFile, File JavaDoc buildDir)
91         {
92         if(!buildDir.isDirectory())
93             throw new IllegalArgumentException JavaDoc(buildDir + " is not a directory");
94
95         this.testFile = testFile;
96         this.buildDir = buildDir;
97         this.name = testFile.getName();
98         if(name.endsWith(".xml"))
99             name = name.substring(0, name.lastIndexOf(".xml"));
100         }
101
102     public int countTestCases()
103         { return 1; }
104     
105     public void run(TestResult result)
106         {
107         result.startTest(this);
108
109         System.out.println(this + " ...");
110         
111         try {
112             build();
113             run();
114             }
115         catch(AssertionFailedError e)
116             { result.addFailure(this, e); }
117         catch(Exception JavaDoc e)
118             { result.addError(this, e); }
119         
120         result.endTest(this);
121         }
122     
123     private void run()
124         throws MackerIsMadException, ListenerException, RulesException,
125                IOException JavaDoc, ClassParseException, AssertionFailedError
126         {
127         Macker macker = new Macker();
128
129         RecordingListener recordingListener = new RecordingListener();
130         macker.addListener(recordingListener);
131         
132         for(Iterator rsIter = rulesFile.iterator(); rsIter.hasNext();)
133             macker.addRuleSet((RuleSet) rsIter.next());
134         for(Iterator cfIter = classFiles.iterator(); cfIter.hasNext();)
135             macker.addClass((File JavaDoc) cfIter.next());
136         
137         macker.setPrintThreshold(null);
138         
139         Collection actualAngerEvents;
140         try {
141             macker.check();
142             actualAngerEvents = Collections.EMPTY_LIST;
143         } catch(MackerIsMadException mime) {
144             actualAngerEvents = mime.getEvents();
145         }
146         
147         EventRecording actual = recordingListener.getRecording();
148         
149         StringWriter JavaDoc mismatches = new StringWriter JavaDoc();
150         PrintWriter JavaDoc out = new PrintWriter JavaDoc(mismatches);
151         out.println("Mismatched events:");
152         if(!expected.compare(actual, out))
153             {
154             System.out.println();
155             System.out.println("Excepted:");
156             expected.dump(System.out, 4);
157             System.out.println();
158             System.out.println("Actual:");
159             actual.dump(System.out, 4);
160             throw new AssertionFailedError(mismatches.toString());
161             }
162         if(expectedAngerEvents >= 0 && actualAngerEvents.size() != expectedAngerEvents)
163             throw new AssertionFailedError(
164                 "Expected " + expectedAngerEvents + " anger events, but got " + actualAngerEvents.size() + ": " + actualAngerEvents);
165         }
166     
167     private void dump(Collection c)
168         {
169         for(Iterator i = c.iterator(); i.hasNext(); )
170             System.out.println(" " + i.next());
171         }
172     
173     public String JavaDoc getName()
174         { return "RecordingTest \"" + name + '"'; }
175     
176     public String JavaDoc toString()
177         { return getName(); }
178     
179     // -----------------------------------------------------------------
180

181     private void build()
182         throws Exception JavaDoc
183         {
184         SAXBuilder saxBuilder = new SAXBuilder(false);
185         Element rootElem = saxBuilder.build(testFile).getRootElement();
186         
187         buildTestClasses(rootElem.getChild("test-classes"));
188         buildRulesFile(rootElem.getChild("rules-file"));
189         buildExpectedEvents(rootElem.getChild("expected-events"));
190         }
191     
192     private void buildTestClasses(Element testClassesElem)
193         throws Exception JavaDoc
194         {
195         File JavaDoc baseDir = new File JavaDoc(buildDir, name);
196         File JavaDoc srcDir = new File JavaDoc(baseDir, "src");
197         File JavaDoc classesDir = new File JavaDoc(baseDir, "classes");
198         
199         List/*<String>*/ javacArgs = new ArrayList();
200         javacArgs.add("-d");
201         javacArgs.add(classesDir.getPath());
202         classesDir.mkdirs();
203         
204         for(Iterator sourceIter = testClassesElem.getChildren("source").iterator(); sourceIter.hasNext(); )
205             {
206             Element sourceElem = (Element) sourceIter.next();
207             String JavaDoc packName = sourceElem.getAttributeValue("package");
208             String JavaDoc className = sourceElem.getAttributeValue("class");
209             packName = (packName == null) ? "" : packName + ".";
210             
211             String JavaDoc sourceCode = sourceElem.getText();
212             
213             char pathSeparatorChar =
214                 System.getProperty("os.name").toLowerCase().equals("mac os x")
215                     ? '/' // erroneously comes out as ':' on OS X -- shame on Apple!
216
: File.pathSeparatorChar;
217             File JavaDoc sourceFile = new File JavaDoc(
218                 srcDir,
219                 packName.replace('.', pathSeparatorChar) + className + ".java");
220             sourceFile.getParentFile().mkdirs();
221
222             BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(sourceFile));
223             try {
224                 out.write(sourceCode);
225                 out.flush();
226                 }
227             finally
228                 {
229                 try { out.close(); }
230                 catch(Exception JavaDoc e) { }
231                 }
232             javacArgs.add(sourceFile.getPath());
233             }
234
235         int compilerResult = new com.sun.tools.javac.Main().compile( //! can be static ref in 1.4
236
(String JavaDoc[]) javacArgs.toArray(new String JavaDoc[0]));
237         if(compilerResult != 0)
238             throw new Exception JavaDoc("compile failed (result code " + compilerResult + ")");
239         
240         classFiles = new ArrayList();
241         findFiles(classesDir, classFiles);
242         }
243     
244     private void findFiles(File JavaDoc file, Collection files)
245         {
246         if(file.isDirectory())
247             {
248             File JavaDoc[] contents = file.listFiles();
249             for(int f = 0; f < contents.length; f++)
250                 findFiles(contents[f], files);
251             }
252         else
253             files.add(file);
254         }
255     
256     private void buildRulesFile(Element rulesFileElem)
257         throws RulesException
258         { rulesFile = new RuleSetBuilder().build(rulesFileElem); }
259     
260     private void buildExpectedEvents(Element expectedEventsElem)
261         {
262         expected = new RuleSetRecording(null);
263         expected.read(expectedEventsElem);
264         expectedAngerEvents = Integer.parseInt(expectedEventsElem.getAttributeValue("expectedAngerEvents", "-1"));
265         }
266     
267     // -----------------------------------------------------------------
268

269     private String JavaDoc name;
270     private File JavaDoc testFile;
271     private File JavaDoc buildDir;
272     
273     private Collection/*<RuleSet>*/ rulesFile;
274     private Collection/*<File>*/ classFiles;
275
276     private EventRecording expected;
277     private int expectedAngerEvents;
278     }
279
280
281
282
Popular Tags