KickJava   Java API By Example, From Geeks To Geeks.

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


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.nodes.*;
24 import org.openide.util.HelpCtx;
25 import org.openide.util.NbBundle;
26 import org.openide.util.actions.CookieAction;
27 import org.openide.loaders.*;
28 import org.openide.src.*;
29 import org.openide.filesystems.*;
30 import org.openide.cookies.*;
31 import java.lang.reflect.*;
32 import java.util.*;
33 import java.io.*;
34 import junit.framework.*;
35 import org.netbeans.junit.*;
36
37 public class CreateTestActionTest extends NbTestCase {
38
39     public CreateTestActionTest(java.lang.String JavaDoc testName) {
40         super(testName);
41     }
42     
43     public static void main(java.lang.String JavaDoc[] args) {
44         junit.textui.TestRunner.run(suite());
45     }
46     
47     public static Test suite() {
48         TestSuite suite = new TestSuite(CreateTestActionTest.class);
49         
50         return suite;
51     }
52     
53     /** Test of getName method, of class org.netbeans.modules.junit.CreateTestAction. */
54     public void testGetName() {
55         System.out.println("testGetName");
56         String JavaDoc name = TO.getName();
57         assertTrue(null != name);
58     }
59     
60     /** Test of getHelpCtx method, of class org.netbeans.modules.junit.CreateTestAction. */
61     public void testGetHelpCtx() {
62         System.out.println("testGetHelpCtx");
63         HelpCtx hc = TO.getHelpCtx();
64         assertTrue(null != hc);
65     }
66     
67     /** Test of cookieClasses method, of class org.netbeans.modules.junit.CreateTestAction. */
68     public void testCookieClasses() {
69         System.out.println("testCookieClasses");
70         Class JavaDoc[] c = TO.cookieClasses();
71         assertTrue(null != c);
72     }
73     
74     /** Test of iconResource method, of class org.netbeans.modules.junit.CreateTestAction. */
75     public void testIconResource() {
76         System.out.println("testIconResource");
77         String JavaDoc icon = TO.iconResource();
78         assertTrue(null != icon);
79     }
80     
81     /** Test of mode method, of class org.netbeans.modules.junit.CreateTestAction. */
82     public void testMode() {
83         System.out.println("testMode");
84         TO.mode();
85     }
86     
87     /** Test of performAction method, of class org.netbeans.modules.junit.CreateTestAction. */
88     public void testPerformAction() throws Exception JavaDoc {
89         System.out.println("testPerformAction");
90         
91         // check if called from IDE
92
if (null == System.getProperty("netbeans.home")) {
93             fail("This tast can run within the IDE only.");
94         }
95         
96         LocalFileSystem fsPass = new LocalFileSystem();
97         LocalFileSystem fsTest = new LocalFileSystem();
98         LocalFileSystem fsSrc = new LocalFileSystem();
99         fsPass.setRootDirectory(new File(appendSlash(m_pathData) + "CreateTestAction/pass"));
100         fsTest.setRootDirectory(new File(appendSlash(m_pathData) + "CreateTestAction/test"));
101         fsSrc.setRootDirectory(new File(appendSlash(m_pathData) + "CreateTestAction/src"));
102         TopManager.getDefault().getRepository().addFileSystem(fsPass);
103         TopManager.getDefault().getRepository().addFileSystem(fsTest);
104         TopManager.getDefault().getRepository().addFileSystem(fsSrc);
105
106         JUnitSettings js = JUnitSettings.getDefault();
107         setGenerateFlags(js, true);
108         js.setFileSystem(fsTest.getSystemName());
109         
110         assertTrue("Can't clean up the test directory.", delete(fsTest.getRoot(), false));
111         TO.performAction(new Node[] { DataObject.find(fsTest.getRoot()).getNodeDelegate() });
112         
113         assertDirectories(fsTest.getRootDirectory(), fsPass.getRootDirectory());
114     }
115     
116     /* protected members */
117     protected CreateTestAction TO = null;
118     protected String JavaDoc m_pathData = null;
119     
120     protected void setUp() {
121         if (null == TO)
122             TO = (CreateTestAction)CreateTestAction.findObject(CreateTestAction.class, true);
123
124         if (null == m_pathData)
125             m_pathData = System.getProperty("xdata");
126     }
127
128     protected void tearDown() {
129     }
130
131     // private members
132
private String JavaDoc appendSlash(String JavaDoc path) {
133         if (null == path)
134             return new String JavaDoc();
135         
136         if (!path.endsWith("\\") && !path.endsWith("/"))
137             return path + "\\";
138         
139         return path;
140     }
141     
142     private final void assertDirectories(File test, File pass) {
143         File fKids[];
144         
145         // assert all childern
146
fKids = test.listFiles();
147         for(int i = 0; i < fKids.length; i++) {
148             assertDirectories(fKids[i], new File(pass, fKids[i].getName()));
149         }
150         
151         if (!test.isDirectory()) {
152             assertFile(test, new File(pass, test.getName()), new File(System.getProperty("xresults")));
153         }
154         else {
155             // find missings
156
File fPassKids[] = pass.listFiles();
157             for(int i = 0; i < fPassKids.length; i++) {
158                 if (!new File(test, fPassKids[i].getName()).exists())
159                     fail("The file or directory '" + fPassKids[i].getAbsolutePath() + "' is missing.");
160             }
161         }
162     }
163
164     private final boolean delete(FileObject fo, boolean deleteCurrent) {
165         FileObject foKids[];
166         FileLock lock = null;
167         
168         // delete all childern
169
foKids = fo.getChildren();
170         for(int i = 0; i < foKids.length; i++) {
171             if (!delete(foKids[i], true))
172                 return false;
173         }
174         
175         if (!deleteCurrent)
176             return true;
177         
178         try {
179             lock = fo.lock();
180             fo.delete(lock);
181         }
182         catch (IOException e) {
183             return false;
184         }
185         finally {
186             if (null != lock)
187                 lock.releaseLock();
188         }
189         return true;
190     }
191     
192     private void setGenerateFlags(JUnitSettings js, boolean flag) {
193         js.setMembersPublic(flag); assertTrue(flag == js.isMembersPublic());
194         js.setMembersProtected(flag); assertTrue(flag == js.isMembersProtected());
195         js.setMembersPackage(flag); assertTrue(flag == js.isMembersPackage());
196         js.setBodyComments(flag); assertTrue(flag == js.isBodyComments());
197         js.setBodyContent(flag); assertTrue(flag == js.isBodyContent());
198         js.setJavaDoc(flag); assertTrue(flag == js.isJavaDoc());
199     }
200 }
201
Popular Tags