KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > ZipUtilTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: ZipUtilTest.java 15:35:28 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.util;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URISyntaxException JavaDoc;
27 import java.util.zip.ZipFile JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.objectweb.petals.PetalsException;
32
33 /**
34  * Class used to test the zip util
35  *
36  * @author ddesjardins - eBMWebsourcing
37  */

38 public class ZipUtilTest extends TestCase {
39
40     /**
41      * Base directory of the project
42      */

43     private String JavaDoc baseDir;
44
45     /**
46      * Set up the base directory for the test
47      */

48     public void setUp() {
49         baseDir = this.getClass().getResource(".").toString();
50         baseDir = baseDir.substring(0, baseDir.indexOf("target"));
51         baseDir = baseDir.substring(baseDir.indexOf(":") + 1);
52     }
53
54     /**
55      * Try to extract a zip into a directory
56      *
57      * @throws PetalsException
58      * @throws IOException
59      */

60     public void testExplodeZip() throws PetalsException, IOException JavaDoc {
61         File JavaDoc extractedDir = new File JavaDoc(baseDir + "src" + File.separator
62             + "test-data" + File.separator + "extracted");
63         extractedDir.delete();
64         extractedDir.mkdir();
65         ZipUtil.explodeIntoDirectory(new ZipFile JavaDoc(baseDir + "src"
66             + File.separator + "test-data" + File.separator
67             + "test-ziputil.zip"), extractedDir);
68         if (extractedDir.listFiles().length != 3) {
69             fail();
70         }
71     }
72
73     /**
74      * Test exception throwing
75      *
76      * @throws PetalsException
77      * @throws IOException
78      */

79     public void testExplodeZip1() {
80         File JavaDoc extractedDir = new File JavaDoc(baseDir + "src" + File.separator
81             + "test-data" + File.separator + "extracted");
82         try {
83             ZipUtil.explodeIntoDirectory(null, extractedDir);
84             fail();
85         } catch (Exception JavaDoc e) {
86
87         }
88     }
89
90     /**
91      * Test exception throwing
92      *
93      * @throws PetalsException
94      * @throws IOException
95      */

96     public void testExplodeZip2() {
97         try {
98             ZipUtil.explodeIntoDirectory(new ZipFile JavaDoc(baseDir + "src"
99                 + File.separator + "test-data" + File.separator
100                 + "test-ziputil.zip"), null);
101             fail();
102         } catch (Exception JavaDoc e) {
103
104         }
105     }
106
107     /**
108      * Try to get an entry of a zip as a temporary file
109      *
110      * @throws IOException
111      */

112     public void testGetEntryAsTemp() throws IOException JavaDoc {
113         assertNotNull(ZipUtil.getEntryAsTemp(new ZipFile JavaDoc(baseDir + "src"
114             + File.separator + "test-data" + File.separator
115             + "test-ziputil.zip"), "META-INF/jbi.xml"));
116     }
117
118     /**
119      * Try to get an entry that does not exist in zip as a temporary file
120      *
121      * @throws IOException
122      */

123     public void testGetEntryAsTemp1() throws IOException JavaDoc {
124         File JavaDoc entryFile = ZipUtil.getEntryAsTemp(new ZipFile JavaDoc(baseDir + "src"
125             + File.separator + "test-data" + File.separator
126             + "test-ziputil.zip"), "META-INF" + File.separator + "manifest.mf");
127         if (entryFile != null) {
128             fail();
129         }
130     }
131
132     /**
133      * Test exception throwing
134      *
135      */

136     public void testGetEntryAsTemp2() {
137         try {
138             ZipUtil.getEntryAsTemp(null, "META-INF" + File.separator
139                 + "manifest.mf");
140             fail();
141         } catch (Exception JavaDoc e) {
142
143         }
144     }
145
146     /**
147      * Test exception throwing
148      *
149      */

150     public void testGetEntryAsTemp3() {
151         try {
152             ZipUtil.getEntryAsTemp(new ZipFile JavaDoc(baseDir + "src" + File.separator
153                 + "test-data" + File.separator + "test-ziputil.zip"), null);
154             fail();
155         } catch (Exception JavaDoc e) {
156         }
157     }
158
159     /**
160      * Test exception throwing
161      *
162      */

163     public void testGetEntryAsTemp4() {
164         try {
165             ZipUtil.getEntryAsTemp(new ZipFile JavaDoc(baseDir + "src" + File.separator
166                 + "test-data" + File.separator + "test-ziputil.zip"), "");
167             fail();
168         } catch (Exception JavaDoc e) {
169         }
170     }
171
172     /**
173      * Test if a zip file has a specific entry (the entry exists)
174      *
175      * @throws IOException
176      */

177     public void testHasEntry() throws IOException JavaDoc {
178         assertTrue(ZipUtil.hasEntry(new ZipFile JavaDoc(baseDir + "src"
179             + File.separator + "test-data" + File.separator
180             + "test-ziputil.zip"), "META-INF/jbi.xml"));
181     }
182
183     /**
184      * Test if a zip file has a specific entry (the entry does not exist)
185      *
186      * @throws IOException
187      */

188     public void testHasEntry1() throws IOException JavaDoc {
189         assertFalse(ZipUtil.hasEntry(new ZipFile JavaDoc(baseDir + "src"
190             + File.separator + "test-data" + File.separator
191             + "test-ziputil.zip"), "test.xml"));
192     }
193
194     /**
195      * Try to open a zip file from a URI
196      *
197      * @throws PetalsException
198      * @throws URISyntaxException
199      */

200     public void testOpenZipFile() throws PetalsException, URISyntaxException JavaDoc {
201         assertNotNull(ZipUtil.openZipFile(new File JavaDoc(baseDir + "src"
202             + File.separator + "test-data" + File.separator
203             + "test-ziputil.zip").toURI()));
204     }
205
206 }
207
Popular Tags