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.publishers;
037:
038: import junit.framework.TestCase;
039: import net.sourceforge.cruisecontrol.CruiseControlException;
040: import net.sourceforge.cruisecontrol.util.XMLLogHelper;
041: import org.jdom.Element;
042:
043: /**
044: * Unit test for the Jabber publisher which publishes
045: * a link to the build results via Jabber Instant Messaging framework.
046: * Currently, tests are limited to message generation and parameter
047: * validation. Testing of the XMPPConnection mechanism will require a
048: * publicly accessible Jabber server to order to login as the recipient.
049: * Only then can this test represent 100% code coverage.
050: *
051: * @see net.sourceforge.cruisecontrol.publishers.JabberPublisher
052: * @see net.sourceforge.cruisecontrol.publishers.LinkJabberPublisher
053: *
054: * @author <a href="mailto:jonas_edgeworth@cal.berkeley.edu">Jonas Edgeworth</a>
055: * @version 1.0
056: */
057: public class YahooPublisherTest extends TestCase {
058:
059: private YahooPublisher publisher;
060:
061: private XMLLogHelper successLogHelper;
062:
063: private String baseURLString = "http://mybuildserver.com:8080/buildservlet/BuildServlet";
064:
065: protected void setUp() throws Exception {
066: successLogHelper = createLogHelper(true, true);
067: publisher = new YahooPublisher();
068: }
069:
070: protected XMLLogHelper createLogHelper(boolean success,
071: boolean lastBuildSuccess) {
072: Element cruisecontrolElement = new Element("cruisecontrol");
073: Element infoElement = new Element("info");
074:
075: Element logFileElement = new Element("property");
076: logFileElement.setAttribute("name", "logfile");
077: logFileElement.setAttribute("value", "log20020206120000.xml");
078: infoElement.addContent(logFileElement);
079: cruisecontrolElement.addContent(infoElement);
080:
081: Element projectNameElement = new Element("property");
082: projectNameElement.setAttribute("name", "projectname");
083: projectNameElement.setAttribute("value", "Yahootest");
084: infoElement.addContent(projectNameElement);
085:
086: Element buildElement = new Element("build");
087: if (!success) {
088: buildElement.setAttribute("error", "Something went wrong");
089: }
090: cruisecontrolElement.addContent(buildElement);
091:
092: return new XMLLogHelper(cruisecontrolElement);
093: }
094:
095: /**
096: * Test the message creation process assuring that the
097: * generated string matches the associated build URL
098: */
099: public void testCreateMessage() throws CruiseControlException {
100: publisher.setBuildResultsURL(baseURLString);
101: assertEquals(
102: "Build results for successful build of project Yahootest: "
103: + baseURLString + "?log=log20020206120000",
104: publisher.createMessage(successLogHelper));
105: }
106:
107: /**
108: * Test the message creation process assuring that the
109: * generated string matches the associated build URL
110: * with additional parameters passed included.
111: */
112: public void testQuestionMarkInBuildResultsURL()
113: throws CruiseControlException {
114: publisher.setBuildResultsURL(baseURLString + "?key=value");
115:
116: assertEquals(
117: "Build results for successful build of project Yahootest: "
118: + baseURLString
119: + "?key=value&log=log20020206120000", publisher
120: .createMessage(successLogHelper));
121: }
122:
123: /**
124: * Test all validation scenarios related to the
125: * configuration parameters for the LinkJabberPublisher.
126: * Currently, the validation checks the following:<p>
127: * If host is null<br>
128: * If username is null<br>
129: * If password is null<br>
130: * If recipient is null<br>
131: * If buildResultsURL is null<br>
132: * If username is not of the form username@myserver.com<br>
133: * If recipient is of the form recipient@myserver.com<p>
134: * Validation has not been tested against groupchat configurations.
135: */
136: public void testValidate() {
137: // Test if user is null
138: publisher.setUsername(null);
139: publisher.setPassword("asdfdsaf");
140: publisher.setRecipient("recipient");
141: try {
142: publisher.validate();
143: fail("should throw exception if user not set");
144: } catch (CruiseControlException e) {
145: }
146: // Test if username is null
147: publisher.setUsername("tester");
148: publisher.setPassword(null);
149: try {
150: publisher.validate();
151: fail("should throw exception if password not set");
152: } catch (CruiseControlException e) {
153: }
154: // Test is username is of the incorrect form
155: publisher.setUsername("username@adsfdsaf.com");
156: try {
157: publisher.validate();
158: fail("should throw exception if username is of the wrong form");
159: } catch (CruiseControlException e) {
160: }
161: // Test if recipient is null
162: publisher.setPassword("adsfadsf");
163: publisher.setRecipient(null);
164: try {
165: publisher.validate();
166: fail("should throw exception if recipient not set");
167: } catch (CruiseControlException e) {
168: }
169: // Test if recipient is of the incorrect form
170: publisher.setRecipient("recipient");
171: try {
172: publisher.validate();
173: fail("should throw exception if recipient is of the wrong form");
174: } catch (CruiseControlException e) {
175: }
176: // Test if build results url is null
177: publisher.setRecipient("recipient@foo.com");
178: publisher.setBuildResultsURL(null);
179: try {
180: publisher.validate();
181: fail("should throw exception if BuildResultURL not set");
182: } catch (CruiseControlException e) {
183: }
184: }
185:
186: }
|