View Javadoc
1   package org.apache.maven.plugins.shade.resource;
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  import org.apache.maven.plugins.shade.relocation.Relocator;
23  import org.junit.Test;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.ByteArrayOutputStream;
27  import java.io.File;
28  import java.io.FileOutputStream;
29  import java.io.InputStream;
30  import java.util.Collections;
31  import java.util.Properties;
32  import java.util.jar.JarFile;
33  import java.util.jar.JarOutputStream;
34  import java.util.zip.ZipEntry;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertFalse;
38  import static org.junit.Assert.assertNull;
39  import static org.junit.Assert.assertTrue;
40  
41  /**
42   * Test for {@link GroovyResourceTransformer}.
43   *
44   */
45  public class GroovyResourceTransformerTest
46  {
47  
48      private static InputStream stream( Properties props )
49          throws Exception
50      {
51          ByteArrayOutputStream baos = new ByteArrayOutputStream();
52          props.store( baos, null );
53          return new ByteArrayInputStream( baos.toByteArray() );
54  
55      }
56  
57      private static InputStream module( String name, String version, String extensionClasses,
58                                         String staticExtensionClasses )
59          throws Exception
60      {
61          Properties desc = new Properties();
62          desc.setProperty( "moduleName", name );
63          desc.setProperty( "moduleVersion", version );
64          if ( extensionClasses != null )
65          {
66              desc.setProperty( "extensionClasses", extensionClasses );
67          }
68          if ( staticExtensionClasses != null )
69          {
70              desc.setProperty( "staticExtensionClasses", staticExtensionClasses );
71          }
72          return stream( desc );
73      }
74  
75      private static Properties transform( GroovyResourceTransformer transformer )
76          throws Exception
77      {
78          File tempJar = File.createTempFile( "shade.", ".jar" );
79          tempJar.deleteOnExit();
80  
81          try ( FileOutputStream fos = new FileOutputStream( tempJar );
82                JarOutputStream jaos = new JarOutputStream( fos ) )
83          {
84              transformer.modifyOutputStream( jaos );
85          }
86          
87          Properties desc = null;
88          try ( JarFile jar = new JarFile( tempJar ) )
89          {
90              ZipEntry entry = jar.getEntry( GroovyResourceTransformer.EXT_MODULE_NAME );
91              if ( entry != null )
92              {
93                  desc = new Properties();
94                  desc.load( jar.getInputStream( entry ) );
95              }
96          }
97          return desc;
98      }
99  
100     @Test
101     public void testFilter()
102     {
103         GroovyResourceTransformer transformer = new GroovyResourceTransformer();
104         assertTrue( transformer.canTransformResource( GroovyResourceTransformer.EXT_MODULE_NAME ) );
105         assertTrue( transformer.canTransformResource( GroovyResourceTransformer.EXT_MODULE_NAME_LEGACY ) );
106         assertFalse( transformer.canTransformResource( "somethingElse" ) );
107         assertFalse( transformer.canTransformResource( JarFile.MANIFEST_NAME ) );
108     }
109 
110     @Test
111     public void testEmpty()
112         throws Exception
113     {
114         GroovyResourceTransformer transformer = new GroovyResourceTransformer();
115         assertFalse( transformer.hasTransformedResource() );
116         assertNull( transform( transformer ) );
117     }
118 
119     @Test
120     public void testSpecifyModuleName()
121         throws Exception
122     {
123         GroovyResourceTransformer transformer = new GroovyResourceTransformer();
124         transformer.setExtModuleName( "the-module-name" );
125         transformer.setExtModuleVersion( "2.0" );
126         transformer.processResource( GroovyResourceTransformer.EXT_MODULE_NAME,
127                                      module( "mod1", "1.0", "some.ext", "some.staticExt" ),
128                                      Collections.<Relocator>emptyList(), 0 );
129         Properties desc = transform( transformer );
130         assertEquals( "the-module-name", desc.getProperty( "moduleName" ) );
131         assertEquals( "2.0", desc.getProperty( "moduleVersion" ) );
132         assertEquals( "some.ext", desc.getProperty( "extensionClasses" ) );
133         assertEquals( "some.staticExt", desc.getProperty( "staticExtensionClasses" ) );
134     }
135 
136     @Test
137     public void testConcatenation()
138         throws Exception
139     {
140         GroovyResourceTransformer transformer = new GroovyResourceTransformer();
141         transformer.processResource( GroovyResourceTransformer.EXT_MODULE_NAME,
142                                      module( "mod1", "1.0", "some.ext1", null ),
143                                      Collections.<Relocator>emptyList(), 0 );
144         transformer.processResource( GroovyResourceTransformer.EXT_MODULE_NAME,
145                                      module( "mod2", "1.0", null, "some.staticExt1" ),
146                                      Collections.<Relocator>emptyList(), 0 );
147         transformer.processResource( GroovyResourceTransformer.EXT_MODULE_NAME,
148                                      module( "mod3", "1.0", "", "" ),
149                                      Collections.<Relocator>emptyList(), 0 );
150         transformer.processResource( GroovyResourceTransformer.EXT_MODULE_NAME,
151                                      module( "mod4", "1.0", "some.ext2", "some.staticExt2" ),
152                                      Collections.<Relocator>emptyList(), 0 );
153         Properties desc = transform( transformer );
154         assertEquals( "no-module-name", desc.getProperty( "moduleName" ) );
155         assertEquals( "1.0", desc.getProperty( "moduleVersion" ) );
156         assertEquals( "some.ext1,some.ext2", desc.getProperty( "extensionClasses" ) );
157         assertEquals( "some.staticExt1,some.staticExt2", desc.getProperty( "staticExtensionClasses" ) );
158     }
159 }