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 */ 17 package libcore.java.util.jar; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.ByteArrayOutputStream; 21 import java.io.File; 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 import java.net.MalformedURLException; 25 import java.net.URL; 26 import java.util.jar.Attributes; 27 import java.util.jar.Manifest; 28 import junit.framework.TestCase; 29 import tests.support.resource.Support_Resources; 30 31 public 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 } 145