1   package org.apache.felix.bundleplugin;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.util.Arrays;
26  import java.util.HashMap;
27  import java.util.LinkedHashSet;
28  import java.util.Map;
29  import java.util.Properties;
30  import java.util.Set;
31  import java.util.TreeMap;
32  import java.util.jar.Manifest;
33  
34  import org.apache.maven.model.Organization;
35  import org.apache.maven.plugin.testing.ArtifactStubFactory;
36  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
37  import org.apache.maven.project.MavenProject;
38  import org.apache.maven.shared.osgi.DefaultMaven2OsgiConverter;
39  import org.osgi.framework.Constants;
40  
41  import aQute.lib.osgi.Analyzer;
42  import aQute.lib.osgi.Builder;
43  import aQute.lib.osgi.Jar;
44  
45  
46  /**
47   * Test for {@link BundlePlugin}.
48   * 
49   * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
50   */
51  public class BundlePluginTest extends AbstractBundlePluginTest
52  {
53  
54      private BundlePlugin plugin;
55  
56  
57      protected void setUp() throws Exception
58      {
59          super.setUp();
60          plugin = new BundlePlugin();
61          plugin.setMaven2OsgiConverter( new DefaultMaven2OsgiConverter() );
62          plugin.setBuildDirectory( "." );
63          plugin.setOutputDirectory( new File( getBasedir(), "target" + File.separatorChar + "scratch" ) );
64      }
65  
66  
67      public void testConvertVersionToOsgi()
68      {
69          String osgiVersion;
70  
71          osgiVersion = plugin.convertVersionToOsgi( "2.1.0-SNAPSHOT" );
72          assertEquals( "2.1.0.SNAPSHOT", osgiVersion );
73  
74          osgiVersion = plugin.convertVersionToOsgi( "2.1-SNAPSHOT" );
75          assertEquals( "2.1.0.SNAPSHOT", osgiVersion );
76  
77          osgiVersion = plugin.convertVersionToOsgi( "2-SNAPSHOT" );
78          assertEquals( "2.0.0.SNAPSHOT", osgiVersion );
79  
80          osgiVersion = plugin.convertVersionToOsgi( "2" );
81          assertEquals( "2.0.0", osgiVersion );
82  
83          osgiVersion = plugin.convertVersionToOsgi( "2.1" );
84          assertEquals( "2.1.0", osgiVersion );
85  
86          osgiVersion = plugin.convertVersionToOsgi( "2.1.3" );
87          assertEquals( "2.1.3", osgiVersion );
88  
89          osgiVersion = plugin.convertVersionToOsgi( "2.1.3.4" );
90          assertEquals( "2.1.3.4", osgiVersion );
91  
92          osgiVersion = plugin.convertVersionToOsgi( "4aug2000r7-dev" );
93          assertEquals( "0.0.0.4aug2000r7-dev", osgiVersion );
94  
95          osgiVersion = plugin.convertVersionToOsgi( "1.1-alpha-2" );
96          assertEquals( "1.1.0.alpha-2", osgiVersion );
97  
98          osgiVersion = plugin.convertVersionToOsgi( "1.0-alpha-16-20070122.203121-13" );
99          assertEquals( "1.0.0.alpha-16-20070122_203121-13", osgiVersion );
100 
101         osgiVersion = plugin.convertVersionToOsgi( "1.0-20070119.021432-1" );
102         assertEquals( "1.0.0.20070119_021432-1", osgiVersion );
103 
104         osgiVersion = plugin.convertVersionToOsgi( "1-20070119.021432-1" );
105         assertEquals( "1.0.0.20070119_021432-1", osgiVersion );
106 
107         osgiVersion = plugin.convertVersionToOsgi( "1.4.1-20070217.082013-7" );
108         assertEquals( "1.4.1.20070217_082013-7", osgiVersion );
109     }
110 
111 
112     public void testReadExportedModules() throws Exception
113     {
114         File osgiBundleFile = getTestBundle();
115 
116         assertTrue( osgiBundleFile.exists() );
117 
118         MavenProject project = getMavenProjectStub();
119 
120         //        PackageVersionAnalyzer analyzer = new PackageVersionAnalyzer();
121         Builder analyzer = new Builder();
122         Jar jar = new Jar( "name", osgiBundleFile );
123         analyzer.setJar( jar );
124         analyzer.setClasspath( new Jar[]
125             { jar } );
126 
127         analyzer.setProperty( Analyzer.EXPORT_PACKAGE, "*" );
128         analyzer.calcManifest();
129 
130         assertEquals( 3, analyzer.getExports().size() );
131 
132         analyzer.close();
133     }
134 
135 
136     public void testTransformDirectives() throws Exception
137     {
138         Map instructions = new TreeMap();
139 
140         instructions.put( "a", "1" );
141         instructions.put( "-a", "2" );
142         instructions.put( "_a", "3" );
143         instructions.put( "A", "3" );
144         instructions.put( "_A", "1" );
145         instructions.put( "_b", "4" );
146         instructions.put( "b", "6" );
147         instructions.put( "_B", "6" );
148         instructions.put( "-B", "5" );
149         instructions.put( "B", "4" );
150 
151         instructions.put( "z", null );
152         instructions.put( "_z", null );
153 
154         Map transformedInstructions = BundlePlugin.transformDirectives( instructions );
155 
156         assertEquals( "1", transformedInstructions.get( "a" ) );
157         assertEquals( "3", transformedInstructions.get( "-a" ) );
158         assertEquals( null, transformedInstructions.get( "_a" ) );
159         assertEquals( "3", transformedInstructions.get( "A" ) );
160         assertEquals( "1", transformedInstructions.get( "-A" ) );
161         assertEquals( null, transformedInstructions.get( "_A" ) );
162         assertEquals( null, transformedInstructions.get( "_b" ) );
163         assertEquals( "4", transformedInstructions.get( "-b" ) );
164         assertEquals( "6", transformedInstructions.get( "b" ) );
165         assertEquals( null, transformedInstructions.get( "_B" ) );
166         assertEquals( "6", transformedInstructions.get( "-B" ) );
167         assertEquals( "4", transformedInstructions.get( "B" ) );
168 
169         assertEquals( "", transformedInstructions.get( "z" ) );
170         assertEquals( "", transformedInstructions.get( "-z" ) );
171     }
172 
173 
174     public void testDefaultPropertiesIncludeOrganization()
175     {
176         final Organization organization = new Organization();
177         organization.setName( "Example Organization" );
178         organization.setUrl( "http://example.org" );
179 
180         // MavenProjectStub.setOrganization(Organization) doesn't do anything, so we have to make it work this way
181         MavenProject project = new MavenProjectStub()
182         {
183             @Override
184             public Organization getOrganization()
185             {
186                 return organization;
187             }
188         };
189         project.setGroupId( "group" );
190         project.setArtifactId( "project" );
191         project.setVersion( "1.2.3.4" );
192 
193         Properties properties = plugin.getDefaultProperties( project );
194         assertEquals( organization.getName(), properties.getProperty( "project.organization.name" ) );
195         assertEquals( organization.getName(), properties.getProperty( "pom.organization.name" ) );
196         assertEquals( organization.getUrl(), properties.getProperty( "project.organization.url" ) );
197         assertEquals( organization.getUrl(), properties.getProperty( "pom.organization.url" ) );
198     }
199 
200 
201     public void testVersion() throws Exception
202     {
203         String cleanupVersion = Builder.cleanupVersion( "0.0.0.4aug2000r7-dev" );
204         assertEquals( "0.0.0.4aug2000r7-dev", cleanupVersion );
205     }
206 
207 
208     public void testPackageInfoDetection() throws Exception
209     {
210         MavenProject project = getMavenProjectStub();
211         project.addCompileSourceRoot( getBasedir() + "/src/test/java" );
212 
213         String resourcePaths = plugin.getMavenResourcePaths( project );
214 
215         assertEquals( "org/apache/felix/bundleplugin/packageinfo="
216             + "src/test/java/org/apache/felix/bundleplugin/packageinfo", resourcePaths );
217     }
218 
219 
220     public void testEmbedDependencyPositiveClauses() throws Exception
221     {
222         ArtifactStubFactory artifactFactory = new ArtifactStubFactory( plugin.getOutputDirectory(), true );
223 
224         Set artifacts = new LinkedHashSet();
225 
226         artifacts.addAll( artifactFactory.getClassifiedArtifacts() );
227         artifacts.addAll( artifactFactory.getScopedArtifacts() );
228         artifacts.addAll( artifactFactory.getTypedArtifacts() );
229 
230         MavenProject project = getMavenProjectStub();
231         project.setDependencyArtifacts( artifacts );
232 
233         Map instructions = new HashMap();
234         instructions.put( DependencyEmbedder.EMBED_DEPENDENCY, "*;classifier=;type=jar;scope=compile,"
235             + "*;classifier=;type=jar;scope=runtime" );
236         Properties props = new Properties();
237 
238         Builder builder = plugin.buildOSGiBundle( project, instructions, props, plugin.getClasspath( project ) );
239         Manifest manifest = builder.getJar().getManifest();
240 
241         String bcp = manifest.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
242         assertEquals( ".," + "compile-1.0.jar,b-1.0.jar,runtime-1.0.jar", bcp );
243 
244         String eas = manifest.getMainAttributes().getValue( "Embedded-Artifacts" );
245         assertEquals( "compile-1.0.jar;g=\"g\";a=\"compile\";v=\"1.0\"," + "b-1.0.jar;g=\"g\";a=\"b\";v=\"1.0\","
246             + "runtime-1.0.jar;g=\"g\";a=\"runtime\";v=\"1.0\"", eas );
247     }
248 
249 
250     public void testEmbedDependencyNegativeClauses() throws Exception
251     {
252         ArtifactStubFactory artifactFactory = new ArtifactStubFactory( plugin.getOutputDirectory(), true );
253 
254         Set artifacts = new LinkedHashSet();
255 
256         artifacts.addAll( artifactFactory.getClassifiedArtifacts() );
257         artifacts.addAll( artifactFactory.getScopedArtifacts() );
258         artifacts.addAll( artifactFactory.getTypedArtifacts() );
259 
260         MavenProject project = getMavenProjectStub();
261         project.setDependencyArtifacts( artifacts );
262 
263         Map instructions = new HashMap();
264         instructions.put( DependencyEmbedder.EMBED_DEPENDENCY, "!type=jar, !artifactId=c" );
265         Properties props = new Properties();
266 
267         Builder builder = plugin.buildOSGiBundle( project, instructions, props, plugin.getClasspath( project ) );
268         Manifest manifest = builder.getJar().getManifest();
269 
270         String bcp = manifest.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
271         assertEquals( ".," + "e-1.0.rar," + "a-1.0.war," + "d-1.0.zip", bcp );
272 
273         String eas = manifest.getMainAttributes().getValue( "Embedded-Artifacts" );
274         assertEquals( "e-1.0.rar;g=\"g\";a=\"e\";v=\"1.0\"," + "a-1.0.war;g=\"g\";a=\"a\";v=\"1.0\","
275             + "d-1.0.zip;g=\"g\";a=\"d\";v=\"1.0\"", eas );
276     }
277 
278 
279     public void testEmbedDependencyDuplicateKeys() throws Exception
280     {
281         ArtifactStubFactory artifactFactory = new ArtifactStubFactory( plugin.getOutputDirectory(), true );
282 
283         Set artifacts = new LinkedHashSet();
284 
285         artifacts.addAll( artifactFactory.getClassifiedArtifacts() );
286         artifacts.addAll( artifactFactory.getScopedArtifacts() );
287         artifacts.addAll( artifactFactory.getTypedArtifacts() );
288 
289         MavenProject project = getMavenProjectStub();
290         project.setDependencyArtifacts( artifacts );
291 
292         Map instructions = new HashMap();
293         instructions.put( DependencyEmbedder.EMBED_DEPENDENCY, "c;type=jar,c;type=sources" );
294         Properties props = new Properties();
295 
296         Builder builder = plugin.buildOSGiBundle( project, instructions, props, plugin.getClasspath( project ) );
297         Manifest manifest = builder.getJar().getManifest();
298 
299         String bcp = manifest.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
300         assertEquals( ".," + "c-1.0-three.jar," + "c-1.0.sources", bcp );
301 
302         String eas = manifest.getMainAttributes().getValue( "Embedded-Artifacts" );
303         assertEquals( "c-1.0-three.jar;g=\"g\";a=\"c\";v=\"1.0\";c=\"three\","
304             + "c-1.0.sources;g=\"g\";a=\"c\";v=\"1.0\"", eas );
305     }
306 
307 
308     public void testEmbedDependencyMissingPositiveKey() throws Exception
309     {
310         ArtifactStubFactory artifactFactory = new ArtifactStubFactory( plugin.getOutputDirectory(), true );
311 
312         Set artifacts = new LinkedHashSet();
313 
314         artifacts.addAll( artifactFactory.getClassifiedArtifacts() );
315         artifacts.addAll( artifactFactory.getScopedArtifacts() );
316         artifacts.addAll( artifactFactory.getTypedArtifacts() );
317 
318         MavenProject project = getMavenProjectStub();
319         project.setDependencyArtifacts( artifacts );
320 
321         Map instructions = new HashMap();
322         instructions.put( DependencyEmbedder.EMBED_DEPENDENCY, "artifactId=a|b" );
323         Properties props = new Properties();
324 
325         Builder builder = plugin.buildOSGiBundle( project, instructions, props, plugin.getClasspath( project ) );
326         Manifest manifest = builder.getJar().getManifest();
327 
328         String bcp = manifest.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
329         assertEquals( ".," + "a-1.0-one.jar," + "b-1.0-two.jar," + "a-1.0.war," + "b-1.0.jar", bcp );
330 
331         String eas = manifest.getMainAttributes().getValue( "Embedded-Artifacts" );
332         assertEquals( "a-1.0-one.jar;g=\"g\";a=\"a\";v=\"1.0\";c=\"one\","
333             + "b-1.0-two.jar;g=\"g\";a=\"b\";v=\"1.0\";c=\"two\"," + "a-1.0.war;g=\"g\";a=\"a\";v=\"1.0\","
334             + "b-1.0.jar;g=\"g\";a=\"b\";v=\"1.0\"", eas );
335     }
336 
337 
338     public void testEmbedDependencyMissingNegativeKey() throws Exception
339     {
340         ArtifactStubFactory artifactFactory = new ArtifactStubFactory( plugin.getOutputDirectory(), true );
341 
342         Set artifacts = new LinkedHashSet();
343 
344         artifacts.addAll( artifactFactory.getClassifiedArtifacts() );
345         artifacts.addAll( artifactFactory.getScopedArtifacts() );
346         artifacts.addAll( artifactFactory.getTypedArtifacts() );
347 
348         MavenProject project = getMavenProjectStub();
349         project.setDependencyArtifacts( artifacts );
350         Properties props = new Properties();
351 
352         Map instructions1 = new HashMap();
353         instructions1.put( DependencyEmbedder.EMBED_DEPENDENCY, "!scope=compile" );
354         Builder builder1 = plugin.buildOSGiBundle( project, instructions1, props, plugin.getClasspath( project ) );
355         Manifest manifest1 = builder1.getJar().getManifest();
356 
357         Map instructions2 = new HashMap();
358         instructions2.put( DependencyEmbedder.EMBED_DEPENDENCY, "scope=!compile" );
359         Builder builder2 = plugin.buildOSGiBundle( project, instructions2, props, plugin.getClasspath( project ) );
360         Manifest manifest2 = builder2.getJar().getManifest();
361 
362         String bcp1 = manifest1.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
363         assertEquals( ".," + "test-1.0.jar," + "provided-1.0.jar," + "runtime-1.0.jar," + "system-1.0.jar", bcp1 );
364 
365         String eas1 = manifest1.getMainAttributes().getValue( "Embedded-Artifacts" );
366         assertEquals( "test-1.0.jar;g=\"g\";a=\"test\";v=\"1.0\","
367             + "provided-1.0.jar;g=\"g\";a=\"provided\";v=\"1.0\"," + "runtime-1.0.jar;g=\"g\";a=\"runtime\";v=\"1.0\","
368             + "system-1.0.jar;g=\"g\";a=\"system\";v=\"1.0\"", eas1 );
369 
370         String bcp2 = manifest2.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
371         assertEquals( bcp1, bcp2 );
372 
373         String eas2 = manifest2.getMainAttributes().getValue( "Embedded-Artifacts" );
374         assertEquals( eas1, eas2 );
375     }
376 
377 
378     public void testPropertySanitizing() throws Exception
379     {
380         MavenProject project = getMavenProjectStub();
381 
382         Properties props = new Properties();
383 
384         props.put( new File( "A" ), new File( "B" ) );
385         props.put( new int[4], new HashMap( 2 ) );
386         props.put( Arrays.asList( 1, "two", 3.0 ), new float[5] );
387 
388         props.put( "A", new File( "B" ) );
389         props.put( "4", new HashMap( 2 ) );
390         props.put( "1, two, 3.0", new char[5] );
391 
392         Builder builder = plugin.getOSGiBuilder( project, new HashMap(), props, plugin.getClasspath( project ) );
393 
394         File file = new File( getBasedir(), "target" + File.separatorChar + "test.props" );
395         builder.getProperties().store( new FileOutputStream( file ), "TEST" );
396     }
397 }