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