001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2007, 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.dashboard.web;
037:
038: import java.util.Arrays;
039: import java.util.Collection;
040: import java.util.HashMap;
041: import java.util.List;
042: import java.util.Map;
043:
044: import net.sourceforge.cruisecontrol.BuildLoopInformation.ProjectInfo;
045: import net.sourceforge.cruisecontrol.dashboard.Build;
046: import net.sourceforge.cruisecontrol.dashboard.BuildDetail;
047: import net.sourceforge.cruisecontrol.dashboard.BuildSummary;
048: import net.sourceforge.cruisecontrol.dashboard.LogFile;
049: import net.sourceforge.cruisecontrol.dashboard.CurrentStatus;
050: import net.sourceforge.cruisecontrol.dashboard.PreviousResult;
051: import net.sourceforge.cruisecontrol.dashboard.BuildLiveDetail;
052: import net.sourceforge.cruisecontrol.dashboard.testhelpers.DataUtils;
053: import net.sourceforge.cruisecontrol.dashboard.repository.BuildInformationRepository;
054: import net.sourceforge.cruisecontrol.dashboard.service.BuildLoopQueryService;
055: import net.sourceforge.cruisecontrol.dashboard.service.BuildService;
056: import net.sourceforge.cruisecontrol.dashboard.service.HistoricalBuildSummariesService;
057: import net.sourceforge.cruisecontrol.dashboard.service.BuildSummaryService;
058: import net.sourceforge.cruisecontrol.dashboard.service.BuildSummaryUIService;
059: import net.sourceforge.cruisecontrol.dashboard.service.ConfigurationService;
060: import net.sourceforge.cruisecontrol.dashboard.service.DashboardConfigFileFactory;
061: import net.sourceforge.cruisecontrol.dashboard.service.DashboardXmlConfigService;
062: import net.sourceforge.cruisecontrol.dashboard.service.EnvironmentService;
063: import net.sourceforge.cruisecontrol.dashboard.service.WidgetPluginService;
064:
065: import org.apache.commons.lang.StringUtils;
066: import org.jmock.Mock;
067: import org.jmock.cglib.MockObjectTestCase;
068: import org.joda.time.DateTime;
069: import org.springframework.web.servlet.ModelAndView;
070:
071: public class GetActiveBuildInfoControllerTest extends
072: MockObjectTestCase {
073:
074: private BuildDetailController controller;
075:
076: private Mock buildLoopService;
077:
078: private Mock mockBuildSummariesService;
079:
080: private Mock mockBuildService;
081: private static final String PROJECT_NAME = "connectfour";
082:
083: protected void setUp() throws Exception {
084: List allBuilds = Arrays.asList(new Build[] { new BuildSummary(
085: "", PreviousResult.PASSED,
086: DataUtils.PASSING_BUILD_LBUILD_0_XML) });
087: mockBuildSummariesService = mock(
088: HistoricalBuildSummariesService.class, new Class[] {
089: ConfigurationService.class,
090: BuildSummaryService.class }, new Object[] {
091: null, null });
092: mockBuildSummariesService.expects(once())
093: .method("getLastest25").withAnyArguments().will(
094: returnValue(allBuilds));
095: mockBuildSummariesService.expects(once()).method(
096: "getDurationFromLastSuccessfulBuild")
097: .withAnyArguments().will(returnValue("1 days 3 hours"));
098: buildLoopService = mock(BuildLoopQueryService.class,
099: new Class[] { EnvironmentService.class,
100: BuildInformationRepository.class },
101: new Object[] { null, null });
102: mockBuildService = mock(BuildService.class,
103: new Class[] { ConfigurationService.class,
104: BuildLoopQueryService.class }, new Object[] {
105: null, null });
106:
107: Mock mockConfigService = mock(DashboardXmlConfigService.class,
108: new Class[] { DashboardConfigFileFactory.class },
109: new Object[] { null });
110: mockConfigService.expects(atLeastOnce()).method(
111: "getStoryTrackers").will(returnValue(new HashMap()));
112: HistoricalBuildSummariesService buildSummariesService = (HistoricalBuildSummariesService) mockBuildSummariesService
113: .proxy();
114: controller = new BuildDetailController(
115: (BuildService) mockBuildService.proxy(),
116: buildSummariesService, new WidgetPluginService(
117: (DashboardXmlConfigService) mockConfigService
118: .proxy()), new BuildSummaryUIService(
119: buildSummariesService,
120: (DashboardXmlConfigService) mockConfigService
121: .proxy()),
122: (BuildLoopQueryService) buildLoopService.proxy());
123: }
124:
125: public void testShouldReturnDurationFromLastSuccessfulBuildInBuildingStatus()
126: throws Throwable {
127: mockBuildSummariesService.expects(atLeastOnce()).method(
128: "getLatest").withAnyArguments().will(returnValue(null));
129: mockBuildService.expects(once()).method("getActiveBuild").will(
130: returnValue(getActiveBuild()));
131: buildLoopService.expects(once()).method("getProjectInfo").will(
132: returnValue(connectfourInfo()));
133: ModelAndView mov = controller.live(PROJECT_NAME);
134: Map model = mov.getModel();
135: assertTrue(StringUtils.contains((String) model
136: .get("durationToSuccessfulBuild"), "1 days 3 hours"));
137: }
138:
139: private ProjectInfo connectfourInfo() {
140: return new ProjectInfo(PROJECT_NAME, "now building",
141: "2005-12-05T13:09:56");
142: }
143:
144: public void testShouldReturnBuildSince() throws Throwable {
145: mockBuildSummariesService.expects(atLeastOnce()).method(
146: "getLatest").withAnyArguments().will(returnValue(null));
147: mockBuildService.expects(once()).method("getActiveBuild").will(
148: returnValue(getActiveBuild()));
149: buildLoopService.expects(once()).method("getProjectInfo").will(
150: returnValue(connectfourInfo()));
151: ModelAndView mov = controller.live(PROJECT_NAME);
152: Map model = mov.getModel();
153: assertNotNull(model.get("buildSince"));
154: assertFalse(StringUtils.contains((String) model
155: .get("buildSince"), "N/A"));
156: }
157:
158: public void testShouldReturnLastBuildSummaries() throws Throwable {
159: mockBuildSummariesService.expects(atLeastOnce()).method(
160: "getLatest").withAnyArguments().will(returnValue(null));
161: mockBuildService.expects(once()).method("getActiveBuild").will(
162: returnValue(getActiveBuild()));
163: buildLoopService.expects(once()).method("getProjectInfo").will(
164: returnValue(connectfourInfo()));
165: ModelAndView mov = controller.live(PROJECT_NAME);
166: Map model = mov.getModel();
167: assertNotNull(model.get("historicalBuildCmds"));
168: assertEquals(1, ((Collection) model.get("historicalBuildCmds"))
169: .size());
170: }
171:
172: public BuildLiveDetail getActiveBuild() {
173: return new BuildLiveDetail(PROJECT_NAME, PreviousResult.FAILED);
174: }
175:
176: public Build getFailedBuild() {
177: final String logFile = "log20060101121212.xml";
178: return new FailedBuildDetailStub(logFile);
179: }
180:
181: private static class FailedBuildDetailStub extends BuildDetail {
182: private final String logFile;
183:
184: public FailedBuildDetailStub(String logFile) {
185: super (new LogFile(logFile));
186: this .logFile = logFile;
187: }
188:
189: public CurrentStatus getCurrentStatus() {
190: return CurrentStatus.WAITING;
191: }
192:
193: public PreviousResult getPreviousBuildResult() {
194: return PreviousResult.FAILED;
195: }
196:
197: public String getProjectName() {
198: return PROJECT_NAME;
199: }
200:
201: public String getBuildLogFilename() {
202: return logFile;
203: }
204:
205: public DateTime getBuildDate() {
206: return new DateTime();
207: }
208: }
209: }
|