1 package net.sourceforge.cruisecontrol.listeners; 2 3 import junit.framework.TestCase; 4 import net.sourceforge.cruisecontrol.CruiseControlException; 5 import net.sourceforge.cruisecontrol.ProjectState; 6 import net.sourceforge.cruisecontrol.util.Util; 7 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.text.SimpleDateFormat ; 11 import java.util.ArrayList ; 12 import java.util.Date ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 22 public class CurrentBuildStatusListenerTest extends TestCase { 23 private static final String TEST_DIR = "tmp"; 24 private final List filesToClear = new ArrayList (); 25 private CurrentBuildStatusListener listener; 26 27 protected void setUp() throws Exception { 28 listener = new CurrentBuildStatusListener(); 29 } 30 31 protected void tearDown() { 32 listener = null; 33 for (Iterator iterator = filesToClear.iterator(); iterator.hasNext();) { 34 File file = (File ) iterator.next(); 35 if (file.exists()) { 36 file.delete(); 37 } 38 } 39 filesToClear.clear(); 40 } 41 42 public void testValidate() throws CruiseControlException { 43 try { 44 listener.validate(); 45 fail("'file' should be a required attribute"); 46 } catch (CruiseControlException cce) { 47 } 48 49 listener.setFile("somefile"); 50 listener.validate(); 51 52 listener.setFile(System.getProperty("java.io.tmpdir") + File.separator + "filename"); 53 listener.validate(); 54 } 55 56 public void testWritingStatus() throws CruiseControlException, IOException { 57 final String fileName = TEST_DIR + File.separator + "_testCurrentBuildStatus.txt"; 58 listener.setFile(fileName); 59 filesToClear.add(new File (fileName)); 60 61 checkResultForState(fileName, ProjectState.WAITING); 62 checkResultForState(fileName, ProjectState.IDLE); 63 checkResultForState(fileName, ProjectState.QUEUED); 64 checkResultForState(fileName, ProjectState.BOOTSTRAPPING); 65 checkResultForState(fileName, ProjectState.MODIFICATIONSET); 66 checkResultForState(fileName, ProjectState.BUILDING); 67 checkResultForState(fileName, ProjectState.MERGING_LOGS); 68 checkResultForState(fileName, ProjectState.PUBLISHING); 69 checkResultForState(fileName, ProjectState.PAUSED); 70 checkResultForState(fileName, ProjectState.STOPPED); 71 } 72 73 private void checkResultForState(final String fileName, ProjectState state) 74 throws CruiseControlException, IOException { 75 Date date = new Date (); 77 listener.handleEvent(new ProjectStateChangedEvent("projName", state)); 78 SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy HH:mm:ss"); 79 final String dateString = formatter.format(date); 80 final String description = state.getDescription(); 81 String expected = description + " since\n" + dateString; 82 assertEquals(expected, Util.readFileToString(fileName)); 83 } 84 } 85 | Popular Tags |