01: package net.sourceforge.cruisecontrol.bootstrappers;
02:
03: import java.io.File;
04: import java.io.IOException;
05:
06: import junit.framework.TestCase;
07: import net.sourceforge.cruisecontrol.CruiseControlException;
08: import net.sourceforge.cruisecontrol.testutil.TestUtil.FilesToDelete;
09: import net.sourceforge.cruisecontrol.util.Util;
10:
11: public class LockFileBootstrapperTest extends TestCase {
12:
13: private LockFileBootstrapper bootstrapper;
14: private final FilesToDelete filesToDelete = new FilesToDelete();
15:
16: protected void setUp() {
17: bootstrapper = new LockFileBootstrapper();
18: }
19:
20: protected void tearDown() throws Exception {
21: bootstrapper = null;
22: filesToDelete.delete();
23: }
24:
25: public void testAttemptToCreateLockFile() throws IOException,
26: CruiseControlException {
27: File lock = File.createTempFile("test", ".lck");
28: filesToDelete.add(lock);
29: lock.delete();
30: assertFalse(lock.exists());
31:
32: bootstrapper.setLockFile(lock.getAbsolutePath());
33: bootstrapper.setProjectName("project.name");
34:
35: bootstrapper.bootstrap();
36: assertTrue(lock.exists());
37: assertProjectName("project.name", lock);
38:
39: bootstrapper.bootstrap();
40:
41: bootstrapper.setProjectName("different.name");
42:
43: try {
44: bootstrapper.bootstrap();
45: fail("should throw exception when lock already exists with different name");
46: } catch (CruiseControlException expected) {
47: }
48: }
49:
50: private void assertProjectName(String expected, File lock)
51: throws IOException {
52: String actual = Util.readFileToString(lock);
53: assertEquals("project name in file didn't match", expected,
54: actual);
55: }
56:
57: public void testValidateShouldThrowExceptionWhenRequiredAttributesNotSet()
58: throws CruiseControlException {
59: try {
60: bootstrapper.validate();
61: fail("should throw exception when lock file and project name not set");
62: } catch (CruiseControlException expected) {
63: }
64: }
65:
66: public void testValidateShouldThrowExceptionWhenProjectNameNotSet() {
67: bootstrapper.setLockFile("delete.me");
68: try {
69: bootstrapper.validate();
70: fail("should throw exception when project name not set");
71: } catch (CruiseControlException expected) {
72: }
73: }
74:
75: public void testValidateShouldThrowExceptionWhenLockFileNotSet() {
76: bootstrapper.setProjectName("project.name");
77: try {
78: bootstrapper.validate();
79: fail("should throw exception when lock file not set");
80: } catch (CruiseControlException e) {
81: }
82: }
83:
84: public void testValidateShouldntThrowExceptionWhenRequiredAttributesAreSet()
85: throws CruiseControlException {
86: bootstrapper.setLockFile("delete.me");
87: bootstrapper.setProjectName("project.name");
88: bootstrapper.validate();
89: }
90:
91: }
|