From acb1c0e69b1d724c7724aa8a670257ed21d17009 Mon Sep 17 00:00:00 2001 From: Goooler Date: Sat, 30 Nov 2024 16:54:35 +0800 Subject: [PATCH 1/2] Reformat unit test sources for less diffs --- src/test/.editorconfig | 4 + .../SimpleRelocatorParameterTest.groovy | 53 +-- .../relocation/SimpleRelocatorTest.groovy | 393 ++++++++---------- ...pacheLicenseResourceTransformerTest.groovy | 69 +-- ...ApacheNoticeResourceTransformerTest.groovy | 165 ++++---- .../AppendingTransformerTest.groovy | 67 +-- ...omponentsXmlResourceTransformerTest.groovy | 84 ++-- .../ManifestAppenderTransformerTest.groovy | 197 ++++----- .../PropertiesFileTransformerTest.groovy | 116 +++--- .../TransformerTestSupport.groovy | 10 +- .../XmlAppendingTransformerTest.groovy | 67 +-- 11 files changed, 520 insertions(+), 705 deletions(-) create mode 100755 src/test/.editorconfig diff --git a/src/test/.editorconfig b/src/test/.editorconfig new file mode 100755 index 000000000..a54a0eb0d --- /dev/null +++ b/src/test/.editorconfig @@ -0,0 +1,4 @@ +root = true + +[*.{groovy,java}] +indent_size = 2 diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorParameterTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorParameterTest.groovy index 9e47404ca..28bd0a412 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorParameterTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorParameterTest.groovy @@ -1,53 +1,30 @@ -/* - * 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. - */ - package com.github.jengelman.gradle.plugins.shadow.relocation import org.junit.jupiter.api.Test import static org.junit.jupiter.api.Assertions.fail - /** * Modified from org.apache.maven.plugins.shade.relocation.SimpleRelocatorParameterTest.java - * - * Modifications - * @author John Engelman */ class SimpleRelocatorParameterTest { - @Test - void testThatNullPatternInConstructorShouldNotThrowNullPointerException() { - constructThenFailOnNullPointerException(null, "") - } + @Test + void testThatNullPatternInConstructorShouldNotThrowNullPointerException() { + constructThenFailOnNullPointerException(null, "") + } - @Test - void testThatNullShadedPatternInConstructorShouldNotThrowNullPointerException() { - constructThenFailOnNullPointerException("", null) - } + @Test + void testThatNullShadedPatternInConstructorShouldNotThrowNullPointerException() { + constructThenFailOnNullPointerException("", null) + } - private static void constructThenFailOnNullPointerException(String pattern, String shadedPattern) { - try { - new SimpleRelocator(pattern, shadedPattern) - } - catch (NullPointerException ignored) { - fail("Constructor should not throw null pointer exceptions") - } + private static void constructThenFailOnNullPointerException(String pattern, String shadedPattern) { + try { + new SimpleRelocator(pattern, shadedPattern) + } + catch (NullPointerException ignored) { + fail("Constructor should not throw null pointer exceptions") } + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorTest.groovy index f1488c523..a68c4a418 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorTest.groovy @@ -1,22 +1,3 @@ -/* - * 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. - */ - package com.github.jengelman.gradle.plugins.shadow.relocation import com.github.jengelman.gradle.plugins.shadow.ShadowStats @@ -26,199 +7,191 @@ import org.junit.jupiter.api.Test import static org.junit.jupiter.api.Assertions.assertEquals /** - * Test for {@link SimpleRelocator}. - * - * @author Benjamin Bentmann - * @version $Id: SimpleRelocatorTest.java 1342979 2012-05-26 22:05:45Z bimargulies $ - * * Modified from org.apache.maven.plugins.shade.relocation.SimpleRelocatorTest.java - * - * Modifications - * @author John Engelman */ class SimpleRelocatorTest { - private static ShadowStats stats - - @BeforeEach - void setUp() { - stats = new ShadowStats() - } - - @Test - void testCanRelocatePath() { - SimpleRelocator relocator - - relocator = new SimpleRelocator("org.foo", null) - assertEquals(true, relocator.canRelocatePath("org/foo/Class")) - assertEquals(true, relocator.canRelocatePath("org/foo/Class.class")) - assertEquals(true, relocator.canRelocatePath("org/foo/bar/Class")) - assertEquals(true, relocator.canRelocatePath("org/foo/bar/Class.class")) - assertEquals(false, relocator.canRelocatePath("com/foo/bar/Class")) - assertEquals(false, relocator.canRelocatePath("com/foo/bar/Class.class")) - assertEquals(false, relocator.canRelocatePath("org/Foo/Class")) - assertEquals(false, relocator.canRelocatePath("org/Foo/Class.class")) - - // Verify paths starting with '/' - assertEquals(false, relocator.canRelocatePath("/org/Foo/Class")) - assertEquals(false, relocator.canRelocatePath("/org/Foo/Class.class")) - - relocator = new SimpleRelocator("org.foo", null, null, - List.of("org.foo.Excluded", "org.foo.public.*", "org.foo.recurse.**", "org.foo.Public*Stuff")) - assertEquals(true, relocator.canRelocatePath("org/foo/Class")) - assertEquals(true, relocator.canRelocatePath("org/foo/Class.class")) - assertEquals(true, relocator.canRelocatePath("org/foo/excluded")) - assertEquals(false, relocator.canRelocatePath("org/foo/Excluded")) - assertEquals(false, relocator.canRelocatePath("org/foo/Excluded.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/public")) - assertEquals(false, relocator.canRelocatePath("org/foo/public/Class")) - assertEquals(false, relocator.canRelocatePath("org/foo/public/Class.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/public/sub")) - assertEquals(true, relocator.canRelocatePath("org/foo/public/sub/Class")) - assertEquals(true, relocator.canRelocatePath("org/foo/publicRELOC/Class")) - assertEquals(true, relocator.canRelocatePath("org/foo/PrivateStuff")) - assertEquals(true, relocator.canRelocatePath("org/foo/PrivateStuff.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/PublicStuff")) - assertEquals(false, relocator.canRelocatePath("org/foo/PublicStuff.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/PublicUtilStuff")) - assertEquals(false, relocator.canRelocatePath("org/foo/PublicUtilStuff.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/recurse")) - assertEquals(false, relocator.canRelocatePath("org/foo/recurse/Class")) - assertEquals(false, relocator.canRelocatePath("org/foo/recurse/Class.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/recurse/sub")) - assertEquals(false, relocator.canRelocatePath("org/foo/recurse/sub/Class")) - assertEquals(false, relocator.canRelocatePath("org/foo/recurse/sub/Class.class")) - - // Verify edge cases - relocator = new SimpleRelocator("org.f", null) - assertEquals(false, relocator.canRelocatePath("")) // Empty path - assertEquals(false, relocator.canRelocatePath(".class")) // only .class - assertEquals(false, relocator.canRelocatePath("te")) // shorter than path pattern - assertEquals(false, relocator.canRelocatePath("test")) // shorter than path pattern with / - assertEquals(true, relocator.canRelocatePath("org/f")) // equal to path pattern - assertEquals(true, relocator.canRelocatePath("/org/f")) // equal to path pattern with / - } - - @Test - void testCanRelocatePathWithRegex() { - SimpleRelocator relocator - - // Include with Regex - relocator = new SimpleRelocator("org.foo", null, Collections.singletonList("%regex[org/foo/R(\\\$.*)?\$]"), null) - assertEquals(true, relocator.canRelocatePath("org/foo/R.class")) - assertEquals(true, relocator.canRelocatePath("org/foo/R\$string.class")) - assertEquals(true, relocator.canRelocatePath("org/foo/R\$layout.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/Recording/R.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/Recording.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/bar/R\$string.class")) - assertEquals(false, relocator.canRelocatePath("org/R.class")) - assertEquals(false, relocator.canRelocatePath("org/R\$string.class")) - - // Exclude with Regex - relocator = new SimpleRelocator("org.foo", null) - relocator.exclude("%regex[org/foo/.*Factory[0-9].*]") - assertEquals(true, relocator.canRelocatePath("org/foo/Factory.class")) - assertEquals(true, relocator.canRelocatePath("org/foo/FooFactoryMain.class")) - assertEquals(true, relocator.canRelocatePath("org/foo/BarFactory.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/Factory0.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/FooFactory1Main.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/BarFactory2.class")) - - // Include with Regex and normal pattern - relocator = new SimpleRelocator("org.foo", null, - List.of("%regex[org/foo/.*Factory[0-9].*]", "org.foo.public.*")) - assertEquals(true, relocator.canRelocatePath("org/foo/Factory1.class")) - assertEquals(true, relocator.canRelocatePath("org/foo/public/Bar.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/Factory.class")) - assertEquals(false, relocator.canRelocatePath("org/foo/R.class")) - } - - @Test - void testCanRelocateClass() { - SimpleRelocator relocator - - relocator = new SimpleRelocator("org.foo", null) - assertEquals(true, relocator.canRelocateClass("org.foo.Class")) - assertEquals(true, relocator.canRelocateClass("org.foo.bar.Class")) - assertEquals(false, relocator.canRelocateClass("com.foo.bar.Class")) - assertEquals(false, relocator.canRelocateClass("org.Foo.Class")) - - relocator = new SimpleRelocator("org.foo", null, null, - List.of("org.foo.Excluded", "org.foo.public.*", "org.foo.recurse.**", "org.foo.Public*Stuff")) - assertEquals(true, relocator.canRelocateClass("org.foo.Class")) - assertEquals(true, relocator.canRelocateClass("org.foo.excluded")) - assertEquals(false, relocator.canRelocateClass("org.foo.Excluded")) - assertEquals(false, relocator.canRelocateClass("org.foo.public")) - assertEquals(false, relocator.canRelocateClass("org.foo.public.Class")) - assertEquals(false, relocator.canRelocateClass("org.foo.public.sub")) - assertEquals(true, relocator.canRelocateClass("org.foo.public.sub.Class")) - assertEquals(true, relocator.canRelocateClass("org.foo.publicRELOC.Class")) - assertEquals(true, relocator.canRelocateClass("org.foo.PrivateStuff")) - assertEquals(false, relocator.canRelocateClass("org.foo.PublicStuff")) - assertEquals(false, relocator.canRelocateClass("org.foo.PublicUtilStuff")) - assertEquals(false, relocator.canRelocateClass("org.foo.recurse")) - assertEquals(false, relocator.canRelocateClass("org.foo.recurse.Class")) - assertEquals(false, relocator.canRelocateClass("org.foo.recurse.sub")) - assertEquals(false, relocator.canRelocateClass("org.foo.recurse.sub.Class")) - } - - @Test - void testCanRelocateRawString() { - SimpleRelocator relocator - - relocator = new SimpleRelocator("org/foo", null, null, null, true) - assertEquals(true, relocator.canRelocatePath("(I)org/foo/bar/Class")) - - relocator = new SimpleRelocator("^META-INF/org.foo.xml\$", null, null, null, true) - assertEquals(true, relocator.canRelocatePath("META-INF/org.foo.xml")) - } - - //MSHADE-119, make sure that the easy part of this works. - @Test - void testCanRelocateAbsClassPath() { - SimpleRelocator relocator = new SimpleRelocator("org.apache.velocity", "org.apache.momentum") - assertEquals("/org/apache/momentum/mass.properties", relocator.relocatePath(pathContext("/org/apache/velocity/mass.properties"))) - - } - - @Test - void testRelocatePath() { - SimpleRelocator relocator - - relocator = new SimpleRelocator("org.foo", null) - assertEquals("hidden/org/foo/bar/Class.class", relocator.relocatePath(pathContext("org/foo/bar/Class.class"))) - - relocator = new SimpleRelocator("org.foo", "private.stuff") - assertEquals("private/stuff/bar/Class.class", relocator.relocatePath(pathContext("org/foo/bar/Class.class"))) - } - - @Test - void testRelocateClass() { - SimpleRelocator relocator - - relocator = new SimpleRelocator("org.foo", null) - assertEquals("hidden.org.foo.bar.Class", relocator.relocateClass(classContext("org.foo.bar.Class"))) - - relocator = new SimpleRelocator("org.foo", "private.stuff") - assertEquals("private.stuff.bar.Class", relocator.relocateClass(classContext("org.foo.bar.Class"))) - } - - @Test - void testRelocateRawString() { - SimpleRelocator relocator - - relocator = new SimpleRelocator("Lorg/foo", "Lhidden/org/foo", null, null, true) - assertEquals("(I)Lhidden/org/foo/bar/Class", relocator.relocatePath(pathContext("(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(pathContext("META-INF/org.foo.xml"))) - } - - protected static RelocatePathContext pathContext(String path) { - return RelocatePathContext.builder().path(path).stats(stats).build() - } - - protected static RelocateClassContext classContext(String className) { - return RelocateClassContext.builder().className(className).stats(stats).build() - } + private static ShadowStats stats + + @BeforeEach + void setUp() { + stats = new ShadowStats() + } + + @Test + void testCanRelocatePath() { + SimpleRelocator relocator + + relocator = new SimpleRelocator("org.foo", null) + assertEquals(true, relocator.canRelocatePath("org/foo/Class")) + assertEquals(true, relocator.canRelocatePath("org/foo/Class.class")) + assertEquals(true, relocator.canRelocatePath("org/foo/bar/Class")) + assertEquals(true, relocator.canRelocatePath("org/foo/bar/Class.class")) + assertEquals(false, relocator.canRelocatePath("com/foo/bar/Class")) + assertEquals(false, relocator.canRelocatePath("com/foo/bar/Class.class")) + assertEquals(false, relocator.canRelocatePath("org/Foo/Class")) + assertEquals(false, relocator.canRelocatePath("org/Foo/Class.class")) + + // Verify paths starting with '/' + assertEquals(false, relocator.canRelocatePath("/org/Foo/Class")) + assertEquals(false, relocator.canRelocatePath("/org/Foo/Class.class")) + + relocator = new SimpleRelocator("org.foo", null, null, + List.of("org.foo.Excluded", "org.foo.public.*", "org.foo.recurse.**", "org.foo.Public*Stuff")) + assertEquals(true, relocator.canRelocatePath("org/foo/Class")) + assertEquals(true, relocator.canRelocatePath("org/foo/Class.class")) + assertEquals(true, relocator.canRelocatePath("org/foo/excluded")) + assertEquals(false, relocator.canRelocatePath("org/foo/Excluded")) + assertEquals(false, relocator.canRelocatePath("org/foo/Excluded.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/public")) + assertEquals(false, relocator.canRelocatePath("org/foo/public/Class")) + assertEquals(false, relocator.canRelocatePath("org/foo/public/Class.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/public/sub")) + assertEquals(true, relocator.canRelocatePath("org/foo/public/sub/Class")) + assertEquals(true, relocator.canRelocatePath("org/foo/publicRELOC/Class")) + assertEquals(true, relocator.canRelocatePath("org/foo/PrivateStuff")) + assertEquals(true, relocator.canRelocatePath("org/foo/PrivateStuff.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/PublicStuff")) + assertEquals(false, relocator.canRelocatePath("org/foo/PublicStuff.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/PublicUtilStuff")) + assertEquals(false, relocator.canRelocatePath("org/foo/PublicUtilStuff.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/recurse")) + assertEquals(false, relocator.canRelocatePath("org/foo/recurse/Class")) + assertEquals(false, relocator.canRelocatePath("org/foo/recurse/Class.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/recurse/sub")) + assertEquals(false, relocator.canRelocatePath("org/foo/recurse/sub/Class")) + assertEquals(false, relocator.canRelocatePath("org/foo/recurse/sub/Class.class")) + + // Verify edge cases + relocator = new SimpleRelocator("org.f", null) + assertEquals(false, relocator.canRelocatePath("")) // Empty path + assertEquals(false, relocator.canRelocatePath(".class")) // only .class + assertEquals(false, relocator.canRelocatePath("te")) // shorter than path pattern + assertEquals(false, relocator.canRelocatePath("test")) // shorter than path pattern with / + assertEquals(true, relocator.canRelocatePath("org/f")) // equal to path pattern + assertEquals(true, relocator.canRelocatePath("/org/f")) // equal to path pattern with / + } + + @Test + void testCanRelocatePathWithRegex() { + SimpleRelocator relocator + + // Include with Regex + relocator = new SimpleRelocator("org.foo", null, Collections.singletonList("%regex[org/foo/R(\\\$.*)?\$]"), null) + assertEquals(true, relocator.canRelocatePath("org/foo/R.class")) + assertEquals(true, relocator.canRelocatePath("org/foo/R\$string.class")) + assertEquals(true, relocator.canRelocatePath("org/foo/R\$layout.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/Recording/R.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/Recording.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/bar/R\$string.class")) + assertEquals(false, relocator.canRelocatePath("org/R.class")) + assertEquals(false, relocator.canRelocatePath("org/R\$string.class")) + + // Exclude with Regex + relocator = new SimpleRelocator("org.foo", null) + relocator.exclude("%regex[org/foo/.*Factory[0-9].*]") + assertEquals(true, relocator.canRelocatePath("org/foo/Factory.class")) + assertEquals(true, relocator.canRelocatePath("org/foo/FooFactoryMain.class")) + assertEquals(true, relocator.canRelocatePath("org/foo/BarFactory.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/Factory0.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/FooFactory1Main.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/BarFactory2.class")) + + // Include with Regex and normal pattern + relocator = new SimpleRelocator("org.foo", null, + List.of("%regex[org/foo/.*Factory[0-9].*]", "org.foo.public.*")) + assertEquals(true, relocator.canRelocatePath("org/foo/Factory1.class")) + assertEquals(true, relocator.canRelocatePath("org/foo/public/Bar.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/Factory.class")) + assertEquals(false, relocator.canRelocatePath("org/foo/R.class")) + } + + @Test + void testCanRelocateClass() { + SimpleRelocator relocator + + relocator = new SimpleRelocator("org.foo", null) + assertEquals(true, relocator.canRelocateClass("org.foo.Class")) + assertEquals(true, relocator.canRelocateClass("org.foo.bar.Class")) + assertEquals(false, relocator.canRelocateClass("com.foo.bar.Class")) + assertEquals(false, relocator.canRelocateClass("org.Foo.Class")) + + relocator = new SimpleRelocator("org.foo", null, null, + List.of("org.foo.Excluded", "org.foo.public.*", "org.foo.recurse.**", "org.foo.Public*Stuff")) + assertEquals(true, relocator.canRelocateClass("org.foo.Class")) + assertEquals(true, relocator.canRelocateClass("org.foo.excluded")) + assertEquals(false, relocator.canRelocateClass("org.foo.Excluded")) + assertEquals(false, relocator.canRelocateClass("org.foo.public")) + assertEquals(false, relocator.canRelocateClass("org.foo.public.Class")) + assertEquals(false, relocator.canRelocateClass("org.foo.public.sub")) + assertEquals(true, relocator.canRelocateClass("org.foo.public.sub.Class")) + assertEquals(true, relocator.canRelocateClass("org.foo.publicRELOC.Class")) + assertEquals(true, relocator.canRelocateClass("org.foo.PrivateStuff")) + assertEquals(false, relocator.canRelocateClass("org.foo.PublicStuff")) + assertEquals(false, relocator.canRelocateClass("org.foo.PublicUtilStuff")) + assertEquals(false, relocator.canRelocateClass("org.foo.recurse")) + assertEquals(false, relocator.canRelocateClass("org.foo.recurse.Class")) + assertEquals(false, relocator.canRelocateClass("org.foo.recurse.sub")) + assertEquals(false, relocator.canRelocateClass("org.foo.recurse.sub.Class")) + } + + @Test + void testCanRelocateRawString() { + SimpleRelocator relocator + + relocator = new SimpleRelocator("org/foo", null, null, null, true) + assertEquals(true, relocator.canRelocatePath("(I)org/foo/bar/Class")) + + relocator = new SimpleRelocator("^META-INF/org.foo.xml\$", null, null, null, true) + assertEquals(true, relocator.canRelocatePath("META-INF/org.foo.xml")) + } + + //MSHADE-119, make sure that the easy part of this works. + @Test + void testCanRelocateAbsClassPath() { + SimpleRelocator relocator = new SimpleRelocator("org.apache.velocity", "org.apache.momentum") + assertEquals("/org/apache/momentum/mass.properties", relocator.relocatePath(pathContext("/org/apache/velocity/mass.properties"))) + + } + + @Test + void testRelocatePath() { + SimpleRelocator relocator + + relocator = new SimpleRelocator("org.foo", null) + assertEquals("hidden/org/foo/bar/Class.class", relocator.relocatePath(pathContext("org/foo/bar/Class.class"))) + + relocator = new SimpleRelocator("org.foo", "private.stuff") + assertEquals("private/stuff/bar/Class.class", relocator.relocatePath(pathContext("org/foo/bar/Class.class"))) + } + + @Test + void testRelocateClass() { + SimpleRelocator relocator + + relocator = new SimpleRelocator("org.foo", null) + assertEquals("hidden.org.foo.bar.Class", relocator.relocateClass(classContext("org.foo.bar.Class"))) + + relocator = new SimpleRelocator("org.foo", "private.stuff") + assertEquals("private.stuff.bar.Class", relocator.relocateClass(classContext("org.foo.bar.Class"))) + } + + @Test + void testRelocateRawString() { + SimpleRelocator relocator + + relocator = new SimpleRelocator("Lorg/foo", "Lhidden/org/foo", null, null, true) + assertEquals("(I)Lhidden/org/foo/bar/Class", relocator.relocatePath(pathContext("(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(pathContext("META-INF/org.foo.xml"))) + } + + protected static RelocatePathContext pathContext(String path) { + return RelocatePathContext.builder().path(path).stats(stats).build() + } + + protected static RelocateClassContext classContext(String className) { + return RelocateClassContext.builder().className(className).stats(stats).build() + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.groovy index 85f59721d..f3dc80759 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.groovy @@ -1,22 +1,3 @@ -/* - * 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. - */ - package com.github.jengelman.gradle.plugins.shadow.transformers import org.junit.jupiter.api.BeforeEach @@ -26,36 +7,30 @@ import static org.junit.jupiter.api.Assertions.assertFalse import static org.junit.jupiter.api.Assertions.assertTrue /** - * Test for {@link ApacheLicenseResourceTransformer}. - * - * @author Benjamin Bentmann - * @version $Id: ApacheLicenseResourceTransformerTest.java 673906 2008-07-04 05:03:20Z brett $ - * * Modified from org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformerTest.java */ class ApacheLicenseResourceTransformerTest extends TransformerTestSupport { - static { - /* - * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime - * choice to test for improper case-less string comparisions. - */ - Locale.setDefault(new Locale("tr")) - } - - @BeforeEach - void setUp() { - transformer = new ApacheLicenseResourceTransformer() - } - - @Test - void testCanTransformResource() { - assertTrue(transformer.canTransformResource(getFileElement("META-INF/LICENSE"))) - assertTrue(transformer.canTransformResource(getFileElement("META-INF/LICENSE.TXT"))) - assertTrue(transformer.canTransformResource(getFileElement("META-INF/License.txt"))) - assertTrue(transformer.canTransformResource(getFileElement("META-INF/LICENSE.md"))) - assertTrue(transformer.canTransformResource(getFileElement("META-INF/License.md"))) - assertFalse(transformer.canTransformResource(getFileElement("META-INF/MANIFEST.MF"))) - } - + static { + /* + * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime + * choice to test for improper case-less string comparisions. + */ + Locale.setDefault(new Locale("tr")) + } + + @BeforeEach + void setUp() { + transformer = new ApacheLicenseResourceTransformer() + } + + @Test + void testCanTransformResource() { + assertTrue(transformer.canTransformResource(getFileElement("META-INF/LICENSE"))) + assertTrue(transformer.canTransformResource(getFileElement("META-INF/LICENSE.TXT"))) + assertTrue(transformer.canTransformResource(getFileElement("META-INF/License.txt"))) + assertTrue(transformer.canTransformResource(getFileElement("META-INF/LICENSE.md"))) + assertTrue(transformer.canTransformResource(getFileElement("META-INF/License.md"))) + assertFalse(transformer.canTransformResource(getFileElement("META-INF/MANIFEST.MF"))) + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy index 102e35ba2..d7a01aef0 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy @@ -1,22 +1,3 @@ -/* - * 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. - */ - package com.github.jengelman.gradle.plugins.shadow.transformers import com.github.jengelman.gradle.plugins.shadow.ShadowStats @@ -24,87 +5,83 @@ import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test -import static org.junit.jupiter.api.Assertions.assertFalse -import static org.junit.jupiter.api.Assertions.assertTrue -import static org.junit.jupiter.api.Assertions.fail +import static org.junit.jupiter.api.Assertions.* /** - * Tests {@link ApacheLicenseResourceTransformer} parameters. - * * Modified from org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformerParameterTests.java */ class ApacheNoticeResourceTransformerTest extends TransformerTestSupport { - private static final String NOTICE_RESOURCE = "META-INF/NOTICE" - private static ShadowStats stats - - static { - /* - * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime - * choice to test for improper case-less string comparisions. - */ - Locale.setDefault(new Locale("tr")) - } - - @BeforeEach - void setUp() { - transformer = new ApacheNoticeResourceTransformer(objectFactory) - stats = new ShadowStats() - } - - @Test - void testCanTransformResource() { - assertTrue(transformer.canTransformResource(getFileElement("META-INF/NOTICE"))) - assertTrue(transformer.canTransformResource(getFileElement("META-INF/NOTICE.TXT"))) - assertTrue(transformer.canTransformResource(getFileElement("META-INF/Notice.txt"))) - assertTrue(transformer.canTransformResource(getFileElement("META-INF/NOTICE.md"))) - assertTrue(transformer.canTransformResource(getFileElement("META-INF/Notice.md"))) - assertFalse(transformer.canTransformResource(getFileElement("META-INF/MANIFEST.MF"))) - } - - @Test - void testNoParametersShouldNotThrowNullPointerWhenNoInput() { - processAndFailOnNullPointer("") - } - - @Test - void testNoParametersShouldNotThrowNullPointerWhenNoLinesOfInput() { - processAndFailOnNullPointer("Some notice text") + private static final String NOTICE_RESOURCE = "META-INF/NOTICE" + private static ShadowStats stats + + static { + /* + * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime + * choice to test for improper case-less string comparisions. + */ + Locale.setDefault(new Locale("tr")) + } + + @BeforeEach + void setUp() { + transformer = new ApacheNoticeResourceTransformer(objectFactory) + stats = new ShadowStats() + } + + @Test + void testCanTransformResource() { + assertTrue(transformer.canTransformResource(getFileElement("META-INF/NOTICE"))) + assertTrue(transformer.canTransformResource(getFileElement("META-INF/NOTICE.TXT"))) + assertTrue(transformer.canTransformResource(getFileElement("META-INF/Notice.txt"))) + assertTrue(transformer.canTransformResource(getFileElement("META-INF/NOTICE.md"))) + assertTrue(transformer.canTransformResource(getFileElement("META-INF/Notice.md"))) + assertFalse(transformer.canTransformResource(getFileElement("META-INF/MANIFEST.MF"))) + } + + @Test + void testNoParametersShouldNotThrowNullPointerWhenNoInput() { + processAndFailOnNullPointer("") + } + + @Test + void testNoParametersShouldNotThrowNullPointerWhenNoLinesOfInput() { + processAndFailOnNullPointer("Some notice text") + } + + @Test + void testNoParametersShouldNotThrowNullPointerWhenOneLineOfInput() { + processAndFailOnNullPointer("Some notice text\n") + } + + @Test + void testNoParametersShouldNotThrowNullPointerWhenTwoLinesOfInput() { + processAndFailOnNullPointer("Some notice text\nSome notice text\n") + } + + @Test + void testNoParametersShouldNotThrowNullPointerWhenLineStartsWithSlashSlash() { + processAndFailOnNullPointer("Some notice text\n//Some notice text\n") + } + + @Test + void testNoParametersShouldNotThrowNullPointerWhenLineIsSlashSlash() { + processAndFailOnNullPointer("//\n") + } + + @Test + void testNoParametersShouldNotThrowNullPointerWhenLineIsEmpty() { + processAndFailOnNullPointer("\n") + } + + private static void processAndFailOnNullPointer(final String noticeText) { + try { + final ByteArrayInputStream noticeInputStream = new ByteArrayInputStream(noticeText.getBytes()) + final List emptyList = Collections.emptyList() + transformer.transform(TransformerContext.builder().path(NOTICE_RESOURCE).inputStream(noticeInputStream).relocators(emptyList).stats(stats).build()) } - - @Test - void testNoParametersShouldNotThrowNullPointerWhenOneLineOfInput() { - processAndFailOnNullPointer("Some notice text\n") - } - - @Test - void testNoParametersShouldNotThrowNullPointerWhenTwoLinesOfInput() { - processAndFailOnNullPointer("Some notice text\nSome notice text\n") - } - - @Test - void testNoParametersShouldNotThrowNullPointerWhenLineStartsWithSlashSlash() { - processAndFailOnNullPointer("Some notice text\n//Some notice text\n") - } - - @Test - void testNoParametersShouldNotThrowNullPointerWhenLineIsSlashSlash() { - processAndFailOnNullPointer("//\n") - } - - @Test - void testNoParametersShouldNotThrowNullPointerWhenLineIsEmpty() { - processAndFailOnNullPointer("\n") - } - - private static void processAndFailOnNullPointer(final String noticeText) { - try { - final ByteArrayInputStream noticeInputStream = new ByteArrayInputStream(noticeText.getBytes()) - final List emptyList = Collections.emptyList() - transformer.transform(TransformerContext.builder().path(NOTICE_RESOURCE).inputStream(noticeInputStream).relocators(emptyList).stats(stats).build()) - } - catch (NullPointerException ignored) { - fail("Null pointer should not be thrown when no parameters are set.") - } + catch (NullPointerException ignored) { + fail("Null pointer should not be thrown when no parameters are set.") } + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformerTest.groovy index 2102f351c..45704f889 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformerTest.groovy @@ -1,22 +1,3 @@ -/* - * 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. - */ - package com.github.jengelman.gradle.plugins.shadow.transformers import org.junit.jupiter.api.BeforeEach @@ -26,34 +7,30 @@ import static org.junit.jupiter.api.Assertions.assertFalse import static org.junit.jupiter.api.Assertions.assertTrue /** - * Test for {@link AppendingTransformer}. - * - * @author Benjamin Bentmann - * @version $Id: AppendingTransformerTest.java 673906 2008-07-04 05:03:20Z brett $ + * Modified from org.apache.maven.plugins.shade.resource.AppendingTransformerTest.java */ class AppendingTransformerTest extends TransformerTestSupport { - static - { - /* - * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime - * choice to test for improper case-less string comparisions. - */ - Locale.setDefault(new Locale("tr")) - } - - @BeforeEach - void setUp() { - transformer = new AppendingTransformer(objectFactory) - } - - @Test - void testCanTransformResource() { - transformer.resource.set("abcdefghijklmnopqrstuvwxyz") - - assertTrue(transformer.canTransformResource(getFileElement("abcdefghijklmnopqrstuvwxyz"))) - assertTrue(transformer.canTransformResource(getFileElement("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))) - assertFalse(transformer.canTransformResource(getFileElement("META-INF/MANIFEST.MF"))) - } + static { + /* + * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime + * choice to test for improper case-less string comparisions. + */ + Locale.setDefault(new Locale("tr")) + } + + @BeforeEach + void setUp() { + transformer = new AppendingTransformer(objectFactory) + } + + @Test + void testCanTransformResource() { + transformer.resource.set("abcdefghijklmnopqrstuvwxyz") + + assertTrue(transformer.canTransformResource(getFileElement("abcdefghijklmnopqrstuvwxyz"))) + assertTrue(transformer.canTransformResource(getFileElement("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))) + assertFalse(transformer.canTransformResource(getFileElement("META-INF/MANIFEST.MF"))) + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.groovy index edf9ea9e1..537feba60 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.groovy @@ -1,22 +1,3 @@ -/* - * 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. - */ - package com.github.jengelman.gradle.plugins.shadow.transformers import com.github.jengelman.gradle.plugins.shadow.ShadowStats @@ -29,46 +10,41 @@ import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test /** - * Test for {@link ComponentsXmlResourceTransformer}. - * - * @author Brett Porter - * @version $Id: ComponentsXmlResourceTransformerTest.java 1379994 2012-09-02 15:22:49Z hboutemy $ - * * Modified from org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformerTest.java */ class ComponentsXmlResourceTransformerTest extends TransformerTestSupport { - private static ShadowStats stats + private static ShadowStats stats - @BeforeEach - void setUp() { - transformer = new ComponentsXmlResourceTransformer() - stats = new ShadowStats() - } + @BeforeEach + void setUp() { + transformer = new ComponentsXmlResourceTransformer() + stats = new ShadowStats() + } - @Test - void testConfigurationMerging() { + @Test + void testConfigurationMerging() { - XMLUnit.setNormalizeWhitespace(true) + XMLUnit.setNormalizeWhitespace(true) - transformer.transform( - TransformerContext.builder() - .path("components-1.xml") - .inputStream(getClass().getResourceAsStream("/components-1.xml")) - .relocators(Collections. emptyList()) - .stats(stats) - .build()) - transformer.transform( - TransformerContext.builder() - .path("components-1.xml") - .inputStream(getClass().getResourceAsStream("/components-2.xml")) - .relocators(Collections. emptyList()) - .stats(stats) - .build()) - Diff diff = XMLUnit.compareXML( - IOUtil.toString(getClass().getResourceAsStream("/components-expected.xml"), "UTF-8"), - IOUtil.toString(transformer.getTransformedResource(), "UTF-8")) - //assertEquals( IOUtil.toString( getClass().getResourceAsStream( "/components-expected.xml" ), "UTF-8" ), - // IOUtil.toString( transformer.getTransformedResource(), "UTF-8" ).replaceAll("\r\n", "\n") ) - XMLAssert.assertXMLIdentical(diff, true) - } + transformer.transform( + TransformerContext.builder() + .path("components-1.xml") + .inputStream(getClass().getResourceAsStream("/components-1.xml")) + .relocators(Collections. emptyList()) + .stats(stats) + .build()) + transformer.transform( + TransformerContext.builder() + .path("components-1.xml") + .inputStream(getClass().getResourceAsStream("/components-2.xml")) + .relocators(Collections. emptyList()) + .stats(stats) + .build()) + Diff diff = XMLUnit.compareXML( + IOUtil.toString(getClass().getResourceAsStream("/components-expected.xml"), "UTF-8"), + IOUtil.toString(transformer.getTransformedResource(), "UTF-8")) + //assertEquals( IOUtil.toString( getClass().getResourceAsStream( "/components-expected.xml" ), "UTF-8" ), + // IOUtil.toString( transformer.getTransformedResource(), "UTF-8" ).replaceAll("\r\n", "\n") ) + XMLAssert.assertXMLIdentical(diff, true) + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformerTest.groovy index 46e325a6a..48719fce6 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformerTest.groovy @@ -1,22 +1,3 @@ -/* - * 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. - */ - package com.github.jengelman.gradle.plugins.shadow.transformers import com.github.jengelman.gradle.plugins.shadow.ShadowStats @@ -34,107 +15,107 @@ import static org.junit.jupiter.api.Assertions.* * Test for {@link ManifestAppenderTransformer}. */ class ManifestAppenderTransformerTest extends TransformerTestSupport { - private static final String MANIFEST_NAME = "META-INF/MANIFEST.MF" - - @BeforeEach - void setUp() { - transformer = new ManifestAppenderTransformer(objectFactory) + private static final String MANIFEST_NAME = "META-INF/MANIFEST.MF" + + @BeforeEach + void setUp() { + transformer = new ManifestAppenderTransformer(objectFactory) + } + + @Test + void testCanTransformResource() { + transformer.with { + append('Name', 'org/foo/bar/') + append('Sealed', true) } - @Test - void testCanTransformResource() { - transformer.with { - append('Name', 'org/foo/bar/') - append('Sealed', true) - } + assertTrue(transformer.canTransformResource(getFileElement(MANIFEST_NAME))) + assertTrue(transformer.canTransformResource(getFileElement(MANIFEST_NAME.toLowerCase()))) + } - assertTrue(transformer.canTransformResource(getFileElement(MANIFEST_NAME))) - assertTrue(transformer.canTransformResource(getFileElement(MANIFEST_NAME.toLowerCase()))) - } + @Test + void testHasTransformedResource() { + transformer.append('Tag', 'Something') - @Test - void testHasTransformedResource() { - transformer.append('Tag', 'Something') + assertTrue(transformer.hasTransformedResource()) + } - assertTrue(transformer.hasTransformedResource()) - } + @Test + void testHasNotTransformedResource() { + assertFalse(transformer.hasTransformedResource()) + } - @Test - void testHasNotTransformedResource() { - assertFalse(transformer.hasTransformedResource()) - } + @Test + void testTransformation() { + transformer.with { + append('Name', 'org/foo/bar/') + append('Sealed', true) + append('Name', 'com/example/') + append('Sealed', false) - @Test - void testTransformation() { - transformer.with { - append('Name', 'org/foo/bar/') - append('Sealed', true) - append('Name', 'com/example/') - append('Sealed', false) - - transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections. emptyList(), new ShadowStats())) - } - - def testableZipFile = File.createTempFile("testable-zip-file-", ".jar") - def fileOutputStream = new FileOutputStream(testableZipFile) - def bufferedOutputStream = new BufferedOutputStream(fileOutputStream) - def zipOutputStream = new ZipOutputStream(bufferedOutputStream) - - try { - transformer.modifyOutputStream(zipOutputStream, true) - } finally { - zipOutputStream.close() - } - - def targetLines = readFrom(testableZipFile, MANIFEST_NAME) - assertFalse(targetLines.isEmpty()) - assertTrue(targetLines.size() > 4) - - def trailer = targetLines.with { subList(size() - 5, size()) } - assertEquals(asList( - "Name: org/foo/bar/", - "Sealed: true", - "Name: com/example/", - "Sealed: false", - ""), trailer - ) + transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections. emptyList(), new ShadowStats())) } - @Test - void testNoTransformation() { - def sourceLines = getResourceStream(MANIFEST_NAME).readLines() + def testableZipFile = File.createTempFile("testable-zip-file-", ".jar") + def fileOutputStream = new FileOutputStream(testableZipFile) + def bufferedOutputStream = new BufferedOutputStream(fileOutputStream) + def zipOutputStream = new ZipOutputStream(bufferedOutputStream) - transformer.transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections. emptyList(), new ShadowStats())) - - def testableZipFile = File.createTempFile("testable-zip-file-", ".jar") - def fileOutputStream = new FileOutputStream(testableZipFile) - def bufferedOutputStream = new BufferedOutputStream(fileOutputStream) - def zipOutputStream = new ZipOutputStream(bufferedOutputStream) - - try { - transformer.modifyOutputStream(zipOutputStream, true) - } finally { - zipOutputStream.close() - } - def targetLines = readFrom(testableZipFile, MANIFEST_NAME) - - assertEquals(sourceLines, targetLines) + try { + transformer.modifyOutputStream(zipOutputStream, true) + } finally { + zipOutputStream.close() } - static List readFrom(File jarFile, String resourceName) { - def zip = new ZipFile(jarFile) - try { - def entry = zip.getEntry(resourceName) - if (!entry) { - return Collections.emptyList() - } - return zip.getInputStream(entry).readLines() - } finally { - zip.close() - } + def targetLines = readFrom(testableZipFile, MANIFEST_NAME) + assertFalse(targetLines.isEmpty()) + assertTrue(targetLines.size() > 4) + + def trailer = targetLines.with { subList(size() - 5, size()) } + assertEquals(asList( + "Name: org/foo/bar/", + "Sealed: true", + "Name: com/example/", + "Sealed: false", + ""), trailer + ) + } + + @Test + void testNoTransformation() { + def sourceLines = getResourceStream(MANIFEST_NAME).readLines() + + transformer.transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections. emptyList(), new ShadowStats())) + + def testableZipFile = File.createTempFile("testable-zip-file-", ".jar") + def fileOutputStream = new FileOutputStream(testableZipFile) + def bufferedOutputStream = new BufferedOutputStream(fileOutputStream) + def zipOutputStream = new ZipOutputStream(bufferedOutputStream) + + try { + transformer.modifyOutputStream(zipOutputStream, true) + } finally { + zipOutputStream.close() } - - InputStream getResourceStream(String resource) { - this.class.classLoader.getResourceAsStream(resource) + def targetLines = readFrom(testableZipFile, MANIFEST_NAME) + + assertEquals(sourceLines, targetLines) + } + + static List readFrom(File jarFile, String resourceName) { + def zip = new ZipFile(jarFile) + try { + def entry = zip.getEntry(resourceName) + if (!entry) { + return Collections.emptyList() + } + return zip.getInputStream(entry).readLines() + } finally { + zip.close() } + } + + InputStream getResourceStream(String resource) { + this.class.classLoader.getResourceAsStream(resource) + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerTest.groovy index 46233585d..0e3e3c263 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerTest.groovy @@ -14,81 +14,81 @@ import static org.junit.jupiter.api.Assertions.* * Test for {@link PropertiesFileTransformer}. */ final class PropertiesFileTransformerTest extends TransformerTestSupport { - private static final String MANIFEST_NAME = "META-INF/MANIFEST.MF" + private static final String MANIFEST_NAME = "META-INF/MANIFEST.MF" - @BeforeEach - void setUp() { - transformer = new PropertiesFileTransformer(objectFactory) - } + @BeforeEach + void setUp() { + transformer = new PropertiesFileTransformer(objectFactory) + } - @Test - void testHasTransformedResource() { - transformer.transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME))) + @Test + void testHasTransformedResource() { + transformer.transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME))) - assertTrue(transformer.hasTransformedResource()) - } + assertTrue(transformer.hasTransformedResource()) + } - @Test - void testHasNotTransformedResource() { - assertFalse(transformer.hasTransformedResource()) - } + @Test + void testHasNotTransformedResource() { + assertFalse(transformer.hasTransformedResource()) + } - @Test - void testTransformation() { - transformer.transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections. emptyList(), new ShadowStats())) + @Test + void testTransformation() { + transformer.transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections. emptyList(), new ShadowStats())) - def testableZipFile = doTransformAndGetTransformedFile(transformer, false) - def targetLines = readFrom(testableZipFile, MANIFEST_NAME) + def testableZipFile = doTransformAndGetTransformedFile(transformer, false) + def targetLines = readFrom(testableZipFile, MANIFEST_NAME) - assertFalse(targetLines.isEmpty()) + assertFalse(targetLines.isEmpty()) - assertTrue(targetLines.contains("Manifest-Version=1.0")) - } + assertTrue(targetLines.contains("Manifest-Version=1.0")) + } - @Test - void testTransformationPropertiesAreReproducible() { - transformer.transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections. emptyList(), new ShadowStats())) + @Test + void testTransformationPropertiesAreReproducible() { + transformer.transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections. emptyList(), new ShadowStats())) - def firstRunTransformedFile = doTransformAndGetTransformedFile(transformer, true) - def firstRunTargetLines = readFrom(firstRunTransformedFile, MANIFEST_NAME) + def firstRunTransformedFile = doTransformAndGetTransformedFile(transformer, true) + def firstRunTargetLines = readFrom(firstRunTransformedFile, MANIFEST_NAME) - Thread.sleep(1000) // wait for 1sec to ensure timestamps in properties would change + Thread.sleep(1000) // wait for 1sec to ensure timestamps in properties would change - def secondRunTransformedFile = doTransformAndGetTransformedFile(transformer, true) - def secondRunTargetLines = readFrom(secondRunTransformedFile, MANIFEST_NAME) + def secondRunTransformedFile = doTransformAndGetTransformedFile(transformer, true) + def secondRunTargetLines = readFrom(secondRunTransformedFile, MANIFEST_NAME) - assertEquals(firstRunTargetLines, secondRunTargetLines) - } - - static File doTransformAndGetTransformedFile(final PropertiesFileTransformer transformer, final boolean preserveFileTimestamps) { - def testableZipFile = File.createTempFile("testable-zip-file-", ".jar") - def fileOutputStream = new FileOutputStream(testableZipFile) - def bufferedOutputStream = new BufferedOutputStream(fileOutputStream) - def zipOutputStream = new ZipOutputStream(bufferedOutputStream) + assertEquals(firstRunTargetLines, secondRunTargetLines) + } - try { - transformer.modifyOutputStream(zipOutputStream, preserveFileTimestamps) - } finally { - zipOutputStream.close() - } + static File doTransformAndGetTransformedFile(final PropertiesFileTransformer transformer, final boolean preserveFileTimestamps) { + def testableZipFile = File.createTempFile("testable-zip-file-", ".jar") + def fileOutputStream = new FileOutputStream(testableZipFile) + def bufferedOutputStream = new BufferedOutputStream(fileOutputStream) + def zipOutputStream = new ZipOutputStream(bufferedOutputStream) - return testableZipFile + try { + transformer.modifyOutputStream(zipOutputStream, preserveFileTimestamps) + } finally { + zipOutputStream.close() } - static List readFrom(File jarFile, String resourceName) { - def zip = new ZipFile(jarFile) - try { - def entry = zip.getEntry(resourceName) - if (!entry) { - return Collections.emptyList() - } - return zip.getInputStream(entry).readLines() - } finally { - zip.close() - } + return testableZipFile + } + + static List readFrom(File jarFile, String resourceName) { + def zip = new ZipFile(jarFile) + try { + def entry = zip.getEntry(resourceName) + if (!entry) { + return Collections.emptyList() + } + return zip.getInputStream(entry).readLines() + } finally { + zip.close() } + } - InputStream getResourceStream(String resource) { - this.class.classLoader.getResourceAsStream(resource) - } + InputStream getResourceStream(String resource) { + this.class.classLoader.getResourceAsStream(resource) + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.groovy index a89d1ed41..67e448b3d 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.groovy @@ -6,10 +6,10 @@ import org.gradle.api.internal.file.DefaultFileTreeElement import org.gradle.testfixtures.ProjectBuilder abstract class TransformerTestSupport { - protected static T transformer - protected static final def objectFactory = ProjectBuilder.builder().build().objects + protected static T transformer + protected static final def objectFactory = ProjectBuilder.builder().build().objects - protected static FileTreeElement getFileElement(String path) { - return new DefaultFileTreeElement(null, RelativePath.parse(true, path), null, null) - } + protected static FileTreeElement getFileElement(String path) { + return new DefaultFileTreeElement(null, RelativePath.parse(true, path), null, null) + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.groovy index 51cfb6c62..02913b03d 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.groovy @@ -1,22 +1,3 @@ -/* - * 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. - */ - package com.github.jengelman.gradle.plugins.shadow.transformers import org.junit.jupiter.api.BeforeEach @@ -26,35 +7,29 @@ import static org.junit.jupiter.api.Assertions.assertFalse import static org.junit.jupiter.api.Assertions.assertTrue /** - * Test for {@link XmlAppendingTransformer}. - * - * @author Benjamin Bentmann - * @version $Id: XmlAppendingTransformerTest.java 673906 2008-07-04 05:03:20Z brett $ - * * Modified from org.apache.maven.plugins.shade.resource.XmlAppendingTransformerTest.java */ class XmlAppendingTransformerTest extends TransformerTestSupport { - static { - /* - * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime - * choice to test for improper case-less string comparisons. - */ - Locale.setDefault(new Locale("tr")) - } - - @BeforeEach - void setUp() { - transformer = new XmlAppendingTransformer(objectFactory) - } - - @Test - void testCanTransformResource() { - transformer.resource.set("abcdefghijklmnopqrstuvwxyz") - - assertTrue(transformer.canTransformResource(getFileElement("abcdefghijklmnopqrstuvwxyz"))) - assertTrue(transformer.canTransformResource(getFileElement("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))) - assertFalse(transformer.canTransformResource(getFileElement("META-INF/MANIFEST.MF"))) - } - + static { + /* + * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime + * choice to test for improper case-less string comparisons. + */ + Locale.setDefault(new Locale("tr")) + } + + @BeforeEach + void setUp() { + transformer = new XmlAppendingTransformer(objectFactory) + } + + @Test + void testCanTransformResource() { + transformer.resource.set("abcdefghijklmnopqrstuvwxyz") + + assertTrue(transformer.canTransformResource(getFileElement("abcdefghijklmnopqrstuvwxyz"))) + assertTrue(transformer.canTransformResource(getFileElement("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))) + assertFalse(transformer.canTransformResource(getFileElement("META-INF/MANIFEST.MF"))) + } } From 002cc5d0667f4ee9013e59f23e0505e850525b3b Mon Sep 17 00:00:00 2001 From: Goooler Date: Sat, 30 Nov 2024 17:04:54 +0800 Subject: [PATCH 2/2] Fix merge --- .../ApacheLicenseResourceTransformerTest.groovy | 6 +----- .../ApacheNoticeResourceTransformerTest.groovy | 6 +----- .../shadow/transformers/AppendingTransformerTest.groovy | 6 +----- .../shadow/transformers/TransformerTestSupport.groovy | 8 ++++++++ .../transformers/XmlAppendingTransformerTest.groovy | 6 +----- 5 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.groovy index f3dc80759..27117c51c 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.groovy @@ -12,11 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue class ApacheLicenseResourceTransformerTest extends TransformerTestSupport { static { - /* - * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime - * choice to test for improper case-less string comparisions. - */ - Locale.setDefault(new Locale("tr")) + setupTurkishLocale() } @BeforeEach diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy index d7a01aef0..0490305e1 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy @@ -16,11 +16,7 @@ class ApacheNoticeResourceTransformerTest extends TransformerTestSupport { static { - /* - * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime - * choice to test for improper case-less string comparisions. - */ - Locale.setDefault(new Locale("tr")) + setupTurkishLocale() } @BeforeEach diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.groovy index 67e448b3d..4cca70207 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.groovy @@ -12,4 +12,12 @@ abstract class TransformerTestSupport { protected static FileTreeElement getFileElement(String path) { return new DefaultFileTreeElement(null, RelativePath.parse(true, path), null, null) } + + /** + * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime + * choice to test for improper case-less string comparisons. + */ + protected static setupTurkishLocale() { + Locale.setDefault(new Locale("tr")) + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.groovy index 02913b03d..71c02736c 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.groovy @@ -12,11 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue class XmlAppendingTransformerTest extends TransformerTestSupport { static { - /* - * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime - * choice to test for improper case-less string comparisons. - */ - Locale.setDefault(new Locale("tr")) + setupTurkishLocale() } @BeforeEach