1 package com.atlassian.plugin.osgi.factory.transform.stage;
2
3 import com.atlassian.plugin.JarPluginArtifact;
4 import com.atlassian.plugin.PluginAccessor;
5 import com.atlassian.plugin.PluginParseException;
6 import com.atlassian.plugin.osgi.container.OsgiContainerManager;
7 import com.atlassian.plugin.osgi.factory.OsgiPlugin;
8 import com.atlassian.plugin.osgi.factory.transform.TransformContext;
9 import com.atlassian.plugin.osgi.factory.transform.model.SystemExports;
10 import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
11 import com.atlassian.plugin.test.PluginJarBuilder;
12 import com.atlassian.plugin.util.PluginUtils;
13 import junit.framework.TestCase;
14 import org.apache.log4j.spi.Filter;
15 import org.osgi.framework.Constants;
16 import org.osgi.framework.ServiceReference;
17
18 import javax.print.attribute.AttributeSet;
19 import java.io.ByteArrayInputStream;
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.jar.Attributes;
26 import java.util.jar.Manifest;
27
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Matchers.contains;
30 import static org.mockito.Matchers.eq;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34
35 public class TestGenerateManifestStage extends TestCase
36 {
37 private GenerateManifestStage stage;
38 private OsgiContainerManager osgiContainerManager;
39
40 @Override
41 public void setUp()
42 {
43 stage = new GenerateManifestStage();
44 osgiContainerManager = mock(OsgiContainerManager.class);
45 when(osgiContainerManager.getRegisteredServices()).thenReturn(new ServiceReference[0]);
46 }
47
48
49 public void testGenerateManifest() throws Exception
50 {
51 final File file = new PluginJarBuilder()
52 .addFormattedResource(
53 "atlassian-plugin.xml",
54 "<atlassian-plugin key='com.atlassian.plugins.example' name='Example Plugin'>",
55 " <plugin-info>",
56 " <description>",
57 " A sample plugin for demonstrating the file format.",
58 " </description>",
59 " <version>1.1</version>",
60 " <vendor name='Atlassian Software Systems Pty Ltd' url='http://www.atlassian.com'/>",
61 " </plugin-info>",
62 "</atlassian-plugin>")
63 .addFormattedJava(
64 "com.mycompany.myapp.Foo",
65 "package com.mycompany.myapp; public class Foo {}")
66 .build();
67
68 final TransformContext context = new TransformContext(Collections.<HostComponentRegistration> emptyList(), SystemExports.NONE, new JarPluginArtifact(file),
69 null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
70 context.setShouldRequireSpring(true);
71
72 final Attributes attrs = executeStage(context);
73
74 assertEquals("1.1", attrs.getValue(Constants.BUNDLE_VERSION));
75 assertEquals("com.atlassian.plugins.example", attrs.getValue(Constants.BUNDLE_SYMBOLICNAME));
76 assertEquals("A sample plugin for demonstrating the file format.", attrs.getValue(Constants.BUNDLE_DESCRIPTION));
77 assertEquals("Atlassian Software Systems Pty Ltd", attrs.getValue(Constants.BUNDLE_VENDOR));
78 assertEquals("http://www.atlassian.com", attrs.getValue(Constants.BUNDLE_DOCURL));
79 assertEquals(null, attrs.getValue(Constants.EXPORT_PACKAGE));
80 assertEquals(".", attrs.getValue(Constants.BUNDLE_CLASSPATH));
81 assertEquals("com.atlassian.plugins.example", attrs.getValue(OsgiPlugin.ATLASSIAN_PLUGIN_KEY));
82 assertEquals("*;timeout:=60", attrs.getValue("Spring-Context"));
83 assertEquals(null, attrs.getValue(Constants.IMPORT_PACKAGE));
84
85 }
86
87 public void testGenerateManifestWithProperInferredImports() throws Exception
88 {
89
90 final File file = new PluginJarBuilder().addPluginInformation("someKey", "someName", "1.0").build();
91 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(file), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
92 context.getExtraImports().add(AttributeSet.class.getPackage().getName());
93 final Attributes attrs = executeStage(context);
94
95 assertTrue(attrs.getValue(Constants.IMPORT_PACKAGE).contains(AttributeSet.class.getPackage().getName()));
96
97 }
98
99 public void testGenerateManifestWithCustomTimeout() throws Exception
100 {
101 try
102 {
103 System.setProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT, "333");
104 stage = new GenerateManifestStage();
105 final File file = new PluginJarBuilder().addPluginInformation("someKey", "someName", "1.0").build();
106 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(file), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
107 context.setShouldRequireSpring(true);
108 final Attributes attrs = executeStage(context);
109
110 assertEquals("*;timeout:=333", attrs.getValue("Spring-Context"));
111 }
112 finally
113 {
114 System.clearProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT);
115 }
116
117 }
118
119 public void testGenerateManifestWithExistingSpringContextTimeout() throws Exception
120 {
121 try
122 {
123 System.setProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT, "333");
124 stage = new GenerateManifestStage();
125 final File file = new PluginJarBuilder().addPluginInformation("someKey", "someName", "1.0")
126 .addFormattedResource("META-INF/MANIFEST.MF",
127 "Manifest-Version: 1.0",
128 "Spring-Context: *;timeout:=60",
129 "Bundle-Version: 4.2.0.jira40",
130 "Bundle-SymbolicName: my.foo.symbolicName")
131 .build();
132 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(file), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
133 context.setShouldRequireSpring(true);
134 final Attributes attrs = executeStage(context);
135
136 assertEquals("*;timeout:=333", attrs.getValue("Spring-Context"));
137 }
138 finally
139 {
140 System.clearProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT);
141 }
142 }
143
144 public void testGenerateManifestWithExistingSpringContextTimeoutNoSystemProperty() throws Exception
145 {
146 stage = new GenerateManifestStage();
147 final File file = new PluginJarBuilder().addPluginInformation("someKey", "someName", "1.0")
148 .addFormattedResource("META-INF/MANIFEST.MF",
149 "Manifest-Version: 1.0",
150 "Spring-Context: *;timeout:=60",
151 "Bundle-Version: 4.2.0.jira40",
152 "Bundle-SymbolicName: my.foo.symbolicName")
153 .build();
154 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(file), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
155 context.setShouldRequireSpring(true);
156 final Attributes attrs = executeStage(context);
157
158 assertEquals("*;timeout:=60", attrs.getValue("Spring-Context"));
159 }
160
161 public void testGenerateManifestSpringContextTimeoutNoTimeoutInHeader() throws Exception
162 {
163 try
164 {
165 System.setProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT, "789");
166 stage = new GenerateManifestStage();
167 final File file = new PluginJarBuilder().addPluginInformation("someKey", "someName", "1.0")
168 .addFormattedResource("META-INF/MANIFEST.MF",
169 "Manifest-Version: 1.0",
170 "Spring-Context: *;create-asynchronously:=false",
171 "Bundle-Version: 4.2.0.jira40",
172 "Bundle-SymbolicName: my.foo.symbolicName")
173 .build();
174 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(file), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
175 context.setShouldRequireSpring(true);
176 final Attributes attrs = executeStage(context);
177 assertEquals("*;create-asynchronously:=false;timeout:=789", attrs.getValue("Spring-Context"));
178 }
179 finally
180 {
181 System.clearProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT);
182 }
183 }
184
185 public void testGenerateManifestSpringContextTimeoutTimeoutAtTheBeginning() throws Exception
186 {
187 try
188 {
189 System.setProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT, "789");
190 stage = new GenerateManifestStage();
191 final File file = new PluginJarBuilder().addPluginInformation("someKey", "someName", "1.0")
192 .addFormattedResource("META-INF/MANIFEST.MF",
193 "Manifest-Version: 1.0",
194 "Spring-Context: timeout:=123;config/account-data-context.xml;create-asynchrously:=false",
195 "Bundle-Version: 4.2.0.jira40",
196 "Bundle-SymbolicName: my.foo.symbolicName")
197 .build();
198 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(file), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
199 context.setShouldRequireSpring(true);
200 final Attributes attrs = executeStage(context);
201 assertEquals("timeout:=789;config/account-data-context.xml;create-asynchrously:=false", attrs.getValue("Spring-Context"));
202 }
203 finally
204 {
205 System.clearProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT);
206 }
207 }
208
209 public void testGenerateManifestSpringContextTimeoutTimeoutInTheMiddle() throws Exception
210 {
211 try
212 {
213 System.setProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT, "789");
214 stage = new GenerateManifestStage();
215 final File file = new PluginJarBuilder().addPluginInformation("someKey", "someName", "1.0")
216 .addFormattedResource("META-INF/MANIFEST.MF",
217 "Manifest-Version: 1.0",
218 "Spring-Context: config/account-data-context.xml;timeout:=123;create-asynchrously:=false",
219 "Bundle-Version: 4.2.0.jira40",
220 "Bundle-SymbolicName: my.foo.symbolicName")
221 .build();
222 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(file), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
223 context.setShouldRequireSpring(true);
224 final Attributes attrs = executeStage(context);
225 assertEquals("config/account-data-context.xml;timeout:=789;create-asynchrously:=false", attrs.getValue("Spring-Context"));
226 }
227 finally
228 {
229 System.clearProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT);
230 }
231 }
232
233
234 public void testGenerateManifestMergeHostComponentImportsWithExisting() throws Exception
235 {
236 final File plugin = new PluginJarBuilder("plugin")
237 .addFormattedResource("META-INF/MANIFEST.MF",
238 "Manifest-Version: 1.0",
239 "Import-Package: javax.swing",
240 "Bundle-SymbolicName: my.foo.symbolicName",
241 "Bundle-Version: 1.0",
242 "Bundle-ClassPath: .,foo")
243 .addResource("foo/bar.txt", "Something")
244 .addPluginInformation("innerjarcp", "Some name", "1.0")
245 .build();
246
247 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(plugin), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
248 context.getExtraImports().add(AttributeSet.class.getPackage().getName());
249 final Attributes attrs = executeStage(context);
250 assertEquals("my.foo.symbolicName", attrs.getValue(Constants.BUNDLE_SYMBOLICNAME));
251 assertEquals(".,foo", attrs.getValue(Constants.BUNDLE_CLASSPATH));
252 assertEquals("innerjarcp", attrs.getValue(OsgiPlugin.ATLASSIAN_PLUGIN_KEY));
253 final String importPackage = attrs.getValue(Constants.IMPORT_PACKAGE);
254 assertTrue(importPackage.contains(AttributeSet.class.getPackage().getName()));
255 assertTrue(importPackage.contains("javax.swing"));
256 }
257
258 public void testGenerateManifestInvalidVersionWithExisting() throws Exception
259 {
260 final File plugin = new PluginJarBuilder("plugin")
261 .addFormattedResource("META-INF/MANIFEST.MF",
262 "Manifest-Version: 1.0",
263 "Bundle-SymbolicName: my.foo.symbolicName",
264 "Bundle-Version: beta1",
265 "Bundle-ClassPath: .,foo\n")
266 .addResource("foo/bar.txt", "Something")
267 .addPluginInformation("innerjarcp", "Some name", "1.0")
268 .build();
269
270 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(plugin), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
271 try
272 {
273 executeStage(context);
274 fail("Should have complained about bad plugin version");
275 }
276 catch (PluginParseException ex)
277 {
278 ex.printStackTrace();
279
280 }
281 }
282
283 public void testGenerateManifestInvalidVersion() throws Exception
284 {
285 final File plugin = new PluginJarBuilder("plugin")
286 .addPluginInformation("innerjarcp", "Some name", "beta1.0")
287 .build();
288
289 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(plugin), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
290 try
291 {
292 executeStage(context);
293 fail("Should have complained about bad plugin version");
294 }
295 catch (PluginParseException ex)
296 {
297 ex.printStackTrace();
298
299 }
300
301 }
302
303 public void testGenerateManifestWithExistingManifestNoSpringButDescriptor() throws Exception
304 {
305 final File plugin = new PluginJarBuilder("plugin")
306 .addFormattedResource("META-INF/MANIFEST.MF",
307 "Manifest-Version: 1.0",
308 "Bundle-SymbolicName: my.foo.symbolicName",
309 "Bundle-Version: 1.0",
310 "Bundle-ClassPath: .,foo")
311 .addResource("foo/bar.txt", "Something")
312 .addPluginInformation("innerjarcp", "Some name", "1.0")
313 .build();
314
315 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(plugin), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
316 final Attributes attrs = executeStage(context);
317 assertEquals("innerjarcp", attrs.getValue("Atlassian-Plugin-Key"));
318 assertNotNull(attrs.getValue("Spring-Context"));
319
320 }
321
322 public void testThatGeneratingManifestWithExistingManifestWithSimilarSpringAndAtlassianPluginKeyDoesNotRecreateTheManifest() throws Exception
323 {
324 final File file = new PluginJarBuilder().addPluginInformation("someKey", "someName", "1.0")
325 .addFormattedResource("META-INF/MANIFEST.MF",
326 "Manifest-Version: 1.0",
327 "Spring-Context: *;timeout:=60",
328 "Atlassian-Plugin-Key: someKey",
329 "Bundle-Version: 4.2.0.jira40",
330 "Bundle-SymbolicName: my.foo.symbolicName")
331 .build();
332
333 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(file), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
334 final Attributes attrs = executeStage(context);
335 assertEquals("someKey", attrs.getValue("Atlassian-Plugin-Key"));
336 assertEquals("*;timeout:=60", attrs.getValue("Spring-Context"));
337 assertNull(context.getFileOverrides().get("META-INF/MANIFEST.MF"));
338 }
339
340 public void testThatGeneratingManifestWithExistingManifestWithDifferentSpringTimeoutRecreatesTheManifest() throws Exception
341 {
342 try
343 {
344 System.setProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT, "333");
345 stage = new GenerateManifestStage();
346 final File file = new PluginJarBuilder().addPluginInformation("someKey", "someName", "1.0")
347 .addFormattedResource("META-INF/MANIFEST.MF",
348 "Manifest-Version: 1.0",
349 "Spring-Context: *;timeout:=60",
350 "Atlassian-Plugin-Key: someKey",
351 "Bundle-Version: 4.2.0.jira40",
352 "Bundle-SymbolicName: my.foo.symbolicName")
353 .build();
354
355 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(file), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
356 final Attributes attrs = executeStage(context);
357 assertEquals("someKey", attrs.getValue("Atlassian-Plugin-Key"));
358 assertEquals("*;timeout:=333", attrs.getValue("Spring-Context"));
359 assertNotNull(context.getFileOverrides().get("META-INF/MANIFEST.MF"));
360 }
361 finally
362 {
363 System.clearProperty(PluginUtils.ATLASSIAN_PLUGINS_ENABLE_WAIT);
364 }
365 }
366
367 public void testThatGeneratingManifestWithExistingManifestWithDifferentAtlassianPluginKeyRecreatesTheManifest() throws Exception
368 {
369 final File file = new PluginJarBuilder().addPluginInformation("someKey", "someName", "1.0")
370 .addFormattedResource("META-INF/MANIFEST.MF",
371 "Manifest-Version: 1.0",
372 "Spring-Context: *;timeout:=60",
373 "Atlassian-Plugin-Key: anotherKey",
374 "Bundle-Version: 4.2.0.jira40",
375 "Bundle-SymbolicName: my.foo.symbolicName")
376 .build();
377
378 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(file), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
379 final Attributes attrs = executeStage(context);
380 assertEquals("someKey", attrs.getValue("Atlassian-Plugin-Key"));
381 assertEquals("*;timeout:=60", attrs.getValue("Spring-Context"));
382 assertNotNull(context.getFileOverrides().get("META-INF/MANIFEST.MF"));
383 }
384
385 public void testGenerateManifestWithExistingManifestWithSpringWithDescriptor() throws Exception
386 {
387 final File plugin = new PluginJarBuilder("plugin")
388 .addFormattedResource("META-INF/MANIFEST.MF",
389 "Manifest-Version: 1.0",
390 "Bundle-SymbolicName: my.foo.symbolicName",
391 "Bundle-Version: 1.0",
392 "Spring-Context: *",
393 "Bundle-ClassPath: .,foo")
394 .addResource("foo/bar.txt", "Something")
395 .addPluginInformation("innerjarcp", "Some name", "1.0")
396 .build();
397
398 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(plugin), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
399 final Attributes attrs = executeStage(context);
400 assertEquals("innerjarcp", attrs.getValue("Atlassian-Plugin-Key"));
401 assertEquals("*", attrs.getValue("Spring-Context"));
402 }
403
404 public void testGenerateManifestNoExistingManifestButDescriptor() throws Exception
405 {
406 final File plugin = new PluginJarBuilder("plugin")
407 .addResource("foo/bar.txt", "Something")
408 .addPluginInformation("innerjarcp", "Some name", "1.0")
409 .build();
410
411 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(plugin), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
412 final Attributes attrs = executeStage(context);
413 assertEquals("innerjarcp", attrs.getValue("Atlassian-Plugin-Key"));
414 assertNotNull(attrs.getValue("Spring-Context"));
415
416 }
417
418 public void testGenerateManifestWarnNoTimeout() throws Exception
419 {
420 org.slf4j.Logger log = mock(org.slf4j.Logger.class);
421 GenerateManifestStage.log = log;
422
423 final File plugin = new PluginJarBuilder("plugin")
424 .addFormattedResource("META-INF/MANIFEST.MF",
425 "Manifest-Version: 1.0",
426 "Import-Package: javax.swing",
427 "Bundle-SymbolicName: my.foo.symbolicName",
428 "Bundle-Version: 1.0",
429 "Spring-Context: *",
430 "Bundle-ClassPath: .,foo")
431 .addResource("foo/bar.txt", "Something")
432 .addPluginInformation("innerjarcp", "Some name", "1.0")
433 .build();
434
435 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(plugin), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
436 context.setShouldRequireSpring(true);
437 context.getExtraImports().add(AttributeSet.class.getPackage().getName());
438 executeStage(context);
439 verify(log).warn(contains("Please add ';timeout:="), any(), eq("60"));
440 }
441
442 public void testGenerateManifest_innerjarsInImports() throws Exception
443 {
444 final File innerJar = new PluginJarBuilder("innerjar")
445 .addFormattedJava("my.Foo",
446 "package my;",
447 "import org.apache.log4j.Logger;",
448 "public class Foo{",
449 " Logger log;",
450 "}")
451 .build();
452 assertNotNull(innerJar);
453 final File plugin = new PluginJarBuilder("plugin")
454 .addJava("my.Bar", "package my;import org.apache.log4j.spi.Filter; public class Bar{Filter log;}")
455 .addFile("META-INF/lib/innerjar.jar", innerJar)
456 .addPluginInformation("innerjarcp", "Some name", "1.0")
457 .build();
458
459 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(plugin), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
460 context.addBundleClasspathJar("META-INF/lib/innerjar.jar");
461 final Attributes attrs = executeStage(context);
462
463 assertEquals("1.0", attrs.getValue(Constants.BUNDLE_VERSION));
464 assertEquals("innerjarcp", attrs.getValue(Constants.BUNDLE_SYMBOLICNAME));
465
466 final Collection classpathEntries = Arrays.asList(attrs.getValue(Constants.BUNDLE_CLASSPATH).split(","));
467 assertEquals(2, classpathEntries.size());
468 assertTrue(classpathEntries.contains("."));
469 assertTrue(classpathEntries.contains("META-INF/lib/innerjar.jar"));
470
471 final Collection imports = Arrays.asList(attrs.getValue("Import-Package").split(","));
472 assertEquals(2, imports.size());
473 assertTrue(imports.contains(org.apache.log4j.Logger.class.getPackage().getName() + ";resolution:=\"optional\""));
474 assertTrue(imports.contains(Filter.class.getPackage().getName() + ";resolution:=\"optional\""));
475 }
476
477 public void testGenerateManifestWithBundleInstructions() throws Exception
478 {
479 final File plugin = new PluginJarBuilder("plugin")
480 .addPluginInformation("test.plugin", "test.plugin", "1.0")
481 .addJava("foo.MyClass", "package foo; public class MyClass{}")
482 .addJava("foo.internal.MyPrivateClass", "package foo.internal; public class MyPrivateClass{}")
483 .build();
484
485 final TransformContext context = new TransformContext(null, SystemExports.NONE, new JarPluginArtifact(plugin), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
486 context.getBndInstructions().put("Export-Package", "!*.internal.*,*");
487 final Attributes attrs = executeStage(context);
488 assertEquals("test.plugin", attrs.getValue(Constants.BUNDLE_SYMBOLICNAME));
489 assertEquals("foo;version=\"1.0\"", attrs.getValue(Constants.EXPORT_PACKAGE));
490 }
491
492 public void testGenerateManifestWithHostAndExternalImports() throws Exception
493 {
494 final File plugin = new PluginJarBuilder("plugin")
495 .addPluginInformation("test.plugin", "test.plugin", "1.0")
496 .build();
497
498 SystemExports exports = new SystemExports("foo.bar,foo.baz;version=\"1.0\"");
499 final TransformContext context = new TransformContext(null, exports, new JarPluginArtifact(plugin), null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
500 context.getBndInstructions().put("Import-Package", "foo.bar,foo.baz");
501 final Attributes attrs = executeStage(context);
502 assertEquals("test.plugin", attrs.getValue(Constants.BUNDLE_SYMBOLICNAME));
503
504 String imports = attrs.getValue(Constants.IMPORT_PACKAGE);
505 assertTrue(imports.contains("foo.baz;version=\"[1.0,1.0]\""));
506 assertTrue(imports.contains("foo.bar"));
507 }
508
509 private Attributes executeStage(final TransformContext context) throws IOException
510 {
511 stage.execute(context);
512 Manifest mf;
513 if (context.getFileOverrides().get("META-INF/MANIFEST.MF") != null)
514 {
515 mf = new Manifest(new ByteArrayInputStream(context.getFileOverrides().get("META-INF/MANIFEST.MF")));
516 }
517 else
518 {
519 mf = context.getManifest();
520 }
521 return mf.getMainAttributes();
522 }
523 }