Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 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 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 21 | |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 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; |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 29 | import java.util.jar.Manifest; |
| 30 | |
Guillaume Nodet | 1c9211a | 2014-05-20 08:56:13 +0000 | [diff] [blame] | 31 | import aQute.bnd.osgi.Analyzer; |
| 32 | import aQute.bnd.osgi.Jar; |
| 33 | import aQute.bnd.osgi.Verifier; |
Guillaume Nodet | 7bf509f | 2014-05-19 07:12:48 +0000 | [diff] [blame] | 34 | import aQute.libg.generics.Create; |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 35 | import junit.framework.TestCase; |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 36 | |
Guillaume Nodet | 7bf509f | 2014-05-19 07:12:48 +0000 | [diff] [blame] | 37 | import org.apache.felix.utils.manifest.Clause; |
| 38 | import org.apache.felix.utils.manifest.Parser; |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 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; |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 44 | import org.apache.maven.model.Resource; |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 45 | import org.apache.maven.plugin.testing.AbstractMojoTestCase; |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 46 | import org.apache.maven.plugin.testing.stubs.MavenProjectStub; |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 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; |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 51 | import org.osgi.framework.Constants; |
| 52 | |
Stuart McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 53 | import aQute.bnd.osgi.Builder; |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 54 | |
| 55 | |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 56 | public class BlueprintComponentTest extends AbstractMojoTestCase |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 57 | { |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 58 | |
Guillaume Nodet | 1c9211a | 2014-05-20 08:56:13 +0000 | [diff] [blame] | 59 | public void testBlueprintServices() throws Exception |
| 60 | { |
Guillaume Nodet | da4289b | 2014-06-17 07:55:31 +0000 | [diff] [blame] | 61 | test( "service" ); |
Guillaume Nodet | 1c9211a | 2014-05-20 08:56:13 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public void testBlueprintGeneric() throws Exception |
| 65 | { |
| 66 | test( "generic" ); |
| 67 | } |
| 68 | |
| 69 | protected void test(String mode) throws Exception |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 70 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 71 | MavenProjectStub project = new MavenProjectStub() |
| 72 | { |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 73 | private final List resources = new ArrayList(); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 74 | |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 75 | |
| 76 | @Override |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 77 | public void addResource( Resource resource ) |
| 78 | { |
| 79 | resources.add( resource ); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | @Override |
| 84 | public List getResources() |
| 85 | { |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 86 | return resources; |
| 87 | } |
Stuart McCulloch | 5c9b67d0 | 2011-06-28 16:06:24 +0000 | [diff] [blame] | 88 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 89 | |
Stuart McCulloch | 5c9b67d0 | 2011-06-28 16:06:24 +0000 | [diff] [blame] | 90 | @Override |
| 91 | public File getBasedir() |
| 92 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 93 | return new File( "target/tmp/basedir" ); |
Stuart McCulloch | 5c9b67d0 | 2011-06-28 16:06:24 +0000 | [diff] [blame] | 94 | } |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 95 | }; |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 96 | project.setGroupId("group"); |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 97 | project.setArtifactId( "artifact" ); |
| 98 | project.setVersion( "1.1.0.0" ); |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 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 | |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 108 | Resource r = new Resource(); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 109 | r.setDirectory( new File( "src/test/resources" ).getAbsoluteFile().getCanonicalPath() ); |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 110 | r.setIncludes(Arrays.asList("**/*.*")); |
| 111 | project.addResource(r); |
| 112 | project.addCompileSourceRoot(new File("src/test/resources").getAbsoluteFile().getCanonicalPath()); |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 113 | |
| 114 | ManifestPlugin plugin = new ManifestPlugin(); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 115 | plugin.setBuildDirectory( "target/tmp/basedir/target" ); |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 116 | plugin.setOutputDirectory(new File("target/tmp/basedir/target/classes")); |
| 117 | setVariableValueToObject(plugin, "m_dependencyGraphBuilder", lookup(DependencyGraphBuilder.class.getName(), "default")); |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 118 | |
| 119 | Map instructions = new HashMap(); |
Guillaume Nodet | 1c9211a | 2014-05-20 08:56:13 +0000 | [diff] [blame] | 120 | instructions.put( "service_mode", mode ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 121 | instructions.put( "Test", "Foo" ); |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 122 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 123 | instructions.put( "nsh_interface", "foo.bar.Namespace" ); |
| 124 | instructions.put( "nsh_namespace", "ns" ); |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 125 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 126 | instructions.put( "Export-Service", "p7.Foo;mk=mv" ); |
| 127 | instructions.put( "Import-Service", "org.osgi.service.cm.ConfigurationAdmin;availability:=optional" ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 128 | |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 129 | Properties props = new Properties(); |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 130 | DependencyNode dependencyGraph = plugin.buildDependencyGraph(project); |
| 131 | Builder builder = plugin.buildOSGiBundle( project, dependencyGraph, instructions, props, plugin.getClasspath( project, dependencyGraph ) ); |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 132 | |
| 133 | Manifest manifest = builder.getJar().getManifest(); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 134 | String expSvc = manifest.getMainAttributes().getValue( Constants.EXPORT_SERVICE ); |
| 135 | String impSvc = manifest.getMainAttributes().getValue( Constants.IMPORT_SERVICE ); |
| 136 | assertNotNull( expSvc ); |
| 137 | assertNotNull( impSvc ); |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 138 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 139 | String impPkg = manifest.getMainAttributes().getValue( Constants.IMPORT_PACKAGE ); |
Guillaume Nodet | 7bf509f | 2014-05-19 07:12:48 +0000 | [diff] [blame] | 140 | List<String> pkgs = Create.list(); |
| 141 | for (Clause clause : Parser.parseHeader(impPkg)) |
| 142 | { |
| 143 | pkgs.add(clause.getName()); |
| 144 | } |
Guillaume Nodet | 1c9211a | 2014-05-20 08:56:13 +0000 | [diff] [blame] | 145 | for ( int i = 1; i <= 14; i++ ) |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 146 | { |
| 147 | assertTrue( pkgs.contains( "p" + i ) ); |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 148 | } |
Guillaume Nodet | 1c9211a | 2014-05-20 08:56:13 +0000 | [diff] [blame] | 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(); |
Guillaume Nodet | 09157a9 | 2010-03-10 17:40:45 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | } |