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.BufferedReader;
045: import java.io.File;
046: import java.io.PrintStream;
047: import java.io.Reader;
048: import org.apache.tools.ant.module.api.support.ActionUtils;
049: import org.netbeans.api.project.ProjectManager;
050: import org.netbeans.modules.apisupport.project.layers.LayerTestBase;
051: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
052: import org.netbeans.modules.apisupport.project.ui.customizer.ModuleDependency;
053: import org.netbeans.modules.apisupport.project.ui.customizer.SuiteProperties;
054: import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
055: import org.netbeans.spi.project.support.ant.EditableProperties;
056: import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
057: import org.openide.DialogDescriptor;
058: import org.openide.execution.ExecutorTask;
059: import org.openide.filesystems.FileLock;
060: import org.openide.filesystems.FileObject;
061: import org.openide.modules.SpecificationVersion;
062:
063: /**
064: * Tests ProjectXMLManager class.
065: *
066: * @author Petr Zajac
067: */
068: public class CompilationDependencyTest extends TestBase {
069:
070: private final static String WINDOWS = "org.openide.windows";
071:
072: static {
073: // #65461: do not try to load ModuleInfo instances from ant module
074: System.setProperty(
075: "org.netbeans.core.startup.ModuleSystem.CULPRIT",
076: "true");
077: LayerTestBase.Lkp.setLookup(new Object[0]);
078: DialogDisplayerImpl
079: .returnFromNotify(DialogDescriptor.NO_OPTION);
080: }
081:
082: public CompilationDependencyTest(String testName) {
083: super (testName);
084: }
085:
086: protected @Override
087: void setUp() throws Exception {
088: clearWorkDir();
089: super .setUp();
090: InstalledFileLocatorImpl.registerDestDir(destDirF);
091: TestAntLogger.getDefault().setEnabled(true);
092:
093: }
094:
095: protected @Override
096: void tearDown() throws Exception {
097: TestAntLogger.getDefault().setEnabled(false);
098: }
099:
100: public void testInvalidSpecVersion() throws Exception {
101: NbModuleProject testingProject = TestBase
102: .generateStandaloneModule(getWorkDir(), "testing");
103: testingProject.open();
104:
105: FileObject buildScript = findBuildXml(testingProject);
106: assertNotNull(buildScript);
107: ExecutorTask et = ActionUtils.runTarget(buildScript,
108: new String[] { "jar" }, null);
109: et.waitFinished();
110: assertEquals("Error during ant ...", 0, et.result());
111: SpecificationVersion invalid = new SpecificationVersion("1000");
112: Util
113: .addDependency(testingProject, WINDOWS, null, invalid,
114: true);
115: ProjectManager.getDefault().saveProject(testingProject);
116: et = ActionUtils.runTarget(buildScript, new String[] { "clean",
117: "jar" }, null);
118: et.waitFinished();
119:
120: // it must fail but I don't know why it passed
121: assertFalse("Error during ant ...", 0 == et.result());
122: assertFalse(
123: "Successfully compiled when is invalid specification version",
124: testingProject.getModuleJarLocation().exists());
125: }
126:
127: public void testCompileAgainstPublicPackage() throws Exception {
128: NbModuleProject testingProject = TestBase
129: .generateStandaloneModule(getWorkDir(), "testing");
130: testingProject.open();
131: FileObject buildScript = findBuildXml(testingProject);
132: assertNotNull(buildScript);
133:
134: Util.addDependency(testingProject, WINDOWS);
135: ProjectManager.getDefault().saveProject(testingProject);
136:
137: FileObject javaFo = testingProject.getSourceDirectory()
138: .getFileObject("org/example/testing").createData(
139: "JavaFile.java");
140: FileLock lock = javaFo.lock();
141: PrintStream ps = new PrintStream(javaFo.getOutputStream(lock));
142: ps.println("package org.example.testing;");
143: ps.println("import org.netbeans.modules.openide.windows.*;");
144: ps.println("public class JavaFile {}");
145: ps.close();
146: lock.releaseLock();
147:
148: ExecutorTask et = ActionUtils.runTarget(buildScript,
149: new String[] { "clean", "netbeans" }, null);
150: et.waitFinished();
151:
152: assertFalse(
153: "project was successfully compiled against non public package",
154: testingProject.getModuleJarLocation().exists());
155:
156: ProjectXMLManager testingPXM = new ProjectXMLManager(
157: testingProject);
158: testingPXM.removeDependency(WINDOWS);
159: ModuleEntry module = testingProject.getModuleList().getEntry(
160: WINDOWS);
161: ModuleDependency newDep = new ModuleDependency(module, module
162: .getReleaseVersion(), module.getSpecificationVersion(),
163: true, true);
164: testingPXM.addDependency(newDep);
165: ProjectManager.getDefault().saveProject(testingProject);
166:
167: et = ActionUtils.runTarget(buildScript, new String[] { "clean",
168: "netbeans" }, null);
169: Reader reader = et.getInputOutput().getIn();
170: BufferedReader breader = new BufferedReader(reader);
171: et.waitFinished();
172: String line = null;
173: while ((line = breader.readLine()) != null) {
174: log(line);
175: System.out.println(line);
176: }
177: assertTrue(
178: "compilation failed for implementation dependency: no file "
179: + testingProject.getModuleJarLocation(),
180: testingProject.getModuleJarLocation().exists());
181: }
182:
183: public void testCompileAgainstRemovedModule68716() throws Exception {
184: SuiteProject suite = TestBase.generateSuite(new File(
185: getWorkDir(), "projects"), "suite");
186: NbModuleProject proj = TestBase.generateSuiteComponent(suite,
187: "mod1");
188: suite.open();
189: Util.addDependency(proj, WINDOWS);
190:
191: // remove WINDOWS from platform
192: EditableProperties ep = suite.getHelper().getProperties(
193: "nbproject/platform.properties");
194: ep.setProperty(SuiteProperties.DISABLED_MODULES_PROPERTY,
195: WINDOWS);
196: suite.getHelper().putProperties(
197: "nbproject/platform.properties", ep);
198: ProjectManager.getDefault().saveProject(proj);
199: ProjectManager.getDefault().saveProject(suite);
200:
201: // build project
202: FileObject buildScript = proj.getProjectDirectory()
203: .getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
204: assertNotNull(buildScript);
205: ExecutorTask et = ActionUtils.runTarget(buildScript,
206: new String[] { "clean", "netbeans" }, null);
207: et.waitFinished();
208: assertFalse(
209: "project was successfully compiled against removed module from platform",
210: proj.getModuleJarLocation().exists());
211: }
212:
213: private static FileObject findBuildXml(final NbModuleProject project) {
214: return project.getProjectDirectory().getFileObject(
215: GeneratedFilesHelper.BUILD_XML_PATH);
216: }
217:
218: }
|