001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others. All rights reserved. This
003: * program and the accompanying materials are made available under the terms of
004: * the Eclipse Public License v1.0 which accompanies this distribution, and is
005: * available at http://www.eclipse.org/legal/epl-v10.html
006: *
007: * Contributors: IBM Corporation - initial API and implementation
008: ******************************************************************************/package org.eclipse.pde.build.internal.tests;
009:
010: import java.io.*;
011: import java.util.*;
012: import java.util.jar.Attributes;
013:
014: import org.apache.tools.ant.Project;
015: import org.apache.tools.ant.Target;
016: import org.apache.tools.ant.types.Path;
017: import org.eclipse.core.resources.*;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.pde.build.tests.BuildConfiguration;
020: import org.eclipse.pde.build.tests.PDETestCase;
021: import org.eclipse.pde.internal.build.*;
022: import org.eclipse.pde.internal.build.site.BuildTimeSiteFactory;
023: import org.eclipse.update.core.IIncludedFeatureReference;
024: import org.eclipse.update.core.model.*;
025:
026: public class ScriptGenerationTests extends PDETestCase {
027:
028: // Test that script generation works when buildDirectory does not contain a plugins subdirectory
029: public void testBug147292() throws Exception {
030: IFolder buildFolder = newTest("147292");
031:
032: String bundleId = "org.eclipse.pde.build.test.147292";
033: Utils.generateBundle(buildFolder, bundleId);
034:
035: //getScriptGenerationProperties sets buildDirectory to buildFolder by default
036: Properties properties = BuildConfiguration
037: .getScriptGenerationProperties(buildFolder, "plugin",
038: bundleId);
039: properties.put("baseLocation", buildFolder.getLocation()
040: .toOSString());
041: generateScripts(buildFolder, properties);
042:
043: // test passes if generateScripts did not throw an exception
044: assertResourceFile(buildFolder, "build.xml");
045: }
046:
047: // Test that the order in which archivesFormat and configInfo are set does not matter
048: public void testBug148288() throws Exception {
049: IProject buildProject = newTest();
050:
051: class MyBuildScriptGenerator extends BuildScriptGenerator {
052: public HashMap getArchivesFormat() {
053: return super .getArchivesFormat();
054: }
055: }
056: ;
057:
058: String location = buildProject.getLocation().toOSString();
059: MyBuildScriptGenerator generator = new MyBuildScriptGenerator();
060: generator.setElements(new String[] {});
061: generator.setWorkingDirectory(location);
062: BuildTimeSiteFactory.setInstalledBaseSite(location);
063: AbstractScriptGenerator.setConfigInfo("win32, win32, x86");
064: generator.setArchivesFormat("win32, win32, x86 - antZip");
065: generator.generate();
066:
067: HashMap map = generator.getArchivesFormat();
068: assertEquals(map.size(), 1);
069: Config config = (Config) map.keySet().iterator().next();
070: assertEquals(map.get(config), "antZip");
071:
072: clearStatics();
073:
074: generator = new MyBuildScriptGenerator();
075: generator.setElements(new String[] {});
076: generator.setWorkingDirectory(location);
077: BuildTimeSiteFactory.setInstalledBaseSite(location);
078: generator.setArchivesFormat("win32, win32, x86 - folder");
079: AbstractScriptGenerator.setConfigInfo("win32, win32, x86");
080: generator.generate();
081:
082: map = generator.getArchivesFormat();
083: assertEquals(map.size(), 1);
084: config = (Config) map.keySet().iterator().next();
085: assertEquals(map.get(config), "folder");
086: }
087:
088: // Test script generation for bundles using Bundle-RequiredExecutionEnvironment
089: // when the state does not contain org.eclipse.osgi
090: public void testBug178447() throws Exception {
091: IFolder buildFolder = newTest("178447");
092:
093: String bundleId = "org.eclipse.pde.build.test.178447";
094: Attributes manifestAdditions = new Attributes();
095: manifestAdditions.put(new Attributes.Name(
096: "Bundle-RequiredExecutionEnvironment"), "J2SE-1.3");
097: Utils.generateBundleManifest(buildFolder, bundleId, "1.0.0",
098: manifestAdditions);
099: Utils.generatePluginBuildProperties(buildFolder, null);
100:
101: Properties properties = BuildConfiguration
102: .getScriptGenerationProperties(buildFolder, "plugin",
103: bundleId);
104: properties.put("baseLocation", buildFolder.getLocation()
105: .toOSString());
106: generateScripts(buildFolder, properties);
107:
108: // test passes if generateScripts did not throw an exception
109: assertResourceFile(buildFolder, "build.xml");
110: }
111:
112: // Test the use of customBuildCallbacks.buildpath
113: public void testBug183869() throws Exception {
114: IFolder buildFolder = newTest("183869");
115:
116: Utils.generateAllElements(buildFolder, "a.feature");
117:
118: Properties buildProperties = BuildConfiguration
119: .getBuilderProperties(buildFolder);
120: Utils.storeBuildProperties(buildFolder, buildProperties);
121:
122: runBuild(buildFolder);
123:
124: assertResourceFile(buildFolder, "log.log");
125: String[] lines = new String[] { "[echo] Hello Plugin!",
126: "[echo] Hello Feature!" };
127: assertLogContainsLines(buildFolder.getFile("log.log"), lines);
128: }
129:
130: // test platform.xml
131: public void testBug183924() throws Exception {
132: IFolder buildFolder = newTest("183924");
133: IFolder configFolder = Utils.createFolder(buildFolder,
134: "configuration/org.eclipse.update");
135:
136: //Figure out the version of the org.eclipse.rcp feature
137: String baseLocation = Platform.getInstallLocation().getURL()
138: .getPath();
139: File features = new File(baseLocation, "features");
140: FileFilter filter = new FileFilter() {
141: public boolean accept(File pathname) {
142: return pathname.getName()
143: .startsWith("org.eclipse.rcp_");
144: }
145: };
146: File rcp[] = features.listFiles(filter);
147: assertTrue(rcp.length > 0);
148: String name = rcp[0].getName();
149: String version = name.substring("org.eclipse.rcp_".length(),
150: name.length());
151:
152: // copy platform.xml and set the baseLocation and rcp version
153: IFile sourceXML = buildFolder.getFile("platform.xml");
154: Map replacements = new HashMap();
155: replacements.put("BASE_LOCATION", baseLocation);
156: replacements.put("RCP_VERSION", version);
157: Utils.transferAndReplace(sourceXML.getLocationURI().toURL(),
158: configFolder.getFile("platform.xml"), replacements);
159:
160: //Generate Scripts for org.eclipse.rcp, expect to find it through platform.xml
161: Properties properties = BuildConfiguration
162: .getScriptGenerationProperties(buildFolder, "feature",
163: "org.eclipse.rcp");
164: properties.put("baseLocation", buildFolder.getLocation()
165: .toOSString());
166: generateScripts(buildFolder, properties);
167:
168: //platform.xml has MANAGED-ONLY policy, expect to not find org.eclipse.core.resources
169: properties = BuildConfiguration.getScriptGenerationProperties(
170: buildFolder, "plugin", "org.eclipse.core.resources");
171: properties.put("baseLocation", buildFolder.getLocation()
172: .toOSString());
173: try {
174: //this is expected to fail
175: generateScripts(buildFolder, properties);
176: assertTrue(false);
177: } catch (Exception e) {
178: assertTrue(e
179: .getMessage()
180: .endsWith(
181: "Unable to find element: org.eclipse.core.resources."));
182: }
183: }
184:
185: // test that the order of features passed to FeatureGenerator is preserved
186: public void testBug187809() throws Exception {
187: IFolder buildFolder = newTest("187809");
188:
189: Utils.generateFeature(buildFolder, "sdk", new String[] { "foo",
190: "bar", "disco" }, null);
191:
192: assertResourceFile(buildFolder, "features/sdk/feature.xml");
193: IFile feature = buildFolder.getFile("features/sdk/feature.xml");
194: FeatureModelFactory factory = new FeatureModelFactory();
195: InputStream stream = new BufferedInputStream(feature
196: .getLocationURI().toURL().openStream());
197: FeatureModel model = null;
198: try {
199: model = factory.parseFeature(stream);
200: } finally {
201: stream.close();
202: }
203: IIncludedFeatureReference[] included = model
204: .getFeatureIncluded();
205: assertEquals(included.length, 3);
206: assertEquals(included[0].getVersionedIdentifier()
207: .getIdentifier(), "foo");
208: assertEquals(included[1].getVersionedIdentifier()
209: .getIdentifier(), "bar");
210: assertEquals(included[2].getVersionedIdentifier()
211: .getIdentifier(), "disco");
212: }
213:
214: // Test that & characters in classpath are escaped properly
215: public void testBug125577() throws Exception {
216: IFolder buildFolder = newTest("125577");
217: Utils.createFolder(buildFolder, "plugins");
218:
219: //Create Bundle A
220: IFolder bundleA = buildFolder.getFolder("plugins/A & A");
221: bundleA.create(true, true, null);
222: Utils.generateBundle(bundleA, "A");
223:
224: //Create Bundle B
225: IFolder bundleB = buildFolder.getFolder("plugins/B");
226: bundleB.create(true, true, null);
227: Utils.generatePluginBuildProperties(bundleB, null);
228:
229: // Bundle B requires Bundle A
230: Attributes manifestAdditions = new Attributes();
231: manifestAdditions.put(new Attributes.Name("Require-Bundle"),
232: "A");
233: Utils.generateBundleManifest(bundleB, "B", "1.0.0",
234: manifestAdditions);
235:
236: generateScripts(buildFolder, BuildConfiguration
237: .getScriptGenerationProperties(buildFolder, "plugin",
238: "B"));
239:
240: assertResourceFile(bundleB, "build.xml");
241: //if the & was not escaped, it won't be a valid ant script
242: assertValidAntScript(bundleB.getFile("build.xml"));
243: }
244:
245: public void testSimpleClasspath() throws Exception {
246: IFolder buildFolder = newTest("SimpleClasspath");
247:
248: Utils.generatePluginBuildProperties(buildFolder, null);
249: Attributes manifestAdditions = new Attributes();
250: manifestAdditions.put(new Attributes.Name("Require-Bundle"),
251: "org.eclipse.equinox.preferences");
252: Utils.generateBundleManifest(buildFolder, "bundle", "1.0.0",
253: manifestAdditions);
254:
255: generateScripts(buildFolder, BuildConfiguration
256: .getScriptGenerationProperties(buildFolder, "plugin",
257: "bundle"));
258:
259: IFile buildScript = buildFolder.getFile("build.xml");
260: Project antProject = assertValidAntScript(buildScript);
261: Target dot = (Target) antProject.getTargets().get("@dot");
262: assertNotNull(dot);
263: Object child = AntUtils.getFirstChildByName(dot, "path");
264: assertNotNull(child);
265: assertTrue(child instanceof Path);
266: String path = child.toString();
267:
268: //Assert classpath has correct contents
269: int idx[] = { 0,
270: path.indexOf("org.eclipse.equinox.preferences"),
271: path.indexOf("org.eclipse.osgi"),
272: path.indexOf("org.eclipse.equinox.common"),
273: path.indexOf("org.eclipse.equinox.registry"),
274: path.indexOf("org.eclipse.core.jobs") };
275: for (int i = 0; i < idx.length - 1; i++) {
276: assertTrue(idx[i] < idx[i + 1]);
277: }
278: }
279:
280: public void testPluginPath() throws Exception {
281: IFolder buildFolder = newTest("PluginPath");
282: IFolder bundleA = Utils.createFolder(buildFolder,
283: "plugins/bundleA");
284: IFolder bundleB = Utils.createFolder(buildFolder,
285: "other/bundleB");
286:
287: Utils.generateBundle(bundleA, "bundleA");
288: Utils.generateBundle(bundleB, "bundleB");
289: Utils.generateFeature(buildFolder, "rcp", null, new String[] {
290: "bundleA", "bundleB" });
291:
292: Properties props = BuildConfiguration
293: .getScriptGenerationProperties(buildFolder, "feature",
294: "rcp");
295: props.put("pluginPath", bundleB.getLocation().toOSString());
296:
297: // generateScripts will assert if bundleB is not found
298: generateScripts(buildFolder, props);
299: }
300:
301: public void testBug128901_filteredDependencyCheck()
302: throws Exception {
303: IFolder buildFolder = newTest("128901");
304: IFolder bundleFolder = Utils.createFolder(buildFolder,
305: "plugins/bundle");
306:
307: Utils.generatePluginBuildProperties(bundleFolder, null);
308: Attributes manifestAdditions = new Attributes();
309: manifestAdditions.put(new Attributes.Name("Require-Bundle"),
310: "org.eclipse.equinox.registry");
311: Utils.generateBundleManifest(bundleFolder, "bundle", "1.0.0",
312: manifestAdditions);
313:
314: Utils.generateFeature(buildFolder, "rcp", null,
315: new String[] { "bundle", "org.eclipse.osgi",
316: "org.eclipse.equinox.common",
317: "org.eclipse.equinox.registry",
318: "org.eclipse.core.jobs" });
319:
320: Properties props = BuildConfiguration
321: .getScriptGenerationProperties(buildFolder, "feature",
322: "rcp");
323: props.put("filteredDependencyCheck", "true");
324:
325: generateScripts(buildFolder, props);
326:
327: // org.eclipse.core.runtime.compatibility.registry is an optional bundle, which should have been excluded
328: //from the state by the filtering, check that is isn't in the classpath
329: IFile buildScript = bundleFolder.getFile("build.xml");
330: Project antProject = assertValidAntScript(buildScript);
331: Target dot = (Target) antProject.getTargets().get("@dot");
332: Object child = AntUtils.getFirstChildByName(dot, "path");
333: assertTrue(child instanceof Path);
334: String path = child.toString();
335: assertTrue(path
336: .indexOf("org.eclipse.core.runtime.compatibility.registry") == -1);
337: }
338:
339: public void testBug198536() throws Exception {
340: final IFolder buildFolder = newTest("198536");
341:
342: StringBuffer buffer = new StringBuffer();
343: buffer
344: .append("<feature id=\"foo\" version=\"1.0.0.qualifier\"> \n");
345: buffer
346: .append(" <plugin version=\"0.0.0\" id=\"foo\" /> \n");
347: buffer
348: .append(" <plugin version=\"1.0.0.id_qualifier\" id=\"bar\" /> \n");
349: buffer
350: .append(" <plugin id=\"foo.version\" version=\"0.0.0\" /> \n");
351: buffer
352: .append("</feature> \n");
353:
354: IFile featureXML = buildFolder.getFile("feature.xml");
355: FileOutputStream stream = null;
356: try {
357: stream = new FileOutputStream(featureXML.getLocation()
358: .toFile());
359: stream.write(buffer.toString().getBytes());
360: } finally {
361: if (stream != null)
362: stream.close();
363: }
364:
365: buffer = new StringBuffer();
366: buffer
367: .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n");
368: buffer
369: .append("<project name=\"project\" default=\"default\"> \n");
370: buffer
371: .append(" <target name=\"default\"> \n");
372: buffer
373: .append(" <eclipse.idReplacer \n");
374: buffer.append(" featureFilePath=\""
375: + featureXML.getLocation().toOSString() + "\" \n");
376: buffer
377: .append(" selfVersion=\"1.0.0.ABCDE\" \n");
378: buffer
379: .append(" featureIds=\"\" \n");
380: buffer
381: .append(" pluginIds=\"foo:0.0.0,1.0.0.vA,bar:1.0.0,1.0.0.id_v,foo.version:0.0.0,2.1.2\" \n");
382: buffer
383: .append(" /> \n");
384: buffer
385: .append(" </target> \n");
386: buffer
387: .append("</project> \n");
388:
389: final IFile buildXML = buildFolder.getFile("build.xml");
390: try {
391: stream = new FileOutputStream(buildXML.getLocation()
392: .toFile());
393: stream.write(buffer.toString().getBytes());
394: } finally {
395: if (stream != null)
396: stream.close();
397: }
398:
399: runAntScript(buildXML.getLocation().toOSString(),
400: new String[] { "default" }, buildFolder.getLocation()
401: .toOSString(), null);
402:
403: FeatureModelFactory factory = new FeatureModelFactory();
404: InputStream inputStream = new BufferedInputStream(featureXML
405: .getLocationURI().toURL().openStream());
406: try {
407: FeatureModel feature = factory.parseFeature(inputStream);
408: PluginEntryModel[] pluginEntryModels = feature
409: .getPluginEntryModels();
410: assertEquals(pluginEntryModels[0].getPluginIdentifier(),
411: "foo");
412: assertEquals(pluginEntryModels[0].getPluginVersion(),
413: "1.0.0.vA");
414: assertEquals(pluginEntryModels[1].getPluginIdentifier(),
415: "bar");
416: assertEquals(pluginEntryModels[1].getPluginVersion(),
417: "1.0.0.id_v");
418: assertEquals(pluginEntryModels[2].getPluginIdentifier(),
419: "foo.version");
420: assertEquals(pluginEntryModels[2].getPluginVersion(),
421: "2.1.2");
422: } finally {
423: inputStream.close();
424: }
425:
426: }
427: }
|