1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package libcore.java.util.jar;
18
19import java.io.ByteArrayInputStream;
20import java.io.ByteArrayOutputStream;
21import java.io.File;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.net.MalformedURLException;
25import java.net.URL;
26import java.util.jar.Attributes;
27import java.util.jar.Manifest;
28import junit.framework.TestCase;
29import tests.support.resource.Support_Resources;
30
31public class OldManifestTest extends TestCase {
32
33    public void test_ConstructorLjava_util_jar_Manifest() {
34        // Test for method java.util.jar.Manifest()
35        Manifest emptyManifest = new Manifest();
36        Manifest emptyClone = new Manifest(emptyManifest);
37        assertTrue("Should have no entries", emptyClone.getEntries().isEmpty());
38        assertTrue("Should have no main attributes", emptyClone
39                .getMainAttributes().isEmpty());
40        assertEquals(emptyClone, emptyManifest);
41        assertEquals(emptyClone, emptyManifest.clone());
42    }
43
44    private void assertAttribute(Attributes attr, String name, String value) {
45        assertEquals("Incorrect " + name, value, attr.getValue(name));
46    }
47
48    private void checkManifest(Manifest manifest) {
49        Attributes main = manifest.getMainAttributes();
50        assertAttribute(main, "Bundle-Name", "ClientSupport");
51        assertAttribute(main, "Bundle-Description",
52                "Provides SessionService, AuthenticationService. Extends RegistryService.");
53        assertAttribute(main, "Bundle-Activator",
54                "com.ibm.ive.eccomm.client.support.ClientSupportActivator");
55        assertAttribute(
56                main,
57                "Import-Package",
58                "com.ibm.ive.eccomm.client.services.log,com.ibm.ive.eccomm.client.services.registry,com.ibm.ive.eccomm.service.registry; specification-version=1.0.0,com.ibm.ive.eccomm.service.session; specification-version=1.0.0,com.ibm.ive.eccomm.service.framework; specification-version=1.2.0,org.osgi.framework; specification-version=1.0.0,org.osgi.service.log; specification-version=1.0.0,com.ibm.ive.eccomm.flash; specification-version=1.2.0,com.ibm.ive.eccomm.client.xml,com.ibm.ive.eccomm.client.http.common,com.ibm.ive.eccomm.client.http.client");
59        assertAttribute(
60                main,
61                "Import-Service",
62                "org.osgi.service.log.LogReaderServiceorg.osgi.service.log.LogService,com.ibm.ive.eccomm.service.registry.RegistryService");
63        assertAttribute(
64                main,
65                "Export-Package",
66                "com.ibm.ive.eccomm.client.services.authentication; specification-version=1.0.0,com.ibm.ive.eccomm.service.authentication; specification-version=1.0.0,com.ibm.ive.eccomm.common; specification-version=1.0.0,com.ibm.ive.eccomm.client.services.registry.store; specification-version=1.0.0");
67        assertAttribute(
68                main,
69                "Export-Service",
70                "com.ibm.ive.eccomm.service.authentication.AuthenticationService,com.ibm.ive.eccomm.service.session.SessionService");
71        assertAttribute(main, "Bundle-Vendor", "IBM");
72        assertAttribute(main, "Bundle-Version", "1.2.0");
73    }
74
75    public void test_clone() throws IOException {
76        Manifest emptyManifest = new Manifest();
77        Manifest emptyClone = (Manifest) emptyManifest.clone();
78        assertTrue("Should have no entries", emptyClone.getEntries().isEmpty());
79        assertTrue("Should have no main attributes", emptyClone
80                .getMainAttributes().isEmpty());
81        assertEquals(emptyClone, emptyManifest);
82        assertEquals(emptyManifest.clone().getClass().getName(),
83                "java.util.jar.Manifest");
84
85        Manifest manifest = new Manifest(new URL(Support_Resources
86                .getURL("manifest/hyts_MANIFEST.MF")).openStream());
87        Manifest manifestClone = (Manifest) manifest.clone();
88        manifestClone.getMainAttributes();
89        checkManifest(manifestClone);
90    }
91
92    public void test_equals() throws IOException {
93        Manifest manifest1 = new Manifest(new URL(Support_Resources.getURL(
94                "manifest/hyts_MANIFEST.MF")).openStream());
95        Manifest manifest2 = new Manifest(new URL(Support_Resources.getURL(
96                "manifest/hyts_MANIFEST.MF")).openStream());
97        Manifest manifest3 = new Manifest();
98
99        assertTrue(manifest1.equals(manifest1));
100        assertTrue(manifest1.equals(manifest2));
101        assertFalse(manifest1.equals(manifest3));
102        assertFalse(manifest1.equals(this));
103    }
104
105    public void test_writeLjava_io_OutputStream() throws IOException {
106        byte b[];
107        Manifest manifest1 = null;
108        Manifest manifest2 = null;
109        try {
110            manifest1 = new Manifest(new URL(Support_Resources
111                    .getURL("manifest/hyts_MANIFEST.MF")).openStream());
112        } catch (MalformedURLException e) {
113            fail("Malformed URL");
114        }
115
116        ByteArrayOutputStream baos = new ByteArrayOutputStream();
117
118        manifest1.write(baos);
119
120        b = baos.toByteArray();
121
122        File f = File.createTempFile("111", "111");
123        FileOutputStream fos = new FileOutputStream(f);
124        fos.close();
125        try {
126            manifest1.write(fos);
127            fail("IOException expected");
128        } catch (IOException e) {
129            // expected
130        }
131        f.delete();
132
133        ByteArrayInputStream bais = new ByteArrayInputStream(b);
134
135        try {
136            manifest2 = new Manifest(bais);
137        } catch (MalformedURLException e) {
138            fail("Malformed URL");
139        }
140
141        assertTrue(manifest1.equals(manifest2));
142    }
143
144    public void test_write_no_version() throws Exception {
145        // If you write a manifest with no MANIFEST_VERSION, your attributes don't get written out.
146        assertEquals(null, doRoundTrip(null));
147        // But they do if you supply a MANIFEST_VERSION.
148        assertEquals("image/pr0n", doRoundTrip(Attributes.Name.MANIFEST_VERSION));
149        assertEquals("image/pr0n", doRoundTrip("Signature-Version"));
150        assertEquals(null, doRoundTrip("Random-String-Version"));
151    }
152
153    private String doRoundTrip(Object versionName) throws Exception {
154        Manifest m1 = new Manifest();
155        m1.getMainAttributes().put(Attributes.Name.CONTENT_TYPE, "image/pr0n");
156        if (versionName != null) {
157            m1.getMainAttributes().putValue(versionName.toString(), "1.2.3");
158        }
159        ByteArrayOutputStream os = new ByteArrayOutputStream();
160        m1.write(os);
161
162        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
163        Manifest m2 = new Manifest();
164        m2.read(is);
165        return (String) m2.getMainAttributes().get(Attributes.Name.CONTENT_TYPE);
166    }
167
168    public void test_write_two_versions() throws Exception {
169        // It's okay to have two versions.
170        Manifest m1 = new Manifest();
171        m1.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
172        m1.getMainAttributes().put(Attributes.Name.SIGNATURE_VERSION, "2.0");
173        m1.getMainAttributes().putValue("Aardvark-Version", "3.0");
174        ByteArrayOutputStream os = new ByteArrayOutputStream();
175        m1.write(os);
176
177        // The Manifest-Version takes precedence,
178        // and the Signature-Version gets no special treatment.
179        String[] lines = new String(os.toByteArray(), "UTF-8").split("\r\n");
180        assertEquals("Manifest-Version: 1.0", lines[0]);
181        assertEquals("Aardvark-Version: 3.0", lines[1]);
182        assertEquals("Signature-Version: 2.0", lines[2]);
183    }
184}
185