1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.archive.tests.java.util.jar;
18
19import dalvik.annotation.KnownFailure;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetClass;
22import dalvik.annotation.TestTargetNew;
23
24import junit.framework.TestCase;
25
26import tests.support.resource.Support_Resources;
27
28import java.beans.PropertyChangeEvent;
29import java.beans.PropertyChangeListener;
30import java.io.File;
31import java.io.FileInputStream;
32import java.io.FileOutputStream;
33import java.io.IOException;
34import java.util.Map;
35import java.util.jar.JarFile;
36import java.util.jar.JarOutputStream;
37import java.util.jar.Pack200;
38import java.util.jar.Pack200.Packer;
39import java.util.jar.Pack200.Unpacker;
40
41@TestTargetClass(Pack200.Unpacker.class)
42public class Pack200UnpackerTest extends TestCase {
43    Unpacker unpacker;
44    Map properties;
45
46    @TestTargetNew(
47        level = TestLevel.COMPLETE,
48        notes = "",
49        method = "properties",
50        args = {}
51    )
52    @KnownFailure("No Implementation in Android!")
53    public void testProperties() {
54        assertTrue(properties.size()>0);
55    }
56
57    @TestTargetNew(
58        level = TestLevel.COMPLETE,
59        notes = "",
60        method = "unpack",
61        args = {java.io.File.class, java.util.jar.JarOutputStream.class}
62    )
63    @KnownFailure("No Implementation in Android!")
64    public void testUnpackInputStreamJarOutputStream() throws IOException {
65        File resources = Support_Resources.createTempFolder();
66        //Use junit4.jar file for testing pack200 compressing rate.
67        //file can be changed to any other.
68        Support_Resources.copyFile(resources, null, "junit4-4.3.1.jar");
69        File jarFile = new File(resources, "junit4-4.3.1.jar");
70        JarFile jf = new JarFile(jarFile);
71        int jarEntries = jf.size();
72
73        File packFile1 = Support_Resources.createTempFile("pack200_1");
74        File packFile2 = Support_Resources.createTempFile("pack200_2");
75        File packFile3 = Support_Resources.createTempFile("pack200_3");
76        FileOutputStream fos1 = new FileOutputStream(packFile1);
77        FileOutputStream fos2 = new FileOutputStream(packFile2);
78        FileOutputStream fos3 = new FileOutputStream(packFile3);
79        properties.put(Packer.EFFORT, "0");
80        Packer packer = Pack200.newPacker();
81        packer.pack(jf, fos1);
82        jf.close();
83        fos1.close();
84        jf = new JarFile(jarFile);
85        properties.put(Packer.EFFORT, "1");
86        packer.pack(jf, fos2);
87        jf.close();
88        fos2.close();
89        jf = new JarFile(jarFile);
90        properties.put(Packer.EFFORT, "9");
91        packer.pack(jf, fos3);
92        jf.close();
93        fos3.close();
94
95        File jarFile1 = Support_Resources.createTempFile("jar_1");
96        File jarFile2 = Support_Resources.createTempFile("jar_2");
97        File jarFile3 = Support_Resources.createTempFile("jar_3");
98        JarOutputStream jos1 = new JarOutputStream(new FileOutputStream(jarFile1));
99        JarOutputStream jos2 = new JarOutputStream(new FileOutputStream(jarFile2));
100        JarOutputStream jos3 = new JarOutputStream(new FileOutputStream(jarFile3));
101
102        unpacker.unpack(packFile1, jos1);
103        unpacker.unpack(packFile2, jos2);
104        unpacker.unpack(packFile3, jos3);
105
106        jos1.close();
107        jos2.close();
108        jos3.close();
109
110        assertEquals(jarFile1.length(), jarFile2.length());
111        assertEquals(jarFile2.length(), jarFile3.length());
112
113        assertEquals(jarEntries, new JarFile(jarFile1).size());
114        assertEquals(jarEntries, new JarFile(jarFile2).size());
115        assertEquals(jarEntries, new JarFile(jarFile3).size());
116    }
117
118    @TestTargetNew(
119        level = TestLevel.COMPLETE,
120        notes = "",
121        method = "unpack",
122        args = {java.io.InputStream.class, java.util.jar.JarOutputStream.class}
123    )
124    @KnownFailure("No Implementation in Android!")
125    public void testUnpackFileJarOutputStream() throws IOException {
126        File resources = Support_Resources.createTempFolder();
127        //Use junit4.jar file for testing pack200 compressing rate.
128        //file can be changed to any other.
129        Support_Resources.copyFile(resources, null, "junit4-4.3.1.jar");
130        File jarFile = new File(resources, "junit4-4.3.1.jar");
131        JarFile jf = new JarFile(jarFile);
132        int jarEntries = jf.size();
133
134        File packFile1 = Support_Resources.createTempFile("pack200_1");
135        File packFile2 = Support_Resources.createTempFile("pack200_2");
136        File packFile3 = Support_Resources.createTempFile("pack200_3");
137        FileOutputStream fos1 = new FileOutputStream(packFile1);
138        FileOutputStream fos2 = new FileOutputStream(packFile2);
139        FileOutputStream fos3 = new FileOutputStream(packFile3);
140        properties.put(Packer.EFFORT, "0");
141        Packer packer = Pack200.newPacker();
142        packer.pack(jf, fos1);
143        jf.close();
144        fos1.close();
145        jf = new JarFile(jarFile);
146        properties.put(Packer.EFFORT, "1");
147        packer.pack(jf, fos2);
148        jf.close();
149        fos2.close();
150        jf = new JarFile(jarFile);
151        properties.put(Packer.EFFORT, "9");
152        packer.pack(jf, fos3);
153        jf.close();
154        fos3.close();
155
156        File jarFile1 = Support_Resources.createTempFile("jar_1");
157        File jarFile2 = Support_Resources.createTempFile("jar_2");
158        File jarFile3 = Support_Resources.createTempFile("jar_3");
159        JarOutputStream jos1 = new JarOutputStream(new FileOutputStream(jarFile1));
160        JarOutputStream jos2 = new JarOutputStream(new FileOutputStream(jarFile2));
161        JarOutputStream jos3 = new JarOutputStream(new FileOutputStream(jarFile3));
162        FileInputStream fis1 = new FileInputStream(packFile1);
163        FileInputStream fis2 = new FileInputStream(packFile2);
164        FileInputStream fis3 = new FileInputStream(packFile3);
165
166        unpacker.unpack(fis1, jos1);
167        unpacker.unpack(fis2, jos2);
168        unpacker.unpack(fis3, jos3);
169
170        jos1.close();
171        jos2.close();
172        jos3.close();
173
174        assertEquals(jarFile1.length(), jarFile2.length());
175        assertEquals(jarFile2.length(), jarFile3.length());
176
177        assertEquals(jarEntries, new JarFile(jarFile1).size());
178        assertEquals(jarEntries, new JarFile(jarFile2).size());
179        assertEquals(jarEntries, new JarFile(jarFile3).size());
180    }
181
182    class MyPCL implements PropertyChangeListener {
183        boolean flag = false;
184
185        public boolean isCalled() {
186            return flag;
187        }
188
189        public void propertyChange(PropertyChangeEvent arg0) {
190            flag = true;
191        }
192    }
193
194    @TestTargetNew(
195        level = TestLevel.COMPLETE,
196        notes = "",
197        method = "addPropertyChangeListener",
198        args = {java.beans.PropertyChangeListener.class}
199    )
200    @KnownFailure("No Implementation in Android!")
201    public void testAddPropertyChangeListener() {
202        MyPCL pcl = new MyPCL();
203        unpacker.addPropertyChangeListener(pcl);
204        assertFalse(pcl.isCalled());
205        properties.put(Unpacker.PROGRESS, "0");
206        assertTrue(pcl.isCalled());
207    }
208
209    @TestTargetNew(
210        level = TestLevel.COMPLETE,
211        notes = "",
212        method = "removePropertyChangeListener",
213        args = {java.beans.PropertyChangeListener.class}
214    )
215    @KnownFailure("No Implementation in Android!")
216    public void testRemovePropertyChangeListener() {
217        MyPCL pcl = new MyPCL();
218        unpacker.addPropertyChangeListener(pcl);
219        assertFalse(pcl.isCalled());
220        unpacker.removePropertyChangeListener(pcl);
221        properties.put(Unpacker.PROGRESS, "7");
222        assertFalse(pcl.isCalled());
223    }
224
225    @Override
226    protected void setUp() {
227        unpacker = Pack200.newUnpacker();
228        properties = unpacker.properties();
229    }
230
231    @Override
232    protected void tearDown() {
233        unpacker = null;
234        properties = null;
235    }
236}
237