KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > report > TestReport


1 package csdl.jblanket.report;
2
3 import csdl.jblanket.methodset.MethodSet;
4
5 import java.io.File JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7 import java.io.FileNotFoundException JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15
16 import org.apache.tools.ant.util.FileUtils;
17
18 import org.jdom.Document;
19 import org.jdom.Element;
20 import org.jdom.JDOMException;
21 import org.jdom.input.SAXBuilder;
22
23 /**
24  * Tests the operations of the jblanketreport Ant task.
25  * <p>
26  * If using Ant to execute this test class a 'jblanket.testdir' system property needs to be set
27  * in the build.xml file. If running test class from command line, must provide -D argument with
28  * 'jblanket.testdir' to set the system property. This value is used in the testFileOperations
29  * method.
30  * <p>
31  * The 'jblanket.dir' system property also needs to be temporarily overrided by setting its
32  * value in the build.xml file. This value should be the same as 'jblanket.testdir'. If
33  * running test class from command line, must provide -D argument with 'jblanket.testdir'
34  * to set the system property. This value is used in the testFileOperations method.
35  *
36  * @author Joy M. Agustin
37  * @version $Id: TestReport.java,v 1.2 2004/11/07 08:53:27 timshadel Exp $
38  */

39 public class TestReport extends TestCase {
40
41   /** Tag for the report format */
42   private final String JavaDoc reportFormat = "-reportFormat";
43
44   /** Describes if one-line methods should be excluded from the coverage measurement */
45   private final String JavaDoc excludeOneLineMethods = "-excludeOneLineMethods";
46   /** Describes if constructors should be excluded from the coverage measurement */
47   private final String JavaDoc excludeConstructors = "-excludeConstructors";
48
49   /** Tag for the totalfile */
50   private final String JavaDoc totalFile = "-totalFile";
51   /** Tag for the testfile */
52   private final String JavaDoc testFile = "-testedFile";
53   /** Tag for the untestedfile */
54   private final String JavaDoc untestedFile = "-untestedFile";
55   /** Tag for the untestedfile */
56   private final String JavaDoc untestableFile = "-untestableFile";
57   /** Tag for the onelinefile */
58   private final String JavaDoc oneLineFile = "-oneLineFile";
59   /** Tag for the onelinefile */
60   private final String JavaDoc constructorFile = "-constructorFile";
61
62   /** Describes if one-line methods should be excluded from the coverage measurement */
63   private final boolean excludeOneLineMethodsValue = true;
64   /** Describes if constructors should be excluded from the coverage measurement */
65   private final boolean excludeConstructorsValue = true;
66
67   /** Frame format for report */
68   private final String JavaDoc frames = "frames";
69
70   /** Name of total methods XML file */
71   private final String JavaDoc totalXmlFile = "totalMethods.xml";
72   /** Name of tested methods XML file */
73   private final String JavaDoc testXmlFile = "testedMethods.xml";
74   /** Name of untested methods XML file */
75   private final String JavaDoc untestedXmlFile = "untestedMethods.xml";
76   /** Name of untested methods XML file */
77   private final String JavaDoc untestableXmlFile = "untestableMethods.xml";
78   /** Name of one-line methods XML file */
79   private final String JavaDoc oneLineXmlFile = "onelineMethods.xml";
80   /** Name of constructors XML file */
81   private final String JavaDoc constructorXmlFile = "constructorMethods.xml";
82
83   /** Name of intermediate cover XML file */
84   private final String JavaDoc coverFile1 = "COVER-csdl.foo.TestBar.xml";
85   /** Name of intermediate cover XML file */
86   private final String JavaDoc coverFile2 = "COVER-csdl.foo.TestBar2.xml";
87   /** Name of aggregate cover XML file */
88   private final String JavaDoc coverFile3 = "COVER-MethodSets.xml";
89
90   /** MethodSet for totalFile */
91   private MethodSet methodSetTotal;
92   /** MethodSet for testFile */
93   private MethodSet methodSetTest;
94   /** MethodSet for untestedFile */
95   private MethodSet methodSetDiff;
96   /** MethodSet for oneLineFile */
97   private MethodSet methodSetOneLine;
98   /** MethodSet for constructorFile */
99   private MethodSet methodSetConstructor;
100
101   /** Directory containing test data */
102   private File JavaDoc xmlTestdataDir;
103   /** Name of directory containing test data */
104   private final String JavaDoc testdataDirName = "testdata";
105   /** Name of directory containing XML test data */
106   private final String JavaDoc xmlTestdataDirName = "xml";
107   /** Prefix for copied files */
108   private final String JavaDoc prefix = "test.";
109
110   /** Directory for holding JBlanket output */
111   private String JavaDoc jblanketDir;
112
113   /**
114    * Required for JUnit.
115    *
116    * @param name Test case name.
117    */

118   public TestReport(String JavaDoc name) {
119     
120     super(name);
121   }
122
123   /**
124    * Sets up instance variables for testing.
125    *
126    * @throws Exception if cannot move files from /jblanket/testdata/xml directory to
127    * 'jblanket.testdir'.
128    */

129   public void setUp() throws Exception JavaDoc {
130
131     String JavaDoc testdataDir = System.getProperty("jblanket.data.dir");
132
133     this.xmlTestdataDir = new File JavaDoc(testdataDir, this.xmlTestdataDirName);
134     this.jblanketDir = System.getProperty("jblanket.testdir");
135     assertNotNull("Checking for presence of jblanket.testdir.", jblanketDir);
136
137     // move needed files to jblanket.testdir
138
FileUtils util = FileUtils.newFileUtils();
139     util.copyFile(new File JavaDoc(this.xmlTestdataDir, this.totalXmlFile),
140                   new File JavaDoc(this.jblanketDir, this.totalXmlFile));
141     util.copyFile(new File JavaDoc(this.xmlTestdataDir, this.oneLineXmlFile),
142                   new File JavaDoc(this.jblanketDir, this.oneLineXmlFile));
143     util.copyFile(new File JavaDoc(this.xmlTestdataDir, this.constructorXmlFile),
144                   new File JavaDoc(this.jblanketDir, this.constructorXmlFile));
145     util.copyFile(new File JavaDoc(this.xmlTestdataDir, this.coverFile1),
146                   new File JavaDoc(this.jblanketDir, this.coverFile1));
147     util.copyFile(new File JavaDoc(this.xmlTestdataDir, this.coverFile2),
148                   new File JavaDoc(this.jblanketDir, this.coverFile2));
149     util.copyFile(new File JavaDoc(this.xmlTestdataDir, this.coverFile3),
150                   new File JavaDoc(this.jblanketDir, this.prefix + this.coverFile3));
151   }
152
153   /**
154    * Tests the jblanketreport Ant task process.
155    *
156    * @throws Exception If problems occur.
157    */

158   public void testReport() throws Exception JavaDoc {
159     
160     // format arguments
161
ArrayList JavaDoc args = new ArrayList JavaDoc();
162     args.add(this.reportFormat);
163     args.add(this.frames);
164     args.add(this.totalFile);
165     args.add(this.totalXmlFile);
166     args.add(this.testFile);
167     args.add(this.testXmlFile);
168     args.add(this.untestedFile);
169     args.add(this.untestedXmlFile);
170     args.add(this.untestableFile);
171     args.add(this.untestableXmlFile);
172     args.add(this.excludeOneLineMethods);
173     args.add(new Boolean JavaDoc(this.excludeOneLineMethodsValue));
174     args.add(this.oneLineFile);
175     args.add(this.oneLineXmlFile);
176     args.add(this.excludeConstructors);
177     args.add(new Boolean JavaDoc(this.excludeConstructorsValue));
178     args.add(this.constructorFile);
179     args.add(this.constructorXmlFile);
180
181     // create the report
182
JBlanketReport.main(args);
183
184     // verify the values of the test, untested, and COVER-MethodSets XML files.
185
verifyFileContents(new File JavaDoc(this.jblanketDir, this.prefix + this.coverFile3),
186                        new File JavaDoc(this.jblanketDir, this.coverFile3));
187   }
188
189   /**
190    * Verifies that elements in <code>newFile</code> are the same as <code>oldFile</code>.
191    *
192    * @param oldFile the file to compare against.
193    * @param newFile the file created from testing.
194    * @throws FileNotFoundException if cannot find either newFile or oldFile.
195    * @throws JDOMException if cannot read from either newFile or oldFile.
196    * @throws IOException if cannot create SAXBuilder for either oldFile or newFile.
197    */

198   private void verifyFileContents(File JavaDoc oldFile, File JavaDoc newFile)
199     throws FileNotFoundException JavaDoc, JDOMException, IOException JavaDoc {
200
201     // create inputstreams for the two input files
202
FileInputStream JavaDoc stream1 = new FileInputStream JavaDoc(oldFile);
203     FileInputStream JavaDoc stream2 = new FileInputStream JavaDoc(newFile);
204
205     // use JDOM to read elements in XML file
206
SAXBuilder builder1 = new SAXBuilder();
207     SAXBuilder builder2 = new SAXBuilder();
208     Document doc1 = builder1.build(stream1);
209     Document doc2 = builder2.build(stream2);
210
211     // load method information
212
Element rootElement1 = doc1.getRootElement();
213     Element rootElement2 = doc2.getRootElement();
214
215     // check each element in second file
216
Iterator JavaDoc j = rootElement2.getChildren().iterator();
217
218     // check each element in first file
219
for (Iterator JavaDoc i = rootElement1.getChildren().iterator(); i.hasNext(); ) {
220       Element element1 = (Element) i.next();
221       Element element2 = (Element) j.next();
222
223       // check the "tested" value
224
String JavaDoc value1, value2;
225       value1 = element1.getAttributeValue("tested");
226       value2 = element2.getAttributeValue("tested");
227       assertEquals("Checking tested values", value1, value2);
228
229       // check the "untested" value
230
value1 = element1.getAttributeValue("untested");
231       value2 = element2.getAttributeValue("untested");
232       assertEquals("Checking untested values", value1, value2);
233
234       // check the "oneline" value
235
value1 = element1.getAttributeValue("oneline");
236       value2 = element2.getAttributeValue("oneline");
237       assertEquals("Checking oneline values", value1, value2);
238     }
239   }
240
241   /**
242    * Provide stand-alone execution of this test case during initial development.
243    *
244    * @param args The command line arguments
245    */

246   public static void main(String JavaDoc[] args) {
247     
248     System.out.println("JUnit testing csdl.jblanket.report package.");
249     //Runs all no-arg methods starting with "test".
250
TestRunner.run(new TestSuite(TestReport.class));
251   }
252 }
253
Popular Tags