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.io.File;
039: import java.util.HashMap;
040:
041: import net.sourceforge.cruisecontrol.dashboard.service.BuildLoopQueryService;
042: import net.sourceforge.cruisecontrol.dashboard.service.ConfigurationService;
043: import net.sourceforge.cruisecontrol.dashboard.service.DashboardXmlConfigService;
044: import net.sourceforge.cruisecontrol.dashboard.service.EnvironmentService;
045: import net.sourceforge.cruisecontrol.dashboard.testhelpers.DataUtils;
046:
047: import org.jmock.Mock;
048: import org.jmock.cglib.MockObjectTestCase;
049: import org.springframework.mock.web.MockHttpServletRequest;
050: import org.springframework.mock.web.MockHttpServletResponse;
051: import org.springframework.web.servlet.ModelAndView;
052:
053: public class ConfigInterceptorTest extends MockObjectTestCase {
054: private MockHttpServletRequest request;
055:
056: private MockHttpServletResponse response;
057:
058: private Mock mockConfiguration;
059:
060: private ConfigurationService configurationMock;
061:
062: private ConfigInterceptor interceptor;
063: private ModelAndView modelAndView;
064:
065: protected void setUp() throws Exception {
066: request = new MockHttpServletRequest();
067: response = new MockHttpServletResponse();
068: mockConfiguration = mock(ConfigurationService.class,
069: new Class[] { EnvironmentService.class,
070: DashboardXmlConfigService.class,
071: BuildLoopQueryService.class }, new Object[] {
072: null, null, null });
073: configurationMock = (ConfigurationService) mockConfiguration
074: .proxy();
075: interceptor = new ConfigInterceptor(configurationMock);
076: modelAndView = new ModelAndView("model", new HashMap());
077: }
078:
079: public void testShouldSetHasDashboardConfigToFalseWhenTheDashboardConfigLocationIsEmpty()
080: throws Exception {
081: expectsIsForceBuildEnabledCalled(true);
082: expectsGetDashboardConfigLocationCalled(null);
083: interceptor.postHandle(request, response, null, modelAndView);
084: assertEquals("false", modelAndView.getModel().get(
085: ConfigInterceptor.HAS_DASHBOARD_CONFIG_KEY));
086: }
087:
088: public void testShouldSetHasDashboardConfigToFalseWhenTheDashboardConfigLocationIsInvalid()
089: throws Exception {
090: expectsIsForceBuildEnabledCalled(true);
091: expectsGetDashboardConfigLocationCalled("not.exist");
092: interceptor.postHandle(request, response, null, modelAndView);
093: assertEquals("false", modelAndView.getModel().get(
094: ConfigInterceptor.HAS_DASHBOARD_CONFIG_KEY));
095: }
096:
097: public void testShouldSetHasDashboardConfigToTrueWhenTheCruiseLogIsNotEmpty()
098: throws Exception {
099: File tempFile = DataUtils.createTempFile("config", "file");
100: expectsIsForceBuildEnabledCalled(true);
101: expectsGetDashboardConfigLocationCalled(tempFile.getPath());
102: interceptor.postHandle(request, response, null, modelAndView);
103: assertEquals("true", modelAndView.getModel().get(
104: ConfigInterceptor.HAS_DASHBOARD_CONFIG_KEY));
105: }
106:
107: public void testShouldSetGlobalForceBuildEnabledToTrueWhenForceBuildEnabled()
108: throws Exception {
109: expectsIsForceBuildEnabledCalled(true);
110: expectsGetDashboardConfigLocationCalled(null);
111: interceptor.postHandle(request, response, null, modelAndView);
112: assertEquals("true", modelAndView.getModel().get(
113: ConfigInterceptor.FORCE_BUILD_ENABLED_KEY));
114: }
115:
116: public void testShouldSetGlobalForceBuildEnabledToFalseWhenForceBuildDisabled()
117: throws Exception {
118: expectsIsForceBuildEnabledCalled(false);
119: expectsGetDashboardConfigLocationCalled(null);
120: interceptor.postHandle(request, response, null, modelAndView);
121: assertEquals("false", modelAndView.getModel().get(
122: ConfigInterceptor.FORCE_BUILD_ENABLED_KEY));
123: }
124:
125: private void expectsIsForceBuildEnabledCalled(boolean returnValue) {
126: mockConfiguration.expects(once()).method("isForceBuildEnabled")
127: .will(returnValue(returnValue));
128: }
129:
130: private void expectsGetDashboardConfigLocationCalled(
131: String returnValue) {
132: mockConfiguration.expects(once()).method(
133: "getDashboardConfigLocation").will(
134: returnValue(returnValue));
135: }
136:
137: }
|