View Javadoc

1   package org.apache.maven.plugins.shade.mojo;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.DefaultArtifact;
24  import org.apache.maven.artifact.factory.ArtifactFactory;
25  import org.apache.maven.artifact.handler.ArtifactHandler;
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
28  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
29  import org.apache.maven.artifact.resolver.ArtifactResolver;
30  import org.apache.maven.artifact.resolver.DefaultArtifactResolver;
31  import org.apache.maven.artifact.versioning.VersionRange;
32  import org.apache.maven.plugins.shade.Shader;
33  import org.apache.maven.plugins.shade.filter.Filter;
34  import org.apache.maven.plugins.shade.relocation.Relocator;
35  import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
36  import org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer;
37  import org.apache.maven.project.MavenProject;
38  import org.codehaus.plexus.PlexusTestCase;
39  
40  import java.io.File;
41  import java.lang.reflect.Field;
42  import java.lang.reflect.Method;
43  import java.net.URL;
44  import java.net.URLClassLoader;
45  import java.util.ArrayList;
46  import java.util.Arrays;
47  import java.util.LinkedHashSet;
48  import java.util.List;
49  import java.util.Set;
50  
51  /**
52   * @author Jason van Zyl
53   * @author Mauro Talevi
54   */
55  public class ShadeMojoTest
56      extends PlexusTestCase
57  {
58      public void testShaderWithDefaultShadedPattern()
59          throws Exception
60      {
61          shaderWithPattern( null, new File( "target/foo-default.jar" ) );
62      }
63  
64      public void testShaderWithCustomShadedPattern()
65          throws Exception
66      {
67          shaderWithPattern( "org/shaded/plexus/util", new File( "target/foo-custom.jar" ) );
68      }
69  
70      public void testShaderWithExclusions()
71          throws Exception
72      {
73          File jarFile = new File( getBasedir(), "target/unit/foo-bar.jar" );
74  
75          Shader s = (Shader) lookup( Shader.ROLE, "default" );
76  
77          Set set = new LinkedHashSet();
78          set.add( new File( getBasedir(), "src/test/jars/test-artifact-1.0-SNAPSHOT.jar" ) );
79  
80          List<Relocator> relocators = new ArrayList<Relocator>();
81          relocators.add( new SimpleRelocator( "org.codehaus.plexus.util", "hidden", null, Arrays.asList(
82              new String[]{ "org.codehaus.plexus.util.xml.Xpp3Dom", "org.codehaus.plexus.util.xml.pull.*" } ) ) );
83  
84          List resourceTransformers = new ArrayList();
85  
86          List filters = new ArrayList();
87  
88          s.shade( set, jarFile, filters, relocators, resourceTransformers );
89  
90          ClassLoader cl = new URLClassLoader( new URL[]{ jarFile.toURI().toURL() } );
91          Class c = cl.loadClass( "org.apache.maven.plugins.shade.Lib" );
92  
93          Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" );
94          assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) );
95  
96          Method method = c.getDeclaredMethod( "getClassRealmPackageImport", new Class[0] );
97          assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null, new Object[0] ) );
98      }
99  
100     /**
101      * Tests if a Filter is installed correctly, also if createSourcesJar is set to true.
102      *
103      * @throws Exception
104      */
105     public void testShadeWithFilter()
106         throws Exception
107     {
108         ShadeMojo mojo = new ShadeMojo();
109 
110         // set createSourcesJar = true
111         Field createSourcesJar = ShadeMojo.class.getDeclaredField( "createSourcesJar" );
112         createSourcesJar.setAccessible( true );
113         createSourcesJar.set( mojo, Boolean.TRUE );
114 
115         // configure artifactFactory for mojo
116         ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
117         Field artifactFactoryField = ShadeMojo.class.getDeclaredField( "artifactFactory" );
118         artifactFactoryField.setAccessible( true );
119         artifactFactoryField.set( mojo, artifactFactory );
120 
121         // configure artifactResolver (mocked) for mojo
122         ArtifactResolver mockArtifactResolver = new DefaultArtifactResolver()
123         {
124 
125             public void resolve( Artifact artifact, List remoteRepos, ArtifactRepository repo )
126                 throws ArtifactResolutionException, ArtifactNotFoundException
127             {
128                 // artifact is resolved
129                 artifact.setResolved( true );
130 
131                 // set file
132                 artifact.setFile( new File(
133                     artifact.getArtifactId() + "-" + artifact.getVersion() + ( artifact.getClassifier() != null ? "-"
134                         + artifact.getClassifier() : "" ) + ".jar" ) );
135             }
136 
137         };
138         Field artifactResolverField = ShadeMojo.class.getDeclaredField( "artifactResolver" );
139         artifactResolverField.setAccessible( true );
140         artifactResolverField.set( mojo, mockArtifactResolver );
141 
142         // create and configure MavenProject
143         MavenProject project = new MavenProject();
144         ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE );
145         Artifact artifact = new DefaultArtifact( "org.apache.myfaces.core", "myfaces-impl",
146                                                  VersionRange.createFromVersion( "2.0.1-SNAPSHOT" ), "compile", "jar",
147                                                  null, artifactHandler );
148         mockArtifactResolver.resolve( artifact, null, null ); // setFile and setResolved
149         project.setArtifact( artifact );
150         Field projectField = ShadeMojo.class.getDeclaredField( "project" );
151         projectField.setAccessible( true );
152         projectField.set( mojo, project );
153 
154         // create and configure the ArchiveFilter
155         ArchiveFilter archiveFilter = new ArchiveFilter();
156         Field archiveFilterArtifact = ArchiveFilter.class.getDeclaredField( "artifact" );
157         archiveFilterArtifact.setAccessible( true );
158         archiveFilterArtifact.set( archiveFilter, "org.apache.myfaces.core:myfaces-impl" );
159 
160         // add ArchiveFilter to mojo
161         Field filtersField = ShadeMojo.class.getDeclaredField( "filters" );
162         filtersField.setAccessible( true );
163         filtersField.set( mojo, new ArchiveFilter[]{ archiveFilter } );
164 
165         // invoke getFilters()
166         Method getFilters = ShadeMojo.class.getDeclaredMethod( "getFilters", new Class[0] );
167         getFilters.setAccessible( true );
168         List filters = (List) getFilters.invoke( mojo, new Object[0] );
169 
170         // assertions - there must be one filter
171         assertEquals( 1, filters.size() );
172 
173         // the filter must be able to filter the binary and the sources jar
174         Filter filter = (Filter) filters.get( 0 );
175         assertTrue( filter.canFilter( new File( "myfaces-impl-2.0.1-SNAPSHOT.jar" ) ) ); // binary jar
176         assertTrue( filter.canFilter( new File( "myfaces-impl-2.0.1-SNAPSHOT-sources.jar" ) ) ); // sources jar
177     }
178 
179     public void shaderWithPattern( String shadedPattern, File jar )
180         throws Exception
181     {
182         Shader s = (Shader) lookup( Shader.ROLE );
183 
184         Set set = new LinkedHashSet();
185 
186         set.add( new File( getBasedir(), "src/test/jars/test-project-1.0-SNAPSHOT.jar" ) );
187 
188         set.add( new File( getBasedir(), "src/test/jars/plexus-utils-1.4.1.jar" ) );
189 
190         List relocators = new ArrayList();
191 
192         relocators.add( new SimpleRelocator( "org/codehaus/plexus/util", shadedPattern, null, Arrays.asList(
193             new String[]{ "org/codehaus/plexus/util/xml/Xpp3Dom", "org/codehaus/plexus/util/xml/pull.*" } ) ) );
194 
195         List resourceTransformers = new ArrayList();
196 
197         resourceTransformers.add( new ComponentsXmlResourceTransformer() );
198 
199         List filters = new ArrayList();
200 
201         s.shade( set, jar, filters, relocators, resourceTransformers );
202     }
203 
204 }