1 26 27 package net.sourceforge.cobertura.ant; 28 29 import java.io.File ; 30 import java.io.FilenameFilter ; 31 import java.io.IOException ; 32 import java.util.Arrays ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 import junit.framework.TestCase; 37 import net.sourceforge.cobertura.reporting.JUnitXMLHelper; 38 39 import org.apache.tools.ant.Project; 40 import org.apache.tools.ant.taskdefs.Java; 41 import org.apache.tools.ant.types.Path; 42 import org.apache.tools.ant.types.Path.PathElement; 43 import org.jdom.Document; 44 import org.jdom.Element; 45 import org.jdom.JDOMException; 46 import org.jdom.xpath.XPath; 47 48 55 public class FunctionalTest extends TestCase 56 { 57 58 private final static File BASEDIR = new File ((System.getProperty("basedir") != null) ? System 59 .getProperty("basedir") : ".", "examples/functionaltest1"); 60 61 public static void testInstrumentUsingIncludesAndExcludes() throws Exception 62 { 63 runTestAntScript("includes-and-excludes", "test-includes-and-excludes"); 64 verify("includes-and-excludes"); 65 } 66 67 public static void testInstrumentUsingClassPath() throws Exception 68 { 69 runTestAntScript("classpath", "test-classpath"); 70 verify("classpath"); 71 } 72 73 public static void testInstrumentUsingWar() throws Exception 74 { 75 runTestAntScript("classpath", "test-war"); 76 verify("war"); 77 } 78 79 private static void verify(String testName) throws Exception 80 { 81 verifyXml(testName); 82 verifyHtml(testName); 83 } 84 85 private static void verifyXml(String testName) throws Exception 86 { 87 List classesList = getClassElements(); 89 assertTrue("Test " + testName + ": Did not find any classes listed in the XML report.", 90 classesList.size() > 0); 91 92 boolean firstPackageFound = false; 95 boolean secondPackageFound = false; 96 for (Iterator iter = classesList.iterator(); iter.hasNext();) 97 { 98 Element classElement = (Element)iter.next(); 99 String className = classElement.getAttributeValue("name"); 100 if (className.equals("test.first.A")) 101 { 102 firstPackageFound = true; 103 } 104 else if (className.equals("test.second.A")) 105 { 106 secondPackageFound = true; 107 } 108 else 109 fail("Test " 110 + testName 111 + ": Found a class with the name '" 112 + className 113 + "' in the XML report, but was only expecting either 'test.first.A' or 'test.second.A'."); 114 verifyClass(testName, classElement); 115 } 116 assertTrue("Test " + testName + ": Did not find class 'test.first.A' in the XML report.", 117 firstPackageFound); 118 assertTrue("Test " + testName + ": Did not find class 'test.second.A' in the XML report.", 119 secondPackageFound); 120 } 121 122 127 private static List getClassElements() throws IOException , JDOMException 128 { 129 File xmlFile = new File (BASEDIR, "reports/cobertura-xml/coverage.xml"); 130 Document document = JUnitXMLHelper.readXmlFile(xmlFile, true); 131 XPath xpath = XPath.newInstance("/coverage/packages/package/classes/class"); 132 List classesList = xpath.selectNodes(document); 133 return classesList; 134 } 135 136 141 private static void verifyClass(String testName, Element classElement) 142 { 143 Element methodsElement = classElement.getChild("methods"); 145 List methodList = methodsElement.getChildren("method"); 146 assertTrue("Test " + testName + ": Did not find any methods listed in the class " 147 + classElement.getAttributeValue("name"), methodList.size() > 0); 148 boolean callMethodFound = false; 149 boolean dontCallMethodFound = false; 150 for (Iterator iter = methodList.iterator(); iter.hasNext();) 151 { 152 Element methodElement = (Element)iter.next(); 153 String methodName = methodElement.getAttributeValue("name"); 154 if (methodName.equals("call")) 155 { 156 if (callMethodFound) 157 { 158 fail("Test " + testName 159 + ": Found more than one instance of the method 'call' in the class " 160 + classElement.getAttributeValue("name")); 161 } 162 callMethodFound = true; 163 verifyMethod(testName, classElement, methodElement, 1); 164 } 165 else if (methodName.equals("dontCall")) 166 { 167 if (dontCallMethodFound) 168 { 169 fail("Test " 170 + testName 171 + ": Found more than one instance of the method 'dontCall' in the class " 172 + classElement.getAttributeValue("name")); 173 } 174 dontCallMethodFound = true; 175 verifyMethod(testName, classElement, methodElement, 0); 176 } 177 else if (methodName.equals("<init>") || methodName.equals("someMethod")) 178 { 179 } 181 else 182 { 183 fail("Test " + testName + ": Found method " + methodName + " in the class " 184 + classElement.getAttributeValue("name") 185 + ", but was only expecting either 'call' or 'dontCall'."); 186 } 187 } 188 assertTrue("Test " + testName + ": Did not find method 'call' in the class " 189 + classElement.getAttributeValue("name"), callMethodFound); 190 assertTrue("Test " + testName + ": Did not find method 'dontCall' in the class " 191 + classElement.getAttributeValue("name"), dontCallMethodFound); 192 } 193 194 198 private static void verifyMethod(String testName, Element classElement, Element methodElement, 199 int expectedHits) 200 { 201 Element linesElement = methodElement.getChild("lines"); 202 List lineList = linesElement.getChildren("line"); 203 assertTrue("Test " + testName + ", class " + classElement.getAttributeValue("name") 204 + ": Did not find any lines in the method " 205 + methodElement.getAttributeValue("name"), lineList.size() > 0); 206 207 for (Iterator iter = lineList.iterator(); iter.hasNext();) 208 { 209 Element lineElement = (Element)iter.next(); 210 String hitsString = lineElement.getAttributeValue("hits"); 211 int hits = Integer.parseInt(hitsString); 212 assertEquals("Test " + testName + ", class " + classElement.getAttributeValue("name") 213 + ": Found incorrect hit count for the method " 214 + methodElement.getAttributeValue("name"), expectedHits, hits); 215 } 216 } 217 218 private static void verifyHtml(String testName) throws Exception 219 { 220 File htmlReportDir = new File (BASEDIR, "reports/cobertura-html"); 221 222 String htmlFiles[] = htmlReportDir.list(new FilenameFilter () 224 { 225 226 public boolean accept(File dir, String name) 227 { 228 return name.endsWith(".html"); 229 } 230 }); 231 Arrays.sort(htmlFiles); 232 233 assertTrue(htmlFiles.length >= 5); 234 235 String [] requiredFiles = { "index.html", "help.html", "frame-packages.html", 237 "frame-summary.html", "frame-sourcefiles.html" }; 238 239 for (int i = 0; i < requiredFiles.length; i++) 240 { 241 if (!containsFile(htmlFiles, requiredFiles[i])) 242 { 243 fail("Test " + testName + ": File " + requiredFiles[i] 244 + " not found among report files"); 245 } 246 } 247 248 String previousPrefix = "NONE"; 250 for (int i = 0; i < htmlFiles.length; i++) 251 { 252 if (containsFile(requiredFiles, htmlFiles[i]) 254 || !htmlFiles[i].startsWith(previousPrefix)) 255 { 256 JUnitXMLHelper.readXmlFile(new File (htmlReportDir, htmlFiles[i]), true); 257 } 258 if (htmlFiles[i].length() > 7) 259 { 260 previousPrefix = htmlFiles[i].substring(0, 7); 261 } 262 else 263 { 264 previousPrefix = htmlFiles[i]; 265 } 266 } 267 } 268 269 private static boolean containsFile(String [] files, String fileName) 270 { 271 for (int i = 0; i < files.length; i++) 272 { 273 if (files[i].equals(fileName)) 274 return true; 275 } 276 return false; 277 } 278 279 283 private static void runTestAntScript(String testName, String target) throws IOException 284 { 285 Java task = new Java(); 286 task.setTaskName("java"); 287 task.setProject(new Project()); 288 task.init(); 289 290 task.setClassname("org.apache.tools.ant.launch.Launcher"); 292 task.setFork(true); 293 294 AntUtil.transferCoberturaDataFileProperty(task); 295 296 task.createArg().setValue("-f"); 297 task.createArg().setValue(BASEDIR + "/build.xml"); 298 task.createArg().setValue(target); 299 300 task.setFailonerror(true); 301 302 File outputFile = Util.createTemporaryTextFile("cobertura-test"); 304 task.setOutput(outputFile); 305 306 Path classpath = task.createClasspath(); 308 PathElement pathElement = classpath.createPathElement(); 309 pathElement.setPath(System.getProperty("java.class.path")); 310 311 try 312 { 313 task.execute(); 314 } 315 finally 316 { 317 if (outputFile.exists()) 318 { 319 System.out.println("\n\n\nOutput from Ant for " + testName 321 + " test:\n----------------------------------------\n" 322 + Util.getText(outputFile) + "----------------------------------------"); 323 outputFile.delete(); 324 } 325 } 326 } 327 328 } 329 | Popular Tags |