1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.DefaultProjectBuilderConfiguration;
42 import org.apache.maven.project.ProjectBuilderConfiguration;
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 ProjectBuilderConfiguration projectBuilderConfiguration = new DefaultProjectBuilderConfiguration();
103 projectBuilderConfiguration.setLocalRepository(null);
104 project.setProjectBuilderConfiguration(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 setVariableValueToObject(plugin, "m_dependencyGraphBuilder", lookup(DependencyGraphBuilder.class.getName(), "default"));
116
117 Map instructions = new HashMap();
118 instructions.put( "service_mode", mode );
119 instructions.put( "Test", "Foo" );
120
121 instructions.put( "nsh_interface", "foo.bar.Namespace" );
122 instructions.put( "nsh_namespace", "ns" );
123
124 instructions.put( "Export-Service", "p7.Foo;mk=mv" );
125 instructions.put( "Import-Service", "org.osgi.service.cm.ConfigurationAdmin;availability:=optional" );
126
127 Properties props = new Properties();
128 DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
129 Builder builder = plugin.buildOSGiBundle( project, dependencyGraph, instructions, props, plugin.getClasspath( project, dependencyGraph ) );
130
131 Manifest manifest = builder.getJar().getManifest();
132 String impSvc = manifest.getMainAttributes().getValue( Constants.IMPORT_SERVICE );
133 if ("service".equals(mode)) {
134 String expSvc = manifest.getMainAttributes().getValue( Constants.EXPORT_SERVICE );
135 assertNotNull( expSvc );
136 assertTrue( expSvc.contains("beanRef.Foo;osgi.service.blueprint.compname=myBean") );
137 } else {
138 String prvCap = manifest.getMainAttributes().getValue( Constants.PROVIDE_CAPABILITY );
139 assertNotNull( prvCap );
140 assertTrue( prvCap.contains("osgi.service;effective:=active;objectClass=\"beanRef.Foo\";osgi.service.blueprint.compname=myBean") );
141 }
142
143 assertNotNull( impSvc );
144
145 String impPkg = manifest.getMainAttributes().getValue( Constants.IMPORT_PACKAGE );
146 List<String> pkgs = Create.list();
147 for (Clause clause : Parser.parseHeader(impPkg))
148 {
149 pkgs.add(clause.getName());
150 }
151 for ( int i = 1; i <= 14; i++ )
152 {
153 assertTrue( pkgs.contains( "p" + i ) );
154 }
155
156 try (Verifier verifier = new Verifier(builder)) {
157 verifier.verify();
158 }
159 }
160
161 public void testAnalyzer() throws Exception
162 {
163 Analyzer analyzer = new Analyzer();
164 Manifest manifest = new Manifest();
165 manifest.read(getClass().getResourceAsStream("/test.mf"));
166 Jar jar = new Jar("name");
167 jar.setManifest(manifest);
168 analyzer.setJar(jar);
169 analyzer.analyze();
170
171 try (Verifier verifier = new Verifier(analyzer)) {
172 verifier.verify();
173 }
174 }
175
176 }