View Javadoc
1   package org.apache.maven.plugins.shade.relocation;
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  
23  import junit.framework.TestCase;
24  
25  import java.util.Arrays;
26  import java.util.Collections;
27  
28  /**
29   * Test for {@link SimpleRelocator}.
30   *
31   * @author Benjamin Bentmann
32   *
33   */
34  public class SimpleRelocatorTest
35      extends TestCase
36  {
37  
38      public void testCanRelocatePath()
39      {
40          SimpleRelocator relocator;
41  
42          relocator = new SimpleRelocator( "org.foo", null, null, null );
43          assertTrue( relocator.canRelocatePath( "org/foo/Class" ) );
44          assertTrue( relocator.canRelocatePath( "org/foo/Class.class" ) );
45          assertTrue( relocator.canRelocatePath( "org/foo/bar/Class" ) );
46          assertTrue( relocator.canRelocatePath( "org/foo/bar/Class.class" ) );
47          assertFalse( relocator.canRelocatePath( "com/foo/bar/Class" ) );
48          assertFalse( relocator.canRelocatePath( "com/foo/bar/Class.class" ) );
49          assertFalse( relocator.canRelocatePath( "org/Foo/Class" ) );
50          assertFalse( relocator.canRelocatePath( "org/Foo/Class.class" ) );
51  
52          relocator = new SimpleRelocator( "org.foo", null, null, Arrays.asList(
53                  "org.foo.Excluded", "org.foo.public.*", "org.foo.Public*Stuff") );
54          assertTrue( relocator.canRelocatePath( "org/foo/Class" ) );
55          assertTrue( relocator.canRelocatePath( "org/foo/Class.class" ) );
56          assertTrue( relocator.canRelocatePath( "org/foo/excluded" ) );
57          assertFalse( relocator.canRelocatePath( "org/foo/Excluded" ) );
58          assertFalse( relocator.canRelocatePath( "org/foo/Excluded.class" ) );
59          assertFalse( relocator.canRelocatePath( "org/foo/public" ) );
60          assertFalse( relocator.canRelocatePath( "org/foo/public/Class" ) );
61          assertFalse( relocator.canRelocatePath( "org/foo/public/Class.class" ) );
62          assertTrue( relocator.canRelocatePath( "org/foo/publicRELOC/Class" ) );
63          assertTrue( relocator.canRelocatePath( "org/foo/PrivateStuff" ) );
64          assertTrue( relocator.canRelocatePath( "org/foo/PrivateStuff.class" ) );
65          assertFalse( relocator.canRelocatePath( "org/foo/PublicStuff" ) );
66          assertFalse( relocator.canRelocatePath( "org/foo/PublicStuff.class" ) );
67          assertFalse( relocator.canRelocatePath( "org/foo/PublicUtilStuff" ) );
68          assertFalse( relocator.canRelocatePath( "org/foo/PublicUtilStuff.class" ) );
69      }
70  
71      public void testCanRelocateClass()
72      {
73          SimpleRelocator relocator;
74  
75          relocator = new SimpleRelocator( "org.foo", null, null, null );
76          assertTrue( relocator.canRelocateClass( "org.foo.Class" ) );
77          assertTrue( relocator.canRelocateClass( "org.foo.bar.Class" ) );
78          assertFalse( relocator.canRelocateClass( "com.foo.bar.Class" ) );
79          assertFalse( relocator.canRelocateClass( "org.Foo.Class" ) );
80  
81          relocator = new SimpleRelocator( "org.foo", null, null, Arrays.asList(
82                  "org.foo.Excluded", "org.foo.public.*", "org.foo.Public*Stuff") );
83          assertTrue( relocator.canRelocateClass( "org.foo.Class" ) );
84          assertTrue( relocator.canRelocateClass( "org.foo.excluded" ) );
85          assertFalse( relocator.canRelocateClass( "org.foo.Excluded" ) );
86          assertFalse( relocator.canRelocateClass( "org.foo.public" ) );
87          assertFalse( relocator.canRelocateClass( "org.foo.public.Class" ) );
88          assertTrue( relocator.canRelocateClass( "org.foo.publicRELOC.Class" ) );
89          assertTrue( relocator.canRelocateClass( "org.foo.PrivateStuff" ) );
90          assertFalse( relocator.canRelocateClass( "org.foo.PublicStuff" ) );
91          assertFalse( relocator.canRelocateClass( "org.foo.PublicUtilStuff" ) );
92      }
93  
94      public void testCanRelocateRawString()
95      {
96          SimpleRelocator relocator;
97  
98          relocator = new SimpleRelocator( "org/foo", null, null, null, true );
99          assertTrue( relocator.canRelocatePath( "(I)org/foo/bar/Class;" ) );
100         
101         relocator = new SimpleRelocator( "^META-INF/org.foo.xml$", null, null, null, true );
102         assertTrue( relocator.canRelocatePath( "META-INF/org.foo.xml" ) );
103     }
104 
105     //MSHADE-119, make sure that the easy part of this works.
106     public void testCanRelocateAbsClassPath() 
107     {
108         SimpleRelocator relocator = new SimpleRelocator( "org.apache.velocity", "org.apache.momentum", null, null );
109         assertEquals("/org/apache/momentum/mass.properties", relocator.relocatePath( "/org/apache/velocity/mass.properties" ) );
110     }
111 
112     public void testCanRelocateAbsClassPathWithExcludes()
113     {
114         SimpleRelocator relocator = new SimpleRelocator( "org/apache/velocity", "org/apache/momentum", null,
115                                                          Arrays.asList( "org/apache/velocity/excluded/*" ) );
116         assertTrue( relocator.canRelocatePath( "/org/apache/velocity/mass.properties" ) );
117         assertTrue( relocator.canRelocatePath( "org/apache/velocity/mass.properties" ) );
118         assertFalse( relocator.canRelocatePath( "/org/apache/velocity/excluded/mass.properties" ) );
119         assertFalse( relocator.canRelocatePath( "org/apache/velocity/excluded/mass.properties" ) );
120     }
121 
122     public void testCanRelocateAbsClassPathWithIncludes()
123     {
124         SimpleRelocator relocator = new SimpleRelocator( "org/apache/velocity", "org/apache/momentum",
125                                                          Arrays.asList( "org/apache/velocity/included/*" ), null );
126         assertFalse( relocator.canRelocatePath( "/org/apache/velocity/mass.properties" ) );
127         assertFalse( relocator.canRelocatePath( "org/apache/velocity/mass.properties" ) );
128         assertTrue( relocator.canRelocatePath( "/org/apache/velocity/included/mass.properties" ) );
129         assertTrue( relocator.canRelocatePath( "org/apache/velocity/included/mass.properties" ) );
130     }
131 
132     public void testRelocatePath()
133     {
134         SimpleRelocator relocator;
135 
136         relocator = new SimpleRelocator( "org.foo", null, null, null );
137         assertEquals( "hidden/org/foo/bar/Class.class", relocator.relocatePath( "org/foo/bar/Class.class" ) );
138 
139         relocator = new SimpleRelocator( "org.foo", "private.stuff", null, null );
140         assertEquals( "private/stuff/bar/Class.class", relocator.relocatePath( "org/foo/bar/Class.class" ) );
141     }
142 
143     public void testRelocateClass()
144     {
145         SimpleRelocator relocator;
146 
147         relocator = new SimpleRelocator( "org.foo", null, null, null );
148         assertEquals( "hidden.org.foo.bar.Class", relocator.relocateClass( "org.foo.bar.Class" ) );
149 
150         relocator = new SimpleRelocator( "org.foo", "private.stuff", null, null );
151         assertEquals( "private.stuff.bar.Class", relocator.relocateClass( "org.foo.bar.Class" ) );
152     }
153 
154     public void testRelocateRawString()
155     {
156         SimpleRelocator relocator;
157 
158         relocator = new SimpleRelocator( "Lorg/foo", "Lhidden/org/foo", null, null, true );
159         assertEquals( "(I)Lhidden/org/foo/bar/Class;", relocator.relocatePath( "(I)Lorg/foo/bar/Class;" ) );
160 
161         relocator = new SimpleRelocator( "^META-INF/org.foo.xml$", "META-INF/hidden.org.foo.xml", null, null, true );
162         assertEquals( "META-INF/hidden.org.foo.xml", relocator.relocatePath( "META-INF/org.foo.xml" ) );
163     }
164     
165     public void testRelocateMavenFiles()
166     {
167         SimpleRelocator relocator =
168             new SimpleRelocator( "META-INF/maven", "META-INF/shade/maven", null,
169                                  Collections.singletonList( "META-INF/maven/com.foo.bar/artifactId/pom.*" ) );
170         assertFalse( relocator.canRelocatePath( "META-INF/maven/com.foo.bar/artifactId/pom.properties" ) );
171         assertFalse( relocator.canRelocatePath( "META-INF/maven/com.foo.bar/artifactId/pom.xml" ) );
172         assertTrue( relocator.canRelocatePath( "META-INF/maven/com/foo/bar/artifactId/pom.properties" ) );
173         assertTrue( relocator.canRelocatePath( "META-INF/maven/com/foo/bar/artifactId/pom.xml" ) );
174         assertTrue( relocator.canRelocatePath( "META-INF/maven/com-foo-bar/artifactId/pom.properties" ) );
175         assertTrue( relocator.canRelocatePath( "META-INF/maven/com-foo-bar/artifactId/pom.xml" ) );
176 
177     }
178 }