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