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