001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2005, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol;
037:
038: import java.util.ArrayList;
039: import java.util.List;
040:
041: import junit.framework.TestCase;
042: import net.sourceforge.cruisecontrol.labelincrementers.DefaultLabelIncrementer;
043: import net.sourceforge.cruisecontrol.testutil.TestUtil;
044:
045: import java.io.File;
046:
047: public class ProjectConfigTest extends TestCase {
048:
049: private final TestUtil.FilesToDelete filesToDelete = new TestUtil.FilesToDelete();
050:
051: private ProjectConfig config;
052:
053: protected void setUp() {
054: config = new ProjectConfig();
055: config.setName("test");
056: }
057:
058: protected void tearDown() {
059: config = null;
060:
061: filesToDelete.delete();
062: }
063:
064: public void testBuildAfterFailedShouldDefaultToTrue() {
065: assertTrue(config.shouldBuildAfterFailed());
066: config.setBuildAfterFailed(false);
067: assertFalse(config.shouldBuildAfterFailed());
068: }
069:
070: public void testValidate_ScheduleRequired()
071: throws CruiseControlException {
072: try {
073: config.validate();
074: fail("a schedule should have been required by ProjectConfig");
075: } catch (CruiseControlException expected) {
076: assertEquals("project requires a schedule", expected
077: .getMessage());
078: }
079:
080: config.add(new MockSchedule());
081: config.validate();
082:
083: filesToDelete.add(new File(TestUtil.getTargetDir(), "logs"));
084: }
085:
086: public void testValidateCallsSubelementValidates()
087: throws CruiseControlException {
088: MockSchedule schedule = new MockSchedule();
089: config.add(schedule);
090: MockBootstrappers bootstrappers = new MockBootstrappers();
091: config.add(bootstrappers);
092: MockModificationSet modificationSet = new MockModificationSet();
093: config.add(modificationSet);
094: MockListeners listeners = new MockListeners();
095: config.add(listeners);
096: MockPublishers publishers = new MockPublishers();
097: config.add(publishers);
098: MockLog log = new MockLog();
099: config.add(log);
100:
101: config.validate();
102:
103: assertTrue(schedule.validateWasCalled());
104: assertTrue(bootstrappers.validateWasCalled());
105: assertTrue(modificationSet.validateWasCalled());
106: assertTrue(listeners.validateWasCalled());
107: assertTrue(publishers.validateWasCalled());
108: assertTrue(log.validateWasCalled());
109: }
110:
111: public void testReadProject() {
112: String tempDir = System.getProperty("java.io.tmpdir");
113: ProjectConfig projectConfig = new ProjectConfig();
114: Project project = projectConfig.readProject(tempDir);
115: assertNotNull(project);
116: assertTrue(project.getBuildForced());
117: }
118:
119: public void testShouldBeAbleToGetCommitMessage() throws Exception {
120: MockModificationSet modificationSet = new MockModificationSet();
121: config.add(new DefaultLabelIncrementer());
122: config.add(modificationSet);
123: config.configureProject();
124: List modifications = config.getModifications();
125:
126: assertEquals(2, modifications.size());
127: for (int i = 0; i < 2; i++) {
128: Modification modification = (Modification) modifications
129: .get(i);
130: assertEquals("user" + i, modification.userName);
131: assertEquals("comment" + i, modification.comment);
132: }
133: assertTrue(modificationSet.getCurrentModificationsWasCalled);
134: }
135:
136: private static class MockBootstrappers extends
137: ProjectConfig.Bootstrappers {
138:
139: private boolean validateWasCalled = false;
140:
141: public void validate() throws CruiseControlException {
142: validateWasCalled = true;
143: }
144:
145: public boolean validateWasCalled() {
146: return validateWasCalled;
147: }
148:
149: }
150:
151: private static class MockModificationSet extends ModificationSet {
152:
153: private boolean validateWasCalled = false;
154: private boolean getCurrentModificationsWasCalled = false;
155:
156: public void validate() throws CruiseControlException {
157: validateWasCalled = true;
158: }
159:
160: public boolean validateWasCalled() {
161: return validateWasCalled;
162: }
163:
164: public List getCurrentModifications() {
165: getCurrentModificationsWasCalled = true;
166: List modications = new ArrayList();
167: Modification modification = new Modification();
168: modification.userName = "user0";
169: modification.comment = "comment0";
170: modications.add(modification);
171: modification = new Modification();
172: modification.userName = "user1";
173: modification.comment = "comment1";
174: modications.add(modification);
175: return modications;
176: }
177:
178: }
179:
180: private static class MockListeners extends ProjectConfig.Listeners {
181:
182: private boolean validateWasCalled = false;
183:
184: public void validate() throws CruiseControlException {
185: validateWasCalled = true;
186: }
187:
188: public boolean validateWasCalled() {
189: return validateWasCalled;
190: }
191:
192: }
193:
194: private static class MockPublishers extends
195: ProjectConfig.Publishers {
196:
197: private boolean validateWasCalled = false;
198:
199: public void validate() throws CruiseControlException {
200: validateWasCalled = true;
201: }
202:
203: public boolean validateWasCalled() {
204: return validateWasCalled;
205: }
206:
207: }
208:
209: private static class MockLog extends Log {
210:
211: private boolean validateWasCalled = false;
212:
213: public void validate() throws CruiseControlException {
214: validateWasCalled = true;
215: }
216:
217: public boolean validateWasCalled() {
218: return validateWasCalled;
219: }
220:
221: }
222:
223: }
|