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.JarInputStream;
37import java.util.jar.Pack200;
38import java.util.jar.Pack200.Packer;
39
40@TestTargetClass(Pack200.Packer.class)
41public class Pack200PackerTest extends TestCase {
42    Packer packer;
43    Map properties;
44
45    @TestTargetNew(
46        level = TestLevel.COMPLETE,
47        notes = "",
48        method = "properties",
49        args = {}
50    )
51    @KnownFailure("No Implementation in Android!")
52    public void testProperties() {
53        assertTrue(properties.size()>0);
54    }
55
56    @TestTargetNew(
57        level = TestLevel.COMPLETE,
58        notes = "",
59        method = "pack",
60        args = {java.util.jar.JarFile.class, java.io.OutputStream.class}
61    )
62    @KnownFailure("No Implementation in Android!")
63    public void testPackJarFileOutputStream() throws IOException {
64        File resources = Support_Resources.createTempFolder();
65        //Use junit4.jar file for testing pack200 compressing rate.
66        //file can be changed to any other.
67        Support_Resources.copyFile(resources, null, "junit4-4.3.1.jar");
68        File jarFile = new File(resources, "junit4-4.3.1.jar");
69        JarFile jf = new JarFile(jarFile);
70
71        File packFile1 = Support_Resources.createTempFile("pack200_1");
72        File packFile2 = Support_Resources.createTempFile("pack200_2");
73        File packFile3 = Support_Resources.createTempFile("pack200_3");
74        FileOutputStream fos1 = new FileOutputStream(packFile1);
75        FileOutputStream fos2 = new FileOutputStream(packFile2);
76        FileOutputStream fos3 = new FileOutputStream(packFile3);
77        properties.put(Packer.EFFORT, "0");
78        packer.pack(jf, fos1);
79        jf.close();
80        fos1.close();
81        jf = new JarFile(jarFile);
82        properties.put(Packer.EFFORT, "1");
83        packer.pack(jf, fos2);
84        jf.close();
85        fos2.close();
86        jf = new JarFile(jarFile);
87        properties.put(Packer.EFFORT, "9");
88        packer.pack(jf, fos3);
89        jf.close();
90        fos3.close();
91        assertTrue(jarFile.length()!=packFile1.length());
92        assertTrue(packFile1.length()>packFile2.length());
93        assertTrue(packFile2.length()>packFile3.length());
94    }
95
96    @TestTargetNew(
97        level = TestLevel.COMPLETE,
98        notes = "",
99        method = "pack",
100        args = {java.util.jar.JarInputStream.class, java.io.OutputStream.class}
101    )
102    @KnownFailure("No Implementation in Android!")
103    public void testPackJarInputStreamOutputStream() throws IOException {
104        File resources = Support_Resources.createTempFolder();
105        //Use junit4.jar file for testing pack200 compressing rate.
106        //file can be changed to any other.
107        Support_Resources.copyFile(resources, null, "junit4-4.3.1.jar");
108        File jarFile = new File(resources, "junit4-4.3.1.jar");
109        JarInputStream jis = new JarInputStream(new FileInputStream(jarFile));
110
111        File packFile1 = Support_Resources.createTempFile("pack200_1");
112        File packFile2 = Support_Resources.createTempFile("pack200_2");
113        File packFile3 = Support_Resources.createTempFile("pack200_3");
114        FileOutputStream fos1 = new FileOutputStream(packFile1);
115        FileOutputStream fos2 = new FileOutputStream(packFile2);
116        FileOutputStream fos3 = new FileOutputStream(packFile3);
117        properties.put(Packer.EFFORT, "0");
118        packer.pack(jis, fos1);
119        fos1.close();
120        jis = new JarInputStream(new FileInputStream(jarFile));
121        properties.put(Packer.EFFORT, "1");
122        packer.pack(jis, fos2);
123        fos2.close();
124        jis = new JarInputStream(new FileInputStream(jarFile));
125        properties.put(Packer.EFFORT, "9");
126        packer.pack(jis, fos3);
127        fos3.close();
128        assertTrue(jarFile.length()!=packFile1.length());
129        assertTrue(packFile1.length()>packFile2.length());
130        assertTrue(packFile2.length()>packFile3.length());
131    }
132
133    class MyPCL implements PropertyChangeListener {
134        boolean flag = false;
135
136        public boolean isCalled() {
137            return flag;
138        }
139
140        public void propertyChange(PropertyChangeEvent arg0) {
141            flag = true;
142        }
143    }
144
145    @TestTargetNew(
146        level = TestLevel.COMPLETE,
147        notes = "",
148        method = "addPropertyChangeListener",
149        args = {java.beans.PropertyChangeListener.class}
150    )
151    @KnownFailure("No Implementation in Android!")
152    public void testAddPropertyChangeListener() {
153        MyPCL pcl = new MyPCL();
154        packer.addPropertyChangeListener(pcl);
155        assertFalse(pcl.isCalled());
156        properties.put(Packer.EFFORT, "7");
157        assertTrue(pcl.isCalled());
158    }
159
160    @TestTargetNew(
161        level = TestLevel.COMPLETE,
162        notes = "",
163        method = "removePropertyChangeListener",
164        args = {java.beans.PropertyChangeListener.class}
165    )
166    @KnownFailure("No Implementation in Android!")
167    public void testRemovePropertyChangeListener() {
168        MyPCL pcl = new MyPCL();
169        packer.addPropertyChangeListener(pcl);
170        assertFalse(pcl.isCalled());
171        packer.removePropertyChangeListener(pcl);
172        properties.put(Packer.EFFORT, "7");
173        assertFalse(pcl.isCalled());
174    }
175
176    @Override
177    protected void setUp() {
178        packer = Pack200.newPacker();
179        properties = packer.properties();
180    }
181
182    @Override
183    protected void tearDown() {
184        packer = null;
185        properties = null;
186    }
187}
188