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