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-2006 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 org.openide.*;
045: import org.openide.src.*;
046: import org.openide.filesystems.*;
047: import org.openide.loaders.*;
048: import org.openide.cookies.*;
049: import org.netbeans.modules.java.*;
050:
051: import junit.framework.*;
052: import org.netbeans.junit.*;
053:
054: import java.io.*;
055: import java.lang.reflect.*;
056: import java.util.*;
057:
058: public class TestCreatorTest extends NbTestCase implements
059: DataLoader.RecognizedFiles {
060:
061: public TestCreatorTest(java.lang.String testName) {
062: super (testName);
063: }
064:
065: public static void main(java.lang.String[] args) {
066: junit.textui.TestRunner.run(suite());
067: }
068:
069: public static Test suite() {
070: return new TestSuite(TestCreatorTest.class);
071: }
072:
073: /** Test of createTestClass method, of class org.netbeans.modules.junit.TestCreator. */
074: public void testCreateTestClass() throws Exception {//IOException, ClassNotFoundException, SourceException {
075: System.out.println("testCreateTestClass");
076:
077: JUnitSettings js = JUnitSettings.getDefault();
078:
079: setGenerateFlags(js, true);
080: TestCreator.initialize();
081:
082: JavaDataLoader jdl = new JavaDataLoader();
083: LocalFileSystem fsData = new LocalFileSystem();
084: fsData.setRootDirectory(new File(appendSlash(m_pathData)
085: + "CreateTestClass"));
086:
087: FileObject foSrc = fsData.findResource("src");
088: FileObject foTrg = fsData.findResource("trg");
089: FileObject foPass = fsData.findResource("pass");
090:
091: DataObject doTempl = jdl.findDataObject(fsData
092: .findResource(CLASS_TEMPLATE), this );
093: DataFolder doTrg = (DataFolder) DataFolder.find(foTrg);
094:
095: FileObject foList[] = foSrc.getChildren();
096: for (int i = 0; i < foList.length; i++) {
097: FileObject foEntry = foList[i];
098: if (foEntry.getExt().equals("java")) {
099: DataObject doSrcEntry = null;
100: DataObject doTrgEntry = null;
101: String name = foEntry.getName() + "Test";
102: File fTmp = new File(appendSlash(fsData
103: .getRootDirectory().getPath())
104: + appendSlash(foTrg.getNameExt())
105: + name
106: + ".java");
107:
108: fTmp.delete();
109: doSrcEntry = jdl.findDataObject(foEntry, this );
110: doTrgEntry = doTempl.createFromTemplate(doTrg, name);
111:
112: ClassElement ceSrc = getClassElementFromDO(doSrcEntry);
113: ClassElement ceTrg = getClassElementFromDO(doTrgEntry);
114: TestCreator.createTestClass(ceSrc, ceTrg);
115: SaveCookie sc = (SaveCookie) doTrgEntry
116: .getCookie(SaveCookie.class);
117: sc.save();
118:
119: assertFile(getFile(doTrgEntry.getPrimaryFile()),
120: new File(getFile(foPass), name + ".java"),
121: new File(System.getProperty("xresults")));
122: }
123: }
124:
125: DataObject doSrcEntry = DataObject.find(fsData
126: .findResource("src/TestClass001.java"));
127: DataObject doTrgEntry;
128: ClassElement ceSrc = getClassElementFromDO(doSrcEntry);
129: ClassElement ceTrg;
130:
131: setGenerateFlags(js, false);
132:
133: new File(appendSlash(fsData.getRootDirectory().getPath())
134: + "trg/TestPublic.java").delete();
135: js.setMembersPublic(true);
136: assertTrue(true == js.isMembersPublic());
137: TestCreator.initialize();
138: doTrgEntry = doTempl.createFromTemplate(doTrg, "TestPublic");
139: ceTrg = getClassElementFromDO(doTrgEntry);
140: TestCreator.createTestClass(ceSrc, ceTrg);
141: ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
142: js.setMembersPublic(false);
143: assertTrue(false == js.isMembersPublic());
144: assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(
145: getFile(foPass), "TestPublic.java"), new File(System
146: .getProperty("xresults")));
147:
148: new File(appendSlash(fsData.getRootDirectory().getPath())
149: + "trg/TestProtected.java").delete();
150: js.setMembersProtected(true);
151: assertTrue(true == js.isMembersProtected());
152: TestCreator.initialize();
153: doTrgEntry = doTempl.createFromTemplate(doTrg, "TestProtected");
154: ceTrg = getClassElementFromDO(doTrgEntry);
155: TestCreator.createTestClass(ceSrc, ceTrg);
156: ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
157: js.setMembersProtected(false);
158: assertTrue(false == js.isMembersProtected());
159: assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(
160: getFile(foPass), "TestProtected.java"), new File(System
161: .getProperty("xresults")));
162:
163: new File(appendSlash(fsData.getRootDirectory().getPath())
164: + "trg/TestPackage.java").delete();
165: js.setMembersPackage(true);
166: assertTrue(true == js.isMembersPackage());
167: TestCreator.initialize();
168: doTrgEntry = doTempl.createFromTemplate(doTrg, "TestPackage");
169: ceTrg = getClassElementFromDO(doTrgEntry);
170: TestCreator.createTestClass(ceSrc, ceTrg);
171: ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
172: js.setMembersPackage(false);
173: assertTrue(false == js.isMembersPackage());
174: assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(
175: getFile(foPass), "TestPackage.java"), new File(System
176: .getProperty("xresults")));
177:
178: new File(appendSlash(fsData.getRootDirectory().getPath())
179: + "trg/TestBodyComments.java").delete();
180: js.setMembersPublic(true);
181: assertTrue(true == js.isMembersPublic());
182: TestCreator.initialize();
183: js.setBodyComments(true);
184: assertTrue(true == js.isBodyComments());
185: doTrgEntry = doTempl.createFromTemplate(doTrg,
186: "TestBodyComments");
187: ceTrg = getClassElementFromDO(doTrgEntry);
188: TestCreator.createTestClass(ceSrc, ceTrg);
189: ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
190: js.setBodyComments(false);
191: assertTrue(false == js.isBodyComments());
192: assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(
193: getFile(foPass), "TestBodyComments.java"), new File(
194: System.getProperty("xresults")));
195:
196: new File(appendSlash(fsData.getRootDirectory().getPath())
197: + "trg/TestBodyContent.java").delete();
198: js.setBodyContent(true);
199: assertTrue(true == js.isBodyContent());
200: doTrgEntry = doTempl.createFromTemplate(doTrg,
201: "TestBodyContent");
202: ceTrg = getClassElementFromDO(doTrgEntry);
203: TestCreator.createTestClass(ceSrc, ceTrg);
204: ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
205: js.setBodyContent(false);
206: assertTrue(false == js.isBodyContent());
207: assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(
208: getFile(foPass), "TestBodyContent.java"), new File(
209: System.getProperty("xresults")));
210:
211: new File(appendSlash(fsData.getRootDirectory().getPath())
212: + "trg/TestJavaDoc.java").delete();
213: js.setJavaDoc(true);
214: assertTrue(true == js.isJavaDoc());
215: doTrgEntry = doTempl.createFromTemplate(doTrg, "TestJavaDoc");
216: ceTrg = getClassElementFromDO(doTrgEntry);
217: TestCreator.createTestClass(ceSrc, ceTrg);
218: ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
219: js.setJavaDoc(false);
220: assertTrue(false == js.isJavaDoc());
221: assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(
222: getFile(foPass), "TestJavaDoc.java"), new File(System
223: .getProperty("xresults")));
224: }
225:
226: /** Test of createTestSuit method, of class org.netbeans.modules.junit.TestCreator. */
227: public void testCreateTestSuit() throws Exception {
228: System.out.println("testCreateTestSuit");
229:
230: JavaDataLoader jdl = new JavaDataLoader();
231: LocalFileSystem fsData = new LocalFileSystem();
232: fsData.setRootDirectory(new File(appendSlash(m_pathData)
233: + "CreateTestSuite"));
234:
235: FileObject foPass = fsData.findResource(TESTSUITE
236: + ".java.pass");
237: DataObject doTempl = jdl.findDataObject(fsData
238: .findResource(CLASS_TEMPLATE), this );
239:
240: File fTmp = new File(appendSlash(fsData.getRootDirectory()
241: .getPath())
242: + TESTSUITE + ".java");
243:
244: fTmp.delete();
245: DataObject doTrgEntry = doTempl.createFromTemplate(
246: (DataFolder) DataFolder.find(fsData.getRoot()),
247: TESTSUITE);
248: ClassElement ceTrg = getClassElementFromDO(doTrgEntry);
249:
250: LinkedList lst = new LinkedList();
251: lst.add("a.b.c.TestClass1");
252: lst.add("a.b.c.TestClass2");
253: lst.add("TestClass3");
254:
255: TestCreator.createTestSuit(lst, "some.test.pckg", ceTrg);
256: SaveCookie sc = (SaveCookie) doTrgEntry
257: .getCookie(SaveCookie.class);
258: sc.save();
259:
260: assertFile(getFile(doTrgEntry.getPrimaryFile()),
261: getFile(foPass), new File(System
262: .getProperty("xresults")));
263: }
264:
265: /** Test of initialize method, of class org.netbeans.modules.junit.TestCreator. */
266: public void testInitialize() {
267: System.out.println("testInitialize");
268:
269: // functionality of initialize method is verified in createTestClass method test
270: }
271:
272: /** Test of isClassTestable method, of class org.netbeans.modules.junit.TestCreator. */
273: public void testIsClassTestable() throws Exception {
274: System.out.println("testIsClassTestable");
275:
276: FileObject foClass;
277: DataObject doClass;
278: ClassElement clazz;
279: JUnitSettings js = JUnitSettings.getDefault();
280: JavaDataLoader jdl = new JavaDataLoader();
281: LocalFileSystem fsData = new LocalFileSystem();
282: fsData.setRootDirectory(new File(appendSlash(m_pathData)
283: + "IsClassTestable"));
284:
285: foClass = fsData.findResource("SimpleClass.java");
286: doClass = jdl.findDataObject(foClass, this );
287: clazz = getClassElementFromDO(doClass);
288: assertTrue(true == TestCreator.isClassTestable(clazz));
289:
290: foClass = fsData.findResource("NonPublicClass.java");
291: doClass = jdl.findDataObject(foClass, this );
292: clazz = getClassElementFromDO(doClass);
293: assertTrue(false == TestCreator.isClassTestable(clazz));
294:
295: foClass = fsData.findResource("AbstractClass.java");
296: doClass = jdl.findDataObject(foClass, this );
297: clazz = getClassElementFromDO(doClass);
298: assertTrue(true == TestCreator.isClassTestable(clazz));
299:
300: foClass = fsData.findResource("SimpleInterface.java");
301: doClass = jdl.findDataObject(foClass, this );
302: clazz = getClassElementFromDO(doClass);
303: assertTrue(false == TestCreator.isClassTestable(clazz));
304:
305: js.setGenerateExceptionClasses(false);
306: assertTrue(false == js.isGenerateExceptionClasses());
307: foClass = fsData.findResource("ExceptionClass.java");
308: doClass = jdl.findDataObject(foClass, this );
309: clazz = getClassElementFromDO(doClass);
310: assertTrue(false == TestCreator.isClassTestable(clazz));
311:
312: js.setGenerateExceptionClasses(true);
313: assertTrue(true == js.isGenerateExceptionClasses());
314: assertTrue(true == TestCreator.isClassTestable(clazz));
315: }
316:
317: // protected members
318: protected static final String PROP_CLASSPATH = "java.class.path";
319: protected String m_pathData = null;
320:
321: protected void setUp() {
322: if (null == m_pathData)
323: m_pathData = System.getProperty("xdata");
324: }
325:
326: protected void tearDown() {
327: }
328:
329: protected class FileExtFilter implements FileFilter {
330: protected String extension = null;
331:
332: public FileExtFilter(String extension) {
333: this .extension = extension;
334: }
335:
336: public boolean accept(File pathname) {
337: String ext = null;
338: int i;
339:
340: if (-1 != (i = pathname.getPath().lastIndexOf('.')))
341: ext = pathname.getPath().substring(i + 1);
342:
343: if (null == ext)
344: return (null == extension);
345:
346: return extension.equals(ext);
347: }
348: }
349:
350: // private members
351: private static final String CLASS_TEMPLATE = "ClassTemplate.java";
352: private static final String TESTSUITE = "TestSuite";
353:
354: private String appendSlash(String path) {
355: if (null == path)
356: return new String();
357:
358: if (!path.endsWith("\\") && !path.endsWith("/"))
359: return path + "\\";
360:
361: return path;
362: }
363:
364: private ClassElement getClassElementFromDO(DataObject dO) {
365: SourceCookie sc;
366: SourceElement se;
367:
368: sc = (SourceCookie) dO.getCookie(SourceCookie.class);
369: se = sc.getSource();
370: return se.getClass(Identifier.create(dO.getPrimaryFile()
371: .getName()));
372: }
373:
374: private File getFile(FileObject fo)
375: throws FileStateInvalidException {
376: StringBuffer name = new StringBuffer();
377: LocalFileSystem lfs = (LocalFileSystem) fo.getFileSystem();
378:
379: name.append(lfs.getRootDirectory().getPath());
380: if (0 != name.length())
381: name.append("/");
382: name.append(fo.getPackageNameExt('/', '.'));
383:
384: return new File(name.toString());
385: }
386:
387: private void setGenerateFlags(JUnitSettings js, boolean flag) {
388: js.setMembersPublic(flag);
389: assertTrue(flag == js.isMembersPublic());
390: js.setMembersProtected(flag);
391: assertTrue(flag == js.isMembersProtected());
392: js.setMembersPackage(flag);
393: assertTrue(flag == js.isMembersPackage());
394: js.setBodyComments(flag);
395: assertTrue(flag == js.isBodyComments());
396: js.setBodyContent(flag);
397: assertTrue(flag == js.isBodyContent());
398: js.setJavaDoc(flag);
399: assertTrue(flag == js.isJavaDoc());
400: }
401:
402: public void markRecognized(FileObject fo) {
403: }
404:
405: }
|