KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > TestGeneratorSetup


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.EnumSet JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.lang.model.element.Element;
29 import javax.lang.model.element.ExecutableElement;
30 import javax.lang.model.element.Modifier;
31 import javax.lang.model.element.NestingKind;
32 import javax.lang.model.element.TypeElement;
33 import javax.lang.model.util.ElementFilter;
34 import org.netbeans.api.java.source.CompilationInfo;
35 import org.netbeans.modules.junit.plugin.JUnitPlugin.CreateTestParam;
36 import static javax.lang.model.element.Modifier.ABSTRACT;
37 import static javax.lang.model.element.Modifier.PRIVATE;
38 import static javax.lang.model.element.Modifier.PROTECTED;
39 import static javax.lang.model.element.Modifier.PUBLIC;
40 import static javax.lang.model.element.Modifier.STATIC;
41 import static org.openide.NotifyDescriptor.WARNING_MESSAGE;
42 import static org.netbeans.modules.junit.TestCreator.ACCESS_MODIFIERS;
43
44 /**
45  *
46  * @author Marian Petras
47  */

48 public final class TestGeneratorSetup implements TestabilityJudge {
49     /* the class is final only for performance reasons */
50     
51     /* attributes - private */
52     static private final String JavaDoc JUNIT_SUPER_CLASS_NAME = "TestCase";
53     static private final String JavaDoc JUNIT_FRAMEWORK_PACKAGE_NAME = "junit.framework";
54     
55     static private final String JavaDoc METHOD_NAME_SETUP = "setUp"; //NOI18N
56
static private final String JavaDoc METHOD_NAME_TEARDOWN = "tearDown"; //NOI18N
57
static private final String JavaDoc CLASS_COMMENT_LINE1 = "TestCreator.javaClass.addTestsHereComment.l1";
58     static private final String JavaDoc CLASS_COMMENT_LINE2 = "TestCreator.javaClass.addTestsHereComment.l2";
59     
60     /** should test classes be skipped during generation of tests? */
61     private boolean skipTestClasses = true;
62     /** should package-private classes be skipped during generation of tests? */
63     private boolean skipPkgPrivateClasses = false;
64     /** should abstract classes be skipped during generation of tests? */
65     private boolean skipAbstractClasses = false;
66     /** should exception classes be skipped during generation of tests? */
67     private boolean skipExceptionClasses = false;
68     /**
69      * should test suite classes be generated when creating tests for folders
70      * and/or packages?
71      */

72     private boolean generateSuiteClasses = true;
73     /**
74      * bitmap defining whether public/protected methods should be tested
75      *
76      * @see #testPackagePrivateMethods
77      */

78     private Set JavaDoc<Modifier> methodAccessModifiers
79             = AbstractTestGenerator.createModifierSet(Modifier.PUBLIC,
80                                                       Modifier.PROTECTED);
81     /**
82      * should package-private methods be tested?
83      *
84      * @see #methodAccessModifiers
85      */

86     private boolean testPkgPrivateMethods = true;
87     /**
88      * should default method bodies be generated for newly created test methods?
89      *
90      * @see #generateMethodJavadoc
91      * @see #generateMethodBodyComments
92      */

93     private boolean generateDefMethodBody = true;
94     /**
95      * should Javadoc comment be generated for newly created test methods?
96      *
97      * @see #generateDefMethodBody
98      * @see #generateMethodBodyComments
99      */

100     private boolean generateMethodJavadoc = true;
101     /**
102      * should method body comment be generated for newly created test methods?
103      *
104      * @see #generateDefMethodBody
105      * @see #generateMethodJavadoc
106      */

107     private boolean generateSourceCodeHints = true;
108     /**
109      * should {@code setUp()} (or {@code @Before}) method be generated
110      * in test classes?
111      *
112      * @see #generateTestTearDown
113      * @see #generateClassSetUp
114      * @see #generateMainMethod
115      */

116     private boolean generateTestSetUp = true;
117     /**
118      * should {@code tearDown()} (or {@code @After}) method be generated
119      * in test classes?
120      *
121      * @see #generateTestSetUp
122      * @see #generateClassTearDown
123      * @see #generateMainMethod
124      */

125     private boolean generateTestTearDown = true;
126     /**
127      * should {@code @BeforeClass} method be generated in test classes?
128      *
129      * @see #generateClassTearDown
130      * @see #generateTestSetUp
131      * @see #generateMainMethod
132      */

133     private boolean generateClassSetUp = true;
134     /**
135      * should {@code @AfterClass} method be generated in test classes?
136      *
137      * @see #generateClassSetUp
138      * @see #generateTestTearDown
139      * @see #generateMainMethod
140      */

141     private boolean generateClassTearDown = true;
142     /**
143      * should static method <code>main(String args[])</code>
144      * be generated in test classes?
145      *
146      * @see #generateSetUp
147      * @see #generateTearDown
148      */

149     private boolean generateMainMethod = true;
150     
151     /* public methods */
152     
153     /**
154      * Creates a new <code>TestCreator</code>.
155      *
156      * @param loadDefaults <code>true</code> if defaults should be loaded
157      * from <code>JUnitSettings</code>;
158      * <code>false</code> otherwise
159      */

160     public TestGeneratorSetup(boolean loadDefaults) {
161         if (loadDefaults) {
162             loadDefaults();
163         }
164     }
165     
166     /**
167      *
168      */

169     public TestGeneratorSetup(Map JavaDoc<CreateTestParam, Object JavaDoc> params) {
170         final JUnitSettings settings = JUnitSettings.getDefault();
171         
172         skipTestClasses = !JUnitSettings.GENERATE_TESTS_FROM_TEST_CLASSES;
173         
174         skipPkgPrivateClasses = !Boolean.TRUE.equals(params.get(
175                                         CreateTestParam.INC_PKG_PRIVATE_CLASS));
176         skipAbstractClasses = !Boolean.TRUE.equals(params.get(
177                                         CreateTestParam.INC_ABSTRACT_CLASS));
178         skipExceptionClasses = !Boolean.TRUE.equals(params.get(
179                                         CreateTestParam.INC_EXCEPTION_CLASS));
180         generateSuiteClasses = Boolean.TRUE.equals(params.get(
181                                         CreateTestParam.INC_GENERATE_SUITE));
182         
183         methodAccessModifiers.clear();
184         if (Boolean.TRUE.equals(params.get(CreateTestParam.INC_PUBLIC))) {
185             methodAccessModifiers.add(Modifier.PUBLIC);
186         }
187         if (Boolean.TRUE.equals(params.get(CreateTestParam.INC_PROTECTED))) {
188             methodAccessModifiers.add(Modifier.PROTECTED);
189         }
190         testPkgPrivateMethods = Boolean.TRUE.equals(params.get(
191                                         CreateTestParam.INC_PKG_PRIVATE));
192         generateDefMethodBody = Boolean.TRUE.equals(params.get(
193                                         CreateTestParam.INC_METHOD_BODIES));
194         generateMethodJavadoc = Boolean.TRUE.equals(params.get(
195                                         CreateTestParam.INC_JAVADOC));
196         generateSourceCodeHints = Boolean.TRUE.equals(params.get(
197                                         CreateTestParam.INC_CODE_HINT));
198         generateTestSetUp = Boolean.TRUE.equals(params.get(
199                                         CreateTestParam.INC_SETUP));
200         generateTestTearDown = Boolean.TRUE.equals(params.get(
201                                         CreateTestParam.INC_TEAR_DOWN));
202         
203         generateMainMethod = settings.isGenerateMainMethod();
204     }
205
206     
207     /**
208      * Loads default settings from <code>JUnitSettings</code>.
209      */

210     private void loadDefaults() {
211         final JUnitSettings settings = JUnitSettings.getDefault();
212         
213         skipTestClasses = JUnitSettings.GENERATE_TESTS_FROM_TEST_CLASSES;
214         skipPkgPrivateClasses = !settings.isIncludePackagePrivateClasses();
215         skipAbstractClasses = !settings.isGenerateAbstractImpl();
216         skipExceptionClasses = !settings.isGenerateExceptionClasses();
217         generateSuiteClasses = settings.isGenerateSuiteClasses();
218         
219         methodAccessModifiers.clear();
220         if (settings.isMembersPublic()) {
221             methodAccessModifiers.add(Modifier.PUBLIC);
222         }
223         if (settings.isMembersProtected()) {
224             methodAccessModifiers.add(Modifier.PROTECTED);
225         }
226         testPkgPrivateMethods = settings.isMembersPackage();
227         
228         generateDefMethodBody = settings.isBodyContent();
229         generateMethodJavadoc = settings.isJavaDoc();
230         generateSourceCodeHints = settings.isBodyComments();
231         generateTestSetUp = settings.isGenerateSetUp();
232         generateTestTearDown = settings.isGenerateTearDown();
233         generateMainMethod = settings.isGenerateMainMethod();
234     }
235     
236     /**
237      * Sets whether tests for test classes should be generated
238      * The default is <code>true</code>.
239      *
240      * @param test <code>false</code> if test classes should be skipped
241      * during test creation;
242      * <code>true</code> otherwise
243      */

244     public void setSkipTestClasses(boolean skip) {
245         this.skipTestClasses = skip;
246     }
247     
248     /**
249      */

250     public boolean isSkipTestClasses() {
251         return skipTestClasses;
252     }
253     
254     /**
255      * Sets whether tests for package-private classes should be generated
256      * The default is <code>false</code>.
257      *
258      * @param test <code>false</code> if package-private classes should
259      * be skipped during test creation;
260      * <code>true</code> otherwise
261      */

262     public void setSkipPackagePrivateClasses(boolean skip) {
263         this.skipPkgPrivateClasses = skip;
264     }
265
266     /**
267      */

268     public boolean isSkipPackagePrivateClasses() {
269         return skipPkgPrivateClasses;
270     }
271     
272     /**
273      * Sets whether tests for abstract classes should be generated
274      * The default is <code>false</code>.
275      *
276      * @param test <code>false</code> if abstract classes should be skipped
277      * during test creation;
278      * <code>true</code> otherwise
279      */

280     public void setSkipAbstractClasses(boolean skip) {
281         this.skipAbstractClasses = skip;
282     }
283
284     /**
285      */

286     public boolean isSkipAbstractClasses() {
287         return skipAbstractClasses;
288     }
289     
290     /**
291      * Sets whether tests for exception classes should be generated
292      * The default is <code>false</code>.
293      *
294      * @param test <code>false</code> if exception classes should be skipped
295      * during test creation;
296      * <code>true</code> otherwise
297      */

298     public void setSkipExceptionClasses(boolean skip) {
299         this.skipExceptionClasses = skip;
300     }
301
302     /**
303      */

304     public boolean isSkipExceptionClasses() {
305         return skipExceptionClasses;
306     }
307     
308     /**
309      * Sets whether test suite classes should be generated when creating tests
310      * for folders and/or packages.
311      *
312      * @param generate <code>true</code> if test suite classes should
313      * be generated; <code>false</code> otherwise
314      */

315     public void setGenerateSuiteClasses(boolean generate) {
316         this.generateSuiteClasses = generate;
317     }
318
319     /**
320      */

321     public boolean isGenerateSuiteClasses() {
322         return generateSuiteClasses;
323     }
324     
325     /**
326      * Sets whether public methods should be tested or not.
327      * The default is <code>true</code>.
328      *
329      * @param test <code>true</code> if public methods should be tested;
330      * <code>false</code> if public methods should be skipped
331      */

332     public void setTestPublicMethods(boolean test) {
333         if (test) {
334             methodAccessModifiers.add(Modifier.PUBLIC);
335         } else {
336             methodAccessModifiers.remove(Modifier.PUBLIC);
337         }
338     }
339
340     /**
341      */

342     public boolean isTestPublicMethods() {
343         return methodAccessModifiers.contains(Modifier.PUBLIC);
344     }
345     
346     /**
347      * Sets whether protected methods should be tested or not.
348      * The default is <code>true</code>.
349      *
350      * @param test <code>true</code> if protected methods should be tested;
351      * <code>false</code> if protected methods should be skipped
352      */

353     public void setTestProtectedMethods(boolean test) {
354         if (test) {
355             methodAccessModifiers.add(Modifier.PROTECTED);
356         } else {
357             methodAccessModifiers.remove(Modifier.PROTECTED);
358         }
359     }
360     
361     /**
362      */

363     public boolean isTestProtectedMethods() {
364         return methodAccessModifiers.contains(Modifier.PROTECTED);
365     }
366     
367     /**
368      * Tells which methods should be tested - public, protected, private
369      * or a combination of these.
370      *
371      * @return {@code EnumSet} of access modifiers that determine which methods
372      * should be tested (for which test skeletons should be created).
373      */

374     public EnumSet JavaDoc<Modifier> getMethodAccessModifiers() {
375         return EnumSet.copyOf(methodAccessModifiers);
376     }
377     
378     /**
379      * Sets whether package-private methods should be tested or not.
380      * The default is <code>true</code>.
381      *
382      * @param test <code>true</code> if package-private methods should be
383      * tested;
384      * <code>false</code> if package-private methods should be
385      * skipped
386      */

387     public void setTestPackagePrivateMethods(boolean test) {
388         this.testPkgPrivateMethods = test;
389     }
390
391     /**
392      */

393     public boolean isTestPackagePrivateMethods() {
394         return testPkgPrivateMethods;
395     }
396     
397     /**
398      * Sets whether default method bodies should be generated for newly created
399      * test methods.
400      * The default is <code>true</code>.
401      *
402      * @param generate <code>true</code> if default method bodies should
403      * be generated; <code>false</code> otherwise
404      */

405     public void setGenerateDefMethodBody(boolean generate) {
406         this.generateDefMethodBody = generate;
407     }
408
409     /**
410      */

411     public boolean isGenerateDefMethodBody() {
412         return generateDefMethodBody;
413     }
414     
415     /**
416      * Sets whether Javadoc comment should be generated for newly created
417      * test methods.
418      * The default is <code>true</code>.
419      *
420      * @param generate <code>true</code> if Javadoc comment should
421      * be generated; <code>false</code> otherwise
422      */

423     public void setGenerateMethodJavadoc(boolean generate) {
424         this.generateMethodJavadoc = generate;
425     }
426
427     /**
428      */

429     public boolean isGenerateMethodJavadoc() {
430         return generateMethodJavadoc;
431     }
432     
433     /**
434      * Sets whether method body comment should be generated for newly created
435      * test methods.
436      * The default is <code>true</code>.
437      *
438      * @param generate <code>true</code> if method body comment should
439      * be generated; <code>false</code> otherwise
440      */

441     public void setGenerateMethodBodyComment(boolean generate) {
442         this.generateSourceCodeHints = generate;
443     }
444
445     /**
446      */

447     public boolean isGenerateMethodBodyComment() {
448         return generateSourceCodeHints;
449     }
450     
451     /**
452      * Sets whether <code>setUp()</code> method should be generated
453      * in test classes being created/updated.
454      * The default is <code>true</code>.
455      *
456      * @param generate <code>true</code> if <code>setUp()</code> method
457      * should be generated; <code>false</code> otherwise
458      * @see #setGenerateTearDown
459      * @see #setGenerateMainMethod
460      */

461     public void setGenerateSetUp(boolean generate) {
462         this.generateTestSetUp = generate;
463     }
464
465     /**
466      */

467     public boolean isGenerateSetUp() {
468         return generateTestSetUp;
469     }
470     
471     /**
472      * Sets whether <code>tearDown()</code> method should be generated
473      * in test classes being created/updated.
474      * The default is <code>true</code>.
475      *
476      * @param generate <code>true</code> if <code>tearDown()</code> method
477      * should be generated; <code>false</code> otherwise
478      * @see #setGenerateSetUp
479      * @see #setGenerateMainMethod
480      */

481     public void setGenerateTearDown(boolean generate) {
482         this.generateTestTearDown = generate;
483     }
484
485     /**
486      */

487     public boolean isGenerateTearDown() {
488         return generateTestTearDown;
489     }
490     
491     /**
492      * Sets whether <code>setUp()</code> method should be generated
493      * in test classes being created/updated.
494      * The default is <code>true</code>.
495      *
496      * @param generate <code>true</code> if <code>setUp()</code> method
497      * should be generated; <code>false</code> otherwise
498      * @see #setGenerateTearDown
499      * @see #setGenerateMainMethod
500      */

501     public void setGenerateBefore(boolean generate) {
502         setGenerateSetUp(generate);
503     }
504
505     /**
506      */

507     public boolean isGenerateBefore() {
508         return isGenerateSetUp();
509     }
510     
511     /**
512      * Sets whether <code>tearDown()</code> method should be generated
513      * in test classes being created/updated.
514      * The default is <code>true</code>.
515      *
516      * @param generate <code>true</code> if <code>tearDown()</code> method
517      * should be generated; <code>false</code> otherwise
518      * @see #setGenerateSetUp
519      * @see #setGenerateMainMethod
520      */

521     public void setGenerateAfter(boolean generate) {
522         setGenerateTearDown(generate);
523     }
524
525     /**
526      */

527     public boolean isGenerateAfter() {
528         return isGenerateTearDown();
529     }
530     
531     /**
532      * Sets whether <code>setUp()</code> method should be generated
533      * in test classes being created/updated.
534      * The default is <code>true</code>.
535      *
536      * @param generate <code>true</code> if <code>setUp()</code> method
537      * should be generated; <code>false</code> otherwise
538      * @see #setGenerateTearDown
539      * @see #setGenerateMainMethod
540      */

541     public void setGenerateBeforeClass(boolean generate) {
542         this.generateClassSetUp = generate;
543     }
544
545     /**
546      */

547     public boolean isGenerateBeforeClass() {
548         return generateClassSetUp;
549     }
550     
551     /**
552      * Sets whether <code>tearDown()</code> method should be generated
553      * in test classes being created/updated.
554      * The default is <code>true</code>.
555      *
556      * @param generate <code>true</code> if <code>tearDown()</code> method
557      * should be generated; <code>false</code> otherwise
558      * @see #setGenerateSetUp
559      * @see #setGenerateMainMethod
560      */

561     public void setGenerateAfterClass(boolean generate) {
562         this.generateClassTearDown = generate;
563     }
564
565     /**
566      */

567     public boolean isGenerateAfterClass() {
568         return generateClassTearDown;
569     }
570     
571     /**
572      * Sets whether static method <code>main(String args[])</code> should
573      * be generated in test classes.
574      * The default is <code>true</code>.
575      *
576      * @param generate <code>true</code> if the method should be generated;
577      * <code>false</code> otherwise
578      * @see #setGenerateSetUp
579      * @see #setGenerateTearDown
580      */

581     public void setGenerateMainMethod(boolean generate) {
582         this.generateMainMethod = generate;
583     }
584
585     /**
586      */

587     public boolean isGenerateMainMethod() {
588         return generateMainMethod;
589     }
590
591     /**
592      * Checks whether the given class or at least one of its nested classes
593      * is testable.
594      *
595      * @param compInfo used for {@link CompilationInfo#getElements()}
596      * and {@link CompilationInfo#getTypes()}
597      * @param classElem class to be checked
598      * @return TestabilityResult that isOk, if the class is testable or carries
599      * the information why the class is not testable
600      */

601     public TestabilityResult isClassTestable(CompilationInfo compInfo,
602                                              TypeElement classElem) {
603         assert classElem != null;
604         
605         TestabilityResult result = isClassTestableSingle(compInfo, classElem);
606
607         if (result.isTestable()) {
608             return TestabilityResult.OK;
609         }
610
611         List JavaDoc<? extends Element> enclosedElems = classElem.getEnclosedElements();
612         if (enclosedElems.isEmpty()) {
613             /* Not testable, no contained types - no more chance: */
614             return result;
615         }
616         
617         List JavaDoc<TypeElement> enclosedTypes = ElementFilter.typesIn(enclosedElems);
618         if (enclosedTypes.isEmpty()) {
619             /* Not testable, no contained types - no more chance: */
620             return result;
621         }
622         
623         /* Not testable but maybe one of its nested classes is testable: */
624         List JavaDoc<TypeElement> stack
625                = new ArrayList JavaDoc<TypeElement>(Math.max(10, enclosedTypes.size()));
626         stack.addAll(enclosedTypes);
627         int stackSize = stack.size();
628
629         Set JavaDoc<TypeElement> nonTestable = new HashSet JavaDoc<TypeElement>(64);
630         nonTestable.add(classElem);
631
632         do {
633             TypeElement classToCheck = stack.remove(--stackSize);
634             
635             if (!TopClassFinder.isTestable(classElem)) { //it is an annotation
636
continue;
637             }
638
639             if (!nonTestable.add(classToCheck)) {
640                 continue; //we already know this single class is nontestable
641
}
642
643             TestabilityResult resultSingle
644                                 = isClassTestableSingle(compInfo, classToCheck);
645             if (resultSingle.isTestable()) {
646                 return TestabilityResult.OK;
647             } else {
648                 result = TestabilityResult.combine(result, resultSingle);
649             }
650
651             enclosedTypes = ElementFilter.typesIn(classToCheck.getEnclosedElements());
652             if (!enclosedTypes.isEmpty()) {
653                 stack.addAll(enclosedTypes);
654                 stackSize = stack.size();
655             }
656         } while (stackSize != 0);
657
658         /* So not a single contained class is testable - no more chance: */
659         return result;
660     }
661     
662     /**
663      * Checks whether the given class is testable.
664      *
665      * @param jc class to be checked
666      * @return TestabilityResult that isOk, if the class is testable or carries
667      * the information why the class is not testable
668      */

669     private TestabilityResult isClassTestableSingle(CompilationInfo compInfo,
670                                                     TypeElement classElem) {
671         assert classElem != null;
672         
673         TestabilityResult result = TestabilityResult.OK;
674
675         /*
676          * If the class is a test class and test classes should be skipped,
677          * do not check nested classes (skip all):
678          */

679         /* Check if the class itself (w/o nested classes) is testable: */
680         Set JavaDoc<Modifier> modifiers = classElem.getModifiers();
681
682         if (modifiers.contains(PRIVATE))
683             result = TestabilityResult.combine(result, TestabilityResult.PRIVATE_CLASS);
684         if (isSkipTestClasses() && TestUtil.isClassImplementingTestInterface(compInfo, classElem))
685             result = TestabilityResult.combine(result, TestabilityResult.TEST_CLASS);
686         if (isSkipPackagePrivateClasses() && (modifiers.isEmpty() || !EnumSet.copyOf(modifiers).removeAll(ACCESS_MODIFIERS)))
687             result = TestabilityResult.combine(result, TestabilityResult.PACKAGE_PRIVATE_CLASS);
688         if (isSkipAbstractClasses() && modifiers.contains(ABSTRACT))
689             result = TestabilityResult.combine(result, TestabilityResult.ABSTRACT_CLASS);
690         if (!modifiers.contains(STATIC) && (classElem.getNestingKind() != NestingKind.TOP_LEVEL))
691             result = TestabilityResult.combine(result, TestabilityResult.NONSTATIC_INNER_CLASS);
692         if (!hasTestableMethods(classElem))
693             result = TestabilityResult.combine(result, TestabilityResult.NO_TESTEABLE_METHODS);
694         if (isSkipExceptionClasses() && TestUtil.isClassException(compInfo, classElem))
695             result = TestabilityResult.combine(result, TestabilityResult.EXCEPTION_CLASS);
696
697         return result;
698     }
699     
700     /**
701      */

702     private boolean hasTestableMethods(TypeElement classElem) {
703         List JavaDoc<? extends Element> enclosedElems = classElem.getEnclosedElements();
704         if (enclosedElems.isEmpty()) {
705             return false;
706         }
707         
708         List JavaDoc<ExecutableElement> methods = ElementFilter.methodsIn(enclosedElems);
709         if (methods.isEmpty()) {
710             return false;
711         }
712         
713         for (ExecutableElement method : methods) {
714             if (isMethodTestable(method)) {
715                 return true;
716             }
717         }
718         
719         return false;
720     }
721     
722     /**
723      * Checks whether a test for the given method should be created.
724      * Access modifiers of the given method are compared to this creator's
725      * settings.
726      *
727      * @param m method to be checked
728      * @return <code>true</code> if this creator is configured to create tests
729      * for methods having the given method's access modifiers;
730      * <code>false</code> otherwise
731      */

732     public boolean isMethodTestable(ExecutableElement method) {
733         Set JavaDoc<Modifier> modifiers = method.getModifiers();
734         
735         if (modifiers.isEmpty()) {
736             /*
737              * EnumSet.copyOf(modifiers) may throw an exception if 'modifiers'
738              * is empty.
739              */

740             return isTestPackagePrivateMethods();
741         } else {
742             return (isTestPackagePrivateMethods()
743                         && !EnumSet.copyOf(modifiers).removeAll(ACCESS_MODIFIERS))
744                    || EnumSet.copyOf(modifiers).removeAll(getMethodAccessModifiers());
745         }
746     }
747     
748 }
749
Popular Tags