1b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson/*
2b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one or more
3b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * contributor license agreements.  See the NOTICE file distributed with
4b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * this work for additional information regarding copyright ownership.
5b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * The ASF licenses this file to You under the Apache License, Version 2.0
6b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * (the "License"); you may not use this file except in compliance with
7b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * the License.  You may obtain a copy of the License at
8b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson *
9b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
10b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson *
11b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * Unless required by applicable law or agreed to in writing, software
12b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
13b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * See the License for the specific language governing permissions and
15b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * limitations under the License.
16b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson */
17b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
18b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonpackage libcore.java.util.jar;
19b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
20b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.io.File;
21b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.io.IOException;
22b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.util.jar.JarEntry;
23b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.util.jar.JarFile;
24b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.util.zip.ZipEntry;
25b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport junit.framework.TestCase;
26b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport tests.support.resource.Support_Resources;
27b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
28b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
29b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonpublic class OldJarEntryTest extends TestCase {
30b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    private ZipEntry zipEntry;
31b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    private JarEntry jarEntry;
32b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    private JarFile jarFile;
33b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    private final String jarName = "hyts_patch.jar";
34b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    private final String entryName = "foo/bar/A.class";
35b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    private File resources;
36b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
37b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    @Override
38b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    protected void setUp() throws Exception {
39b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        resources = Support_Resources.createTempFolder();
40b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        Support_Resources.copyFile(resources, null, jarName);
41b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        jarFile = new JarFile(new File(resources, jarName));
42b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
43b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
44b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    @Override
45b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    protected void tearDown() throws Exception {
46b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        if (jarFile != null) {
47b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            jarFile.close();
48b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
49b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
50b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
51b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    /**
52b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson     * @throws IOException
53229e34b182b98e1dba15d3dc6341954986ae2b7aBrian Carlstrom     * java.util.jar.JarEntry#JarEntry(java.util.jar.JarEntry)
54b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson     */
55b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_ConstructorLjava_util_jar_JarEntry_on_null() throws IOException {
56b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        JarEntry newJarEntry = new JarEntry(jarFile.getJarEntry(entryName));
57b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertNotNull(newJarEntry);
58b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
59b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        jarEntry = null;
60b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
61b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            newJarEntry = new JarEntry(jarEntry);
62b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("Should throw NullPointerException");
63b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        } catch (NullPointerException e) {
64b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            // Expected
65b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
66b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
67b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
68b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    /**
69229e34b182b98e1dba15d3dc6341954986ae2b7aBrian Carlstrom     * java.util.jar.JarEntry#JarEntry(java.util.zip.ZipEntry)
70b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson     */
71b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_ConstructorLjava_util_zip_ZipEntry() {
72b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertNotNull("Jar file is null", jarFile);
73b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        zipEntry = jarFile.getEntry(entryName);
74b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertNotNull("Zip entry is null", zipEntry);
75b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        jarEntry = new JarEntry(zipEntry);
76b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertNotNull("Jar entry is null", jarEntry);
77b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertEquals("Wrong entry constructed--wrong name", entryName, jarEntry
78b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson                .getName());
79b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertEquals("Wrong entry constructed--wrong size", 311, jarEntry
80b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson                .getSize());
81b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
82b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
83b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    /**
84229e34b182b98e1dba15d3dc6341954986ae2b7aBrian Carlstrom     * java.util.jar.JarEntry#getAttributes()
85b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson     */
86b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_getAttributes() {
87b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        JarFile attrJar = null;
88b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        File file = null;
89b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
90b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
91b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
92b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            attrJar = new JarFile(new File(resources, "Broken_manifest.jar"));
93b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            jarEntry = attrJar.getJarEntry("META-INF/");
94b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            jarEntry.getAttributes();
95b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("IOException expected");
96b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        } catch (IOException e) {
97b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            // expected.
98b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
99b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
100b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
101b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_ConstructorLjava_lang_String() {
102b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertNotNull("Jar file is null", jarFile);
103b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        zipEntry = jarFile.getEntry(entryName);
104b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertNotNull("Zip entry is null", zipEntry);
105b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        jarEntry = new JarEntry(entryName);
106b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertNotNull("Jar entry is null", jarEntry);
107b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertEquals("Wrong entry constructed--wrong name", entryName, jarEntry
108b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson                .getName());
109b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
110b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            jarEntry = new JarEntry((String) null);
111b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("NullPointerException expected");
112b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        } catch (NullPointerException ee) {
113b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            // expected
114b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
115b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        StringBuffer sb = new StringBuffer();
116b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        for (int i = 0; i < 0x10000; i++) {
117b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            sb.append('3');
118b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
119b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
120b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            jarEntry = new JarEntry(new String(sb));
121b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("IllegalArgumentException expected");
122b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        } catch (IllegalArgumentException ee) {
123b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            // expected
124b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
125b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
126b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
127b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_ConstructorLjava_util_jar_JarEntry() {
128b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertNotNull("Jar file is null", jarFile);
129b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        JarEntry je = jarFile.getJarEntry(entryName);
130b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertNotNull("Jar entry is null", je);
131b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        jarEntry = new JarEntry(je);
132b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertNotNull("Jar entry is null", jarEntry);
133b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertEquals("Wrong entry constructed--wrong name", entryName, jarEntry
134b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson                .getName());
135b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        assertEquals("Wrong entry constructed--wrong size", 311, jarEntry
136b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson                .getSize());
137b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
138b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson}
139