1 37 package net.sourceforge.cruisecontrol.listeners; 38 39 import junit.framework.TestCase; 40 import junit.framework.Assert; 41 import net.sourceforge.cruisecontrol.CruiseControlException; 42 import net.sourceforge.cruisecontrol.ProjectState; 43 import net.sourceforge.cruisecontrol.util.Util; 44 45 import java.io.File ; 46 import java.io.IOException ; 47 import java.text.SimpleDateFormat ; 48 import java.util.ArrayList ; 49 import java.util.Date ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 53 56 public class CurrentBuildStatusFTPListenerTest extends TestCase { 57 private static final String TEST_DIR = "tmp"; 58 private final List filesToClear = new ArrayList (); 59 private MockCurrentBuildStatusFTPListener listener; 60 61 class MockCurrentBuildStatusFTPListener extends CurrentBuildStatusFTPListener { 62 private String text; 63 private String expectedText; 64 private String path; 65 private String expectedPath; 66 67 public void setExpectedText(String expectedText) { 68 this.expectedText = expectedText; 69 } 70 71 public void setExpectedPath(String expectedPath) { 72 this.expectedPath = expectedPath; 73 } 74 75 protected void sendFileToFTPPath(String text, String path) throws CruiseControlException { 76 this.text = text; 77 this.path = path; 78 } 79 80 void ensureFileSent() { 81 Assert.assertEquals(expectedPath, path); 82 Assert.assertEquals(expectedText, text); 83 } 84 } 85 86 protected void setUp() throws Exception { 87 listener = new MockCurrentBuildStatusFTPListener(); 88 } 89 90 protected void tearDown() { 91 listener = null; 92 for (Iterator iterator = filesToClear.iterator(); iterator.hasNext();) { 93 File file = (File ) iterator.next(); 94 if (file.exists()) { 95 file.delete(); 96 } 97 } 98 filesToClear.clear(); 99 } 100 101 public void testValidate() throws CruiseControlException { 102 try { 103 listener.validate(); 104 fail("'file' should be a required attribute"); 105 } catch (CruiseControlException cce) { 106 } 107 108 listener.setFile("somefile"); 109 try { 110 listener.validate(); 111 fail("'destdir' should be a required attribute"); 112 } catch (CruiseControlException cce) { 113 } 114 115 listener.setDestDir("destdir"); 116 listener.validate(); 117 } 118 119 public void testWritingStatus() throws CruiseControlException, IOException { 120 final String fileName = TEST_DIR + File.separator + "_testCurrentBuildStatus.txt"; 121 listener.setFile(fileName); 122 listener.setDestDir("/pub"); 123 filesToClear.add(new File (fileName)); 124 125 checkResultForState(fileName, ProjectState.WAITING); 126 checkResultForState(fileName, ProjectState.IDLE); 127 checkResultForState(fileName, ProjectState.QUEUED); 128 checkResultForState(fileName, ProjectState.BOOTSTRAPPING); 129 checkResultForState(fileName, ProjectState.MODIFICATIONSET); 130 checkResultForState(fileName, ProjectState.BUILDING); 131 checkResultForState(fileName, ProjectState.MERGING_LOGS); 132 checkResultForState(fileName, ProjectState.PUBLISHING); 133 checkResultForState(fileName, ProjectState.PAUSED); 134 checkResultForState(fileName, ProjectState.STOPPED); 135 } 136 137 private void checkResultForState(final String fileName, ProjectState state) 138 throws CruiseControlException, IOException { 139 Date date = new Date (); 141 listener.handleEvent(new ProjectStateChangedEvent("projName", state)); 142 SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy HH:mm:ss"); 143 final String dateString = formatter.format(date); 144 final String description = state.getDescription(); 145 String expected = description + " since\n" + dateString; 146 assertEquals(expected, Util.readFileToString(fileName)); 147 148 listener.setExpectedPath("/pub" + File.separator + listener.getFileName()); 149 listener.setExpectedText(expected); 150 listener.ensureFileSent(); 151 } 152 } 153 | Popular Tags |