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
18package libcore.java.util.jar;
19
20import java.io.File;
21import java.io.IOException;
22import java.util.jar.JarEntry;
23import java.util.jar.JarFile;
24import java.util.zip.ZipEntry;
25import junit.framework.TestCase;
26import tests.support.resource.Support_Resources;
27
28
29public class OldJarEntryTest extends TestCase {
30    private ZipEntry zipEntry;
31    private JarEntry jarEntry;
32    private JarFile jarFile;
33    private final String jarName = "hyts_patch.jar";
34    private final String entryName = "foo/bar/A.class";
35    private File resources;
36
37    @Override
38    protected void setUp() throws Exception {
39        resources = Support_Resources.createTempFolder();
40        Support_Resources.copyFile(resources, null, jarName);
41        jarFile = new JarFile(new File(resources, jarName));
42    }
43
44    @Override
45    protected void tearDown() throws Exception {
46        if (jarFile != null) {
47            jarFile.close();
48        }
49    }
50
51    /**
52     * @throws IOException
53     * java.util.jar.JarEntry#JarEntry(java.util.jar.JarEntry)
54     */
55    public void test_ConstructorLjava_util_jar_JarEntry_on_null() throws IOException {
56        JarEntry newJarEntry = new JarEntry(jarFile.getJarEntry(entryName));
57        assertNotNull(newJarEntry);
58
59        jarEntry = null;
60        try {
61            newJarEntry = new JarEntry(jarEntry);
62            fail("Should throw NullPointerException");
63        } catch (NullPointerException e) {
64            // Expected
65        }
66    }
67
68    /**
69     * java.util.jar.JarEntry#JarEntry(java.util.zip.ZipEntry)
70     */
71    public void test_ConstructorLjava_util_zip_ZipEntry() {
72        assertNotNull("Jar file is null", jarFile);
73        zipEntry = jarFile.getEntry(entryName);
74        assertNotNull("Zip entry is null", zipEntry);
75        jarEntry = new JarEntry(zipEntry);
76        assertNotNull("Jar entry is null", jarEntry);
77        assertEquals("Wrong entry constructed--wrong name", entryName, jarEntry
78                .getName());
79        assertEquals("Wrong entry constructed--wrong size", 311, jarEntry
80                .getSize());
81    }
82
83    /**
84     * java.util.jar.JarEntry#getAttributes()
85     */
86    public void test_getAttributes() {
87        JarFile attrJar = null;
88        File file = null;
89
90        Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
91        try {
92            attrJar = new JarFile(new File(resources, "Broken_manifest.jar"));
93            jarEntry = attrJar.getJarEntry("META-INF/");
94            jarEntry.getAttributes();
95            fail("IOException expected");
96        } catch (IOException e) {
97            // expected.
98        }
99    }
100
101    public void test_ConstructorLjava_lang_String() {
102        assertNotNull("Jar file is null", jarFile);
103        zipEntry = jarFile.getEntry(entryName);
104        assertNotNull("Zip entry is null", zipEntry);
105        jarEntry = new JarEntry(entryName);
106        assertNotNull("Jar entry is null", jarEntry);
107        assertEquals("Wrong entry constructed--wrong name", entryName, jarEntry
108                .getName());
109        try {
110            jarEntry = new JarEntry((String) null);
111            fail("NullPointerException expected");
112        } catch (NullPointerException ee) {
113            // expected
114        }
115        StringBuffer sb = new StringBuffer();
116        for (int i = 0; i < 0x10000; i++) {
117            sb.append('3');
118        }
119        try {
120            jarEntry = new JarEntry(new String(sb));
121            fail("IllegalArgumentException expected");
122        } catch (IllegalArgumentException ee) {
123            // expected
124        }
125    }
126
127    public void test_ConstructorLjava_util_jar_JarEntry() {
128        assertNotNull("Jar file is null", jarFile);
129        JarEntry je = jarFile.getJarEntry(entryName);
130        assertNotNull("Jar entry is null", je);
131        jarEntry = new JarEntry(je);
132        assertNotNull("Jar entry is null", jarEntry);
133        assertEquals("Wrong entry constructed--wrong name", entryName, jarEntry
134                .getName());
135        assertEquals("Wrong entry constructed--wrong size", 311, jarEntry
136                .getSize());
137    }
138}
139