1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import junit.framework.TestCase; 40 import net.sourceforge.cruisecontrol.CruiseControlException; 41 import net.sourceforge.cruisecontrol.Modification; 42 import net.sourceforge.cruisecontrol.util.Util; 43 44 import java.io.BufferedWriter ; 45 import java.io.File ; 46 import java.io.FileWriter ; 47 import java.io.IOException ; 48 import java.io.PrintWriter ; 49 import java.util.Date ; 50 import java.util.GregorianCalendar ; 51 import java.util.List ; 52 53 public class FileSystemTest extends TestCase { 54 55 public FileSystemTest(String name) { 56 super(name); 57 } 58 59 public void testValidate() { 60 FileSystem fs = new FileSystem(); 61 62 try { 63 fs.validate(); 64 fail("FileSystem should throw exceptions when required attributes are not set."); 65 } catch (CruiseControlException e) { 66 assertEquals("'folder' is required for FileSystem", e.getMessage()); 67 } 68 69 File tempDirectory = new File (System.getProperty("java.io.tmpdir")); 70 tempDirectory = new File (tempDirectory, "filesystemtest2" + System.currentTimeMillis()); 72 fs.setFolder(tempDirectory.getAbsolutePath()); 73 74 try { 75 fs.validate(); 76 fail("FileSystem should throw exceptions when folder doesn't exist."); 77 } catch (CruiseControlException e) { 78 assertTrue(e.getMessage().indexOf("must exist") > -1); 79 } 80 81 assertTrue(tempDirectory.mkdir()); 82 83 try { 84 fs.validate(); 85 } catch (CruiseControlException e) { 86 fail("FileSystem should not throw exceptions when required attributes are set."); 87 } finally { 88 Util.deleteFile(tempDirectory); 89 } 90 } 91 92 public void testGettingModifications() throws Exception { 93 File tempDirectory = new File (System.getProperty("java.io.tmpdir")); 95 96 tempDirectory = new File (tempDirectory, 98 "filesystemtest" + System.currentTimeMillis()); 99 assertTrue(tempDirectory.mkdir()); 100 101 try { 102 FileSystem fsystem = new FileSystem(); 104 fsystem.setFolder(tempDirectory.getAbsolutePath()); 105 106 Date startTime = new GregorianCalendar (2000, 0, 1).getTime(); 108 Date timeOne = new Date (startTime.getTime() + 2000); 109 Date timeTwo = new Date (timeOne.getTime() + 2000); 110 Date timeThree = new Date (timeTwo.getTime() + 2000); 111 List mods = fsystem.getModifications(startTime, timeOne); 112 assertNotNull(mods); 113 assertEquals(0, mods.size()); 114 115 File tempFile; 117 118 tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory); 119 writeContent(tempFile, "testing"); 120 tempFile.setLastModified(timeOne.getTime()); 121 122 tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory); 123 writeContent(tempFile, "testing 2"); 124 tempFile.setLastModified(timeOne.getTime()); 125 126 mods = fsystem.getModifications(startTime, timeOne); 128 assertNotNull(mods); 129 assertEquals(2, mods.size()); 130 131 tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory); 133 writeContent(tempFile, "testing 3"); 134 tempFile.setLastModified(timeTwo.getTime()); 135 136 tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory); 137 writeContent(tempFile, "testing 4"); 138 tempFile.setLastModified(timeTwo.getTime()); 139 140 tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory); 141 writeContent(tempFile, "testing 5"); 142 tempFile.setLastModified(timeTwo.getTime()); 143 144 mods = fsystem.getModifications(timeOne, timeTwo); 146 assertNotNull(mods); 147 assertEquals(3, mods.size()); 148 149 tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory); 151 writeContent(tempFile, "testing 6"); 152 tempFile.setLastModified(timeThree.getTime()); 153 154 mods = fsystem.getModifications(timeTwo, timeThree); 156 assertNotNull(mods); 157 assertEquals(1, mods.size()); 158 159 Modification modification = (Modification) mods.get(0); 161 assertEquals(tempFile.getName(), modification.getFileName()); 162 assertEquals(tempFile.getParent(), modification.getFolderName()); 163 assertEquals(tempFile.lastModified(), modification.modifiedTime.getTime()); 164 } finally { 165 Util.deleteFile(tempDirectory); 166 } 167 } 168 169 private static void writeContent(File file, String content) 170 throws IOException { 171 PrintWriter writer = 172 new PrintWriter (new BufferedWriter (new FileWriter (file))); 173 writer.print(content); 174 writer.flush(); 175 writer.close(); 176 } 177 } | Popular Tags |