    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 junit.framework.TestCase;
    32
    33  import org.apache.maven.model.Resource;
    34  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
    35  import org.osgi.framework.Constants;
    36
    37  import aQute.bnd.osgi.Builder;
    38
    39
    40  public class BlueprintComponentTest extends TestCase
    41  {
    42
    43      public void testBlueprint() throws Exception
    44      {
    45          MavenProjectStub project = new MavenProjectStub()
    46          {
    47              private final List resources = new ArrayList();
    48
    49
    50              @Override
    51              public void addResource( Resource resource )
    52              {
    53                  resources.add( resource );
    54              }
    55
    56
    57              @Override
    58              public List getResources()
    59              {
    60                  return resources;
    61              }
    62
    63
    64              @Override
    65              public File getBasedir()
    66              {
    67                  return new File( "target/tmp/basedir" );
    68              }
    69          };
    70          project.setGroupId( "group" );
    71          project.setArtifactId( "artifact" );
    72          project.setVersion( "1.1.0.0" );
    73          Resource r = new Resource();
    74          r.setDirectory( new File( "src/test/resources" ).getAbsoluteFile().getCanonicalPath() );
    75          r.setIncludes( Arrays.asList( "**/*.*" ) );
    76          project.addResource( r );
    77          project.addCompileSourceRoot( new File( "src/test/resources" ).getAbsoluteFile().getCanonicalPath() );
    78
    79          ManifestPlugin plugin = new ManifestPlugin();
    80          plugin.setBuildDirectory( "target/tmp/basedir/target" );
    81          plugin.setOutputDirectory( new File( "target/tmp/basedir/target/classes" ) );
    82
    83          Map instructions = new HashMap();
    84          instructions.put( "Test", "Foo" );
    85
    86          instructions.put( "nsh_interface", "foo.bar.Namespace" );
    87          instructions.put( "nsh_namespace", "ns" );
    88
    89          instructions.put( "Export-Service", "p7.Foo;mk=mv" );
    90          instructions.put( "Import-Service", "org.osgi.service.cm.ConfigurationAdmin;availability:=optional" );
    91
    92          Properties props = new Properties();
    93          Builder builder = plugin.buildOSGiBundle( project, instructions, props, plugin.getClasspath( project ) );
    94
    95          Manifest manifest = builder.getJar().getManifest();
    96          String expSvc = manifest.getMainAttributes().getValue( Constants.EXPORT_SERVICE );
    97          String impSvc = manifest.getMainAttributes().getValue( Constants.IMPORT_SERVICE );
    98          assertNotNull( expSvc );
    99          assertNotNull( impSvc );
    100
    101         String impPkg = manifest.getMainAttributes().getValue( Constants.IMPORT_PACKAGE );
    102         List<String> pkgs = Arrays.asList( impPkg.split( "," ) );
    103         for ( int i = 1; i <= 13; i++ )
    104         {
    105             assertTrue( pkgs.contains( "p" + i ) );
    106         }
    107     }
    108
    109 }