001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.junit;
043:
044: import java.io.IOException;
045: import java.util.Collections;
046: import java.util.EnumSet;
047: import java.util.List;
048: import java.util.Map;
049: import java.util.Set;
050: import javax.lang.model.element.ExecutableElement;
051: import javax.lang.model.element.Modifier;
052: import javax.lang.model.element.TypeElement;
053: import org.netbeans.api.java.source.CompilationInfo;
054: import org.netbeans.api.java.source.ElementHandle;
055: import org.netbeans.api.java.source.JavaSource;
056: import org.netbeans.api.java.source.ModificationResult;
057: import org.netbeans.modules.junit.plugin.JUnitPlugin.CreateTestParam;
058: import org.openide.filesystems.FileObject;
059:
060: /**
061: *
062: * @author Marian Petras
063: */
064: public final class TestCreator implements TestabilityJudge {
065:
066: /**
067: * bitmap combining modifiers PUBLIC, PROTECTED and PRIVATE
068: *
069: * @see java.lang.reflect.Modifier
070: */
071: static final Set<Modifier> ACCESS_MODIFIERS = EnumSet.of(
072: Modifier.PUBLIC, Modifier.PROTECTED, Modifier.PRIVATE);
073:
074: /** */
075: private final TestGeneratorSetup setup;
076: /** */
077: private final JUnitVersion junitVersion;
078:
079: /** Creates a new instance of TestCreator */
080: TestCreator(boolean loadDefaults, JUnitVersion junitVersion) {
081: setup = new TestGeneratorSetup(loadDefaults);
082: this .junitVersion = junitVersion;
083: }
084:
085: /** Creates a new instance of TestCreator */
086: TestCreator(Map<CreateTestParam, Object> params,
087: JUnitVersion junitVersion) {
088: setup = new TestGeneratorSetup(params);
089: this .junitVersion = junitVersion;
090: }
091:
092: /**
093: */
094: public void createEmptyTest(FileObject testFileObj)
095: throws IOException {
096: AbstractTestGenerator testGenerator;
097: switch (junitVersion) {
098: case JUNIT3:
099: testGenerator = new JUnit3TestGenerator(setup, TestUtil
100: .getSourceLevel(testFileObj));
101: break;
102: case JUNIT4:
103: testGenerator = new JUnit4TestGenerator(setup);
104: break;
105: default:
106: throw new IllegalStateException("junit version not set");//NOI18N
107: }
108: ModificationResult result = JavaSource.forFileObject(
109: testFileObj).runModificationTask(testGenerator);
110: result.commit();
111: }
112:
113: /**
114: *
115: * @return list of names of created classes
116: */
117: public void createSimpleTest(
118: ElementHandle<TypeElement> topClassToTest,
119: FileObject testFileObj, boolean isNewTestClass)
120: throws IOException {
121: AbstractTestGenerator testGenerator;
122: switch (junitVersion) {
123: case JUNIT3:
124: testGenerator = new JUnit3TestGenerator(setup, Collections
125: .singletonList(topClassToTest), null,
126: isNewTestClass, TestUtil
127: .getSourceLevel(testFileObj));
128: break;
129: case JUNIT4:
130: testGenerator = new JUnit4TestGenerator(setup, Collections
131: .singletonList(topClassToTest), null,
132: isNewTestClass);
133: break;
134: default:
135: throw new IllegalStateException("junit version not set");//NOI18N
136: }
137: ModificationResult result = JavaSource.forFileObject(
138: testFileObj).runModificationTask(testGenerator);
139: result.commit();
140: }
141:
142: /**
143: */
144: public List<String> createTestSuite(List<String> suiteMembers,
145: FileObject testFileObj, boolean isNewTestClass)
146: throws IOException {
147: AbstractTestGenerator testGenerator;
148: switch (junitVersion) {
149: case JUNIT3:
150: testGenerator = new JUnit3TestGenerator(setup, null,
151: suiteMembers, isNewTestClass, TestUtil
152: .getSourceLevel(testFileObj));
153: break;
154: case JUNIT4:
155: testGenerator = new JUnit4TestGenerator(setup, null,
156: suiteMembers, isNewTestClass);
157: break;
158: default:
159: throw new IllegalStateException("junit version not set");//NOI18N
160: }
161: ModificationResult result = JavaSource.forFileObject(
162: testFileObj).runModificationTask(testGenerator);
163: result.commit();
164:
165: return testGenerator.getProcessedClassNames();
166: }
167:
168: public TestabilityResult isClassTestable(CompilationInfo compInfo,
169: TypeElement classElem) {
170: return setup.isClassTestable(compInfo, classElem);
171: }
172:
173: public boolean isMethodTestable(ExecutableElement method) {
174: return setup.isMethodTestable(method);
175: }
176:
177: }
|