KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > BrokenPlatformReferenceTest


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.apisupport.project;
21
22 import java.io.File JavaDoc;
23 import java.util.Collections JavaDoc;
24 import org.netbeans.api.project.ProjectManager;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
27 import org.netbeans.modules.apisupport.project.suite.SuiteProjectGenerator;
28 import org.netbeans.modules.apisupport.project.suite.SuiteProjectTest;
29 import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
30 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
31 import org.netbeans.spi.project.support.ant.EditableProperties;
32 import org.netbeans.spi.project.support.ant.PropertyUtils;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.util.SharedClassObject;
36
37 /**
38  * Check that missing or invalid *.properties files do not badly break projects.
39  * More or less corresponds to issue #66404 and others.
40  * @author Jesse Glick
41  */

42 public final class BrokenPlatformReferenceTest extends NbTestCase {
43     
44     public BrokenPlatformReferenceTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     /** a fake but valid-looking install dir; the default NB platform */
49     private File JavaDoc install;
50     /** an alternate valid install dir */
51     private File JavaDoc install2;
52     /** the user dir */
53     private File JavaDoc user;
54     
55     protected void setUp() throws Exception JavaDoc {
56         super.setUp();
57         clearWorkDir();
58         NbPlatform.reset();
59         user = new File JavaDoc(getWorkDir(), "user");
60         user.mkdirs();
61         System.setProperty("netbeans.user", user.getAbsolutePath());
62         install = new File JavaDoc(getWorkDir(), "install");
63         TestBase.makePlatform(install);
64         // Now set up build.properties accordingly:
65
InstalledFileLocatorImpl.registerDestDir(install);
66         ((Install) SharedClassObject.findObject(Install.class, true)).restored();
67         assertEquals("set up run correctly", install.getAbsolutePath(), PropertyUtils.getGlobalProperties().getProperty("nbplatform.default.netbeans.dest.dir"));
68         install2 = new File JavaDoc(getWorkDir(), "install2");
69         TestBase.makePlatform(install2);
70         NbPlatform.addPlatform("install2", install2, "install2");
71     }
72     
73     /** Make sure everything is working as expected when there are no breakages. */
74     public void testEverythingNormal() throws Exception JavaDoc {
75         // Try making a standalone module w/ default platform, confirm loaded OK.
76
File JavaDoc d = new File JavaDoc(getWorkDir(), "standalone");
77         NbModuleProjectGenerator.createStandAloneModule(d, "x", "X", null, null, NbPlatform.PLATFORM_ID_DEFAULT);
78         NbModuleProject p = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(d));
79         NbPlatform pl = p.getPlatform(false);
80         assertNotNull(pl);
81         assertEquals(install, pl.getDestDir());
82         assertEquals(pl, p.getPlatform(true));
83         // Same but w/ a non-default platform.
84
d = new File JavaDoc(getWorkDir(), "standalone2");
85         NbModuleProjectGenerator.createStandAloneModule(d, "x", "X", null, null, "install2");
86         p = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(d));
87         pl = p.getPlatform(false);
88         assertNotNull(pl);
89         assertEquals(install2, pl.getDestDir());
90         // Same for suites.
91
File JavaDoc sd = new File JavaDoc(getWorkDir(), "suite");
92         SuiteProjectGenerator.createSuiteProject(sd, NbPlatform.PLATFORM_ID_DEFAULT);
93         d = new File JavaDoc(getWorkDir(), "suitecomp");
94         NbModuleProjectGenerator.createSuiteComponentModule(d, "x", "X", null, null, sd);
95         SuiteProject s = (SuiteProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(sd));
96         pl = s.getPlatform(false);
97         assertNotNull(pl);
98         assertEquals(install, pl.getDestDir());
99         assertEquals(pl, s.getPlatform(true));
100         p = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(d));
101         assertEquals(pl, p.getPlatform(false));
102         // And again w/ a non-default platform.
103
sd = new File JavaDoc(getWorkDir(), "suite2");
104         SuiteProjectGenerator.createSuiteProject(sd, "install2");
105         d = new File JavaDoc(getWorkDir(), "suitecomp2");
106         NbModuleProjectGenerator.createSuiteComponentModule(d, "x", "X", null, null, sd);
107         s = (SuiteProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(sd));
108         pl = s.getPlatform(false);
109         assertNotNull(pl);
110         assertEquals(install2, pl.getDestDir());
111         p = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(d));
112         assertEquals(pl, p.getPlatform(false));
113     }
114     
115     /** Test that use of default platform is OK even if platform-private.properties is initially missing; must be created. */
116     public void testMissingPlatformPrivatePropertiesDefaultPlatform() throws Exception JavaDoc {
117         // Try making a standalone module w/ default platform.
118
File JavaDoc d = new File JavaDoc(getWorkDir(), "standalone");
119         NbModuleProjectGenerator.createStandAloneModule(d, "x", "X", null, null, NbPlatform.PLATFORM_ID_DEFAULT);
120         NbModuleProject p = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(d));
121         p.open();
122         assertEquals(Collections.singletonMap("user.properties.file", new File JavaDoc(user, "build.properties").getAbsolutePath()),
123                 Util.loadProperties(p.getProjectDirectory().getFileObject("nbproject/private/platform-private.properties")));
124         // Same for suite.
125
File JavaDoc sd = new File JavaDoc(getWorkDir(), "suite");
126         SuiteProjectGenerator.createSuiteProject(sd, NbPlatform.PLATFORM_ID_DEFAULT);
127         SuiteProject s = (SuiteProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(sd));
128         SuiteProjectTest.openSuite(s);
129         assertEquals(Collections.singletonMap("user.properties.file", new File JavaDoc(user, "build.properties").getAbsolutePath()),
130                 Util.loadProperties(s.getProjectDirectory().getFileObject("nbproject/private/platform-private.properties")));
131     }
132     
133     /** Test that use of default platform is still fine even if platform-private.properties is initially incorrect; must be corrected. */
134     public void testIncorrectPlatformPrivatePropertiesDefaultPlatform() throws Exception JavaDoc {
135         // Try making a standalone module w/ default platform.
136
File JavaDoc d = new File JavaDoc(getWorkDir(), "standalone");
137         NbModuleProjectGenerator.createStandAloneModule(d, "x", "X", null, null, NbPlatform.PLATFORM_ID_DEFAULT);
138         FileObject props = FileUtil.createData(FileUtil.toFileObject(d), "nbproject/private/platform-private.properties");
139         Util.storeProperties(props, new EditableProperties(Collections.singletonMap("user.properties.file", "bogus")));
140         NbModuleProject p = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(d));
141         NbPlatform pl = p.getPlatform(true); // with fallback=false, who knows what it will be
142
assertNotNull(pl);
143         assertEquals(install, pl.getDestDir());
144         p.open();
145         assertEquals(Collections.singletonMap("user.properties.file", new File JavaDoc(user, "build.properties").getAbsolutePath()),
146                 Util.loadProperties(props));
147         assertEquals(pl, p.getPlatform(true));
148         assertEquals(pl, p.getPlatform(false)); // now should be corrected even w/o fallback
149
// Same for suite. Check a component module too.
150
File JavaDoc sd = new File JavaDoc(getWorkDir(), "suite");
151         SuiteProjectGenerator.createSuiteProject(sd, NbPlatform.PLATFORM_ID_DEFAULT);
152         props = FileUtil.createData(FileUtil.toFileObject(sd), "nbproject/private/platform-private.properties");
153         Util.storeProperties(props, new EditableProperties(Collections.singletonMap("user.properties.file", "bogus")));
154         d = new File JavaDoc(getWorkDir(), "suitecomp");
155         NbModuleProjectGenerator.createSuiteComponentModule(d, "x", "X", null, null, sd);
156         SuiteProject s = (SuiteProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(sd));
157         p = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(d));
158         pl = s.getPlatform(true);
159         assertNotNull(pl);
160         assertEquals(install, pl.getDestDir());
161         assertEquals(pl, p.getPlatform(true));
162         SuiteProjectTest.openSuite(s);
163         p.open(); // just in case
164
assertEquals(Collections.singletonMap("user.properties.file", new File JavaDoc(user, "build.properties").getAbsolutePath()),
165                 Util.loadProperties(props));
166         assertEquals(pl, s.getPlatform(true));
167         assertEquals(pl, s.getPlatform(false));
168         assertEquals(pl, p.getPlatform(true));
169         assertEquals(pl, p.getPlatform(false));
170     }
171     
172     public void testUsableModuleListForBrokenPlatform() throws Exception JavaDoc {
173         File JavaDoc sd = new File JavaDoc(getWorkDir(), "suite");
174         SuiteProjectGenerator.createSuiteProject(sd, NbPlatform.PLATFORM_ID_DEFAULT);
175         File JavaDoc d = new File JavaDoc(getWorkDir(), "suitecomp");
176         NbModuleProjectGenerator.createSuiteComponentModule(d, "x", "X", null, null, sd);
177         TestBase.delete(sd);
178         NbModuleProject p = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(d));
179         ModuleEntry e = p.getModuleList().getEntry("core");
180         assertNotNull("#67148: can find core.jar from default platform", e);
181         assertEquals("correct JAR path", new File JavaDoc(new File JavaDoc(new File JavaDoc(install, "platform"), "core"), "core.jar"), e.getJarLocation());
182         p.open(); // check for errors
183
}
184     
185     // XXX to test, for suite projects, suite component module projects, and standalone projects:
186
// - return default platform if ${netbeans.dest.dir} undefined in any way or not pointing to valid platform [partly tested]
187
// - 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]
188
// - 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
189
// - all problems are notified to user (maybe move ModuleProperties.reportLostPlatform, and change MP.runFromTests)
190

191 }
192
Popular Tags