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 static org.junit.Assert.assertEquals;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.util.Arrays;
29  import java.util.Collections;
30  import java.util.List;
31  import java.util.jar.Attributes;
32  import java.util.jar.JarFile;
33  import java.util.jar.JarInputStream;
34  import java.util.jar.JarOutputStream;
35  import java.util.jar.Manifest;
36  
37  import org.apache.maven.plugins.shade.relocation.Relocator;
38  import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
39  import org.junit.Before;
40  import org.junit.Test;
41  
42  public class ManifestResourceTransformerTest
43  {
44      private ManifestResourceTransformer transformer;
45      
46      @Before
47      public void setUp() 
48      {
49          transformer = new ManifestResourceTransformer();
50      }
51  
52      @Test
53      public void rewriteDefaultAttributes() throws Exception
54      {
55          final Manifest manifest = new Manifest();
56          final Attributes attributes = manifest.getMainAttributes();
57          attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
58          attributes.putValue("Export-Package",
59                  "javax.decorator;version=\"2.0\";uses:=\"javax.enterprise.inject\"," +
60                          "javax.enterprise.context;version=\"2.0\";uses:=\"javax.enterprise.util,javax.inject\"");
61          attributes.putValue("Import-Package",
62                  "javax.el,javax.enterprise.context;version=\"[2.0,3)\"");
63          attributes.putValue("Provide-Capability",
64                  "osgi.contract;osgi.contract=JavaCDI;uses:=\"" +
65                          "javax.enterprise.context,javax.enterprise.context.spi,javax.enterprise.context.control," +
66                          "javax.enterprise.util,javax.enterprise.inject,javax.enterprise.inject.spi," +
67                          "javax.enterprise.inject.spi.configurator,javax.enterprise.inject.literal," +
68                          "javax.enterprise.inject.se,javax.enterprise.event,javax.decorator\";" +
69                          "version:List<Version>=\"2.0,1.2,1.1,1.0\"");
70          attributes.putValue("Require-Capability",
71                  "osgi.serviceloader;" +
72                          "filter:=\"(osgi.serviceloader=javax.enterprise.inject.se.SeContainerInitializer)\";" +
73                          "cardinality:=multiple," +
74                          "osgi.serviceloader;" +
75                          "filter:=\"(osgi.serviceloader=javax.enterprise.inject.spi.CDIProvider)\";" +
76                          "cardinality:=multiple,osgi.extender;" +
77                          "filter:=\"(osgi.extender=osgi.serviceloader.processor)\"," +
78                          "osgi.contract;osgi.contract=JavaEL;filter:=\"(&(osgi.contract=JavaEL)(version=2.2.0))\"," +
79                          "osgi.contract;osgi.contract=JavaInterceptor;" +
80                          "filter:=\"(&(osgi.contract=JavaInterceptor)(version=1.2.0))\"," +
81                          "osgi.contract;osgi.contract=JavaInject;" +
82                          "filter:=\"(&(osgi.contract=JavaInject)(version=1.0.0))\"," +
83                          "osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.8))\"");
84          
85          List<Relocator> relocators =
86              Collections.<Relocator>singletonList( new SimpleRelocator( "javax", "jakarta",
87                                                                         Collections.<String>emptyList(),
88                                                                         Collections.<String>emptyList() ) );
89          
90          final ByteArrayOutputStream out = transform( manifest, relocators );
91  
92          try ( final JarInputStream jis = new JarInputStream( new ByteArrayInputStream( out.toByteArray() ) ) )
93          {
94              final Attributes attrs = jis.getManifest().getMainAttributes();
95              assertEquals(
96                      "jakarta.decorator;version=\"2.0\";uses:=\"jakarta.enterprise.inject\"," +
97                      "jakarta.enterprise.context;version=\"2.0\";uses:=\"jakarta.enterprise.util," +
98                      "jakarta.inject\"",
99                      attrs.getValue("Export-Package"));
100             assertEquals("jakarta.el,jakarta.enterprise.context;version=\"[2.0,3)\"",
101                     attrs.getValue("Import-Package"));
102             assertEquals(
103                     "osgi.contract;osgi.contract=JavaCDI;" +
104                             "uses:=\"jakarta.enterprise.context," +
105                             "jakarta.enterprise.context.spi,jakarta.enterprise.context.control," +
106                             "jakarta.enterprise.util,jakarta.enterprise.inject,jakarta.enterprise.inject.spi," +
107                             "jakarta.enterprise.inject.spi.configurator,jakarta.enterprise.inject.literal," +
108                             "jakarta.enterprise.inject.se,jakarta.enterprise.event," +
109                             "jakarta.decorator\";version:List<Version>=\"2.0,1.2,1.1,1.0\"",
110                     attrs.getValue("Provide-Capability"));
111             assertEquals(
112                     "osgi.serviceloader;" +
113                             "filter:=\"(osgi.serviceloader=jakarta.enterprise.inject.se.SeContainerInitializer)\";" +
114                             "cardinality:=multiple,osgi.serviceloader;" +
115                             "filter:=\"(osgi.serviceloader=jakarta.enterprise.inject.spi.CDIProvider)\";" +
116                             "cardinality:=multiple,osgi.extender;" +
117                             "filter:=\"(osgi.extender=osgi.serviceloader.processor)\"," +
118                             "osgi.contract;osgi.contract=JavaEL;filter:=\"(&(osgi.contract=JavaEL)(version=2.2.0))\"," +
119                             "osgi.contract;osgi.contract=JavaInterceptor;" +
120                             "filter:=\"(&(osgi.contract=JavaInterceptor)(version=1.2.0))\"," +
121                             "osgi.contract;osgi.contract=JavaInject;" +
122                             "filter:=\"(&(osgi.contract=JavaInject)(version=1.0.0))\"," +
123                             "osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.8))\"",
124                     attrs.getValue("Require-Capability"));
125         }
126     }
127     
128     @Test
129     public void rewriteAdditionalAttributes() throws Exception
130     {
131         final Manifest manifest = new Manifest();
132         final Attributes attributes = manifest.getMainAttributes();
133         attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
134         attributes.putValue("description-custom",
135                 "This jar uses javax packages");
136         
137         List<Relocator> relocators =
138             Collections.<Relocator>singletonList( new SimpleRelocator( "javax", "jakarta",
139                                                                        Collections.<String>emptyList(),
140                                                                        Collections.<String>emptyList() ) );
141         
142         transformer.setAdditionalAttributes( Arrays.asList("description-custom", "attribute-unknown") );
143         final ByteArrayOutputStream out = transform( manifest, relocators );
144 
145         try ( final JarInputStream jis = new JarInputStream( new ByteArrayInputStream( out.toByteArray() ) ) )
146         {
147             final Attributes attrs = jis.getManifest().getMainAttributes();
148             assertEquals( "This jar uses jakarta packages", attrs.getValue( "description-custom" ) );
149         }
150     }
151 
152     private ByteArrayOutputStream transform( final Manifest manifest, List<Relocator> relocators )
153         throws IOException
154     {
155         final ByteArrayOutputStream mboas = new ByteArrayOutputStream();
156         try ( final OutputStream mos = mboas )
157         {
158             manifest.write( mos );
159         }
160         transformer.processResource( JarFile.MANIFEST_NAME, new ByteArrayInputStream( mboas.toByteArray() ),
161                                      relocators, 0 );
162 
163         final ByteArrayOutputStream out = new ByteArrayOutputStream();
164         try ( final JarOutputStream jarOutputStream = new JarOutputStream( out ) )
165         {
166             transformer.modifyOutputStream( jarOutputStream );
167         }
168         return out;
169     }
170 }