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;
037:
038: import java.net.InetAddress;
039: import java.net.UnknownHostException;
040: import java.util.ArrayList;
041: import java.util.Arrays;
042: import java.util.Date;
043: import java.util.List;
044:
045: import junit.framework.TestCase;
046: import net.sourceforge.cruisecontrol.util.DateUtil;
047: import net.sourceforge.cruisecontrol.util.UniqueBuildloopIdentifier;
048:
049: import org.apache.tools.ant.util.DateUtils;
050:
051: public class BuildLoopInformationBuilderTest extends TestCase {
052:
053: private String[] oldProperties;
054:
055: private String[] properties = {
056: BuildLoopInformation.JmxInfo.CRUISECONTROL_JMXPORT,
057: BuildLoopInformation.JmxInfo.CRUISECONTROL_RMIPORT,
058: BuildLoopInformation.JmxInfo.JMX_HTTP_USERNAME,
059: BuildLoopInformation.JmxInfo.JMX_HTTP_PASSWORD };
060:
061: private BuildLoopInformationBuilder builder;
062:
063: protected void setUp() throws Exception {
064: builder = new BuildLoopInformationBuilder(
065: new CruiseControlController());
066: buildLoopInformation = builder.buildBuildLoopInformation();
067: oldProperties = new String[properties.length];
068: for (int i = 0; i < properties.length; i++) {
069: oldProperties[i] = System.getProperty(properties[i]);
070: }
071: }
072:
073: protected void tearDown() throws Exception {
074: for (int i = 0; i < properties.length; i++) {
075: if (oldProperties[i] != null) {
076: System.setProperty(properties[i], oldProperties[i]);
077: }
078: }
079: }
080:
081: public void testShouldGetServerName() throws Exception {
082: String expected = InetAddress.getLocalHost()
083: .getCanonicalHostName();
084: assertEquals(expected, buildLoopInformation.getServerName());
085: }
086:
087: public void testShouldContainBuildloopUUID() throws Exception {
088: String xml = buildLoopInformation.toXml();
089: String uuid = UniqueBuildloopIdentifier.id().toString();
090: assertContains(xml, "<uuid>" + uuid + "</uuid>");
091: }
092:
093: public void testShouldGetJmxInformation() throws Exception {
094: System.setProperty(
095: BuildLoopInformation.JmxInfo.CRUISECONTROL_JMXPORT,
096: "1234");
097: System.setProperty(
098: BuildLoopInformation.JmxInfo.CRUISECONTROL_RMIPORT,
099: "5678");
100: System
101: .setProperty(
102: BuildLoopInformation.JmxInfo.JMX_HTTP_USERNAME,
103: "Chris");
104: System.setProperty(
105: BuildLoopInformation.JmxInfo.JMX_HTTP_PASSWORD,
106: "123asd");
107: builder = new BuildLoopInformationBuilder(
108: new CruiseControlController());
109: BuildLoopInformation.JmxInfo jmxInfo = builder
110: .buildBuildLoopInformation().getJmxInfo();
111:
112: assertEquals(expectedUrl("http", "1234"), jmxInfo
113: .getHttpAdpatorUrl());
114: assertEquals(expectedUrl("rmi", "5678"), jmxInfo.getRmiUrl());
115: assertEquals("Chris", jmxInfo.getUserName());
116: assertEquals("123asd", jmxInfo.getPassword());
117: }
118:
119: private String expectedUrl(String protocol, String port)
120: throws UnknownHostException {
121: return protocol + "://"
122: + InetAddress.getLocalHost().getCanonicalHostName()
123: + ":" + port;
124: }
125:
126: public void testShouldReturnTimestamp() throws Exception {
127: String ts = buildLoopInformation.getTimestamp();
128: long timestamp = DateUtil.parseIso8601(ts).getTime();
129:
130: long now = new Date().getTime();
131:
132: long testingElapse = now - timestamp;
133: assertTrue(0 <= testingElapse && testingElapse < 60 * 1000);
134: }
135:
136: public void testShouldContainJmxInfoInXml() throws Exception {
137: String xml = buildLoopInformation.toXml();
138:
139: assertContains(xml, "<jmx>");
140: assertContains(xml, "<httpurl>" + expectedUrl("http", "1234")
141: + "</httpurl>");
142: assertContains(xml, "<rmiurl>" + expectedUrl("rmi", "5678")
143: + "</rmiurl>");
144: assertContains(xml, "<username>Chris</username>");
145: assertContains(xml, "<password>123asd</password>");
146: }
147:
148: public void testShouldContainProjectsInXml() throws Exception {
149: ProjectState state = ProjectState.BUILDING;
150: BuildLoopInformation buildLoopInformationProvider = createBuildLoopInformationProvider(state);
151: String xml = buildLoopInformationProvider.toXml();
152:
153: assertEquals(1,
154: buildLoopInformationProvider.getProjects().length);
155: assertBasicXml(xml, state);
156:
157: assertEquals(1, buildLoopInformationProvider.getProjects()[0]
158: .getModifications().size());
159: assertContains(xml, "<modifications>");
160: assertContains(xml, "<username>bob</username>");
161: assertContains(xml, "<comment>support security check</comment>");
162: assertContains(xml, "<revision>123</revision>");
163: }
164:
165: public void testShouldNotContainModificationsWhenNotBuilding()
166: throws Exception {
167: ProjectState state = ProjectState.WAITING;
168: BuildLoopInformation buildLoopInformationProvider = createBuildLoopInformationProvider(state);
169: String xml = buildLoopInformationProvider.toXml();
170:
171: assertEquals(1,
172: buildLoopInformationProvider.getProjects().length);
173: assertBasicXml(xml, state);
174:
175: assertEquals(0, buildLoopInformationProvider.getProjects()[0]
176: .getModifications().size());
177: assertContains(xml, "<modifications/>");
178: assertNotContains(xml, "<username>bob</username>");
179: assertNotContains(xml,
180: "<comment>support security check</comment>");
181: assertNotContains(xml, "<revision>123</revision>");
182: }
183:
184: private void assertBasicXml(String xml, ProjectState state) {
185: assertContains(xml, "<projects>");
186: assertContains(xml, "<project>");
187: assertContains(xml, "<name>project1</name>");
188: assertContains(xml, "<status>" + state.getDescription()
189: + "</status>");
190: assertContains(xml, "<buildstarttime>" + dateStringIso8601
191: + "</buildstarttime>");
192: }
193:
194: private BuildLoopInformation createBuildLoopInformationProvider(
195: ProjectState state) {
196: CruiseControlControllerStub controllerStub = new CruiseControlControllerStub(
197: state);
198: BuildLoopInformationBuilder buildInfoBuilder = new BuildLoopInformationBuilder(
199: controllerStub);
200: return buildInfoBuilder.buildBuildLoopInformation();
201: }
202:
203: private void assertNotContains(String xml, String s) {
204: assertTrue("Should not contain " + s + " in " + xml, xml
205: .indexOf(s) == -1);
206: }
207:
208: private void assertContains(String xml, String s) {
209: assertTrue("Should contain " + s + " in " + xml,
210: xml.indexOf(s) > -1);
211: }
212:
213: private static String dateString;
214:
215: private static String dateStringIso8601;
216:
217: private BuildLoopInformation buildLoopInformation;
218:
219: static {
220: dateString = "20031212152235";
221: Date date;
222: try {
223: date = DateUtil.parseFormattedTime(dateString, "");
224: } catch (CruiseControlException e) {
225: throw new RuntimeException(e);
226: }
227: dateStringIso8601 = DateUtils.format(date,
228: DateUtils.ISO8601_DATETIME_PATTERN);
229: }
230:
231: static class CruiseControlControllerStub extends
232: CruiseControlController {
233: private final ProjectState state;
234:
235: CruiseControlControllerStub(ProjectState state) {
236: this .state = state;
237: }
238:
239: public List getProjects() {
240: ProjectConfig p1 = new ProjectConfigStub();
241: return Arrays.asList(new ProjectConfig[] { p1 });
242: }
243:
244: private class ProjectConfigStub extends ProjectConfig {
245:
246: public String getBuildStartTime() {
247: return dateString;
248: }
249:
250: public List getModifications() {
251: if (!isInState(ProjectState.BUILDING)) {
252: throw new RuntimeException(
253: "Should not call this method when not building");
254: }
255: ArrayList list = new ArrayList();
256: Modification m1 = new Modification();
257: m1.comment = "support security check";
258: m1.userName = "bob";
259: m1.revision = "123";
260: list.add(m1);
261: return list;
262: }
263:
264: public String getName() {
265: return "project1";
266: }
267:
268: public String getStatus() {
269: return state.getDescription();
270: }
271:
272: public boolean isInState(ProjectState other) {
273: return state.equals(other);
274: }
275: }
276: }
277: }
|