View Javadoc
1   package org.apache.felix.bundleplugin;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   * http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  import java.io.File;
24  import java.util.Collections;
25  import java.util.Map;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.DefaultArtifact;
29  import org.apache.maven.artifact.handler.ArtifactHandler;
30  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
31  import org.apache.maven.artifact.versioning.VersionRange;
32  import org.apache.maven.plugin.testing.stubs.ArtifactStub;
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
35  
36  
37  /**
38   * Test for {@link BundleAllPlugin}
39   *
40   * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
41   */
42  public class BundleAllPluginTest extends AbstractBundlePluginTest
43  {
44  
45      private BundleAllPlugin plugin;
46  
47  
48      @Override
49      protected void setUp() throws Exception
50      {
51          super.setUp();
52          init();
53      }
54  
55  
56      private void init() throws Exception
57      {
58          plugin = new BundleAllPlugin();
59          File baseDirectory = new File( getBasedir() );
60          File buildDirectory = new File( baseDirectory, "target" );
61          plugin.setBuildDirectory( buildDirectory.getPath() );
62          File outputDirectory = new File( buildDirectory, "test-classes" );
63          plugin.setOutputDirectory( outputDirectory );
64          setVariableValueToObject(plugin, "m_dependencyGraphBuilder", lookup(DependencyGraphBuilder.class.getName(), "default"));
65      }
66  
67  
68      public void testSnapshotMatch()
69      {
70          ArtifactStub artifact = getArtifactStub();
71          String bundleName;
72  
73          artifact.setVersion( "2.1-SNAPSHOT" );
74          bundleName = "group.artifact_2.1.0.20070207_193904_2.jar";
75  
76          assertTrue( plugin.snapshotMatch( artifact, bundleName ) );
77  
78          artifact.setVersion( "2-SNAPSHOT" );
79          assertFalse( plugin.snapshotMatch( artifact, bundleName ) );
80  
81          artifact.setArtifactId( "artifactx" );
82          artifact.setVersion( "2.1-SNAPSHOT" );
83          assertFalse( plugin.snapshotMatch( artifact, bundleName ) );
84      }
85  
86  
87      public void testNoReBundling() throws Exception
88      {
89          File testFile = getTestFile( "target/test-classes/org.apache.maven.maven-model_1.0.0.0.jar" );
90          if ( testFile.exists() )
91          {
92              testFile.delete();
93          }
94  
95          VersionRange versionRange = VersionRange.createFromVersion("1.0.0.0");
96          ArtifactHandler artifactHandler = new DefaultArtifactHandler( "jar" );
97          Artifact artifact = new DefaultArtifact("group","artifact",versionRange, null, "jar", null, artifactHandler);
98  
99          MavenProject project = getMavenProjectStub();
100         project.setGroupId( artifact.getGroupId() );
101         project.setArtifactId( artifact.getArtifactId() );
102         project.setVersion( artifact.getVersion() );
103         project.setArtifact( artifact );
104         project.setArtifacts( Collections.EMPTY_SET );
105         project.setDependencyArtifacts( Collections.EMPTY_SET );
106         File bundleFile = getTestFile( "src/test/resources/org.apache.maven.maven-model_2.1.0.SNAPSHOT.jar" );
107         artifact.setFile( bundleFile );
108 
109         BundleInfo bundleInfo = plugin.bundle( project );
110 
111         Map exports = bundleInfo.getExportedPackages();
112         String[] packages = new String[]
113             { "org.apache.maven.model.io.jdom", "org.apache.maven.model" };
114 
115         for ( int i = 0; i < packages.length; i++ )
116         {
117             assertTrue( "Bundle info does not contain a package that it is  exported in the manifest: " + packages[i],
118                 exports.containsKey( packages[i] ) );
119         }
120 
121         assertFalse( "Bundle info contains a package that it is not exported in the manifest",
122             exports.containsKey( "org.apache.maven.model.io.xpp3" ) );
123     }
124 
125     //    public void testRewriting()
126     //        throws Exception
127     //    {
128     //
129     //        MavenProjectStub project = new MavenProjectStub();
130     //        project.setArtifact( getArtifactStub() );
131     //        project.getArtifact().setFile( getTestBundle() );
132     //        project.setDependencyArtifacts( Collections.EMPTY_SET );
133     //        project.setVersion( project.getArtifact().getVersion() );
134     //
135     //        File output = new File( plugin.getBuildDirectory(), plugin.getBundleName( project ) );
136     //        boolean delete = output.delete();
137     //
138     //        plugin.bundle( project );
139     //
140     //        init();
141     //        try
142     //        {
143     //            plugin.bundle( project );
144     //            fail();
145     //        }
146     //        catch ( RuntimeException e )
147     //        {
148     //            // expected
149     //        }
150     //    }
151 }