KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > LinkJabberPublisherTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.publishers;
38
39 import junit.framework.TestCase;
40 import net.sourceforge.cruisecontrol.CruiseControlException;
41 import net.sourceforge.cruisecontrol.util.XMLLogHelper;
42 import org.jdom.Element;
43
44 /**
45  * Unit test for the Jabber publisher which publishes
46  * a link to the build results via Jabber Instant Messaging framework.
47  * Currently, tests are limited to message generation and parameter
48  * validation. Testing of the XMPPConnection mechanism will require a
49  * publicly accessible Jabber server to order to login as the recipient.
50  * Only then can this test represent 100% code coverage.
51  *
52  * @see net.sourceforge.cruisecontrol.publishers.JabberPublisher
53  * @see net.sourceforge.cruisecontrol.publishers.LinkJabberPublisher
54  *
55  * @author <a HREF="mailto:jonas_edgeworth@cal.berkeley.edu">Jonas Edgeworth</a>
56  * @version 1.0
57  */

58 public class LinkJabberPublisherTest extends TestCase {
59
60     private LinkJabberPublisher publisher;
61
62     private XMLLogHelper successLogHelper;
63
64     private String JavaDoc baseURLString =
65         "http://mybuildserver.com:8080/buildservlet/BuildServlet";
66
67     protected void setUp() throws Exception JavaDoc {
68         successLogHelper = createLogHelper(true, true);
69         publisher = new LinkJabberPublisher();
70     }
71
72     protected XMLLogHelper createLogHelper(boolean success, boolean lastBuildSuccess) {
73         Element cruisecontrolElement = new Element("cruisecontrol");
74         Element infoElement = new Element("info");
75
76         Element logFileElement = new Element("property");
77         logFileElement.setAttribute("name", "logfile");
78         logFileElement.setAttribute("value", "log20020206120000.xml");
79         infoElement.addContent(logFileElement);
80         cruisecontrolElement.addContent(infoElement);
81
82         Element projectNameElement = new Element("property");
83         projectNameElement.setAttribute("name", "projectname");
84         projectNameElement.setAttribute("value", "Jabbertest");
85         infoElement.addContent(projectNameElement);
86
87         Element buildElement = new Element("build");
88         if (!success) {
89             buildElement.setAttribute("error", "Something went wrong");
90         }
91         cruisecontrolElement.addContent(buildElement);
92         
93         return new XMLLogHelper(cruisecontrolElement);
94     }
95
96     /**
97      * Test the message creation process assuring that the
98      * generated string matches the associated build URL
99      * @throws CruiseControlException
100      */

101     public void testCreateMessage() throws CruiseControlException {
102         publisher.setBuildResultsURL(baseURLString);
103         assertEquals(
104             "Build results for successful build of project Jabbertest: " + baseURLString + "?log=log20020206120000",
105             publisher.createMessage(successLogHelper));
106     }
107
108     /**
109      * Test the message creation process assuring that the
110      * generated string matches the associated build URL
111      * with additional parameters passed included.
112      * @throws CruiseControlException
113      */

114     public void testQuestionMarkInBuildResultsURL() throws CruiseControlException {
115         publisher.setBuildResultsURL(baseURLString + "?key=value");
116
117         assertEquals(
118             "Build results for successful build of project Jabbertest: "
119                 + baseURLString
120                 + "?key=value&log=log20020206120000",
121             publisher.createMessage(successLogHelper));
122     }
123
124     /**
125      * Test all validation scenarios related to the
126      * configuration parameters for the LinkJabberPublisher.
127      * Currently, the validation checks the following:<p>
128      * If host is null<br>
129      * If username is null<br>
130      * If password is null<br>
131      * If recipient is null<br>
132      * If buildResultsURL is null<br>
133      * If username is not of the form username@myserver.com<br>
134      * If recipient is of the form recipient@myserver.com<p>
135      * Validation has not been tested against groupchat configurations.
136      */

137     public void testValidate() {
138         // Test if host is null
139
publisher.setPassword("asdfdsaf");
140         publisher.setRecipient("recipient");
141         publisher.setBuildResultsURL("http://foo.com");
142         try {
143             publisher.validate();
144             fail("should throw exception if host not set");
145         } catch (CruiseControlException e) {
146         }
147         // Test if username is null
148
publisher.setHost("host");
149         publisher.setUsername(null);
150         try {
151             publisher.validate();
152             fail("should throw exception if username not set");
153         } catch (CruiseControlException e) {
154         }
155         // Test is username is of the incorrect form
156
publisher.setUsername("username@adsfdsaf.com");
157         try {
158             publisher.validate();
159             fail("should throw exception if username is of the wrong form");
160         } catch (CruiseControlException e) {
161         }
162         // Test if password is null
163
publisher.setUsername("username");
164         publisher.setPassword(null);
165         try {
166             publisher.validate();
167             fail("should throw exception if password not set");
168         } catch (CruiseControlException e) {
169         }
170         // Test if recipient is null
171
publisher.setPassword("adsfadsf");
172         publisher.setRecipient(null);
173         try {
174             publisher.validate();
175             fail("should throw exception if recipient not set");
176         } catch (CruiseControlException e) {
177         }
178         // Test if recipient is of the incorrect form
179
publisher.setRecipient("recipient");
180         try {
181             publisher.validate();
182             fail("should throw exception if recipient is of the wrong form");
183         } catch (CruiseControlException e) {
184         }
185         // Test if build results url is null
186
publisher.setRecipient("recipient@foo.com");
187         publisher.setBuildResultsURL(null);
188         try {
189             publisher.validate();
190             fail("should throw exception if BuildResultURL not set");
191         } catch (CruiseControlException e) {
192         }
193     }
194
195 }
Popular Tags