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.apisupport.project;
043:
044: import java.io.File;
045: import java.util.Collections;
046: import org.netbeans.api.project.ProjectManager;
047: import org.netbeans.junit.NbTestCase;
048: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
049: import org.netbeans.modules.apisupport.project.suite.SuiteProjectGenerator;
050: import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
051: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
052: import org.netbeans.spi.project.support.ant.EditableProperties;
053: import org.netbeans.spi.project.support.ant.PropertyUtils;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.util.SharedClassObject;
057:
058: /**
059: * Check that missing or invalid *.properties files do not badly break projects.
060: * More or less corresponds to issue #66404 and others.
061: * @author Jesse Glick
062: */
063: public final class BrokenPlatformReferenceTest extends NbTestCase {
064:
065: public BrokenPlatformReferenceTest(String name) {
066: super (name);
067: }
068:
069: /** a fake but valid-looking install dir; the default NB platform */
070: private File install;
071: /** an alternate valid install dir */
072: private File install2;
073: /** the user dir */
074: private File user;
075:
076: protected @Override
077: void setUp() throws Exception {
078: super .setUp();
079: clearWorkDir();
080: NbPlatform.reset();
081: user = new File(getWorkDir(), "user");
082: user.mkdirs();
083: System.setProperty("netbeans.user", user.getAbsolutePath());
084: install = new File(getWorkDir(), "install");
085: TestBase.makePlatform(install);
086: // Now set up build.properties accordingly:
087: InstalledFileLocatorImpl.registerDestDir(install);
088: SharedClassObject.findObject(Install.class, true).restored();
089: assertEquals("set up run correctly", install.getAbsolutePath(),
090: PropertyUtils.getGlobalProperties().getProperty(
091: "nbplatform.default.netbeans.dest.dir"));
092: install2 = new File(getWorkDir(), "install2");
093: TestBase.makePlatform(install2);
094: NbPlatform.addPlatform("install2", install2, "install2");
095: }
096:
097: /** Make sure everything is working as expected when there are no breakages. */
098: public void testEverythingNormal() throws Exception {
099: // Try making a standalone module w/ default platform, confirm loaded OK.
100: File d = new File(getWorkDir(), "standalone");
101: NbModuleProjectGenerator.createStandAloneModule(d, "x", "X",
102: null, null, NbPlatform.PLATFORM_ID_DEFAULT);
103: NbModuleProject p = (NbModuleProject) ProjectManager
104: .getDefault().findProject(FileUtil.toFileObject(d));
105: NbPlatform pl = p.getPlatform(false);
106: assertNotNull(pl);
107: assertEquals(install, pl.getDestDir());
108: assertEquals(pl, p.getPlatform(true));
109: // Same but w/ a non-default platform.
110: d = new File(getWorkDir(), "standalone2");
111: NbModuleProjectGenerator.createStandAloneModule(d, "x", "X",
112: null, null, "install2");
113: p = (NbModuleProject) ProjectManager.getDefault().findProject(
114: FileUtil.toFileObject(d));
115: pl = p.getPlatform(false);
116: assertNotNull(pl);
117: assertEquals(install2, pl.getDestDir());
118: // Same for suites.
119: File sd = new File(getWorkDir(), "suite");
120: SuiteProjectGenerator.createSuiteProject(sd,
121: NbPlatform.PLATFORM_ID_DEFAULT);
122: d = new File(getWorkDir(), "suitecomp");
123: NbModuleProjectGenerator.createSuiteComponentModule(d, "x",
124: "X", null, null, sd);
125: SuiteProject s = (SuiteProject) ProjectManager.getDefault()
126: .findProject(FileUtil.toFileObject(sd));
127: pl = s.getPlatform(false);
128: assertNotNull(pl);
129: assertEquals(install, pl.getDestDir());
130: assertEquals(pl, s.getPlatform(true));
131: p = (NbModuleProject) ProjectManager.getDefault().findProject(
132: FileUtil.toFileObject(d));
133: assertEquals(pl, p.getPlatform(false));
134: // And again w/ a non-default platform.
135: sd = new File(getWorkDir(), "suite2");
136: SuiteProjectGenerator.createSuiteProject(sd, "install2");
137: d = new File(getWorkDir(), "suitecomp2");
138: NbModuleProjectGenerator.createSuiteComponentModule(d, "x",
139: "X", null, null, sd);
140: s = (SuiteProject) ProjectManager.getDefault().findProject(
141: FileUtil.toFileObject(sd));
142: pl = s.getPlatform(false);
143: assertNotNull(pl);
144: assertEquals(install2, pl.getDestDir());
145: p = (NbModuleProject) ProjectManager.getDefault().findProject(
146: FileUtil.toFileObject(d));
147: assertEquals(pl, p.getPlatform(false));
148: }
149:
150: /** Test that use of default platform is OK even if platform-private.properties is initially missing; must be created. */
151: public void testMissingPlatformPrivatePropertiesDefaultPlatform()
152: throws Exception {
153: // Try making a standalone module w/ default platform.
154: File d = new File(getWorkDir(), "standalone");
155: NbModuleProjectGenerator.createStandAloneModule(d, "x", "X",
156: null, null, NbPlatform.PLATFORM_ID_DEFAULT);
157: NbModuleProject p = (NbModuleProject) ProjectManager
158: .getDefault().findProject(FileUtil.toFileObject(d));
159: p.open();
160: assertEquals(
161: Collections.singletonMap("user.properties.file",
162: new File(user, "build.properties")
163: .getAbsolutePath()),
164: Util
165: .loadProperties(p
166: .getProjectDirectory()
167: .getFileObject(
168: "nbproject/private/platform-private.properties")));
169: // Same for suite.
170: File sd = new File(getWorkDir(), "suite");
171: SuiteProjectGenerator.createSuiteProject(sd,
172: NbPlatform.PLATFORM_ID_DEFAULT);
173: SuiteProject s = (SuiteProject) ProjectManager.getDefault()
174: .findProject(FileUtil.toFileObject(sd));
175: s.open();
176: assertEquals(
177: Collections.singletonMap("user.properties.file",
178: new File(user, "build.properties")
179: .getAbsolutePath()),
180: Util
181: .loadProperties(s
182: .getProjectDirectory()
183: .getFileObject(
184: "nbproject/private/platform-private.properties")));
185: }
186:
187: /** Test that use of default platform is still fine even if platform-private.properties is initially incorrect; must be corrected. */
188: public void testIncorrectPlatformPrivatePropertiesDefaultPlatform()
189: throws Exception {
190: // Try making a standalone module w/ default platform.
191: File d = new File(getWorkDir(), "standalone");
192: NbModuleProjectGenerator.createStandAloneModule(d, "x", "X",
193: null, null, NbPlatform.PLATFORM_ID_DEFAULT);
194: FileObject props = FileUtil.createData(
195: FileUtil.toFileObject(d),
196: "nbproject/private/platform-private.properties");
197: Util.storeProperties(props, new EditableProperties(Collections
198: .singletonMap("user.properties.file", "bogus")));
199: NbModuleProject p = (NbModuleProject) ProjectManager
200: .getDefault().findProject(FileUtil.toFileObject(d));
201: NbPlatform pl = p.getPlatform(true); // with fallback=false, who knows what it will be
202: assertNotNull(pl);
203: assertEquals(install, pl.getDestDir());
204: p.open();
205: assertEquals(Collections.singletonMap("user.properties.file",
206: new File(user, "build.properties").getAbsolutePath()),
207: Util.loadProperties(props));
208: assertEquals(pl, p.getPlatform(true));
209: assertEquals(pl, p.getPlatform(false)); // now should be corrected even w/o fallback
210: // Same for suite. Check a component module too.
211: File sd = new File(getWorkDir(), "suite");
212: SuiteProjectGenerator.createSuiteProject(sd,
213: NbPlatform.PLATFORM_ID_DEFAULT);
214: props = FileUtil.createData(FileUtil.toFileObject(sd),
215: "nbproject/private/platform-private.properties");
216: Util.storeProperties(props, new EditableProperties(Collections
217: .singletonMap("user.properties.file", "bogus")));
218: d = new File(getWorkDir(), "suitecomp");
219: NbModuleProjectGenerator.createSuiteComponentModule(d, "x",
220: "X", null, null, sd);
221: SuiteProject s = (SuiteProject) ProjectManager.getDefault()
222: .findProject(FileUtil.toFileObject(sd));
223: p = (NbModuleProject) ProjectManager.getDefault().findProject(
224: FileUtil.toFileObject(d));
225: pl = s.getPlatform(true);
226: assertNotNull(pl);
227: assertEquals(install, pl.getDestDir());
228: assertEquals(pl, p.getPlatform(true));
229: s.open();
230: p.open(); // just in case
231: assertEquals(Collections.singletonMap("user.properties.file",
232: new File(user, "build.properties").getAbsolutePath()),
233: Util.loadProperties(props));
234: assertEquals(pl, s.getPlatform(true));
235: assertEquals(pl, s.getPlatform(false));
236: assertEquals(pl, p.getPlatform(true));
237: assertEquals(pl, p.getPlatform(false));
238: }
239:
240: public void testUsableModuleListForBrokenPlatform()
241: throws Exception {
242: File sd = new File(getWorkDir(), "suite");
243: SuiteProjectGenerator.createSuiteProject(sd,
244: NbPlatform.PLATFORM_ID_DEFAULT);
245: File d = new File(getWorkDir(), "suitecomp");
246: NbModuleProjectGenerator.createSuiteComponentModule(d, "x",
247: "X", null, null, sd);
248: TestBase.delete(sd);
249: NbModuleProject p = (NbModuleProject) ProjectManager
250: .getDefault().findProject(FileUtil.toFileObject(d));
251: ModuleEntry e = p.getModuleList().getEntry("core");
252: assertNotNull(
253: "#67148: can find core.jar from default platform in "
254: + p, e);
255: assertEquals("correct JAR path", new File(new File(new File(
256: install, "platform"), "core"), "core.jar"), e
257: .getJarLocation());
258: p.open(); // check for errors
259: }
260:
261: // XXX to test, for suite projects, suite component module projects, and standalone projects:
262: // - return default platform if ${netbeans.dest.dir} undefined in any way or not pointing to valid platform [partly tested]
263: // - OpenProjectHook fixes, or creates, platform-private.properties to point to current build.properties [in progress; need to test non-default platforms valid in new b.props]
264: // - in OPH, platform.properties is fixed to use default if no value for nbplatform.active (and netbeans.dest.dir not independently set!) or points to invalid platform
265: // - all problems are notified to user (maybe move ModuleProperties.reportLostPlatform, and change MP.runFromTests)
266:
267: }
|