KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.EnumSet 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.ExecutableElement;
29 import javax.lang.model.element.Modifier;
30 import javax.lang.model.element.TypeElement;
31 import org.netbeans.api.java.source.CompilationInfo;
32 import org.netbeans.api.java.source.ElementHandle;
33 import org.netbeans.api.java.source.JavaSource;
34 import org.netbeans.api.java.source.ModificationResult;
35 import org.netbeans.modules.junit.plugin.JUnitPlugin.CreateTestParam;
36
37 /**
38  *
39  * @author Marian Petras
40  */

41 public final class TestCreator implements TestabilityJudge {
42     
43     /**
44      * bitmap combining modifiers PUBLIC, PROTECTED and PRIVATE
45      *
46      * @see java.lang.reflect.Modifier
47      */

48     static final Set JavaDoc<Modifier> ACCESS_MODIFIERS
49             = EnumSet.of(Modifier.PUBLIC,
50                          Modifier.PROTECTED,
51                          Modifier.PRIVATE);
52     
53     /** */
54     private final TestGeneratorSetup setup;
55     
56     /** Creates a new instance of TestCreator */
57     TestCreator(boolean loadDefaults) {
58         setup = new TestGeneratorSetup(loadDefaults);
59     }
60     
61     /** Creates a new instance of TestCreator */
62     TestCreator(Map JavaDoc<CreateTestParam, Object JavaDoc> params) {
63         setup = new TestGeneratorSetup(params);
64     }
65     
66     /**
67      */

68     public void createEmptyTest(final JavaSource tstSource) throws IOException JavaDoc {
69         AbstractTestGenerator testGenerator
70                 = new JUnit3TestGenerator(setup);
71         ModificationResult result = tstSource.runModificationTask(testGenerator);
72         result.commit();
73     }
74     
75     /**
76      *
77      * @return list of names of created classes
78      */

79     public void createSimpleTest(ElementHandle<TypeElement> topClassToTest,
80                                  JavaSource tstSource,
81                                  boolean isNewTestClass) throws IOException JavaDoc {
82         AbstractTestGenerator testGenerator
83                 = new JUnit3TestGenerator(setup,
84                                           Collections.singletonList(topClassToTest),
85                                           null,
86                                           isNewTestClass);
87         ModificationResult result = tstSource.runModificationTask(testGenerator);
88         result.commit();
89     }
90     
91     /**
92      */

93     public List JavaDoc<String JavaDoc> createTestSuite(List JavaDoc<String JavaDoc> suiteMembers,
94                                         JavaSource tstSource,
95                                         boolean isNewTestClass) throws IOException JavaDoc {
96         AbstractTestGenerator testGenerator
97                 = new JUnit3TestGenerator(setup,
98                                           null,
99                                           suiteMembers,
100                                           isNewTestClass);
101         ModificationResult result = tstSource.runModificationTask(testGenerator);
102         result.commit();
103         
104         return testGenerator.getProcessedClassNames();
105     }
106     
107     public TestabilityResult isClassTestable(CompilationInfo compInfo,
108                                              TypeElement classElem) {
109         return setup.isClassTestable(compInfo, classElem);
110     }
111
112     public boolean isMethodTestable(ExecutableElement method) {
113         return setup.isMethodTestable(method);
114     }
115
116 }
117
Popular Tags