1 37 38 package net.sourceforge.cruisecontrol.distributed.util; 39 40 import java.io.BufferedReader ; 41 import java.io.File ; 42 import java.io.FileReader ; 43 import java.io.FileWriter ; 44 import java.io.IOException ; 45 import java.io.Writer ; 46 import java.util.Properties ; 47 import java.util.Arrays ; 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 TEMP_FILE = "data.txt"; 58 private String rootTempDirPath; 59 private File rootTempDir; 60 private String filledDirName = "filled"; 61 private String filledDirPath; 62 private File filledDir; 63 private String emptyDirName = "empty"; 64 private String emptyDirPath; 65 private File emptyDir; 66 67 public void setUp() { 68 try { 69 final File dummyTempFile = File.createTempFile("temp", "txt"); 70 dummyTempFile.delete(); 71 rootTempDir = new File (new File (dummyTempFile.getParent()), "testRoot"); 72 rootTempDirPath = rootTempDir.getCanonicalPath(); 73 rootTempDir.mkdir(); 74 filledDir = new File (rootTempDirPath, filledDirName); 75 filledDir.mkdir(); 76 filledDirPath = filledDir.getCanonicalPath(); 77 emptyDir = new File (rootTempDirPath, emptyDirName); 78 emptyDir.mkdir(); 79 emptyDirPath = emptyDir.getCanonicalPath(); 80 } catch (IOException ioe) { 81 String message = "Failed to find temp directory location"; 82 LOG.error(message); 83 System.err.println(message); 84 } 85 86 String tempFile = TEMP_FILE; 87 Writer writer = null; 88 try { 89 filledDirPath = rootTempDirPath + File.separator + filledDirName; 90 writer = new FileWriter (new File (filledDirPath, tempFile)); 91 writer.write("The quick brown fox, yadda, yadda, yadda..."); 92 writer.close(); 93 } catch (IOException ioe) { 94 String 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 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 tempFile = new File (filledDirPath, TEMP_FILE); 122 assertTrue(tempFile.exists()); 123 assertTrue(tempFile.isFile()); 124 125 Util.deleteFile(rootTempDir); 126 File tempDir = new File (rootTempDirPath); 127 assertFalse(tempDir.exists()); 128 } 129 130 public void testEmptyZip() { 131 String emptyZipFilePath = rootTempDir + File.separator + "empty.zip"; 132 ZipUtil.zipFolderContents(emptyZipFilePath, emptyDirPath); 133 File emptyZipFile = new File (emptyZipFilePath); 134 135 assertFalse(emptyZipFile.exists()); } 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 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 e) { 150 assertEquals("Missing folder to zip", e.getMessage()); 151 } 152 } 153 154 public void testUnzip() { 155 String zipFilePath = null; 156 String dirToZip = null; 157 try { 158 zipFilePath = new File (rootTempDirPath, "temp.zip").getCanonicalPath(); 159 dirToZip = new File (filledDirPath).getCanonicalPath(); 160 } catch (IOException ioe) { 161 fail(ioe.getMessage()); 162 } 163 ZipUtil.zipFolderContents(zipFilePath, dirToZip); 164 File zipFile = new File (zipFilePath); 165 assertTrue(zipFile.exists()); 166 assertTrue(zipFile.isFile()); 167 assertTrue(zipFile.length() > 0); 168 zipFile = null; 169 170 File unzipDir = new File (rootTempDirPath, "unzip"); 171 unzipDir.mkdir(); 172 assertTrue(unzipDir.exists()); 173 File unzippedFile = null; 174 try { 175 ZipUtil.unzipFileToLocation(zipFilePath, unzipDir.getCanonicalPath()); 176 unzippedFile = new File (unzipDir.getCanonicalPath(), "data.txt"); 177 } catch (IOException ioe) { 178 fail(ioe.getMessage()); 179 } 180 assertTrue(unzippedFile.exists()); 181 BufferedReader reader; 182 String line = ""; 183 try { 184 reader = new BufferedReader (new FileReader (unzippedFile)); 185 line = reader.readLine(); 186 reader.close(); 187 } catch (IOException ioe) { 188 fail(ioe.getMessage()); 189 } 190 String expectedLine = "The quick brown fox, yadda, yadda, yadda..."; 191 assertEquals(expectedLine, line); 192 unzippedFile = null; 193 } 194 195 204 public static void main(String [] args) throws IOException { 205 Properties properties = (Properties ) PropertiesHelper.loadRequiredProperties("cruise.properties"); 206 File tempDir = new File (File.createTempFile("temp", "txt").getParent()); 207 208 String zipFile = new File (tempDir.getCanonicalPath(), "cruise-logs.zip").getCanonicalPath(); 209 ZipUtil.zipFolderContents(zipFile, new File (properties.getProperty("cruise.run.dir"), "logs") 210 .getCanonicalPath()); 211 212 ZipUtil.unzipFileToLocation(zipFile, tempDir.getCanonicalPath()); 213 } 214 } 215 | Popular Tags |