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: /*
043: * ProjectConfigurationsHelperTest.java
044: * JUnit based test
045: *
046: * Created on 08 February 2006, 18:21
047: */
048: package org.netbeans.spi.project.configurations.support;
049:
050: import java.io.File;
051: import java.io.IOException;
052: import java.util.Collection;
053: import junit.framework.*;
054: import java.util.Map;
055: import java.util.Set;
056: import java.util.logging.Handler;
057: import java.util.logging.LogRecord;
058: import java.util.logging.Logger;
059: import org.netbeans.api.project.Project;
060: import org.netbeans.api.project.ProjectManager;
061: import org.netbeans.spi.project.ProjectConfiguration;
062: import org.netbeans.junit.NbTestCase;
063: import org.netbeans.modules.masterfs.MasterFileSystem;
064: import org.netbeans.modules.mobility.project.J2MEProjectGenerator;
065: import org.netbeans.modules.mobility.project.ProjectConfigurationsHelper;
066: import org.netbeans.modules.mobility.project.TestUtil;
067: import org.netbeans.spi.project.support.ant.AntProjectHelper;
068: import org.openide.filesystems.FileUtil;
069: import org.openide.util.Exceptions;
070:
071: /**
072: *
073: * @author lukas
074: */
075: public class ProjectConfigurationsHelperTest extends NbTestCase {
076:
077: static ProjectConfigurationsHelper instance = null;
078: static AntProjectHelper aph = null;
079: static final Object syncObj = new Object();
080: static {
081: TestUtil.setLookup(new Object[] {},
082: ProjectConfigurationsHelperTest.class.getClassLoader());
083: assertNotNull(MasterFileSystem.settingsFactory(null));
084:
085: Logger.getLogger("org.openide.util.RequestProcessor")
086: .addHandler(new Handler() {
087: public void publish(LogRecord record) {
088: String s = record.getMessage();
089: if (s == null)
090: return;
091: if (s.startsWith("Work finished")
092: && s.indexOf("J2MEProject$6") != -1
093: && s.indexOf("RequestProcessor") != -1) {
094: synchronized (syncObj) {
095: syncObj.notify();
096: }
097: }
098: }
099:
100: public void flush() {
101: }
102:
103: public void close() throws SecurityException {
104: }
105: });
106: }
107:
108: public ProjectConfigurationsHelperTest(String testName) {
109: super (testName);
110: TestUtil.setEnv();
111: }
112:
113: void waitFinished() {
114: while (true) {
115: try {
116: syncObj.wait();
117: break;
118: } catch (InterruptedException ex) {
119: Exceptions.printStackTrace(ex);
120: }
121: }
122: }
123:
124: protected void setUp() throws Exception {
125: clearWorkDir();
126: File workDir = getWorkDir();
127: File proj = new File(workDir, "testProject");
128:
129: System.setProperty("netbeans.user", "test/tiredTester");
130:
131: synchronized (syncObj) {
132: aph = J2MEProjectGenerator.createNewProject(proj,
133: "testProject", null, null, null);
134: waitFinished();
135: }
136: Project p = ProjectManager.getDefault().findProject(
137: FileUtil.toFileObject(proj));
138: assertNotNull(p);
139: instance = p.getLookup().lookup(
140: ProjectConfigurationsHelper.class);
141: assertNotNull(instance);
142: }
143:
144: protected void tearDown() throws Exception {
145: }
146:
147: public static Test suite() {
148: TestSuite suite = new TestSuite(
149: ProjectConfigurationsHelperTest.class);
150:
151: return suite;
152: }
153:
154: /**
155: * Test of getDefaultConfiguration method, of class org.netbeans.spi.project.configurations.support.ProjectConfigurationsHelper.
156: */
157: public void testGetDefaultConfiguration() {
158: System.out.println("getDefaultConfiguration");
159:
160: ProjectConfiguration result = instance
161: .getDefaultConfiguration();
162: assertEquals(result.getDisplayName(), "DefaultConfiguration");
163: }
164:
165: /**
166: * Test of addConfiguration method, of class org.netbeans.spi.project.configurations.support.ProjectConfigurationsHelper.
167: */
168: public void testAddConfiguration() {
169: System.out.println("addConfiguration");
170:
171: boolean result = instance.addConfiguration("MyConfig");
172: assertTrue(result);
173: }
174:
175: /**
176: * Test of getActiveAbilities method, of class org.netbeans.spi.project.configurations.support.ProjectConfigurationsHelper.
177: */
178: public void testGetActiveAbilities() {
179: System.out.println("getActiveAbilities");
180:
181: Map result = instance.getActiveAbilities();
182: assertTrue(result.size() == 1);
183: assertEquals(result.get("DebugLevel"), "debug");
184: }
185:
186: /**
187: * Test of getAllIdentifiers method, of class org.netbeans.spi.project.configurations.support.ProjectConfigurationsHelper.
188: */
189: public void testGetAllIdentifiers() {
190: System.out.println("getAllIdentifiers");
191:
192: boolean includeConfigNames = true;
193:
194: instance.addConfiguration("MyCFG");
195:
196: Set result = instance.getAllIdentifiers(includeConfigNames);
197: assertTrue(result.size() == 2);
198: assertTrue(result.remove("MyCFG"));
199: assertTrue(result.remove("DefaultConfiguration"));
200: }
201:
202: /**
203: * Test of removeConfiguration method, of class org.netbeans.spi.project.configurations.support.ProjectConfigurationsHelper.
204: */
205: public void testRemoveConfiguration() {
206: System.out.println("removeConfiguration");
207:
208: assertTrue(instance.addConfiguration("MyCFG"));
209: Collection<ProjectConfiguration> result = instance
210: .getConfigurations();
211: assertTrue(result.size() == 2);
212: assertTrue(instance
213: .removeConfiguration(new ProjectConfiguration() {
214: public String getDisplayName() {
215: return "MyCFG";
216: }
217: }));
218: result = instance.getConfigurations();
219: assertTrue(result.size() == 1);
220: assertEquals(result.iterator().next().getDisplayName(),
221: "DefaultConfiguration");
222: }
223:
224: /**
225: * Test of getConfigurationByName method, of class org.netbeans.spi.project.configurations.support.ProjectConfigurationsHelper.
226: */
227: public void testGetConfigurationByName() {
228: System.out.println("getConfigurationByName");
229:
230: ProjectConfiguration result = instance
231: .getConfigurationByName("MyCFG");
232: assertNull(result);
233: assertTrue(instance.addConfiguration("MyCFG"));
234: result = instance.getConfigurationByName("MyCFG");
235: assertNotNull(result);
236: assertEquals(result.getDisplayName(), "MyCFG");
237: }
238:
239: /**
240: * Test of setActiveConfiguration method, of class org.netbeans.spi.project.configurations.support.ProjectConfigurationsHelper.
241: */
242: public void testSetActiveConfiguration() {
243: System.out.println("setActiveConfiguration");
244: assertTrue(instance.addConfiguration("MyCFG_1"));
245: assertTrue(instance.addConfiguration("MyCFG_2"));
246: assertTrue(instance.addConfiguration("MyCFG_3"));
247: try {
248: instance.setActiveConfiguration(new ProjectConfiguration() {
249: public String getDisplayName() {
250: return "MyCFG_2";
251: }
252: });
253: } catch (IllegalArgumentException ex) {
254: ex.printStackTrace();
255: } catch (IOException ex) {
256: ex.printStackTrace();
257: }
258:
259: assertEquals(
260: instance.getActiveConfiguration().getDisplayName(),
261: "MyCFG_2");
262: }
263: }
|