View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.felix.bundleplugin;
20  
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Properties;
29  import java.util.jar.Manifest;
30  
31  import org.apache.felix.utils.manifest.Clause;
32  import org.apache.felix.utils.manifest.Parser;
33  import org.apache.maven.artifact.Artifact;
34  import org.apache.maven.artifact.DefaultArtifact;
35  import org.apache.maven.artifact.handler.ArtifactHandler;
36  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
37  import org.apache.maven.artifact.versioning.VersionRange;
38  import org.apache.maven.model.Resource;
39  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
40  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
41  import org.apache.maven.project.DefaultProjectBuildingRequest;
42  import org.apache.maven.project.ProjectBuildingRequest;
43  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
44  import org.apache.maven.shared.dependency.graph.DependencyNode;
45  import org.osgi.framework.Constants;
46  
47  import aQute.bnd.osgi.Analyzer;
48  import aQute.bnd.osgi.Builder;
49  import aQute.bnd.osgi.Jar;
50  import aQute.bnd.osgi.Verifier;
51  import aQute.libg.generics.Create;
52  
53  
54  public class BlueprintComponentTest extends AbstractMojoTestCase
55  {
56  
57      public void testBlueprintServices() throws Exception
58      {
59          test( "service" );
60      }
61  
62      public void testBlueprintGeneric() throws Exception
63      {
64          test( "generic" );
65      }
66  
67      protected void test(String mode) throws Exception
68      {
69          MavenProjectStub project = new MavenProjectStub()
70          {
71              private final List resources = new ArrayList();
72  
73  
74              @Override
75              public void addResource( Resource resource )
76              {
77                  resources.add( resource );
78              }
79  
80  
81              @Override
82              public List getResources()
83              {
84                  return resources;
85              }
86  
87  
88              @Override
89              public File getBasedir()
90              {
91                  return new File( "target/tmp/basedir" );
92              }
93          };
94          project.setGroupId("group");
95          project.setArtifactId( "artifact" );
96          project.setVersion( "1.1.0.0" );
97          VersionRange versionRange = VersionRange.createFromVersion(project.getVersion());
98          ArtifactHandler artifactHandler = new DefaultArtifactHandler( "jar" );
99          Artifact artifact = new DefaultArtifact(project.getGroupId(),project.getArtifactId(),versionRange, null, "jar", null, artifactHandler);
100         project.setArtifact(artifact);
101 
102         ProjectBuildingRequest projectBuilderConfiguration = new DefaultProjectBuildingRequest();
103         projectBuilderConfiguration.setLocalRepository(null);
104         project.setProjectBuildingRequest(projectBuilderConfiguration);
105 
106         Resource r = new Resource();
107         r.setDirectory( new File( "src/test/resources" ).getAbsoluteFile().getCanonicalPath() );
108         r.setIncludes(Arrays.asList("**/*.*"));
109         project.addResource(r);
110         project.addCompileSourceRoot(new File("src/test/resources").getAbsoluteFile().getCanonicalPath());
111 
112         ManifestPlugin plugin = new ManifestPlugin();
113         plugin.setBuildDirectory( "target/tmp/basedir/target" );
114         plugin.setOutputDirectory(new File("target/tmp/basedir/target/classes"));
115 
116         Map instructions = new HashMap();
117         instructions.put( "service_mode", mode );
118         instructions.put( "Test", "Foo" );
119 
120         instructions.put( "nsh_interface", "foo.bar.Namespace" );
121         instructions.put( "nsh_namespace", "ns" );
122 
123         instructions.put( "Export-Service", "p7.Foo;mk=mv" );
124         instructions.put( "Import-Service", "org.osgi.service.cm.ConfigurationAdmin;availability:=optional" );
125 
126         Properties props = new Properties();
127         Builder builder = plugin.buildOSGiBundle( project, instructions, plugin.getClasspath( project) );
128 
129         Manifest manifest = builder.getJar().getManifest();
130         String impSvc = manifest.getMainAttributes().getValue( Constants.IMPORT_SERVICE );
131         if ("service".equals(mode)) {
132             String expSvc = manifest.getMainAttributes().getValue( Constants.EXPORT_SERVICE );
133             assertNotNull( expSvc );
134             assertTrue( expSvc.contains("beanRef.Foo;osgi.service.blueprint.compname=myBean") );
135         } else {
136             String prvCap = manifest.getMainAttributes().getValue( Constants.PROVIDE_CAPABILITY );
137             assertNotNull( prvCap );
138             assertTrue( prvCap.contains("osgi.service;effective:=active;objectClass=\"beanRef.Foo\";osgi.service.blueprint.compname=myBean") );
139         }
140 
141         assertNotNull( impSvc );
142 
143         String impPkg = manifest.getMainAttributes().getValue( Constants.IMPORT_PACKAGE );
144         List<String> pkgs = Create.list();
145         for (Clause clause : Parser.parseHeader(impPkg))
146         {
147             pkgs.add(clause.getName());
148         }
149         for ( int i = 1; i <= 14; i++ )
150         {
151             assertTrue( pkgs.contains( "p" + i ) );
152         }
153 
154         try (Verifier verifier = new Verifier(builder)) {
155             verifier.verify();
156         }
157     }
158 
159     public void testAnalyzer() throws Exception
160     {
161         Analyzer analyzer = new Analyzer();
162         Manifest manifest = new Manifest();
163         manifest.read(getClass().getResourceAsStream("/test.mf"));
164         Jar jar = new Jar("name");
165         jar.setManifest(manifest);
166         analyzer.setJar(jar);
167         analyzer.analyze();
168 
169         try (Verifier verifier = new Verifier(analyzer)) {
170             verifier.verify();
171         }
172     }
173 
174 }