1 package org.apache.maven.plugins.shade.mojo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
53
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
102
103
104
105 public void testShadeWithFilter()
106 throws Exception
107 {
108 ShadeMojo mojo = new ShadeMojo();
109
110
111 Field createSourcesJar = ShadeMojo.class.getDeclaredField( "createSourcesJar" );
112 createSourcesJar.setAccessible( true );
113 createSourcesJar.set( mojo, Boolean.TRUE );
114
115
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
122 ArtifactResolver mockArtifactResolver = new DefaultArtifactResolver()
123 {
124
125 public void resolve( Artifact artifact, List remoteRepos, ArtifactRepository repo )
126 throws ArtifactResolutionException, ArtifactNotFoundException
127 {
128
129 artifact.setResolved( true );
130
131
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
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 );
149 project.setArtifact( artifact );
150 Field projectField = ShadeMojo.class.getDeclaredField( "project" );
151 projectField.setAccessible( true );
152 projectField.set( mojo, project );
153
154
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
161 Field filtersField = ShadeMojo.class.getDeclaredField( "filters" );
162 filtersField.setAccessible( true );
163 filtersField.set( mojo, new ArchiveFilter[]{ archiveFilter } );
164
165
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
171 assertEquals( 1, filters.size() );
172
173
174 Filter filter = (Filter) filters.get( 0 );
175 assertTrue( filter.canFilter( new File( "myfaces-impl-2.0.1-SNAPSHOT.jar" ) ) );
176 assertTrue( filter.canFilter( new File( "myfaces-impl-2.0.1-SNAPSHOT-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 }