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.servlet;
037:
038: import java.io.IOException;
039:
040: import javax.servlet.ServletException;
041:
042: import net.sourceforge.cruisecontrol.ProjectState;
043: import net.sourceforge.cruisecontrol.StatusHelper;
044: import net.sourceforge.cruisecontrol.mock.MockServletConfig;
045: import net.sourceforge.cruisecontrol.mock.MockServletContext;
046: import net.sourceforge.cruisecontrol.mock.MockServletRequest;
047: import net.sourceforge.cruisecontrol.mock.MockServletResponse;
048: import junit.framework.TestCase;
049:
050: public class XmlServletTest extends TestCase {
051:
052: private static final String LOGDIR = "LOGDIR";
053: private static final String SINGLE_PROJECT = "SINGLEPROJECT";
054: private static final String STATUS_FILE = "STATUSFILE";
055:
056: private XmlServlet xmlServlet;
057:
058: protected void setUp() throws Exception {
059: super .setUp();
060: this .xmlServlet = new XmlServlet();
061: }
062:
063: protected void tearDown() throws Exception {
064: this .xmlServlet = null;
065: super .tearDown();
066: }
067:
068: public void testInitServletConfig() throws ServletException {
069: MockServletConfig mockConfig = new MockServletConfig();
070: MockServletContext mockContext = new MockServletContext();
071: mockConfig.setServletContext(mockContext);
072: mockConfig.setInitParameter("logDir", LOGDIR);
073: mockConfig.setInitParameter("singleProject", SINGLE_PROJECT);
074: mockConfig.setInitParameter("currentBuildStatusFile",
075: STATUS_FILE);
076:
077: this .xmlServlet.init(mockConfig);
078: assertEquals(LOGDIR, this .xmlServlet.getLogDirPath());
079: assertEquals(SINGLE_PROJECT, this .xmlServlet.getSingleProject());
080: assertEquals(STATUS_FILE, this .xmlServlet.getStatusFile());
081: }
082:
083: public void testServiceFails() throws ServletException, IOException {
084: MockServletRequest mockRequest = new MockServletRequest();
085: MockServletResponse mockResponse = new MockServletResponse();
086:
087: try {
088: this .xmlServlet.service(mockRequest, mockResponse);
089: fail("Should have thown exception - directory not found");
090: } catch (ServletException e) {
091: // OK
092: }
093: String actual = mockResponse.getWritten();
094: assertEquals("", actual);
095: }
096:
097: public void testActivities() throws ServletException, IOException {
098: assertEquals(XmlServlet.ACTIVITY_BUILDING, this .xmlServlet
099: .getXmlActivity(ProjectState.BUILDING.getDescription()
100: + " more text"));
101: assertEquals(XmlServlet.ACTIVITY_CHECKING_MODIFICATIONS,
102: this .xmlServlet
103: .getXmlActivity(ProjectState.MODIFICATIONSET
104: .getDescription()
105: + " more text"));
106: assertEquals(XmlServlet.ACTIVITY_CHECKING_MODIFICATIONS,
107: this .xmlServlet
108: .getXmlActivity(ProjectState.MERGING_LOGS
109: .getDescription()
110: + " more text"));
111: assertEquals(XmlServlet.ACTIVITY_CHECKING_MODIFICATIONS,
112: this .xmlServlet
113: .getXmlActivity(ProjectState.BOOTSTRAPPING
114: .getDescription()
115: + " more text"));
116: assertEquals(XmlServlet.ACTIVITY_CHECKING_MODIFICATIONS,
117: this .xmlServlet.getXmlActivity(ProjectState.PUBLISHING
118: .getDescription()
119: + " more text"));
120: assertEquals(XmlServlet.ACTIVITY_SLEEPING, this .xmlServlet
121: .getXmlActivity(ProjectState.WAITING.getDescription()
122: + " more text"));
123: assertEquals(XmlServlet.ACTIVITY_SLEEPING, this .xmlServlet
124: .getXmlActivity("SomethingElse"));
125: }
126:
127: public void testStatuses() throws ServletException, IOException {
128: assertEquals(XmlServlet.STATUS_UNKNOWN, this .xmlServlet
129: .getXmlStatus("somethingElse"));
130: assertEquals(XmlServlet.STATUS_SUCCESS, this.xmlServlet
131: .getXmlStatus(StatusHelper.PASSED));
132: assertEquals(XmlServlet.STATUS_FAILURE, this.xmlServlet
133: .getXmlStatus(StatusHelper.FAILED));
134: }
135:
136: }
|