KickJava   Java API By Example, From Geeks To Geeks.

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


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit;
21
22 import org.openide.*;
23 import org.openide.src.*;
24 import org.openide.filesystems.*;
25 import org.openide.loaders.*;
26 import org.openide.cookies.*;
27 import org.netbeans.modules.java.*;
28
29 import junit.framework.*;
30 import org.netbeans.junit.*;
31
32 import java.io.*;
33 import java.lang.reflect.*;
34 import java.util.*;
35
36 public class TestCreatorTest extends NbTestCase implements DataLoader.RecognizedFiles {
37
38     public TestCreatorTest(java.lang.String JavaDoc testName) {
39         super(testName);
40     }
41
42     public static void main(java.lang.String JavaDoc[] args) {
43         junit.textui.TestRunner.run(suite());
44     }
45     
46     public static Test suite() {
47         return new TestSuite(TestCreatorTest.class);
48     }
49     
50     /** Test of createTestClass method, of class org.netbeans.modules.junit.TestCreator. */
51     public void testCreateTestClass() throws Exception JavaDoc {//IOException, ClassNotFoundException, SourceException {
52
System.out.println("testCreateTestClass");
53
54         JUnitSettings js = JUnitSettings.getDefault();
55
56         setGenerateFlags(js, true);
57         TestCreator.initialize();
58         
59         JavaDataLoader jdl = new JavaDataLoader();
60         LocalFileSystem fsData = new LocalFileSystem();
61         fsData.setRootDirectory(new File(appendSlash(m_pathData) + "CreateTestClass"));
62         
63         FileObject foSrc = fsData.findResource("src");
64         FileObject foTrg = fsData.findResource("trg");
65         FileObject foPass = fsData.findResource("pass");
66
67         DataObject doTempl = jdl.findDataObject (fsData.findResource(CLASS_TEMPLATE), this);
68         DataFolder doTrg = (DataFolder) DataFolder.find(foTrg);
69    
70         FileObject foList[] = foSrc.getChildren();
71         for (int i = 0; i< foList.length; i++) {
72             FileObject foEntry = foList[i];
73             if (foEntry.getExt().equals("java")) {
74                 DataObject doSrcEntry = null;
75                 DataObject doTrgEntry = null;
76                 String JavaDoc name = foEntry.getName() + "Test";
77                 File fTmp = new File(appendSlash(fsData.getRootDirectory().getPath()) + appendSlash(foTrg.getNameExt()) + name + ".java");
78                 
79                 fTmp.delete();
80                 doSrcEntry = jdl.findDataObject (foEntry, this);
81                 doTrgEntry = doTempl.createFromTemplate(doTrg, name);
82                 
83                 ClassElement ceSrc = getClassElementFromDO(doSrcEntry);
84                 ClassElement ceTrg = getClassElementFromDO(doTrgEntry);
85                 TestCreator.createTestClass(ceSrc, ceTrg);
86                 SaveCookie sc = (SaveCookie) doTrgEntry.getCookie(SaveCookie.class);
87                 sc.save();
88
89                 assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), name + ".java"), new File(System.getProperty("xresults")));
90             }
91         }
92         
93         DataObject doSrcEntry = DataObject.find(fsData.findResource("src/TestClass001.java"));
94         DataObject doTrgEntry;
95         ClassElement ceSrc = getClassElementFromDO(doSrcEntry);
96         ClassElement ceTrg;
97
98         setGenerateFlags(js, false);
99         
100         new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestPublic.java").delete();
101         js.setMembersPublic(true); assertTrue(true == js.isMembersPublic());
102         TestCreator.initialize();
103         doTrgEntry = doTempl.createFromTemplate(doTrg, "TestPublic");
104         ceTrg = getClassElementFromDO(doTrgEntry);
105         TestCreator.createTestClass(ceSrc, ceTrg);
106         ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
107         js.setMembersPublic(false); assertTrue(false == js.isMembersPublic());
108         assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestPublic.java"), new File(System.getProperty("xresults")));
109
110         new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestProtected.java").delete();
111         js.setMembersProtected(true); assertTrue(true == js.isMembersProtected());
112         TestCreator.initialize();
113         doTrgEntry = doTempl.createFromTemplate(doTrg, "TestProtected");
114         ceTrg = getClassElementFromDO(doTrgEntry);
115         TestCreator.createTestClass(ceSrc, ceTrg);
116         ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
117         js.setMembersProtected(false); assertTrue(false == js.isMembersProtected());
118         assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestProtected.java"), new File(System.getProperty("xresults")));
119         
120         new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestPackage.java").delete();
121         js.setMembersPackage(true); assertTrue(true == js.isMembersPackage());
122         TestCreator.initialize();
123         doTrgEntry = doTempl.createFromTemplate(doTrg, "TestPackage");
124         ceTrg = getClassElementFromDO(doTrgEntry);
125         TestCreator.createTestClass(ceSrc, ceTrg);
126         ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
127         js.setMembersPackage(false); assertTrue(false == js.isMembersPackage());
128         assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestPackage.java"), new File(System.getProperty("xresults")));
129         
130         new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestBodyComments.java").delete();
131         js.setMembersPublic(true); assertTrue(true == js.isMembersPublic());
132         TestCreator.initialize();
133         js.setBodyComments(true); assertTrue(true == js.isBodyComments());
134         doTrgEntry = doTempl.createFromTemplate(doTrg, "TestBodyComments");
135         ceTrg = getClassElementFromDO(doTrgEntry);
136         TestCreator.createTestClass(ceSrc, ceTrg);
137         ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
138         js.setBodyComments(false); assertTrue(false == js.isBodyComments());
139         assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestBodyComments.java"), new File(System.getProperty("xresults")));
140
141         new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestBodyContent.java").delete();
142         js.setBodyContent(true); assertTrue(true == js.isBodyContent());
143         doTrgEntry = doTempl.createFromTemplate(doTrg, "TestBodyContent");
144         ceTrg = getClassElementFromDO(doTrgEntry);
145         TestCreator.createTestClass(ceSrc, ceTrg);
146         ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
147         js.setBodyContent(false); assertTrue(false == js.isBodyContent());
148         assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestBodyContent.java"), new File(System.getProperty("xresults")));
149
150         new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestJavaDoc.java").delete();
151         js.setJavaDoc(true); assertTrue(true == js.isJavaDoc());
152         doTrgEntry = doTempl.createFromTemplate(doTrg, "TestJavaDoc");
153         ceTrg = getClassElementFromDO(doTrgEntry);
154         TestCreator.createTestClass(ceSrc, ceTrg);
155         ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save();
156         js.setJavaDoc(false); assertTrue(false == js.isJavaDoc());
157         assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestJavaDoc.java"), new File(System.getProperty("xresults")));
158     }
159     
160     /** Test of createTestSuit method, of class org.netbeans.modules.junit.TestCreator. */
161     public void testCreateTestSuit() throws Exception JavaDoc {
162         System.out.println("testCreateTestSuit");
163
164         JavaDataLoader jdl = new JavaDataLoader();
165         LocalFileSystem fsData = new LocalFileSystem();
166         fsData.setRootDirectory(new File(appendSlash(m_pathData) + "CreateTestSuite"));
167         
168         FileObject foPass = fsData.findResource(TESTSUITE + ".java.pass");
169         DataObject doTempl = jdl.findDataObject (fsData.findResource(CLASS_TEMPLATE), this);
170
171         File fTmp = new File(appendSlash(fsData.getRootDirectory().getPath()) + TESTSUITE + ".java");
172
173         fTmp.delete();
174         DataObject doTrgEntry = doTempl.createFromTemplate((DataFolder)DataFolder.find(fsData.getRoot()), TESTSUITE);
175         ClassElement ceTrg = getClassElementFromDO(doTrgEntry);
176         
177         LinkedList lst = new LinkedList();
178         lst.add("a.b.c.TestClass1");
179         lst.add("a.b.c.TestClass2");
180         lst.add("TestClass3");
181         
182         TestCreator.createTestSuit(lst, "some.test.pckg", ceTrg);
183         SaveCookie sc = (SaveCookie) doTrgEntry.getCookie(SaveCookie.class);
184         sc.save();
185         
186         assertFile(getFile(doTrgEntry.getPrimaryFile()), getFile(foPass), new File(System.getProperty("xresults")));
187     }
188     
189     /** Test of initialize method, of class org.netbeans.modules.junit.TestCreator. */
190     public void testInitialize() {
191         System.out.println("testInitialize");
192         
193         // functionality of initialize method is verified in createTestClass method test
194
}
195     
196     /** Test of isClassTestable method, of class org.netbeans.modules.junit.TestCreator. */
197     public void testIsClassTestable() throws Exception JavaDoc {
198         System.out.println("testIsClassTestable");
199
200         FileObject foClass;
201         DataObject doClass;
202         ClassElement clazz;
203         JUnitSettings js = JUnitSettings.getDefault();
204         JavaDataLoader jdl = new JavaDataLoader();
205         LocalFileSystem fsData = new LocalFileSystem();
206         fsData.setRootDirectory(new File(appendSlash(m_pathData) + "IsClassTestable"));
207         
208         foClass = fsData.findResource("SimpleClass.java");
209         doClass = jdl.findDataObject (foClass, this);
210         clazz = getClassElementFromDO(doClass);
211         assertTrue(true == TestCreator.isClassTestable(clazz));
212
213         foClass = fsData.findResource("NonPublicClass.java");
214         doClass = jdl.findDataObject (foClass, this);
215         clazz = getClassElementFromDO(doClass);
216         assertTrue(false == TestCreator.isClassTestable(clazz));
217         
218         foClass = fsData.findResource("AbstractClass.java");
219         doClass = jdl.findDataObject (foClass, this);
220         clazz = getClassElementFromDO(doClass);
221         assertTrue(true == TestCreator.isClassTestable(clazz));
222         
223         foClass = fsData.findResource("SimpleInterface.java");
224         doClass = jdl.findDataObject (foClass, this);
225         clazz = getClassElementFromDO(doClass);
226         assertTrue(false == TestCreator.isClassTestable(clazz));
227
228         js.setGenerateExceptionClasses(false);
229         assertTrue(false == js.isGenerateExceptionClasses());
230         foClass = fsData.findResource("ExceptionClass.java");
231         doClass = jdl.findDataObject (foClass, this);
232         clazz = getClassElementFromDO(doClass);
233         assertTrue(false == TestCreator.isClassTestable(clazz));
234         
235         js.setGenerateExceptionClasses(true);
236         assertTrue(true == js.isGenerateExceptionClasses());
237         assertTrue(true == TestCreator.isClassTestable(clazz));
238     }
239
240     // protected members
241
protected static final String JavaDoc PROP_CLASSPATH = "java.class.path";
242     protected String JavaDoc m_pathData = null;
243     
244     protected void setUp() {
245         if (null == m_pathData)
246             m_pathData = System.getProperty("xdata");
247     }
248     
249     protected void tearDown() {
250     }
251     
252     protected class FileExtFilter implements FileFilter {
253         protected String JavaDoc extension = null;
254         public FileExtFilter(String JavaDoc extension) {
255             this.extension = extension;
256         }
257         public boolean accept(File pathname) {
258             String JavaDoc ext = null;
259             int i;
260             
261             if (-1 != (i = pathname.getPath().lastIndexOf('.')))
262                 ext = pathname.getPath().substring(i + 1);
263             
264             if (null == ext)
265                 return (null == extension);
266             
267             return extension.equals(ext);
268         }
269     }
270     
271     // private members
272
private static final String JavaDoc CLASS_TEMPLATE = "ClassTemplate.java";
273     private static final String JavaDoc TESTSUITE = "TestSuite";
274     
275     private String JavaDoc appendSlash(String JavaDoc path) {
276         if (null == path)
277             return new String JavaDoc();
278         
279         if (!path.endsWith("\\") && !path.endsWith("/"))
280             return path + "\\";
281         
282         return path;
283     }
284     
285    private ClassElement getClassElementFromDO(DataObject dO) {
286         SourceCookie sc;
287         SourceElement se;
288
289         sc = (SourceCookie) dO.getCookie(SourceCookie.class);
290         se = sc.getSource();
291         return se.getClass(Identifier.create(dO.getPrimaryFile().getName()));
292     }
293     
294     private File getFile(FileObject fo) throws FileStateInvalidException {
295         StringBuffer JavaDoc name = new StringBuffer JavaDoc();
296         LocalFileSystem lfs = (LocalFileSystem)fo.getFileSystem();
297         
298         name.append(lfs.getRootDirectory().getPath());
299         if (0 != name.length())
300             name.append("/");
301         name.append(fo.getPackageNameExt('/', '.'));
302         
303         return new File(name.toString());
304     }
305     
306     private void setGenerateFlags(JUnitSettings js, boolean flag) {
307         js.setMembersPublic(flag); assertTrue(flag == js.isMembersPublic());
308         js.setMembersProtected(flag); assertTrue(flag == js.isMembersProtected());
309         js.setMembersPackage(flag); assertTrue(flag == js.isMembersPackage());
310         js.setBodyComments(flag); assertTrue(flag == js.isBodyComments());
311         js.setBodyContent(flag); assertTrue(flag == js.isBodyContent());
312         js.setJavaDoc(flag); assertTrue(flag == js.isJavaDoc());
313     }
314     
315     public void markRecognized(FileObject fo) {
316     }
317     
318 }
319
Popular Tags