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  
27  import junit.framework.TestCase;
28  
29  import org.apache.log4j.Logger;
30  import org.apache.log4j.xml.DOMConfigurator;
31  
32  import test.base.jdbs.AllTests;
33  
34  public class FileUtilTest extends TestCase {
35  
36  	private static final transient Logger logger = Logger
37  			.getLogger(FileUtilTest.class.getName());
38  
39  	
40  	/*
41  	 * (non-Javadoc)
42  	 * 
43  	 * @see junit.framework.TestCase#setUp()
44  	 */
45  	@Override
46  	protected void setUp() throws Exception {
47  		DOMConfigurator.configure("log4jConfiguration.xml");
48  		File testDir = new File(AllTests.TEST_DIRECTORY_PATH);
49  		testDir.mkdirs();
50  	}
51  
52  	/*
53  	 * Test method for 'base.util.FileUtil.createCRC32Checksum(File)'
54  	 */
55  	public void testCreateCRC32Checksum() throws IOException {
56  
57  		File inputFile = new File(AllTests.TEST_DIRECTORY_PATH+File.separatorChar+"crc32ChecksumTest.txt");
58  		if (inputFile.createNewFile()) {
59  			inputFile.deleteOnExit();
60  			FileWriter fileWriter = new FileWriter(inputFile);
61  			fileWriter
62  					.append("THIS IS A SIMPLE STRING TO TEST THE CRC32 CHECKSUM CREATION!");
63  			fileWriter.close();
64  
65  			// The expected value has been calculated with
66  			// http://www.lammertbies.nl/comm/info/crc-calculation.html
67  			String expectedValue = "ABAAE717";// This is an hex value, should
68  												// have the prefix setted to
69  												// "0x".
70  			String computedValue = Long.toHexString(
71  					base.util.FileUtil.createCRC32Checksum(inputFile))
72  					.toUpperCase();
73  
74  			logger.info("Expected value: " + expectedValue
75  					+ " computed value: " + computedValue);
76  			assertTrue(expectedValue.equals(computedValue));
77  		}
78  	}
79  
80  	/*
81  	 * Test method for 'base.util.FileUtil.checkCRC32Checksum(File, Long)'
82  	 */
83  	public void testCheckCRC32Checksum() throws IOException {
84  		File inputFile = new File(AllTests.TEST_DIRECTORY_PATH+File.separatorChar+"crc32ChecksumTest.txt");
85  		if (inputFile.createNewFile()) {
86  			inputFile.deleteOnExit();
87  			FileWriter fileWriter = new FileWriter(inputFile);
88  			fileWriter
89  					.append("THIS IS A SIMPLE STRING TO TEST THE CRC32 CHECKSUM CREATION!");
90  			fileWriter.close();
91  
92  			// The expected value has been calculated with
93  			// http://www.lammertbies.nl/comm/info/crc-calculation.html
94  			Long expectedValue = Long.decode("ABAAE717");// This is an
95  															// encoded hex
96  															// value.
97  
98  			assertTrue(base.util.FileUtil.checkCRC32Checksum(inputFile,
99  					expectedValue));
100 		}
101 	}
102 
103 	/*
104 	 * Test method for 'base.util.FileUtil.allFileContent(File)'
105 	 */
106 	public void testAllFileContent() throws IOException {
107 		File dir = new File(AllTests.TEST_DIRECTORY_PATH+File.separatorChar+"testDir");
108 		dir.mkdir();
109 		dir.deleteOnExit();
110 
111 		File file1 = new File(dir, "file1.txt");
112 		file1.createNewFile();
113 		file1.deleteOnExit();
114 
115 		File file2 = new File(dir, "file2.txt");
116 		file2.createNewFile();
117 		file2.deleteOnExit();
118 
119 		File dir1 = new File(dir, "testDir1");
120 		dir1.mkdir();
121 		dir1.deleteOnExit();
122 
123 		File file11 = new File(dir1, "file11.txt");
124 		file11.createNewFile();
125 		file11.deleteOnExit();
126 
127 		File file22 = new File(dir1, "file22.txt");
128 		file22.createNewFile();
129 		file22.deleteOnExit();
130 
131 		File[] allFiles = base.util.FileUtil.allFileContent(dir);
132 
133 		assertEquals(4, allFiles.length);
134 
135 		for (int i = 0; i < allFiles.length; i++) {
136 			assertTrue(allFiles[i].exists());
137 		}
138 	}
139 
140 	/*
141 	 * Test method for 'base.util.FileUtil.deleteDirectory(File)'
142 	 */
143 	public void testDeleteDirectory() {
144 		File directory = new File(AllTests.TEST_DIRECTORY_PATH+File.separatorChar+"deleteDirectoryTest");
145 		directory.mkdir();
146 		assertTrue(directory.exists() && directory.isDirectory());
147 		base.util.FileUtil.deleteDirectory(directory);
148 		assertFalse(directory.exists());
149 
150 		File dir1 = new File(directory,"dir1");
151 		dir1.mkdir();
152 		assertTrue(dir1.exists() && dir1.isDirectory());
153 
154 		File subDir1 = new File(dir1, "subDir1");
155 		subDir1.mkdir();
156 		assertTrue(subDir1.exists() && subDir1.isDirectory());
157 
158 		File subDir2 = new File(dir1, "subDir2");
159 		subDir2.mkdir();
160 		assertTrue(subDir2.exists() && subDir2.isDirectory());
161 
162 		File subDir11 = new File(subDir1, "subDir11");
163 		subDir11.mkdir();
164 		assertTrue(subDir11.exists() && subDir11.isDirectory());
165 
166 		base.util.FileUtil.deleteDirectory(directory);
167 
168 		assertFalse(dir1.exists());
169 		assertFalse(subDir1.exists());
170 		assertFalse(subDir11.exists());
171 		assertFalse(subDir2.exists());
172 	}
173 
174 	/*
175 	 * Test method for 'base.util.FileUtil.createMD5Digest(File)'
176 	 */
177 	public void testCreateMD5Digest() throws IOException {
178 
179 		File inputFile = new File("md5DigestTest.txt");
180 		if (inputFile.createNewFile()) {
181 			inputFile.deleteOnExit();
182 			FileWriter fileWriter = new FileWriter(inputFile);
183 			fileWriter
184 					.append("THIS IS A SIMPLE STRING TO TEST THE Md5 DIGEST CREATION!");
185 			fileWriter.close();
186 
187 			// The expected value has been calculated with
188 			// http://ds.nfshost.com/MD5calc/
189 			String expectedValue = "8e89c8da8f7722162f0cbc5b783af9a9";// This
190 																		// is an
191 																		// hex
192 																		// value,
193 																		// should
194 																		// have
195 																		// the
196 																		// prefix
197 																		// setted
198 																		// to
199 																		// "0x".
200 			String computedValue = base.util.FileUtil
201 					.createMD5Digest(inputFile);
202 
203 			logger.info("Expected value: " + expectedValue
204 					+ " computed value: " + computedValue);
205 			assertTrue(expectedValue.equals(computedValue));
206 		}
207 	}
208 
209 	/*
210 	 * Test method for 'base.util.FileUtil.fileContent(File)'
211 	 */
212 	public void testFileContent() throws IOException {
213 
214 		File inputFile = new File("testFileContent.txt");
215 		if (inputFile.createNewFile()) {
216 			inputFile.deleteOnExit();
217 			StringBuffer sb = new StringBuffer();
218 
219 			for (int i = 0; i < 10; i++)
220 				sb.append("Line " + i + " Test Content!\n");
221 
222 			FileWriter fileWriter = new FileWriter(inputFile);
223 			fileWriter.append(sb.toString());
224 			fileWriter.close();
225 
226 			String expectedValue = sb.toString();
227 			String computedValue = base.util.FileUtil.fileContent(inputFile);
228 
229 			logger.debug("Expected Value:\n" + expectedValue);
230 			logger.debug("Computed Value:\n" + computedValue);
231 
232 			assertEquals(computedValue, expectedValue);
233 		}
234 	}
235 
236 }