1 package org.codehaus.plexus.archiver.jar;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.jar.Attributes;
7 import java.util.jar.Manifest;
8
9 import org.codehaus.plexus.archiver.TestSupport;
10 import org.codehaus.plexus.archiver.util.Streams;
11 import org.junit.jupiter.api.Test;
12
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertNotNull;
15 import static org.junit.jupiter.api.Assertions.fail;
16
17
18
19
20 class JdkManifestFactoryTest extends TestSupport {
21
22 @Test
23 void testGetDefaultManifest() throws Exception {
24 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
25 Manifest manifest = JdkManifestFactory.getDefaultManifest();
26 manifest.write(byteArrayOutputStream);
27 System.out.println(byteArrayOutputStream);
28 }
29
30 @Test
31 void testGetDefaultManifestString() throws Exception {
32 Manifest manifest = getManifest("src/test/resources/manifests/manifestWithClassPath.mf");
33 Manifest manifestWithout = getManifest("src/test/resources/manifests/manifest1.mf");
34 String value = manifest.getMainAttributes().getValue(ManifestConstants.ATTRIBUTE_CLASSPATH);
35 System.out.println("value = " + value);
36 manifestWithout.getMainAttributes().putValue(ManifestConstants.ATTRIBUTE_CLASSPATH, value);
37 String value2 = manifestWithout.getMainAttributes().getValue(ManifestConstants.ATTRIBUTE_CLASSPATH);
38
39 assertEquals(value, value2);
40 }
41
42 @Test
43 void testIllegals() throws ManifestException, IOException {
44 Manifest manifest = getManifest("src/test/resources/manifests/manifest6.mf");
45 assertNotNull(manifest);
46
47 try {
48 getManifest("src/test/resources/manifests/manifest5.mf");
49 fail("We expect to fail");
50 } catch (IOException ignore) {
51
52 }
53 }
54
55 @Test
56 void testMerge() throws ManifestException, IOException {
57 Manifest manifest1 = getManifest("src/test/resources/manifests/manifestMerge1.mf");
58 Manifest manifest2 = getManifest("src/test/resources/manifests/manifestMerge2.mf");
59
60 Manifest target = new Manifest();
61 JdkManifestFactory.merge(target, manifest1, false);
62
63 assertEquals("001", target.getMainAttributes().getValue("Bar"));
64 Attributes fudz = target.getAttributes("Fudz");
65 assertNotNull(fudz);
66 assertEquals("002", fudz.getValue("Bar"));
67 Attributes redz = target.getAttributes("Redz");
68 assertNotNull(redz);
69 assertEquals("002", redz.getValue("Baz"));
70
71 JdkManifestFactory.merge(target, manifest2, false);
72
73 assertEquals("001", target.getMainAttributes().getValue("Bar"));
74 fudz = target.getAttributes("Fudz");
75 assertNotNull(fudz);
76 assertEquals("003", fudz.getValue("Bar"));
77 redz = target.getAttributes("Redz");
78 assertNotNull(redz);
79 assertEquals("002", redz.getValue("Baz"));
80 }
81
82 @Test
83 void testDualClassPath() throws ManifestException, IOException {
84 Manifest manifest = getManifest("src/test/resources/manifests/manifestWithDualClassPath.mf");
85 final Attributes mainAttributes = manifest.getMainAttributes();
86 final String attribute = mainAttributes.getValue("Class-Path");
87
88 }
89
90
91
92
93 private java.util.jar.Manifest getManifest(String filename) throws IOException, ManifestException {
94 try (InputStream r = Streams.fileInputStream(getTestFile(filename))) {
95 return new Manifest(r);
96 }
97 }
98 }