blob: 9bbbc72f5acde4804ed31ad41b9aebfc3d228b62 [file] [log] [blame]
Guillaume Nodet09157a92010-03-10 17:40:45 +00001/*
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 */
19package org.apache.felix.bundleplugin;
20
Stuart McCullochd98b02e2011-06-28 23:20:37 +000021
Guillaume Nodet09157a92010-03-10 17:40:45 +000022import java.io.File;
23import java.util.ArrayList;
24import java.util.Arrays;
25import java.util.HashMap;
26import java.util.List;
27import java.util.Map;
28import java.util.Properties;
Guillaume Nodet09157a92010-03-10 17:40:45 +000029import java.util.jar.Manifest;
30
Guillaume Nodet1c9211a2014-05-20 08:56:13 +000031import aQute.bnd.osgi.Analyzer;
32import aQute.bnd.osgi.Jar;
33import aQute.bnd.osgi.Verifier;
Guillaume Nodet7bf509f2014-05-19 07:12:48 +000034import aQute.libg.generics.Create;
Guillaume Nodet09157a92010-03-10 17:40:45 +000035import junit.framework.TestCase;
Stuart McCullochd98b02e2011-06-28 23:20:37 +000036
Guillaume Nodet7bf509f2014-05-19 07:12:48 +000037import org.apache.felix.utils.manifest.Clause;
38import org.apache.felix.utils.manifest.Parser;
Guillaume Nodeted7b1102015-07-09 19:45:09 +000039import org.apache.maven.artifact.Artifact;
40import org.apache.maven.artifact.DefaultArtifact;
41import org.apache.maven.artifact.handler.ArtifactHandler;
42import org.apache.maven.artifact.handler.DefaultArtifactHandler;
43import org.apache.maven.artifact.versioning.VersionRange;
Guillaume Nodet09157a92010-03-10 17:40:45 +000044import org.apache.maven.model.Resource;
Guillaume Nodeted7b1102015-07-09 19:45:09 +000045import org.apache.maven.plugin.testing.AbstractMojoTestCase;
Guillaume Nodet09157a92010-03-10 17:40:45 +000046import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
Guillaume Nodeted7b1102015-07-09 19:45:09 +000047import org.apache.maven.project.DefaultProjectBuilderConfiguration;
48import org.apache.maven.project.ProjectBuilderConfiguration;
49import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
50import org.apache.maven.shared.dependency.graph.DependencyNode;
Guillaume Nodet09157a92010-03-10 17:40:45 +000051import org.osgi.framework.Constants;
52
Stuart McCulloch42151ee2012-07-16 13:43:38 +000053import aQute.bnd.osgi.Builder;
Stuart McCullochd98b02e2011-06-28 23:20:37 +000054
55
Guillaume Nodeted7b1102015-07-09 19:45:09 +000056public class BlueprintComponentTest extends AbstractMojoTestCase
Stuart McCullochd98b02e2011-06-28 23:20:37 +000057{
Guillaume Nodet09157a92010-03-10 17:40:45 +000058
Guillaume Nodet1c9211a2014-05-20 08:56:13 +000059 public void testBlueprintServices() throws Exception
60 {
Guillaume Nodetda4289b2014-06-17 07:55:31 +000061 test( "service" );
Guillaume Nodet1c9211a2014-05-20 08:56:13 +000062 }
63
64 public void testBlueprintGeneric() throws Exception
65 {
66 test( "generic" );
67 }
68
69 protected void test(String mode) throws Exception
Guillaume Nodet09157a92010-03-10 17:40:45 +000070 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +000071 MavenProjectStub project = new MavenProjectStub()
72 {
Guillaume Nodet09157a92010-03-10 17:40:45 +000073 private final List resources = new ArrayList();
Stuart McCullochd98b02e2011-06-28 23:20:37 +000074
Guillaume Nodet09157a92010-03-10 17:40:45 +000075
76 @Override
Stuart McCullochd98b02e2011-06-28 23:20:37 +000077 public void addResource( Resource resource )
78 {
79 resources.add( resource );
80 }
81
82
83 @Override
84 public List getResources()
85 {
Guillaume Nodet09157a92010-03-10 17:40:45 +000086 return resources;
87 }
Stuart McCulloch5c9b67d02011-06-28 16:06:24 +000088
Stuart McCullochd98b02e2011-06-28 23:20:37 +000089
Stuart McCulloch5c9b67d02011-06-28 16:06:24 +000090 @Override
91 public File getBasedir()
92 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +000093 return new File( "target/tmp/basedir" );
Stuart McCulloch5c9b67d02011-06-28 16:06:24 +000094 }
Guillaume Nodet09157a92010-03-10 17:40:45 +000095 };
Guillaume Nodeted7b1102015-07-09 19:45:09 +000096 project.setGroupId("group");
Guillaume Nodet09157a92010-03-10 17:40:45 +000097 project.setArtifactId( "artifact" );
98 project.setVersion( "1.1.0.0" );
Guillaume Nodeted7b1102015-07-09 19:45:09 +000099 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
Guillaume Nodet09157a92010-03-10 17:40:45 +0000108 Resource r = new Resource();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000109 r.setDirectory( new File( "src/test/resources" ).getAbsoluteFile().getCanonicalPath() );
Guillaume Nodeted7b1102015-07-09 19:45:09 +0000110 r.setIncludes(Arrays.asList("**/*.*"));
111 project.addResource(r);
112 project.addCompileSourceRoot(new File("src/test/resources").getAbsoluteFile().getCanonicalPath());
Guillaume Nodet09157a92010-03-10 17:40:45 +0000113
114 ManifestPlugin plugin = new ManifestPlugin();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000115 plugin.setBuildDirectory( "target/tmp/basedir/target" );
Guillaume Nodeted7b1102015-07-09 19:45:09 +0000116 plugin.setOutputDirectory(new File("target/tmp/basedir/target/classes"));
117 setVariableValueToObject(plugin, "m_dependencyGraphBuilder", lookup(DependencyGraphBuilder.class.getName(), "default"));
Guillaume Nodet09157a92010-03-10 17:40:45 +0000118
119 Map instructions = new HashMap();
Guillaume Nodet1c9211a2014-05-20 08:56:13 +0000120 instructions.put( "service_mode", mode );
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000121 instructions.put( "Test", "Foo" );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000122
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000123 instructions.put( "nsh_interface", "foo.bar.Namespace" );
124 instructions.put( "nsh_namespace", "ns" );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000125
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000126 instructions.put( "Export-Service", "p7.Foo;mk=mv" );
127 instructions.put( "Import-Service", "org.osgi.service.cm.ConfigurationAdmin;availability:=optional" );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000128
Guillaume Nodet09157a92010-03-10 17:40:45 +0000129 Properties props = new Properties();
Guillaume Nodeted7b1102015-07-09 19:45:09 +0000130 DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
131 Builder builder = plugin.buildOSGiBundle( project, dependencyGraph, instructions, props, plugin.getClasspath( project, dependencyGraph ) );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000132
133 Manifest manifest = builder.getJar().getManifest();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000134 String expSvc = manifest.getMainAttributes().getValue( Constants.EXPORT_SERVICE );
135 String impSvc = manifest.getMainAttributes().getValue( Constants.IMPORT_SERVICE );
136 assertNotNull( expSvc );
137 assertNotNull( impSvc );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000138
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000139 String impPkg = manifest.getMainAttributes().getValue( Constants.IMPORT_PACKAGE );
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000140 List<String> pkgs = Create.list();
141 for (Clause clause : Parser.parseHeader(impPkg))
142 {
143 pkgs.add(clause.getName());
144 }
Guillaume Nodet1c9211a2014-05-20 08:56:13 +0000145 for ( int i = 1; i <= 14; i++ )
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000146 {
147 assertTrue( pkgs.contains( "p" + i ) );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000148 }
Guillaume Nodet1c9211a2014-05-20 08:56:13 +0000149
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();
Guillaume Nodet09157a92010-03-10 17:40:45 +0000163 }
164
165}