View Javadoc

1   package org.apache.maven.plugins.shade;
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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.LinkedHashSet;
26  import java.util.List;
27  import java.util.Set;
28  import java.net.URLClassLoader;
29  import java.net.URL;
30  
31  import junit.framework.TestCase;
32  
33  import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
34  import org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer;
35  import org.codehaus.plexus.logging.Logger;
36  import org.codehaus.plexus.logging.console.ConsoleLogger;
37  
38  /**
39   * @author Jason van Zyl
40   * @author Mauro Talevi
41   */
42  public class DefaultShaderTest
43      extends TestCase
44  {
45      private static final String[] EXCLUDES = new String[] {
46          "org/codehaus/plexus/util/xml/Xpp3Dom",
47          "org/codehaus/plexus/util/xml/pull.*" };
48  
49      public void testShaderWithDefaultShadedPattern()
50          throws Exception
51      {
52          shaderWithPattern( null, new File( "target/foo-default.jar" ), EXCLUDES );
53      }
54  
55      public void testShaderWithStaticInitializedClass()
56          throws Exception
57      {
58          Shader s = newShader();
59  
60          Set set = new LinkedHashSet();
61  
62          set.add( new File( "src/test/jars/test-artifact-1.0-SNAPSHOT.jar" ) );
63  
64          List relocators = new ArrayList();
65  
66          relocators.add( new SimpleRelocator( "org.apache.maven.plugins.shade", null, null, null ) );
67  
68          List resourceTransformers = new ArrayList();
69  
70          List filters = new ArrayList();
71  
72          File file = new File( "target/testShaderWithStaticInitializedClass.jar" );
73          s.shade( set, file, filters, relocators, resourceTransformers );
74  
75          URLClassLoader cl = new URLClassLoader( new URL[]{file.toURI().toURL()} );
76          Class c = cl.loadClass( "hidden.org.apache.maven.plugins.shade.Lib" );
77          Object o = c.newInstance();
78          assertEquals( "foo.bar/baz", c.getDeclaredField( "CONSTANT" ).get( o ) );
79      }
80  
81      public void testShaderWithCustomShadedPattern()
82          throws Exception
83      {
84          shaderWithPattern( "org/shaded/plexus/util", new File( "target/foo-custom.jar" ), EXCLUDES );
85      }
86  
87      public void testShaderWithoutExcludesShouldRemoveReferencesOfOriginalPattern()
88          throws Exception
89      {
90          //FIXME:  shaded jar should not include references to org/codehaus/* (empty dirs) or org.codehaus.* META-INF files.
91          shaderWithPattern( "org/shaded/plexus/util", new File( "target/foo-custom-without-excludes.jar" ), new String[] {} );
92      }
93  
94      public void shaderWithPattern( String shadedPattern, File jar, String[] excludes )
95          throws Exception
96      {
97          DefaultShader s = newShader();
98  
99          Set set = new LinkedHashSet();
100 
101         set.add( new File( "src/test/jars/test-project-1.0-SNAPSHOT.jar" ) );
102 
103         set.add( new File( "src/test/jars/plexus-utils-1.4.1.jar" ) );
104 
105         List relocators = new ArrayList();
106 
107         relocators.add( new SimpleRelocator( "org/codehaus/plexus/util", shadedPattern, null, Arrays.asList( excludes ) ) );
108 
109         List resourceTransformers = new ArrayList();
110 
111         resourceTransformers.add( new ComponentsXmlResourceTransformer() );
112 
113         List filters = new ArrayList();
114 
115         s.shade( set, jar, filters, relocators, resourceTransformers );
116     }
117 
118     private static DefaultShader newShader()
119     {
120         DefaultShader s = new DefaultShader();
121 
122         s.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "TEST" ) );
123 
124         return s;
125     }
126 
127 }