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.nodes.*;
046: import org.openide.util.HelpCtx;
047: import org.openide.util.NbBundle;
048: import org.openide.util.actions.CookieAction;
049: import org.openide.loaders.*;
050: import org.openide.src.*;
051: import org.openide.filesystems.*;
052: import org.openide.cookies.*;
053: import java.lang.reflect.*;
054: import java.util.*;
055: import java.io.*;
056: import junit.framework.*;
057: import org.netbeans.junit.*;
058:
059: public class CreateTestActionTest extends NbTestCase {
060:
061: public CreateTestActionTest(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: TestSuite suite = new TestSuite(CreateTestActionTest.class);
071:
072: return suite;
073: }
074:
075: /** Test of getName method, of class org.netbeans.modules.junit.CreateTestAction. */
076: public void testGetName() {
077: System.out.println("testGetName");
078: String name = TO.getName();
079: assertTrue(null != name);
080: }
081:
082: /** Test of getHelpCtx method, of class org.netbeans.modules.junit.CreateTestAction. */
083: public void testGetHelpCtx() {
084: System.out.println("testGetHelpCtx");
085: HelpCtx hc = TO.getHelpCtx();
086: assertTrue(null != hc);
087: }
088:
089: /** Test of cookieClasses method, of class org.netbeans.modules.junit.CreateTestAction. */
090: public void testCookieClasses() {
091: System.out.println("testCookieClasses");
092: Class[] c = TO.cookieClasses();
093: assertTrue(null != c);
094: }
095:
096: /** Test of iconResource method, of class org.netbeans.modules.junit.CreateTestAction. */
097: public void testIconResource() {
098: System.out.println("testIconResource");
099: String icon = TO.iconResource();
100: assertTrue(null != icon);
101: }
102:
103: /** Test of mode method, of class org.netbeans.modules.junit.CreateTestAction. */
104: public void testMode() {
105: System.out.println("testMode");
106: TO.mode();
107: }
108:
109: /** Test of performAction method, of class org.netbeans.modules.junit.CreateTestAction. */
110: public void testPerformAction() throws Exception {
111: System.out.println("testPerformAction");
112:
113: // check if called from IDE
114: if (null == System.getProperty("netbeans.home")) {
115: fail("This tast can run within the IDE only.");
116: }
117:
118: LocalFileSystem fsPass = new LocalFileSystem();
119: LocalFileSystem fsTest = new LocalFileSystem();
120: LocalFileSystem fsSrc = new LocalFileSystem();
121: fsPass.setRootDirectory(new File(appendSlash(m_pathData)
122: + "CreateTestAction/pass"));
123: fsTest.setRootDirectory(new File(appendSlash(m_pathData)
124: + "CreateTestAction/test"));
125: fsSrc.setRootDirectory(new File(appendSlash(m_pathData)
126: + "CreateTestAction/src"));
127: TopManager.getDefault().getRepository().addFileSystem(fsPass);
128: TopManager.getDefault().getRepository().addFileSystem(fsTest);
129: TopManager.getDefault().getRepository().addFileSystem(fsSrc);
130:
131: JUnitSettings js = JUnitSettings.getDefault();
132: setGenerateFlags(js, true);
133: js.setFileSystem(fsTest.getSystemName());
134:
135: assertTrue("Can't clean up the test directory.", delete(fsTest
136: .getRoot(), false));
137: TO.performAction(new Node[] { DataObject.find(fsTest.getRoot())
138: .getNodeDelegate() });
139:
140: assertDirectories(fsTest.getRootDirectory(), fsPass
141: .getRootDirectory());
142: }
143:
144: /* protected members */
145: protected CreateTestAction TO = null;
146: protected String m_pathData = null;
147:
148: protected void setUp() {
149: if (null == TO)
150: TO = (CreateTestAction) CreateTestAction.findObject(
151: CreateTestAction.class, true);
152:
153: if (null == m_pathData)
154: m_pathData = System.getProperty("xdata");
155: }
156:
157: protected void tearDown() {
158: }
159:
160: // private members
161: private String appendSlash(String path) {
162: if (null == path)
163: return new String();
164:
165: if (!path.endsWith("\\") && !path.endsWith("/"))
166: return path + "\\";
167:
168: return path;
169: }
170:
171: private final void assertDirectories(File test, File pass) {
172: File fKids[];
173:
174: // assert all childern
175: fKids = test.listFiles();
176: for (int i = 0; i < fKids.length; i++) {
177: assertDirectories(fKids[i], new File(pass, fKids[i]
178: .getName()));
179: }
180:
181: if (!test.isDirectory()) {
182: assertFile(test, new File(pass, test.getName()), new File(
183: System.getProperty("xresults")));
184: } else {
185: // find missings
186: File fPassKids[] = pass.listFiles();
187: for (int i = 0; i < fPassKids.length; i++) {
188: if (!new File(test, fPassKids[i].getName()).exists())
189: fail("The file or directory '"
190: + fPassKids[i].getAbsolutePath()
191: + "' is missing.");
192: }
193: }
194: }
195:
196: private final boolean delete(FileObject fo, boolean deleteCurrent) {
197: FileObject foKids[];
198: FileLock lock = null;
199:
200: // delete all childern
201: foKids = fo.getChildren();
202: for (int i = 0; i < foKids.length; i++) {
203: if (!delete(foKids[i], true))
204: return false;
205: }
206:
207: if (!deleteCurrent)
208: return true;
209:
210: try {
211: lock = fo.lock();
212: fo.delete(lock);
213: } catch (IOException e) {
214: return false;
215: } finally {
216: if (null != lock)
217: lock.releaseLock();
218: }
219: return true;
220: }
221:
222: private void setGenerateFlags(JUnitSettings js, boolean flag) {
223: js.setMembersPublic(flag);
224: assertTrue(flag == js.isMembersPublic());
225: js.setMembersProtected(flag);
226: assertTrue(flag == js.isMembersProtected());
227: js.setMembersPackage(flag);
228: assertTrue(flag == js.isMembersPackage());
229: js.setBodyComments(flag);
230: assertTrue(flag == js.isBodyComments());
231: js.setBodyContent(flag);
232: assertTrue(flag == js.isBodyContent());
233: js.setJavaDoc(flag);
234: assertTrue(flag == js.isJavaDoc());
235: }
236: }
|