View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.shade.resource;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.InputStream;
26  import java.util.Collections;
27  import java.util.Properties;
28  import java.util.jar.JarFile;
29  import java.util.jar.JarOutputStream;
30  import java.util.zip.ZipEntry;
31  
32  import org.apache.maven.plugins.shade.relocation.Relocator;
33  import org.junit.Test;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertFalse;
37  import static org.junit.Assert.assertNull;
38  import static org.junit.Assert.assertTrue;
39  
40  /**
41   * Test for {@link GroovyResourceTransformer}.
42   *
43   */
44  public class GroovyResourceTransformerTest {
45  
46      private static InputStream stream(Properties props) throws Exception {
47          ByteArrayOutputStream baos = new ByteArrayOutputStream();
48          props.store(baos, null);
49          return new ByteArrayInputStream(baos.toByteArray());
50      }
51  
52      private static InputStream module(
53              String name, String version, String extensionClasses, String staticExtensionClasses) throws Exception {
54          Properties desc = new Properties();
55          desc.setProperty("moduleName", name);
56          desc.setProperty("moduleVersion", version);
57          if (extensionClasses != null) {
58              desc.setProperty("extensionClasses", extensionClasses);
59          }
60          if (staticExtensionClasses != null) {
61              desc.setProperty("staticExtensionClasses", staticExtensionClasses);
62          }
63          return stream(desc);
64      }
65  
66      private static Properties transform(GroovyResourceTransformer transformer) throws Exception {
67          File tempJar = File.createTempFile("shade.", ".jar");
68          tempJar.deleteOnExit();
69  
70          try (FileOutputStream fos = new FileOutputStream(tempJar);
71                  JarOutputStream jaos = new JarOutputStream(fos)) {
72              transformer.modifyOutputStream(jaos);
73          }
74  
75          Properties desc = null;
76          try (JarFile jar = new JarFile(tempJar)) {
77              ZipEntry entry = jar.getEntry(GroovyResourceTransformer.EXT_MODULE_NAME);
78              if (entry != null) {
79                  desc = new Properties();
80                  desc.load(jar.getInputStream(entry));
81              }
82          }
83          return desc;
84      }
85  
86      @Test
87      public void testFilter() {
88          GroovyResourceTransformer transformer = new GroovyResourceTransformer();
89          assertTrue(transformer.canTransformResource(GroovyResourceTransformer.EXT_MODULE_NAME));
90          assertTrue(transformer.canTransformResource(GroovyResourceTransformer.EXT_MODULE_NAME_LEGACY));
91          assertFalse(transformer.canTransformResource("somethingElse"));
92          assertFalse(transformer.canTransformResource(JarFile.MANIFEST_NAME));
93      }
94  
95      @Test
96      public void testEmpty() throws Exception {
97          GroovyResourceTransformer transformer = new GroovyResourceTransformer();
98          assertFalse(transformer.hasTransformedResource());
99          assertNull(transform(transformer));
100     }
101 
102     @Test
103     public void testSpecifyModuleName() throws Exception {
104         GroovyResourceTransformer transformer = new GroovyResourceTransformer();
105         transformer.setExtModuleName("the-module-name");
106         transformer.setExtModuleVersion("2.0");
107         transformer.processResource(
108                 GroovyResourceTransformer.EXT_MODULE_NAME,
109                 module("mod1", "1.0", "some.ext", "some.staticExt"),
110                 Collections.<Relocator>emptyList(),
111                 0);
112         Properties desc = transform(transformer);
113         assertEquals("the-module-name", desc.getProperty("moduleName"));
114         assertEquals("2.0", desc.getProperty("moduleVersion"));
115         assertEquals("some.ext", desc.getProperty("extensionClasses"));
116         assertEquals("some.staticExt", desc.getProperty("staticExtensionClasses"));
117     }
118 
119     @Test
120     public void testConcatenation() throws Exception {
121         GroovyResourceTransformer transformer = new GroovyResourceTransformer();
122         transformer.processResource(
123                 GroovyResourceTransformer.EXT_MODULE_NAME,
124                 module("mod1", "1.0", "some.ext1", null),
125                 Collections.<Relocator>emptyList(),
126                 0);
127         transformer.processResource(
128                 GroovyResourceTransformer.EXT_MODULE_NAME,
129                 module("mod2", "1.0", null, "some.staticExt1"),
130                 Collections.<Relocator>emptyList(),
131                 0);
132         transformer.processResource(
133                 GroovyResourceTransformer.EXT_MODULE_NAME,
134                 module("mod3", "1.0", "", ""),
135                 Collections.<Relocator>emptyList(),
136                 0);
137         transformer.processResource(
138                 GroovyResourceTransformer.EXT_MODULE_NAME,
139                 module("mod4", "1.0", "some.ext2", "some.staticExt2"),
140                 Collections.<Relocator>emptyList(),
141                 0);
142         Properties desc = transform(transformer);
143         assertEquals("no-module-name", desc.getProperty("moduleName"));
144         assertEquals("1.0", desc.getProperty("moduleVersion"));
145         assertEquals("some.ext1,some.ext2", desc.getProperty("extensionClasses"));
146         assertEquals("some.staticExt1,some.staticExt2", desc.getProperty("staticExtensionClasses"));
147     }
148 }