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 aQute.bnd.osgi.Analyzer;
32 import aQute.bnd.osgi.Jar;
33 import aQute.bnd.osgi.Verifier;
34 import aQute.libg.generics.Create;
35 import junit.framework.TestCase;
36
37 import org.apache.felix.utils.manifest.Clause;
38 import org.apache.felix.utils.manifest.Parser;
39 import org.apache.maven.artifact.Artifact;
40 import org.apache.maven.artifact.DefaultArtifact;
41 import org.apache.maven.artifact.handler.ArtifactHandler;
42 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
43 import org.apache.maven.artifact.versioning.VersionRange;
44 import org.apache.maven.model.Resource;
45 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
46 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
47 import org.apache.maven.project.DefaultProjectBuilderConfiguration;
48 import org.apache.maven.project.ProjectBuilderConfiguration;
49 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
50 import org.apache.maven.shared.dependency.graph.DependencyNode;
51 import org.osgi.framework.Constants;
52
53 import aQute.bnd.osgi.Builder;
54
55
56 public class BlueprintComponentTest extends AbstractMojoTestCase
57 {
58
59 public void testBlueprintServices() throws Exception
60 {
61 test( "service" );
62 }
63
64 public void testBlueprintGeneric() throws Exception
65 {
66 test( "generic" );
67 }
68
69 protected void test(String mode) throws Exception
70 {
71 MavenProjectStub project = new MavenProjectStub()
72 {
73 private final List resources = new ArrayList();
74
75
76 @Override
77 public void addResource( Resource resource )
78 {
79 resources.add( resource );
80 }
81
82
83 @Override
84 public List getResources()
85 {
86 return resources;
87 }
88
89
90 @Override
91 public File getBasedir()
92 {
93 return new File( "target/tmp/basedir" );
94 }
95 };
96 project.setGroupId("group");
97 project.setArtifactId( "artifact" );
98 project.setVersion( "1.1.0.0" );
99 VersionRange versionRange = VersionRange.createFromVersion(project.getVersion());
100 ArtifactHandler artifactHandler = new DefaultArtifactHandler( "jar" );
101 Artifact artifact = new DefaultArtifact(project.getGroupId(),project.getArtifactId(),versionRange, null, "jar", null, artifactHandler);
102 project.setArtifact(artifact);
103
104 ProjectBuilderConfiguration projectBuilderConfiguration = new DefaultProjectBuilderConfiguration();
105 projectBuilderConfiguration.setLocalRepository(null);
106 project.setProjectBuilderConfiguration(projectBuilderConfiguration);
107
108 Resource r = new Resource();
109 r.setDirectory( new File( "src/test/resources" ).getAbsoluteFile().getCanonicalPath() );
110 r.setIncludes(Arrays.asList("**/*.*"));
111 project.addResource(r);
112 project.addCompileSourceRoot(new File("src/test/resources").getAbsoluteFile().getCanonicalPath());
113
114 ManifestPlugin plugin = new ManifestPlugin();
115 plugin.setBuildDirectory( "target/tmp/basedir/target" );
116 plugin.setOutputDirectory(new File("target/tmp/basedir/target/classes"));
117 setVariableValueToObject(plugin, "m_dependencyGraphBuilder", lookup(DependencyGraphBuilder.class.getName(), "default"));
118
119 Map instructions = new HashMap();
120 instructions.put( "service_mode", mode );
121 instructions.put( "Test", "Foo" );
122
123 instructions.put( "nsh_interface", "foo.bar.Namespace" );
124 instructions.put( "nsh_namespace", "ns" );
125
126 instructions.put( "Export-Service", "p7.Foo;mk=mv" );
127 instructions.put( "Import-Service", "org.osgi.service.cm.ConfigurationAdmin;availability:=optional" );
128
129 Properties props = new Properties();
130 DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
131 Builder builder = plugin.buildOSGiBundle( project, dependencyGraph, instructions, props, plugin.getClasspath( project, dependencyGraph ) );
132
133 Manifest manifest = builder.getJar().getManifest();
134 String expSvc = manifest.getMainAttributes().getValue( Constants.EXPORT_SERVICE );
135 String impSvc = manifest.getMainAttributes().getValue( Constants.IMPORT_SERVICE );
136 assertNotNull( expSvc );
137 assertNotNull( impSvc );
138
139 String impPkg = manifest.getMainAttributes().getValue( Constants.IMPORT_PACKAGE );
140 List<String> pkgs = Create.list();
141 for (Clause clause : Parser.parseHeader(impPkg))
142 {
143 pkgs.add(clause.getName());
144 }
145 for ( int i = 1; i <= 14; i++ )
146 {
147 assertTrue( pkgs.contains( "p" + i ) );
148 }
149
150 new Verifier(builder).verify();
151 }
152
153 public void testAnalyzer() throws Exception
154 {
155 Analyzer analyzer = new Analyzer();
156 Manifest manifest = new Manifest();
157 manifest.read(getClass().getResourceAsStream("/test.mf"));
158 Jar jar = new Jar("name");
159 jar.setManifest(manifest);
160 analyzer.setJar(jar);
161 analyzer.analyze();
162 new Verifier(analyzer).verify();
163 }
164
165 }