Clover coverage report - XJavaDoc - 1.1 Coverage timestamp: Sun Oct 3 2004 19:56:54 BST FRAMES NO FRAMES |
file stats: | LOC: | 380 |  | Methods: | 30
| NCLOC: | 255 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
CodeTestCase.java | 52.5% | 63.4% | 70% | 62.2% |

1 |  |
     /*
2 |  |
      * Copyright (c) 2001-2003 The XDoclet team
3 |  |
      * All rights reserved.
4 |  |
      */
5 |  |
     package xjavadoc.codeunit;
6 |  |
7 |  |
     import junit.framework.TestCase;
8 |  |
     import xjavadoc.*;
9 |  |
     import xjavadoc.filesystem.ReaderFile;
10 |  |
11 |  |
     import java.util.Iterator;
12 |  |
     import java.util.StringTokenizer;
13 |  |
     import java.io.StringWriter;
14 |  |
     import java.io.File;
15 |  |
     import java.io.Reader;
16 |  |
17 |  |
     /**
18 |  |
      * CodeTestCase is a JUnit extension that will let you compare two sources
19 |  |
      * (typically one we keep as test data and a generated one) on the API level or
20 |  |
      * on the abstract syntax tree (AST) level. This is a lot more powerful than
21 |  |
      * comparing on a character by character basis, because it's only "what matters"
22 |  |
      * that is compared.
23 |  |
      *
24 |  |
      * @author    Aslak Hellesøy
25 |  |
      * @created   24. februar 2003
26 |  |
      */
27 |  |
     public abstract class CodeTestCase extends TestCase
28 |  |
     {
29 | 6 |
         public CodeTestCase() {
30 | 6 |
             super();
31 |  |
         }
32 |  |
33 | 0 |
         public static void assertAstEqualsDir( File expectedDir, File actualDir ) {
34 | 0 |
             if( !expectedDir.isDirectory() ) {
35 | 0 |
                 fail(expectedDir.getAbsolutePath() + " - should have been a directory");
36 |  |
             }
37 |  |
38 | 0 |
             if( !actualDir.isDirectory() ) {
39 | 0 |
                 fail(actualDir.getAbsolutePath() + " - should have been a directory");
40 |  |
             }
41 |  |
42 | 0 |
             File[] expectedChildren =  expectedDir.listFiles();
43 |  |
44 | 0 |
             for (int i = 0; i < expectedChildren.length; i++) {
45 | 0 |
                 File expectedChild = expectedChildren[i];
46 | 0 |
                 File actualChild = getActualChild(actualDir, expectedChild );
47 |  |
48 | 0 |
                 if( !actualChild.exists() ) {
49 | 0 |
                     fail("File should have existed: " + actualChild.getAbsolutePath());
50 |  |
                 }
51 | 0 |
                 if( (expectedChild.isDirectory() && !actualChild.isDirectory()) || (!expectedChild.isDirectory() && actualChild.isDirectory()) ) {
52 | 0 |
                     fail("Incompatible file types: " + expectedChild.getAbsolutePath() + "," + actualChild.getAbsolutePath());
53 |  |
                 }
54 |  |
55 | 0 |
                 if( expectedChild.isDirectory() ) {
56 | 0 |
                     assertAstEqualsDir(expectedChild, actualChild );
57 | 0 |
                 } else if( expectedChild.getName().endsWith(".java") ) {
58 | 0 |
                     System.out.println("Comparing " + expectedChild.getAbsolutePath());
59 | 0 |
                     assertAstEquals(expectedChild, actualChild);
60 |  |
                 } else {
61 | 0 |
                     System.out.println("Ignoring non java file: " + expectedChild.getAbsolutePath());
62 |  |
                 }
63 |  |
             }
64 |  |
         }
65 |  |
66 | 0 |
         private static File getActualChild(File actualDir, File expectedChild) {
67 | 0 |
             return new File(actualDir, expectedChild.getName());
68 |  |
         }
69 |  |
70 |  |
71 |  |
         /**
72 |  |
          * Compares both API and AST. Equivalent to calling {@link #assertAstEquals}
73 |  |
          * and {@link #assertApiEquals}.
74 |  |
          *
75 |  |
          * @param expected  the expected source
76 |  |
          * @param actual    the actual source
77 |  |
          */
78 | 0 |
         public static void assertEquals( File expected, File actual )
79 |  |
         {
80 | 0 |
             assertAstEquals( expected, actual );
81 | 0 |
             assertApiEquals( expected, actual );
82 |  |
         }
83 |  |
84 | 0 |
         public static void assertEquals( Reader expected, Reader actual )
85 |  |
         {
86 | 0 |
             assertAstEquals( expected, actual );
87 | 0 |
             assertApiEquals( expected, actual );
88 |  |
         }
89 |  |
90 |  |
         /**
91 |  |
          * Asserts (tests) that the ASTs of two sources are equal. Does not compare the
92 |  |
          * contents (tokens) of the nodes, and is forgiving with respect to those.
93 |  |
          *
94 |  |
          * @param expected  the expected source
95 |  |
          * @param actual    the actual source
96 |  |
          */
97 | 2 |
         public static void assertAstEquals( File expected, File actual )
98 |  |
         {
99 | 2 |
             checkNotDir(expected, actual);
100 |  |
101 | 2 |
             SourceClass expectedClass = new SourceClass( new XJavaDoc(), expected, true, new XTagFactory() );
102 | 2 |
             SourceClass actualClass = new SourceClass( new XJavaDoc(), actual, true, new XTagFactory() );
103 |  |
104 | 2 |
             assertAstEquals( expectedClass.getCompilationUnit(), actualClass.getCompilationUnit() );
105 |  |
         }
106 |  |
107 | 0 |
         public static void assertAstEquals( Reader expected, Reader actual )
108 |  |
         {
109 | 0 |
             SourceClass expectedClass = new SourceClass( new XJavaDoc(), new ReaderFile(expected), true, new XTagFactory(), null );
110 | 0 |
             SourceClass actualClass = new SourceClass( new XJavaDoc(), new ReaderFile(actual), true, new XTagFactory(), null );
111 |  |
112 | 0 |
             assertAstEquals( expectedClass.getCompilationUnit(), actualClass.getCompilationUnit() );
113 |  |
         }
114 |  |
         /**
115 |  |
          * Asserts (tests) that the APIs of two sources are equal. Does not go into the
116 |  |
          * method bodies to see if the implementation is equal, and is therefore more
117 |  |
          * relaxed than assertAstEquals.
118 |  |
          *
119 |  |
          * @param expected  the expected source
120 |  |
          * @param actual    the actual source
121 |  |
          */
122 | 4 |
         public static void assertApiEquals( File expected, File actual )
123 |  |
         {
124 | 4 |
             checkNotDir(expected, actual);
125 | 4 |
             SourceClass expectedClass = new SourceClass( new XJavaDoc(), expected, false, new XTagFactory() );
126 | 4 |
             SourceClass actualClass = new SourceClass( new XJavaDoc(), actual, false, new XTagFactory() );
127 |  |
128 | 4 |
             assertApiEquals( expectedClass, actualClass );
129 |  |
         }
130 |  |
131 | 0 |
         public static void assertApiEquals( Reader expected, Reader actual )
132 |  |
         {
133 | 0 |
             SourceClass expectedClass = new SourceClass( new XJavaDoc(), new ReaderFile(expected), false, new XTagFactory(), null );
134 | 0 |
             SourceClass actualClass = new SourceClass( new XJavaDoc(), new ReaderFile(actual), false, new XTagFactory(), null );
135 |  |
136 | 0 |
             assertApiEquals( expectedClass, actualClass );
137 |  |
         }
138 |  |
139 | 6 |
         private static void checkNotDir(File expected, File actual) {
140 | 6 |
             if( expected.isDirectory() ) {
141 | 0 |
                 fail(expected.getAbsolutePath() + " - should not have been a directory");
142 |  |
             }
143 |  |
144 | 6 |
             if( actual.isDirectory() ) {
145 | 0 |
                 fail(actual.getAbsolutePath() + " - should not have been a directory");
146 |  |
             }
147 |  |
         }
148 |  |
149 | 54 |
         private static void assertAstEquals( SimpleNode expected, SimpleNode actual )
150 |  |
         {
151 |  |
             // Verify that we have the same AST type
152 | 54 |
             boolean sameNodeType = expected.getType().equals( actual.getType() );
153 |  |
             // Verify that we have the same number of children
154 | 54 |
             boolean sameNumberOfChildren = expected.jjtGetNumChildren() == actual.jjtGetNumChildren();
155 |  |
156 | 54 |
             if( !sameNodeType || !sameNumberOfChildren )
157 |  |
             {
158 |  |
                 // Something is not equal...
159 | 2 |
                 StringWriter expectedWriter = new StringWriter();
160 |  |
161 | 2 |
                 NodePrinter.print( expected, expectedWriter );
162 |  |
163 | 2 |
                 StringWriter actualWriter = new StringWriter();
164 |  |
165 | 2 |
                 NodePrinter.print( actual, actualWriter );
166 |  |
167 |  |
                 // This will always fail. -But we get a nice filtered diff.
168 | 2 |
                 assertEquals( expectedWriter.toString(), expectedWriter.toString(), actualWriter.toString() );
169 |  |
             }
170 |  |
171 | 52 |
             for( int i = 0; i < expected.jjtGetNumChildren(); i++ )
172 |  |
             {
173 | 52 |
                 xjavadoc.SimpleNode expectedChild = ( xjavadoc.SimpleNode ) expected.jjtGetChild( i );
174 | 52 |
                 xjavadoc.SimpleNode actualChild = ( xjavadoc.SimpleNode ) actual.jjtGetChild( i );
175 |  |
176 | 52 |
                 assertAstEquals( expectedChild, actualChild );
177 |  |
             }
178 |  |
         }
179 |  |
180 | 4 |
         private static void assertApiEquals( SourceClass expected, SourceClass actual )
181 |  |
         {
182 | 4 |
             assertEquals( "Package names should be equal", expected.getContainingPackage().getName(), actual.getContainingPackage().getName() );
183 | 4 |
             assertModifiersEqual( "Class modifiers should be equal", expected, actual );
184 | 4 |
             assertNameEquals( "Class names should be equal", expected, actual );
185 | 4 |
             assertSuperclassEquals( expected, actual );
186 | 4 |
             assertInterfacesEqual( expected, actual );
187 | 4 |
             assertFieldsEqual( expected, actual );
188 | 4 |
             assertConstructorsEqual( expected, actual );
189 | 4 |
             assertMethodsEqual( expected, actual );
190 |  |
         }
191 |  |
192 | 4 |
         private static void assertFieldsEqual( XClass expected, XClass actual )
193 |  |
         {
194 | 4 |
             assertEquals( "Number of fields should be equal", expected.getFields().size(), actual.getFields().size() );
195 |  |
196 | 4 |
             Iterator expectedFields = expected.getFields().iterator();
197 | 4 |
             Iterator actualFields = actual.getFields().iterator();
198 |  |
199 | 4 |
             while( expectedFields.hasNext() )
200 |  |
             {
201 | 0 |
                 XField expectedField = ( XField ) expectedFields.next();
202 | 0 |
                 XField actualField = ( XField ) actualFields.next();
203 |  |
204 | 0 |
                 assertFieldEquals( expectedField, actualField );
205 |  |
             }
206 |  |
         }
207 |  |
208 | 4 |
         private static void assertConstructorsEqual( XClass expected, XClass actual )
209 |  |
         {
210 | 4 |
             assertEquals( "Number of constructors should be equal", expected.getConstructors().size(), actual.getConstructors().size() );
211 |  |
212 | 4 |
             Iterator expectedConstructors = expected.getConstructors().iterator();
213 | 4 |
             Iterator actualConstructors = actual.getConstructors().iterator();
214 |  |
215 | 4 |
             while( expectedConstructors.hasNext() )
216 |  |
             {
217 | 0 |
                 XConstructor expectedConstructor = ( XConstructor ) expectedConstructors.next();
218 | 0 |
                 XConstructor actualConstructor = ( XConstructor ) actualConstructors.next();
219 |  |
220 | 0 |
                 assertConstructorEquals( expectedConstructor, actualConstructor );
221 |  |
             }
222 |  |
         }
223 |  |
224 | 4 |
         private static void assertMethodsEqual( XClass expected, XClass actual )
225 |  |
         {
226 | 4 |
             assertEquals( "Number of methods should be equal", expected.getMethods().size(), actual.getMethods().size() );
227 |  |
228 | 4 |
             Iterator expectedMethods = expected.getMethods().iterator();
229 | 4 |
             Iterator actualMethods = actual.getMethods().iterator();
230 |  |
231 | 4 |
             while( expectedMethods.hasNext() )
232 |  |
             {
233 | 4 |
                 XMethod expectedMethod = ( XMethod ) expectedMethods.next();
234 | 4 |
                 XMethod actualMethod = ( XMethod ) actualMethods.next();
235 |  |
236 | 4 |
                 assertMethodEquals( expectedMethod, actualMethod );
237 |  |
             }
238 |  |
         }
239 |  |
240 | 0 |
         private static void assertFieldEquals( XField expected, XField actual )
241 |  |
         {
242 | 0 |
             assertTypeEquals( "Field types should be equal", expected, actual );
243 | 0 |
             assertNameEquals( "Field names should be equal", expected, actual );
244 | 0 |
             assertModifiersEqual( "Field modifiers should be equal", expected, actual );
245 |  |
         }
246 |  |
247 | 0 |
         private static void assertConstructorEquals( XConstructor expected, XConstructor actual )
248 |  |
         {
249 | 0 |
             assertNameEquals( "Constructor names should be equal", expected, actual );
250 | 0 |
             assertModifiersEqual( "Constructor modifiers should be equal", expected, actual );
251 | 0 |
             assertNameWithSignatureEquals( "Constructor signatures should be equal", expected, actual );
252 | 0 |
             assertParametersEqual( "Constructor parameters should be equal", expected, actual );
253 | 0 |
             assertThrownExceptionsEqual( "Constructor exceptions should be equal", expected, actual );
254 |  |
         }
255 |  |
256 | 4 |
         private static void assertMethodEquals( XMethod expected, XMethod actual )
257 |  |
         {
258 | 4 |
             assertTypeEquals( "Method types should be equal", expected.getReturnType(), actual.getReturnType() );
259 | 4 |
             assertNameEquals( "Method names should be equal", expected, actual );
260 | 4 |
             assertModifiersEqual( "Method modifiers should be equal", expected, actual );
261 | 4 |
             assertNameWithSignatureEquals( "Method signatures should be equal", expected, actual );
262 | 4 |
             assertParametersEqual( "Method parameters should be equal", expected, actual );
263 | 4 |
             assertThrownExceptionsEqual( "Method exceptions should be equal", expected, actual );
264 |  |
         }
265 |  |
266 | 2 |
         private static void assertParameterEquals( XParameter expected, XParameter actual )
267 |  |
         {
268 | 2 |
             assertTypeEquals( "Parameter types should be equal", expected, actual );
269 | 2 |
             assertNameEquals( "Parameter names should be equal", expected, actual );
270 |  |
         }
271 |  |
272 | 6 |
         private static void assertTypeEquals( String msg, Type expected, Type actual )
273 |  |
         {
274 | 6 |
             assertEquals( msg, expected.getType().getQualifiedName(), actual.getType().getQualifiedName() );
275 | 6 |
             assertEquals( msg, expected.getDimensionAsString(), actual.getDimensionAsString() );
276 |  |
         }
277 |  |
278 | 10 |
         private static void assertNameEquals( String msg, Named expected, Named actual )
279 |  |
         {
280 | 10 |
             assertEquals( msg, expected.getName(), actual.getName() );
281 |  |
         }
282 |  |
283 | 4 |
         private static void assertSuperclassEquals( SourceClass expected, SourceClass actual )
284 |  |
         {
285 | 4 |
             String expectedSuperclass = expected.getSuperclass() != null ? expected.getSuperclass().getQualifiedName() : null;
286 | 4 |
             String actualSuperclass = actual.getSuperclass() != null ? actual.getSuperclass().getQualifiedName() : null;
287 |  |
288 | 4 |
             assertEquals( "Superclass is equal", expectedSuperclass, actualSuperclass );
289 |  |
         }
290 |  |
291 | 4 |
         private static void assertInterfacesEqual( SourceClass expected, SourceClass actual )
292 |  |
         {
293 | 4 |
             assertEquals( "Implemented interfaces should be equal", expected.getDeclaredInterfaces().size(), actual.getDeclaredInterfaces().size() );
294 |  |
295 | 4 |
             Iterator declaredInterfaces = expected.getDeclaredInterfaces().iterator();
296 |  |
297 | 4 |
             while( declaredInterfaces.hasNext() )
298 |  |
             {
299 | 4 |
                 XClass declaredInterface = ( XClass ) declaredInterfaces.next();
300 |  |
301 | 4 |
                 assertTrue( "Implements " + declaredInterface.getQualifiedName(), actual.isA( declaredInterface.getQualifiedName() ) );
302 |  |
             }
303 |  |
         }
304 |  |
305 | 8 |
         private static void assertModifiersEqual( String msg, XProgramElement expected, XProgramElement actual )
306 |  |
         {
307 | 8 |
             assertEquals( msg, expected.getModifiers(), actual.getModifiers() );
308 |  |
         }
309 |  |
310 | 4 |
         private static void assertNameWithSignatureEquals( String msg, XExecutableMember expected, XExecutableMember actual )
311 |  |
         {
312 | 4 |
             assertEquals( msg, expected.getNameWithSignature( false ), actual.getNameWithSignature( false ) );
313 |  |
         }
314 |  |
315 | 4 |
         private static void assertParametersEqual( String msg, XExecutableMember expected, XExecutableMember actual )
316 |  |
         {
317 | 4 |
             assertEquals( msg, expected.getParameters().size(), actual.getParameters().size() );
318 |  |
319 | 4 |
             Iterator expectedParameters = expected.getParameters().iterator();
320 | 4 |
             Iterator actualParameters = actual.getParameters().iterator();
321 |  |
322 | 4 |
             while( expectedParameters.hasNext() )
323 |  |
             {
324 | 2 |
                 XParameter expectedParameter = ( XParameter ) expectedParameters.next();
325 | 2 |
                 XParameter actualParameter = ( XParameter ) actualParameters.next();
326 |  |
327 | 2 |
                 assertParameterEquals( expectedParameter, actualParameter );
328 |  |
             }
329 |  |
         }
330 |  |
331 | 4 |
         private static void assertThrownExceptionsEqual( String msg, XExecutableMember expected, XExecutableMember actual )
332 |  |
         {
333 | 4 |
             assertEquals( msg, expected.getThrownExceptions().size(), actual.getThrownExceptions().size() );
334 |  |
335 | 4 |
             Iterator expectedThrownExceptions = expected.getThrownExceptions().iterator();
336 |  |
337 | 4 |
             while( expectedThrownExceptions.hasNext() )
338 |  |
             {
339 | 0 |
                 XClass expectedThrownException = ( XClass ) expectedThrownExceptions.next();
340 |  |
341 | 0 |
                 assertTrue( "Throws " + expectedThrownException.getQualifiedName(), actual.throwsException( expectedThrownException.getQualifiedName() ) );
342 |  |
             }
343 |  |
         }
344 |  |
345 |  |
         /**
346 |  |
          * Returns the directory where this class is located, provided that it's not in
347 |  |
          * a jar. This is very useful for accessing the files you want to compare.
348 |  |
          *
349 |  |
          * @return   the directory where this class is located.
350 |  |
          */
351 | 24 |
         protected File getDir()
352 |  |
         {
353 | 24 |
             return new File( getClass().getResource( "/" + getClass().getName().replace( '.', '/' ) + ".class" ).getFile() ).getParentFile();
354 |  |
         }
355 |  |
356 |  |
         /**
357 |  |
          * Returns the root directory of the package hierarchy where this class is
358 |  |
          * located, provided that it's not in a jar. This is very useful for accessing
359 |  |
          * the files you want to compare.
360 |  |
          *
361 |  |
          * @return   the root directory.
362 |  |
          */
363 | 24 |
         protected File getRootDir()
364 |  |
         {
365 | 24 |
             File dir = getDir();
366 | 24 |
             StringTokenizer st = new StringTokenizer( getClass().getName(), "." );
367 |  |
368 |  |
             // foo.bar.Baz = 3 tokens, but only 2 "directories"
369 | 24 |
             for( int i = 0; i < st.countTokens() - 1; i++ )
370 |  |
             {
371 | 24 |
                 dir = dir.getParentFile();
372 |  |
             }
373 | 24 |
             return dir;
374 |  |
         }
375 |  |
376 | 0 |
         protected XJavaDoc getXJavaDoc() {
377 | 0 |
             return new XJavaDoc();
378 |  |
         }
379 |  |
     }
380 |  |