1 /********************************************************************************
2 *InternetCafe is a software solution that helps the management of Cybercafes
3 * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4 * Copyright (C) 2006 Guido Angelo Ingenito
5
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20
21 package test.base.util;
22
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.util.Hashtable;
27
28 import junit.framework.TestCase;
29
30 import org.apache.log4j.Logger;
31
32 import test.base.jdbs.AllTests;
33 import base.util.FileUtil;
34 import base.util.ZipUtil;
35
36 public class ZipUtilTest extends TestCase {
37
38 private static final transient Logger logger = Logger.getLogger(ZipUtilTest.class.getName());
39
40
41
42
43 public void testZipDirectoryFileFile() throws IOException {
44
45 File inputDirectory = new File(AllTests.TEST_DIRECTORY_PATH+File.separatorChar+"ZipDirectoryTest");
46 inputDirectory.mkdir();
47 inputDirectory.deleteOnExit();
48
49 File outputFile = new File(AllTests.TEST_DIRECTORY_PATH+File.separatorChar+"ZipDirectoryTest.zip");
50 outputFile.createNewFile();
51 outputFile.deleteOnExit();
52
53 File[] fileContent = new File[100];
54 for(int i=0;i<fileContent.length;i++){
55 fileContent[i] = new File(inputDirectory,"file"+i+".txt");
56 FileWriter fileWriter = new FileWriter(fileContent[i]);
57 for(int k=0;k<100;k++)
58 fileWriter.append("Line "+k+" : THIS IS A SIMPLE TEST FILE CONTENT!");
59 fileWriter.close();
60 fileContent[i].deleteOnExit();
61 }
62
63 ZipUtil.zipDirectory(inputDirectory,outputFile);
64
65 assertTrue(outputFile.exists());
66 assertTrue(outputFile.length() > 0);
67 assertTrue(outputFile.canRead());
68 assertTrue(outputFile.canWrite());
69 }
70
71
72
73
74 public void testZipFileFileFile() throws IOException {
75
76 File inputFile = new File(AllTests.TEST_DIRECTORY_PATH+File.separatorChar+"File.txt");
77 inputFile.createNewFile();
78 inputFile.deleteOnExit();
79
80
81 FileWriter fileWriter = new FileWriter(inputFile);
82 for(int i=0;i<200;i++)
83 fileWriter.append("Line "+i+": THIS IS A SIMPLE TEXT LINE CONTENT");
84 fileWriter.close();
85
86 assertTrue(inputFile.exists());
87 assertTrue(inputFile.length() > 0);
88 assertTrue(inputFile.canRead());
89 assertTrue(inputFile.canWrite());
90
91 File outputFile = new File(AllTests.TEST_DIRECTORY_PATH+File.separatorChar+"File.zip");
92 outputFile.createNewFile();
93 outputFile.deleteOnExit();
94
95 base.util.ZipUtil.zipFile(inputFile,outputFile);
96
97 assertTrue(outputFile.exists());
98 assertTrue(outputFile.length() > 0);
99 assertTrue(outputFile.canRead());
100 assertTrue(outputFile.canWrite());
101
102 File outputDirectory = new File(AllTests.TEST_DIRECTORY_PATH);
103
104 base.util.ZipUtil.unZipFile(outputFile,outputDirectory);
105
106 Hashtable<String, File> unzippedFiles = new Hashtable<String, File>();
107 File[] outputDirectoryContent = FileUtil.allFileContent(outputDirectory);
108 for(int i=0;i<outputDirectoryContent.length;i++){
109 unzippedFiles.put(outputDirectoryContent[i].getName(),outputDirectoryContent[i]);
110 outputDirectoryContent[i].deleteOnExit();
111 }
112 assertTrue(unzippedFiles.containsKey(inputFile.getName()));
113 assertTrue(unzippedFiles.get(inputFile.getName()).length() == inputFile.length());
114 }
115
116
117
118
119 public void testUnZipFileFileFile() throws IOException {
120
121 if(!AllTests.RUN_SILENTLY){
122
123 File zipFile = null;
124 File destinationDirectory = null;
125
126
127
128 javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
129 chooser.setDialogTitle("Load Zip file...");
130 chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
131 chooser.setApproveButtonText("Load");
132 if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION)
133 zipFile = new File(chooser.getSelectedFile().getAbsolutePath());
134
135
136
137 chooser = new javax.swing.JFileChooser();
138 chooser.setDialogTitle("Select Destination Directory...");
139 chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
140 chooser.setApproveButtonText("Select");
141 if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION)
142 destinationDirectory = new File(chooser.getSelectedFile()
143 .getAbsolutePath());
144
145 if (zipFile != null && destinationDirectory != null)
146 base.util.ZipUtil.unZipFile(zipFile,destinationDirectory);
147 }else logger.warn("Skipping test base.util.ZipUtil.unZipFile(File, File) because it is not provided a RUN_SILENTLY implementation.");
148 }
149
150
151 }