KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > listeners > LockFileListenerTest


1 package net.sourceforge.cruisecontrol.listeners;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import junit.framework.TestCase;
10 import net.sourceforge.cruisecontrol.CruiseControlException;
11 import net.sourceforge.cruisecontrol.ProjectState;
12
13 public class LockFileListenerTest extends TestCase {
14
15     private LockFileListener listener;
16     private List JavaDoc filesToDelete;
17     
18     private static final ProjectStateChangedEvent BOOTSTRAPPING = new ProjectStateChangedEvent("test",
19                     ProjectState.BOOTSTRAPPING);
20     private static final ProjectStateChangedEvent MODIFICATIONSET = new ProjectStateChangedEvent("test",
21                     ProjectState.MODIFICATIONSET);
22     private static final ProjectStateChangedEvent BUILDING = new ProjectStateChangedEvent("test",
23                     ProjectState.BUILDING);
24     private static final ProjectStateChangedEvent MERGING_LOGS = new ProjectStateChangedEvent("test",
25                     ProjectState.MERGING_LOGS);
26     private static final ProjectStateChangedEvent PUBLISHING = new ProjectStateChangedEvent("test",
27                     ProjectState.PUBLISHING);
28
29     private static final ProjectStateChangedEvent IDLE = new ProjectStateChangedEvent("test", ProjectState.IDLE);
30     private static final ProjectStateChangedEvent WAITING = new ProjectStateChangedEvent("test", ProjectState.WAITING);
31     private static final ProjectStateChangedEvent QUEUED = new ProjectStateChangedEvent("test", ProjectState.QUEUED);
32     private static final ProjectStateChangedEvent PAUSED = new ProjectStateChangedEvent("test", ProjectState.PAUSED);
33     private static final ProjectStateChangedEvent STOPPED = new ProjectStateChangedEvent("test", ProjectState.STOPPED);
34     
35     protected void setUp() {
36         listener = new LockFileListener();
37         filesToDelete = new ArrayList JavaDoc();
38     }
39     
40     protected void tearDown() {
41         for (Iterator JavaDoc iter = filesToDelete.iterator(); iter.hasNext();) {
42             File JavaDoc file = (File JavaDoc) iter.next();
43             file.delete();
44         }
45         listener = null;
46         filesToDelete = null;
47     }
48     
49     public void testShouldNotDeleteLockIfDidntGetPastBootstrapping() throws IOException JavaDoc, CruiseControlException {
50         File JavaDoc lock = File.createTempFile("test", ".lck");
51         filesToDelete.add(lock);
52         listener.setLockFile(lock.getAbsolutePath());
53         assertTrue(lock.exists());
54
55         listener.handleEvent(BOOTSTRAPPING);
56         listener.handleEvent(IDLE);
57         listener.handleEvent(WAITING);
58         listener.handleEvent(QUEUED);
59         listener.handleEvent(PAUSED);
60         listener.handleEvent(STOPPED);
61         
62         assertTrue(lock.exists());
63     }
64     
65     public void testShouldDeleteLockIfGotPastBootstrapping() throws IOException JavaDoc, CruiseControlException {
66         File JavaDoc lock = File.createTempFile("test", ".lck");
67         filesToDelete.add(lock);
68         listener.setLockFile(lock.getAbsolutePath());
69         assertTrue(lock.exists());
70         
71         listener.handleEvent(BOOTSTRAPPING);
72         listener.handleEvent(MODIFICATIONSET);
73         listener.handleEvent(BUILDING);
74         listener.handleEvent(MERGING_LOGS);
75         listener.handleEvent(PUBLISHING);
76         assertTrue(lock.exists());
77         
78         listener.handleEvent(IDLE);
79         assertFalse(lock.exists());
80     }
81     
82     public void testShouldThrowExceptionWhenFailsToDeleteLock() throws CruiseControlException {
83         listener = new LockFileListener() {
84             boolean attemptToDeleteLock() {
85                 return false;
86             }
87         };
88         
89         listener.handleEvent(MODIFICATIONSET);
90         try {
91             listener.handleEvent(IDLE);
92             fail("should have thrown exception when lock wasn't deleted");
93         } catch (CruiseControlException expected) {
94         }
95     }
96     
97     public void testValidate() throws CruiseControlException {
98         try {
99             listener.validate();
100             fail("should throw exception when lock file not set");
101         } catch (CruiseControlException expected) {
102         }
103         
104         listener.setLockFile("delete.me");
105         listener.validate();
106     }
107
108 }
109
Popular Tags