1 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 ; 29 import java.io.FileWriter ; 30 import java.io.BufferedWriter ; 31 import java.io.PrintWriter ; 32 import java.io.StringWriter ; 33 import java.io.IOException ; 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 50 TEST_DIR_PROP = "Macker.RecordingTest.testDir", 51 BUILD_DIR_PROP = "Macker.RecordingTest.buildDir"; 52 53 public static TestSuite suite() 54 { 55 String testDirS = System.getProperty(TEST_DIR_PROP); 56 String buildDirS = System.getProperty(BUILD_DIR_PROP); 57 if(testDirS == null) 58 throw new IllegalStateException ("System property " + TEST_DIR_PROP + " not set"); 59 if(buildDirS == null) 60 throw new IllegalStateException ("System property " + BUILD_DIR_PROP + " not set"); 61 62 File testDir = new File (testDirS); 63 File buildDir = new File (buildDirS); 64 if(!testDir.isDirectory()) 65 throw new IllegalArgumentException (testDir + " is not a directory"); 66 if(!buildDir.isDirectory()) 67 throw new IllegalArgumentException (buildDir + " is not a directory"); 68 69 File [] testFiles = testDir.listFiles(); 70 71 Arrays.sort( testFiles, 73 new Comparator() 74 { 75 public int compare(Object f1, Object f2) 76 { 77 long diff = ((File ) f2).lastModified() - ((File ) 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 testFile, File buildDir) 91 { 92 if(!buildDir.isDirectory()) 93 throw new IllegalArgumentException (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 e) 118 { result.addError(this, e); } 119 120 result.endTest(this); 121 } 122 123 private void run() 124 throws MackerIsMadException, ListenerException, RulesException, 125 IOException , 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 ) 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 mismatches = new StringWriter (); 150 PrintWriter out = new PrintWriter (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 getName() 174 { return "RecordingTest \"" + name + '"'; } 175 176 public String toString() 177 { return getName(); } 178 179 181 private void build() 182 throws Exception 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 194 { 195 File baseDir = new File (buildDir, name); 196 File srcDir = new File (baseDir, "src"); 197 File classesDir = new File (baseDir, "classes"); 198 199 List 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 packName = sourceElem.getAttributeValue("package"); 208 String className = sourceElem.getAttributeValue("class"); 209 packName = (packName == null) ? "" : packName + "."; 210 211 String sourceCode = sourceElem.getText(); 212 213 char pathSeparatorChar = 214 System.getProperty("os.name").toLowerCase().equals("mac os x") 215 ? '/' : File.pathSeparatorChar; 217 File sourceFile = new File ( 218 srcDir, 219 packName.replace('.', pathSeparatorChar) + className + ".java"); 220 sourceFile.getParentFile().mkdirs(); 221 222 BufferedWriter out = new BufferedWriter (new FileWriter (sourceFile)); 223 try { 224 out.write(sourceCode); 225 out.flush(); 226 } 227 finally 228 { 229 try { out.close(); } 230 catch(Exception e) { } 231 } 232 javacArgs.add(sourceFile.getPath()); 233 } 234 235 int compilerResult = new com.sun.tools.javac.Main().compile( (String []) javacArgs.toArray(new String [0])); 237 if(compilerResult != 0) 238 throw new Exception ("compile failed (result code " + compilerResult + ")"); 239 240 classFiles = new ArrayList(); 241 findFiles(classesDir, classFiles); 242 } 243 244 private void findFiles(File file, Collection files) 245 { 246 if(file.isDirectory()) 247 { 248 File [] 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 269 private String name; 270 private File testFile; 271 private File buildDir; 272 273 private Collection rulesFile; 274 private Collection classFiles; 275 276 private EventRecording expected; 277 private int expectedAngerEvents; 278 } 279 280 281 282 | Popular Tags |