KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > distributed > util > ZipUtilTest


1 /****************************************************************************
2 * CruiseControl, a Continuous Integration Toolkit
3 * Copyright (c) 2001, ThoughtWorks, Inc.
4 * 651 W Washington Ave. Suite 600
5 * Chicago, IL 60661 USA
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * + Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * + Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 *
20 * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 ****************************************************************************/

37
38 package net.sourceforge.cruisecontrol.distributed.util;
39
40 import java.io.BufferedReader JavaDoc;
41 import java.io.File JavaDoc;
42 import java.io.FileReader JavaDoc;
43 import java.io.FileWriter JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.Writer JavaDoc;
46 import java.util.Properties JavaDoc;
47 import java.util.Arrays JavaDoc;
48
49 import junit.framework.TestCase;
50 import net.sourceforge.cruisecontrol.util.Util;
51
52 import org.apache.log4j.Logger;
53
54 public class ZipUtilTest extends TestCase {
55     
56     private static final Logger LOG = Logger.getLogger(ZipUtilTest.class);
57     private static final String JavaDoc TEMP_FILE = "data.txt";
58     private String JavaDoc rootTempDirPath;
59     private File JavaDoc rootTempDir;
60     private String JavaDoc filledDirName = "filled";
61     private String JavaDoc filledDirPath;
62     private File JavaDoc filledDir;
63     private String JavaDoc emptyDirName = "empty";
64     private String JavaDoc emptyDirPath;
65     private File JavaDoc emptyDir;
66
67     public void setUp() {
68         try {
69             final File JavaDoc dummyTempFile = File.createTempFile("temp", "txt");
70             dummyTempFile.delete();
71             rootTempDir = new File JavaDoc(new File JavaDoc(dummyTempFile.getParent()), "testRoot");
72             rootTempDirPath = rootTempDir.getCanonicalPath();
73             rootTempDir.mkdir();
74             filledDir = new File JavaDoc(rootTempDirPath, filledDirName);
75             filledDir.mkdir();
76             filledDirPath = filledDir.getCanonicalPath();
77             emptyDir = new File JavaDoc(rootTempDirPath, emptyDirName);
78             emptyDir.mkdir();
79             emptyDirPath = emptyDir.getCanonicalPath();
80         } catch (IOException JavaDoc ioe) {
81             String JavaDoc message = "Failed to find temp directory location";
82             LOG.error(message);
83             System.err.println(message);
84         }
85
86         String JavaDoc tempFile = TEMP_FILE;
87         Writer JavaDoc writer = null;
88         try {
89             filledDirPath = rootTempDirPath + File.separator + filledDirName;
90             writer = new FileWriter JavaDoc(new File JavaDoc(filledDirPath, tempFile));
91             writer.write("The quick brown fox, yadda, yadda, yadda...");
92             writer.close();
93         } catch (IOException JavaDoc ioe) {
94             String JavaDoc message = "Failed to create temp file " + tempFile + " at " + filledDirPath;
95             LOG.error(message, ioe);
96             System.err.println(message);
97         }
98     }
99
100     public void tearDown() {
101         Util.deleteFile(rootTempDir);
102         if (rootTempDir.exists()) {
103             final String JavaDoc msg = "Delete file failed: rootTempDir: " + rootTempDir.getAbsolutePath()
104                     + "\n\tContents:\n"
105                     + (rootTempDir.listFiles() != null ? Arrays.asList(rootTempDir.listFiles()) : null);
106             System.out.println(msg);
107         }
108
109     }
110
111     public void testSetup() {
112         assertTrue(rootTempDir.exists());
113         assertTrue(rootTempDir.isDirectory());
114
115         assertTrue(filledDir.exists());
116         assertTrue(filledDir.isDirectory());
117
118         assertTrue(emptyDir.exists());
119         assertTrue(emptyDir.isDirectory());
120
121         File JavaDoc tempFile = new File JavaDoc(filledDirPath, TEMP_FILE);
122         assertTrue(tempFile.exists());
123         assertTrue(tempFile.isFile());
124
125         Util.deleteFile(rootTempDir);
126         File JavaDoc tempDir = new File JavaDoc(rootTempDirPath);
127         assertFalse(tempDir.exists());
128     }
129
130     public void testEmptyZip() {
131         String JavaDoc emptyZipFilePath = rootTempDir + File.separator + "empty.zip";
132         ZipUtil.zipFolderContents(emptyZipFilePath, emptyDirPath);
133         File JavaDoc emptyZipFile = new File JavaDoc(emptyZipFilePath);
134
135         assertFalse(emptyZipFile.exists()); // @todo Should empty zips be created?
136
}
137
138     public void testZipWithIllegalArguments() {
139         try {
140             ZipUtil.zipFolderContents(null, filledDirName);
141             fail("Should throw an exception since zip filename and/or dir is missing");
142         } catch (java.lang.IllegalArgumentException JavaDoc e) {
143             assertEquals("Missing output zip file name", e.getMessage());
144         }
145
146         try {
147             ZipUtil.zipFolderContents("blech.zip", null);
148             fail("Should throw an exception since zip filename and/or dir is missing");
149         } catch (java.lang.IllegalArgumentException JavaDoc e) {
150             assertEquals("Missing folder to zip", e.getMessage());
151         }
152     }
153
154     public void testUnzip() {
155         String JavaDoc zipFilePath = null;
156         String JavaDoc dirToZip = null;
157         try {
158             zipFilePath = new File JavaDoc(rootTempDirPath, "temp.zip").getCanonicalPath();
159             dirToZip = new File JavaDoc(filledDirPath).getCanonicalPath();
160         } catch (IOException JavaDoc ioe) {
161             fail(ioe.getMessage());
162         }
163         ZipUtil.zipFolderContents(zipFilePath, dirToZip);
164         File JavaDoc zipFile = new File JavaDoc(zipFilePath);
165         assertTrue(zipFile.exists());
166         assertTrue(zipFile.isFile());
167         assertTrue(zipFile.length() > 0);
168         zipFile = null;
169
170         File JavaDoc unzipDir = new File JavaDoc(rootTempDirPath, "unzip");
171         unzipDir.mkdir();
172         assertTrue(unzipDir.exists());
173         File JavaDoc unzippedFile = null;
174         try {
175             ZipUtil.unzipFileToLocation(zipFilePath, unzipDir.getCanonicalPath());
176             unzippedFile = new File JavaDoc(unzipDir.getCanonicalPath(), "data.txt");
177         } catch (IOException JavaDoc ioe) {
178             fail(ioe.getMessage());
179         }
180         assertTrue(unzippedFile.exists());
181         BufferedReader JavaDoc reader;
182         String JavaDoc line = "";
183         try {
184             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(unzippedFile));
185             line = reader.readLine();
186             reader.close();
187         } catch (IOException JavaDoc ioe) {
188             fail(ioe.getMessage());
189         }
190         String JavaDoc expectedLine = "The quick brown fox, yadda, yadda, yadda...";
191         assertEquals(expectedLine, line);
192         unzippedFile = null;
193     }
194
195     /**
196      * Test method - zips logs from directory specified by cruise.run.dir
197      * property in cruise.properties file to cruise-log.zip file in system
198      * default temp file location (i.e. C:\Documents and Settings\ <user>\Local
199      * Settings\Temp on Windows XP) then unzips files into same temp directory.
200      *
201      * *WARNING* Could be a large bit of data--you might plan to manually delete
202      * the results...
203      */

204     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
205         Properties JavaDoc properties = (Properties JavaDoc) PropertiesHelper.loadRequiredProperties("cruise.properties");
206         File JavaDoc tempDir = new File JavaDoc(File.createTempFile("temp", "txt").getParent());
207
208         String JavaDoc zipFile = new File JavaDoc(tempDir.getCanonicalPath(), "cruise-logs.zip").getCanonicalPath();
209         ZipUtil.zipFolderContents(zipFile, new File JavaDoc(properties.getProperty("cruise.run.dir"), "logs")
210                 .getCanonicalPath());
211
212         ZipUtil.unzipFileToLocation(zipFile, tempDir.getCanonicalPath());
213     }
214 }
215
Popular Tags