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