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 org.junit.Test;
24  
25  import java.util.Arrays;
26  import java.util.Collections;
27  
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertFalse;
30  import static org.junit.Assert.assertTrue;
31  
32  /**
33   * Test for {@link SimpleRelocator}.
34   *
35   * @author Benjamin Bentmann
36   *
37   */
38  public class SimpleRelocatorTest
39  {
40  
41      @Test
42      public void testNoNpeRelocateClass()
43      {
44          new SimpleRelocator( "foo", "bar", null, null, true ).relocateClass( "foo" );
45      }
46  
47      @Test
48      public void testCanRelocatePath()
49      {
50          SimpleRelocator relocator;
51  
52          relocator = new SimpleRelocator( "org.foo", null, null, null );
53          assertTrue( relocator.canRelocatePath( "org/foo/Class" ) );
54          assertTrue( relocator.canRelocatePath( "org/foo/Class.class" ) );
55          assertTrue( relocator.canRelocatePath( "org/foo/bar/Class" ) );
56          assertTrue( relocator.canRelocatePath( "org/foo/bar/Class.class" ) );
57          assertFalse( relocator.canRelocatePath( "com/foo/bar/Class" ) );
58          assertFalse( relocator.canRelocatePath( "com/foo/bar/Class.class" ) );
59          assertFalse( relocator.canRelocatePath( "org/Foo/Class" ) );
60          assertFalse( relocator.canRelocatePath( "org/Foo/Class.class" ) );
61  
62          relocator = new SimpleRelocator( "org.foo", null, null, Arrays.asList(
63                  "org.foo.Excluded", "org.foo.public.*", "org.foo.Public*Stuff") );
64          assertTrue( relocator.canRelocatePath( "org/foo/Class" ) );
65          assertTrue( relocator.canRelocatePath( "org/foo/Class.class" ) );
66          assertTrue( relocator.canRelocatePath( "org/foo/excluded" ) );
67          assertFalse( relocator.canRelocatePath( "org/foo/Excluded" ) );
68          assertFalse( relocator.canRelocatePath( "org/foo/Excluded.class" ) );
69          assertFalse( relocator.canRelocatePath( "org/foo/public" ) );
70          assertFalse( relocator.canRelocatePath( "org/foo/public/Class" ) );
71          assertFalse( relocator.canRelocatePath( "org/foo/public/Class.class" ) );
72          assertTrue( relocator.canRelocatePath( "org/foo/publicRELOC/Class" ) );
73          assertTrue( relocator.canRelocatePath( "org/foo/PrivateStuff" ) );
74          assertTrue( relocator.canRelocatePath( "org/foo/PrivateStuff.class" ) );
75          assertFalse( relocator.canRelocatePath( "org/foo/PublicStuff" ) );
76          assertFalse( relocator.canRelocatePath( "org/foo/PublicStuff.class" ) );
77          assertFalse( relocator.canRelocatePath( "org/foo/PublicUtilStuff" ) );
78          assertFalse( relocator.canRelocatePath( "org/foo/PublicUtilStuff.class" ) );
79      }
80  
81      @Test
82      public void testCanRelocateClass()
83      {
84          SimpleRelocator relocator;
85  
86          relocator = new SimpleRelocator( "org.foo", null, null, null );
87          assertTrue( relocator.canRelocateClass( "org.foo.Class" ) );
88          assertTrue( relocator.canRelocateClass( "org.foo.bar.Class" ) );
89          assertFalse( relocator.canRelocateClass( "com.foo.bar.Class" ) );
90          assertFalse( relocator.canRelocateClass( "org.Foo.Class" ) );
91  
92          relocator = new SimpleRelocator( "org.foo", null, null, Arrays.asList(
93                  "org.foo.Excluded", "org.foo.public.*", "org.foo.Public*Stuff") );
94          assertTrue( relocator.canRelocateClass( "org.foo.Class" ) );
95          assertTrue( relocator.canRelocateClass( "org.foo.excluded" ) );
96          assertFalse( relocator.canRelocateClass( "org.foo.Excluded" ) );
97          assertFalse( relocator.canRelocateClass( "org.foo.public" ) );
98          assertFalse( relocator.canRelocateClass( "org.foo.public.Class" ) );
99          assertTrue( relocator.canRelocateClass( "org.foo.publicRELOC.Class" ) );
100         assertTrue( relocator.canRelocateClass( "org.foo.PrivateStuff" ) );
101         assertFalse( relocator.canRelocateClass( "org.foo.PublicStuff" ) );
102         assertFalse( relocator.canRelocateClass( "org.foo.PublicUtilStuff" ) );
103     }
104 
105     @Test
106     public void testCanRelocateRawString()
107     {
108         SimpleRelocator relocator;
109 
110         relocator = new SimpleRelocator( "org/foo", null, null, null, true );
111         assertTrue( relocator.canRelocatePath( "(I)org/foo/bar/Class;" ) );
112 
113         relocator = new SimpleRelocator( "^META-INF/org.foo.xml$", null, null, null, true );
114         assertTrue( relocator.canRelocatePath( "META-INF/org.foo.xml" ) );
115     }
116 
117     //MSHADE-119, make sure that the easy part of this works.
118     @Test
119     public void testCanRelocateAbsClassPath()
120     {
121         SimpleRelocator relocator = new SimpleRelocator( "org.apache.velocity", "org.apache.momentum", null, null );
122         assertEquals("/org/apache/momentum/mass.properties", relocator.relocatePath( "/org/apache/velocity/mass.properties" ) );
123     }
124 
125     @Test
126     public void testCanRelocateAbsClassPathWithExcludes()
127     {
128         SimpleRelocator relocator = new SimpleRelocator( "org/apache/velocity", "org/apache/momentum", null,
129                                                          Arrays.asList( "org/apache/velocity/excluded/*" ) );
130         assertTrue( relocator.canRelocatePath( "/org/apache/velocity/mass.properties" ) );
131         assertTrue( relocator.canRelocatePath( "org/apache/velocity/mass.properties" ) );
132         assertFalse( relocator.canRelocatePath( "/org/apache/velocity/excluded/mass.properties" ) );
133         assertFalse( relocator.canRelocatePath( "org/apache/velocity/excluded/mass.properties" ) );
134     }
135 
136     @Test
137     public void testCanRelocateAbsClassPathWithIncludes()
138     {
139         SimpleRelocator relocator = new SimpleRelocator( "org/apache/velocity", "org/apache/momentum",
140                                                          Arrays.asList( "org/apache/velocity/included/*" ), null );
141         assertFalse( relocator.canRelocatePath( "/org/apache/velocity/mass.properties" ) );
142         assertFalse( relocator.canRelocatePath( "org/apache/velocity/mass.properties" ) );
143         assertTrue( relocator.canRelocatePath( "/org/apache/velocity/included/mass.properties" ) );
144         assertTrue( relocator.canRelocatePath( "org/apache/velocity/included/mass.properties" ) );
145     }
146 
147     @Test
148     public void testRelocatePath()
149     {
150         SimpleRelocator relocator;
151 
152         relocator = new SimpleRelocator( "org.foo", null, null, null );
153         assertEquals( "hidden/org/foo/bar/Class.class", relocator.relocatePath( "org/foo/bar/Class.class" ) );
154 
155         relocator = new SimpleRelocator( "org.foo", "private.stuff", null, null );
156         assertEquals( "private/stuff/bar/Class.class", relocator.relocatePath( "org/foo/bar/Class.class" ) );
157     }
158 
159     @Test
160     public void testRelocateClass()
161     {
162         SimpleRelocator relocator;
163 
164         relocator = new SimpleRelocator( "org.foo", null, null, null );
165         assertEquals( "hidden.org.foo.bar.Class", relocator.relocateClass( "org.foo.bar.Class" ) );
166 
167         relocator = new SimpleRelocator( "org.foo", "private.stuff", null, null );
168         assertEquals( "private.stuff.bar.Class", relocator.relocateClass( "org.foo.bar.Class" ) );
169     }
170 
171     @Test
172     public void testRelocateRawString()
173     {
174         SimpleRelocator relocator;
175 
176         relocator = new SimpleRelocator( "Lorg/foo", "Lhidden/org/foo", null, null, true );
177         assertEquals( "(I)Lhidden/org/foo/bar/Class;", relocator.relocatePath( "(I)Lorg/foo/bar/Class;" ) );
178 
179         relocator = new SimpleRelocator( "^META-INF/org.foo.xml$", "META-INF/hidden.org.foo.xml", null, null, true );
180         assertEquals( "META-INF/hidden.org.foo.xml", relocator.relocatePath( "META-INF/org.foo.xml" ) );
181     }
182 
183     @Test
184     public void testRelocateMavenFiles()
185     {
186         SimpleRelocator relocator =
187             new SimpleRelocator( "META-INF/maven", "META-INF/shade/maven", null,
188                                  Collections.singletonList( "META-INF/maven/com.foo.bar/artifactId/pom.*" ) );
189         assertFalse( relocator.canRelocatePath( "META-INF/maven/com.foo.bar/artifactId/pom.properties" ) );
190         assertFalse( relocator.canRelocatePath( "META-INF/maven/com.foo.bar/artifactId/pom.xml" ) );
191         assertTrue( relocator.canRelocatePath( "META-INF/maven/com/foo/bar/artifactId/pom.properties" ) );
192         assertTrue( relocator.canRelocatePath( "META-INF/maven/com/foo/bar/artifactId/pom.xml" ) );
193         assertTrue( relocator.canRelocatePath( "META-INF/maven/com-foo-bar/artifactId/pom.properties" ) );
194         assertTrue( relocator.canRelocatePath( "META-INF/maven/com-foo-bar/artifactId/pom.xml" ) );
195 
196     }
197 
198     private static final String sourceFile =
199             "package org.apache.maven.hello;\n" +
200             "package org.objectweb.asm;\n" +
201             "\n" +
202             "import foo.bar.Bar;\n" +
203             "import zot.baz.Baz;\n" +
204             "import org.apache.maven.exclude1.Ex1;\n" +
205             "import org.apache.maven.exclude1.a.b.Ex1AB;\n" +
206             "import org.apache.maven.sub.exclude2.Ex2;\n" +
207             "import org.apache.maven.sub.exclude2.c.d.Ex2CD;\n" +
208             "import org.apache.maven.In;\n" +
209             "import org.apache.maven.e.InE;\n" +
210             "import org.apache.maven.f.g.InFG;\n" +
211             "import java.io.IOException;\n" +
212             "\n" +
213             "/**\n" +
214             " * Also check out {@link org.apache.maven.hello.OtherClass} and {@link\n" +
215             " * org.apache.maven.hello.YetAnotherClass}\n" +
216             " */\n" +
217             "public class MyClass {\n" +
218             "  private org.apache.maven.exclude1.x.X myX;\n" +
219             "  private org.apache.maven.h.H h;\n" +
220             "  private String ioInput;\n" +
221             "\n" +
222             "  public void doSomething() {\n" +
223             "    String io, val;\n" +
224             "    String noRelocation = \"NoWordBoundaryXXXorg.apache.maven.In\";\n" +
225             "    String relocationPackage = \"org.apache.maven.In\";\n" +
226             "    String relocationPath = \"org/apache/maven/In\";\n" +
227             "  }\n" +
228             "}\n";
229 
230     private static final String relocatedFile =
231             "package com.acme.maven.hello;\n" +
232             "package aj.org.objectweb.asm;\n" +
233             "\n" +
234             "import foo.bar.Bar;\n" +
235             "import zot.baz.Baz;\n" +
236             "import org.apache.maven.exclude1.Ex1;\n" +
237             "import org.apache.maven.exclude1.a.b.Ex1AB;\n" +
238             "import org.apache.maven.sub.exclude2.Ex2;\n" +
239             "import org.apache.maven.sub.exclude2.c.d.Ex2CD;\n" +
240             "import com.acme.maven.In;\n" +
241             "import com.acme.maven.e.InE;\n" +
242             "import com.acme.maven.f.g.InFG;\n" +
243             "import java.io.IOException;\n" +
244             "\n" +
245             "/**\n" +
246             " * Also check out {@link com.acme.maven.hello.OtherClass} and {@link\n" +
247             " * com.acme.maven.hello.YetAnotherClass}\n" +
248             " */\n" +
249             "public class MyClass {\n" +
250             "  private org.apache.maven.exclude1.x.X myX;\n" +
251             "  private com.acme.maven.h.H h;\n" +
252             "  private String ioInput;\n" +
253             "\n" +
254             "  public void doSomething() {\n" +
255             "    String io, val;\n" +
256             "    String noRelocation = \"NoWordBoundaryXXXorg.apache.maven.In\";\n" +
257             "    String relocationPackage = \"com.acme.maven.In\";\n" +
258             "    String relocationPath = \"com/acme/maven/In\";\n" +
259             "  }\n" +
260             "}\n";
261 
262     @Test
263     public void testRelocateSourceWithExcludesRaw()
264     {
265         SimpleRelocator relocator = new SimpleRelocator( "org.apache.maven", "com.acme.maven",
266                 Arrays.asList( "foo.bar", "zot.baz" ),
267                 Arrays.asList( "irrelevant.exclude", "org.apache.maven.exclude1", "org.apache.maven.sub.exclude2" ),
268                 true );
269         assertEquals( sourceFile,  relocator.applyToSourceContent( sourceFile ) );
270     }
271 
272     @Test
273     public void testRelocateSourceWithExcludes()
274     {
275         // Main relocator with in-/excludes
276         SimpleRelocator relocator = new SimpleRelocator( "org.apache.maven", "com.acme.maven",
277                 Arrays.asList( "foo.bar", "zot.baz" ),
278                 Arrays.asList( "irrelevant.exclude", "org.apache.maven.exclude1", "org.apache.maven.sub.exclude2" ) );
279         // Make sure not to replace variables 'io' and 'ioInput', package 'java.io'
280         SimpleRelocator ioRelocator = new SimpleRelocator( "io", "shaded.io", null, null );
281         // Check corner case which was not working in PR #100
282         SimpleRelocator asmRelocator = new SimpleRelocator( "org.objectweb.asm", "aj.org.objectweb.asm", null, null );
283         // Make sure not to replace 'foo' package by path-like 'shaded/foo'
284         SimpleRelocator fooRelocator = new SimpleRelocator( "foo", "shaded.foo", null, Arrays.asList( "foo.bar" ) );
285         assertEquals(
286             relocatedFile,
287             fooRelocator.applyToSourceContent(
288                 asmRelocator.applyToSourceContent(
289                     ioRelocator.applyToSourceContent(
290                         relocator.applyToSourceContent( sourceFile )
291                     )
292                 )
293             )
294         );
295     }
296 }