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:
040: import javax.servlet.http.HttpServletResponse;
041:
042: import net.sourceforge.cruisecontrol.dashboard.service.BuildLoopQueryService;
043: import net.sourceforge.cruisecontrol.dashboard.service.ConfigurationService;
044: import net.sourceforge.cruisecontrol.dashboard.service.DashboardXmlConfigService;
045: import net.sourceforge.cruisecontrol.dashboard.service.EnvironmentService;
046: import net.sourceforge.cruisecontrol.dashboard.testhelpers.DataUtils;
047:
048: import org.jmock.Mock;
049: import org.jmock.cglib.MockObjectTestCase;
050: import org.springframework.mock.web.MockHttpServletRequest;
051: import org.springframework.mock.web.MockHttpServletResponse;
052: import org.springframework.web.servlet.ModelAndView;
053:
054: public class DownloadLogControllerTest extends MockObjectTestCase {
055: private DownloadController controller;
056:
057: private static final String LOG_FILE = "log20051209122103Lbuild.489.xml";
058:
059: private MockHttpServletRequest request;
060:
061: private MockHttpServletResponse response;
062:
063: private Mock configurationMock;
064:
065: protected void setUp() throws Exception {
066: request = new MockHttpServletRequest();
067: response = new MockHttpServletResponse();
068: request.setMethod("GET");
069: request.setRequestURI("/download/log/project1/" + LOG_FILE);
070: configurationMock = mock(ConfigurationService.class,
071: new Class[] { EnvironmentService.class,
072: DashboardXmlConfigService.class,
073: BuildLoopQueryService.class }, new Object[] {
074: null, null, null });
075: controller = new DownloadController(
076: (ConfigurationService) configurationMock.proxy());
077: }
078:
079: private MockHttpServletRequest getRequest() {
080: return request;
081: }
082:
083: private HttpServletResponse getResponse() {
084: return response;
085: }
086:
087: public void testShouldRenderFileViewIfTargetFileExistsAndCanBeRead()
088: throws Exception {
089: File projectLogRoot = DataUtils
090: .getProjectLogDirAsFile("project1");
091: configurationMock.expects(once()).method("getLogRoot").with(
092: eq("project1")).will(returnValue(projectLogRoot));
093: configurationMock.expects(once()).method("getLogRoot").with(
094: eq("project1")).will(returnValue(projectLogRoot));
095: ModelAndView mov = this .controller.log(getRequest(),
096: getResponse());
097: assertEquals("fileView", mov.getViewName());
098: assertEquals(new File(projectLogRoot, LOG_FILE), mov.getModel()
099: .get("targetFile"));
100: }
101:
102: public void testShouldRenderDownloadViewIfPathContainsWhiteSpace()
103: throws Exception {
104: File projectLogRoot = DataUtils
105: .getProjectLogDirAsFile("project space");
106: getRequest()
107: .setRequestURI(
108: "/download/log/project%20space/log20051209122104Lbuild.467.xml");
109: configurationMock.expects(once()).method("getLogRoot").with(
110: eq("project space")).will(returnValue(projectLogRoot));
111: configurationMock.expects(once()).method("getLogRoot").with(
112: eq("project space")).will(returnValue(projectLogRoot));
113: ModelAndView mov = this .controller.log(getRequest(),
114: getResponse());
115: File logfile = new File(projectLogRoot,
116: "log20051209122104Lbuild.467.xml");
117: assertEquals(logfile, mov.getModel().get("targetFile"));
118: }
119:
120: public void testShouldRenderErrorPageIfFileNotExist()
121: throws Exception {
122: getRequest().setRequestURI("/download/log/project1/IDontExist");
123: configurationMock.expects(once()).method("getLogRoot").with(
124: eq("project1")).will(
125: returnValue(new File("IDontExist")));
126: ModelAndView mov = this .controller.log(getRequest(),
127: getResponse());
128: assertEquals("page_error", mov.getViewName());
129: assertEquals("File does not exist.", mov.getModel().get(
130: "errorMessage"));
131: }
132: }
|