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