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.util.Map;
25  import java.util.Properties;
26  import java.util.TreeMap;
27  
28  import org.apache.maven.model.Organization;
29  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.shared.osgi.DefaultMaven2OsgiConverter;
32  
33  import aQute.lib.osgi.Analyzer;
34  import aQute.lib.osgi.Builder;
35  import aQute.lib.osgi.Jar;
36  
37  
38  /**
39   * Test for {@link BundlePlugin}.
40   * 
41   * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
42   */
43  public class BundlePluginTest extends AbstractBundlePluginTest
44  {
45  
46      private BundlePlugin plugin;
47  
48  
49      protected void setUp() throws Exception
50      {
51          super.setUp();
52          plugin = new BundlePlugin();
53          plugin.setMaven2OsgiConverter( new DefaultMaven2OsgiConverter() );
54          plugin.setBuildDirectory( "." );
55          plugin.setOutputDirectory( new File( "." ) );
56      }
57  
58  
59      public void testConvertVersionToOsgi()
60      {
61          String osgiVersion;
62  
63          osgiVersion = plugin.convertVersionToOsgi( "2.1.0-SNAPSHOT" );
64          assertEquals( "2.1.0.SNAPSHOT", osgiVersion );
65  
66          osgiVersion = plugin.convertVersionToOsgi( "2.1-SNAPSHOT" );
67          assertEquals( "2.1.0.SNAPSHOT", osgiVersion );
68  
69          osgiVersion = plugin.convertVersionToOsgi( "2-SNAPSHOT" );
70          assertEquals( "2.0.0.SNAPSHOT", osgiVersion );
71  
72          osgiVersion = plugin.convertVersionToOsgi( "2" );
73          assertEquals( "2.0.0", osgiVersion );
74  
75          osgiVersion = plugin.convertVersionToOsgi( "2.1" );
76          assertEquals( "2.1.0", osgiVersion );
77  
78          osgiVersion = plugin.convertVersionToOsgi( "2.1.3" );
79          assertEquals( "2.1.3", osgiVersion );
80  
81          osgiVersion = plugin.convertVersionToOsgi( "2.1.3.4" );
82          assertEquals( "2.1.3.4", osgiVersion );
83  
84          osgiVersion = plugin.convertVersionToOsgi( "4aug2000r7-dev" );
85          assertEquals( "0.0.0.4aug2000r7-dev", osgiVersion );
86  
87          osgiVersion = plugin.convertVersionToOsgi( "1.1-alpha-2" );
88          assertEquals( "1.1.0.alpha-2", osgiVersion );
89  
90          osgiVersion = plugin.convertVersionToOsgi( "1.0-alpha-16-20070122.203121-13" );
91          assertEquals( "1.0.0.alpha-16-20070122_203121-13", osgiVersion );
92  
93          osgiVersion = plugin.convertVersionToOsgi( "1.0-20070119.021432-1" );
94          assertEquals( "1.0.0.20070119_021432-1", osgiVersion );
95  
96          osgiVersion = plugin.convertVersionToOsgi( "1-20070119.021432-1" );
97          assertEquals( "1.0.0.20070119_021432-1", osgiVersion );
98  
99          osgiVersion = plugin.convertVersionToOsgi( "1.4.1-20070217.082013-7" );
100         assertEquals( "1.4.1.20070217_082013-7", osgiVersion );
101     }
102 
103 
104     public void testReadExportedModules() throws Exception
105     {
106         File osgiBundleFile = getTestBundle();
107 
108         assertTrue( osgiBundleFile.exists() );
109 
110         MavenProject project = new MavenProjectStub();
111         project.setGroupId( "group" );
112         project.setArtifactId( "artifact" );
113         project.setVersion( "1.1.0.0" );
114 
115         //        PackageVersionAnalyzer analyzer = new PackageVersionAnalyzer();
116         Builder analyzer = new Builder();
117         Jar jar = new Jar( "name", osgiBundleFile );
118         analyzer.setJar( jar );
119         analyzer.setClasspath( new Jar[]
120             { jar } );
121 
122         analyzer.setProperty( Analyzer.EXPORT_PACKAGE, "*" );
123         analyzer.calcManifest();
124 
125         assertEquals( 3, analyzer.getExports().size() );
126 
127         analyzer.close();
128     }
129 
130 
131     public void testTransformDirectives() throws Exception
132     {
133         Map instructions = new TreeMap();
134 
135         instructions.put( "a", "1" );
136         instructions.put( "-a", "2" );
137         instructions.put( "_a", "3" );
138         instructions.put( "A", "3" );
139         instructions.put( "_A", "1" );
140         instructions.put( "_b", "4" );
141         instructions.put( "b", "6" );
142         instructions.put( "_B", "6" );
143         instructions.put( "-B", "5" );
144         instructions.put( "B", "4" );
145 
146         instructions.put( "z", null );
147         instructions.put( "_z", null );
148 
149         Map transformedInstructions = BundlePlugin.transformDirectives( instructions );
150 
151         assertEquals( "1", transformedInstructions.get( "a" ) );
152         assertEquals( "3", transformedInstructions.get( "-a" ) );
153         assertEquals( null, transformedInstructions.get( "_a" ) );
154         assertEquals( "3", transformedInstructions.get( "A" ) );
155         assertEquals( "1", transformedInstructions.get( "-A" ) );
156         assertEquals( null, transformedInstructions.get( "_A" ) );
157         assertEquals( null, transformedInstructions.get( "_b" ) );
158         assertEquals( "4", transformedInstructions.get( "-b" ) );
159         assertEquals( "6", transformedInstructions.get( "b" ) );
160         assertEquals( null, transformedInstructions.get( "_B" ) );
161         assertEquals( "6", transformedInstructions.get( "-B" ) );
162         assertEquals( "4", transformedInstructions.get( "B" ) );
163 
164         assertEquals( "", transformedInstructions.get( "z" ) );
165         assertEquals( "", transformedInstructions.get( "-z" ) );
166     }
167 
168 
169     public void testDefaultPropertiesIncludeOrganization()
170     {
171         final Organization organization = new Organization();
172         organization.setName( "Example Organization" );
173         organization.setUrl( "http://example.org" );
174 
175         // MavenProjectStub.setOrganization(Organization) doesn't do anything, so we have to make it work this way
176         MavenProject project = new MavenProjectStub()
177         {
178             @Override
179             public Organization getOrganization()
180             {
181                 return organization;
182             }
183         };
184         project.setGroupId( "group" );
185         project.setArtifactId( "artifact" );
186         project.setVersion( "1.1.0.0" );
187 
188         Properties properties = plugin.getDefaultProperties( project );
189         assertEquals( organization.getName(), properties.getProperty( "project.organization.name" ) );
190         assertEquals( organization.getName(), properties.getProperty( "pom.organization.name" ) );
191         assertEquals( organization.getUrl(), properties.getProperty( "project.organization.url" ) );
192         assertEquals( organization.getUrl(), properties.getProperty( "pom.organization.url" ) );
193     }
194 
195 
196     public void testVersion() throws Exception
197     {
198         String cleanupVersion = Builder.cleanupVersion( "0.0.0.4aug2000r7-dev" );
199         assertEquals( "0.0.0.4aug2000r7-dev", cleanupVersion );
200     }
201 
202 
203     public void testPackageInfoDetection() throws Exception
204     {
205         MavenProject project = new MavenProjectStub();
206         project.addCompileSourceRoot( getBasedir() + "/src/test/java" );
207 
208         String resourcePaths = plugin.getMavenResourcePaths( project );
209 
210         assertEquals( "org/apache/felix/bundleplugin/packageinfo="
211             + "src/test/java/org/apache/felix/bundleplugin/packageinfo", resourcePaths );
212     }
213 }