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.Arrays; 27import java.util.List; 28import java.util.jar.Attributes; 29import java.util.jar.Manifest; 30import junit.framework.TestCase; 31import tests.support.resource.Support_Resources; 32 33public class OldManifestTest extends TestCase { 34 35 public void test_ConstructorLjava_util_jar_Manifest() { 36 // Test for method java.util.jar.Manifest() 37 Manifest emptyManifest = new Manifest(); 38 Manifest emptyClone = new Manifest(emptyManifest); 39 assertTrue("Should have no entries", emptyClone.getEntries().isEmpty()); 40 assertTrue("Should have no main attributes", emptyClone 41 .getMainAttributes().isEmpty()); 42 assertEquals(emptyClone, emptyManifest); 43 assertEquals(emptyClone, emptyManifest.clone()); 44 } 45 46 private void assertAttribute(Attributes attr, String name, String value) { 47 assertEquals("Incorrect " + name, value, attr.getValue(name)); 48 } 49 50 private void checkManifest(Manifest manifest) { 51 Attributes main = manifest.getMainAttributes(); 52 assertAttribute(main, "Bundle-Name", "ClientSupport"); 53 assertAttribute(main, "Bundle-Description", 54 "Provides SessionService, AuthenticationService. Extends RegistryService."); 55 assertAttribute(main, "Bundle-Activator", 56 "com.ibm.ive.eccomm.client.support.ClientSupportActivator"); 57 assertAttribute( 58 main, 59 "Import-Package", 60 "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"); 61 assertAttribute( 62 main, 63 "Import-Service", 64 "org.osgi.service.log.LogReaderServiceorg.osgi.service.log.LogService,com.ibm.ive.eccomm.service.registry.RegistryService"); 65 assertAttribute( 66 main, 67 "Export-Package", 68 "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"); 69 assertAttribute( 70 main, 71 "Export-Service", 72 "com.ibm.ive.eccomm.service.authentication.AuthenticationService,com.ibm.ive.eccomm.service.session.SessionService"); 73 assertAttribute(main, "Bundle-Vendor", "IBM"); 74 assertAttribute(main, "Bundle-Version", "1.2.0"); 75 } 76 77 public void test_clone() throws IOException { 78 Manifest emptyManifest = new Manifest(); 79 Manifest emptyClone = (Manifest) emptyManifest.clone(); 80 assertTrue("Should have no entries", emptyClone.getEntries().isEmpty()); 81 assertTrue("Should have no main attributes", emptyClone 82 .getMainAttributes().isEmpty()); 83 assertEquals(emptyClone, emptyManifest); 84 assertEquals(emptyManifest.clone().getClass().getName(), 85 "java.util.jar.Manifest"); 86 87 Manifest manifest = new Manifest(new URL(Support_Resources 88 .getURL("manifest/hyts_MANIFEST.MF")).openStream()); 89 Manifest manifestClone = (Manifest) manifest.clone(); 90 manifestClone.getMainAttributes(); 91 checkManifest(manifestClone); 92 } 93 94 public void test_equals() throws IOException { 95 Manifest manifest1 = new Manifest(new URL(Support_Resources.getURL( 96 "manifest/hyts_MANIFEST.MF")).openStream()); 97 Manifest manifest2 = new Manifest(new URL(Support_Resources.getURL( 98 "manifest/hyts_MANIFEST.MF")).openStream()); 99 Manifest manifest3 = new Manifest(); 100 101 assertTrue(manifest1.equals(manifest1)); 102 assertTrue(manifest1.equals(manifest2)); 103 assertFalse(manifest1.equals(manifest3)); 104 assertFalse(manifest1.equals(this)); 105 } 106 107 public void test_writeLjava_io_OutputStream() throws IOException { 108 byte b[]; 109 Manifest manifest1 = null; 110 Manifest manifest2 = null; 111 try { 112 manifest1 = new Manifest(new URL(Support_Resources 113 .getURL("manifest/hyts_MANIFEST.MF")).openStream()); 114 } catch (MalformedURLException e) { 115 fail("Malformed URL"); 116 } 117 118 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 119 120 manifest1.write(baos); 121 122 b = baos.toByteArray(); 123 124 File f = File.createTempFile("111", "111"); 125 FileOutputStream fos = new FileOutputStream(f); 126 fos.close(); 127 try { 128 manifest1.write(fos); 129 fail("IOException expected"); 130 } catch (IOException e) { 131 // expected 132 } 133 f.delete(); 134 135 ByteArrayInputStream bais = new ByteArrayInputStream(b); 136 137 try { 138 manifest2 = new Manifest(bais); 139 } catch (MalformedURLException e) { 140 fail("Malformed URL"); 141 } 142 143 assertTrue(manifest1.equals(manifest2)); 144 } 145 146 public void test_write_no_version() throws Exception { 147 // If you write a manifest with no MANIFEST_VERSION, your attributes don't get written out. 148 assertEquals(null, doRoundTrip(null)); 149 // But they do if you supply a MANIFEST_VERSION. 150 assertEquals("image/pr0n", doRoundTrip(Attributes.Name.MANIFEST_VERSION)); 151 assertEquals("image/pr0n", doRoundTrip("Signature-Version")); 152 assertEquals(null, doRoundTrip("Random-String-Version")); 153 } 154 155 private String doRoundTrip(Object versionName) throws Exception { 156 Manifest m1 = new Manifest(); 157 m1.getMainAttributes().put(Attributes.Name.CONTENT_TYPE, "image/pr0n"); 158 if (versionName != null) { 159 m1.getMainAttributes().putValue(versionName.toString(), "1.2.3"); 160 } 161 ByteArrayOutputStream os = new ByteArrayOutputStream(); 162 m1.write(os); 163 164 ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); 165 Manifest m2 = new Manifest(); 166 m2.read(is); 167 return (String) m2.getMainAttributes().get(Attributes.Name.CONTENT_TYPE); 168 } 169 170 public void test_write_two_versions() throws Exception { 171 // It's okay to have two versions. 172 Manifest m1 = new Manifest(); 173 m1.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); 174 m1.getMainAttributes().put(Attributes.Name.SIGNATURE_VERSION, "2.0"); 175 m1.getMainAttributes().putValue("Aardvark-Version", "3.0"); 176 ByteArrayOutputStream os = new ByteArrayOutputStream(); 177 m1.write(os); 178 179 // The Manifest-Version takes precedence, 180 // and the Signature-Version gets no special treatment. 181 List<String> lines = Arrays.asList(new String(os.toByteArray(), "UTF-8").split("\r\n")); 182 // The first line must always contain the Manifest-Version (or the Signature-Version if 183 // the ManifestVersion is missing. 184 assertEquals("Manifest-Version: 1.0", lines.get(0)); 185 186 assertTrue(lines.contains("Aardvark-Version: 3.0")); 187 assertTrue(lines.contains("Signature-Version: 2.0")); 188 } 189} 190