001: /*
002: * Created on Oct 1, 2005
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package net.sourceforge.cruisecontrol.jmx;
008:
009: import java.util.ArrayList;
010: import java.util.List;
011: import java.util.Map;
012: import java.io.File;
013:
014: import junit.framework.TestCase;
015: import net.sourceforge.cruisecontrol.CruiseControlController;
016: import net.sourceforge.cruisecontrol.CruiseControlException;
017: import net.sourceforge.cruisecontrol.ProjectConfig;
018: import net.sourceforge.cruisecontrol.testutil.TestUtil;
019:
020: /**
021: * @author alwick
022: */
023: public class CruiseControlControllerJMXAdaptorTest extends TestCase {
024:
025: private final TestUtil.FilesToDelete filesToDelete = new TestUtil.FilesToDelete();
026:
027: private CruiseControlControllerJMXAdaptor adaptor;
028:
029: protected void setUp() throws Exception {
030: super .setUp();
031: adaptor = new CruiseControlControllerJMXAdaptor(
032: new CruiseControlController());
033: }
034:
035: protected void tearDown() throws Exception {
036: filesToDelete.delete();
037: }
038:
039: public void testShouldReturnAllProjectWithStatus() throws Exception {
040: final ProjectConfig projectConfig = new ProjectConfig() {
041: private static final long serialVersionUID = 1L;
042:
043: public String getName() {
044: return "test";
045: }
046:
047: public String getStatus() {
048: return "now building";
049: }
050:
051: public String getBuildStartTime() {
052: return "20070420061700";
053: }
054:
055: public boolean isPaused() {
056: return false;
057: }
058: };
059: adaptor = new CruiseControlControllerJMXAdaptor(
060: new CruiseControlController() {
061: public List getProjects() {
062: List list = new ArrayList();
063: list.add(projectConfig);
064: return list;
065: }
066: });
067: Map projectsStatus = adaptor.getAllProjectsStatus();
068: assertEquals("now building since 20070420061700",
069: projectsStatus.get("test"));
070: }
071:
072: public void testShouldReturnPausedAsStatus() throws Exception {
073: final ProjectConfig projectConfig = new ProjectConfig() {
074: private static final long serialVersionUID = 1L;
075:
076: public String getName() {
077: return "test";
078: }
079:
080: public String getStatus() {
081: return "waiting for next time to build";
082: }
083:
084: public String getBuildStartTime() {
085: return "20070420061700";
086: }
087:
088: public boolean isPaused() {
089: return true;
090: }
091: };
092: adaptor = new CruiseControlControllerJMXAdaptor(
093: new CruiseControlController() {
094: public List getProjects() {
095: List list = new ArrayList();
096: list.add(projectConfig);
097: return list;
098: }
099: });
100: Map projectsStatus = adaptor.getAllProjectsStatus();
101: assertEquals("paused", projectsStatus.get("test"));
102: }
103:
104: public void testShouldReturnBuildingAsStatusWhenProjectIsBuildingAndPaused()
105: throws Exception {
106: final ProjectConfig projectConfig = new ProjectConfig() {
107: private static final long serialVersionUID = 1L;
108:
109: public String getName() {
110: return "test";
111: }
112:
113: public String getStatus() {
114: return "now building";
115: }
116:
117: public String getBuildStartTime() {
118: return "20070420061700";
119: }
120:
121: public boolean isPaused() {
122: return true;
123: }
124: };
125: adaptor = new CruiseControlControllerJMXAdaptor(
126: new CruiseControlController() {
127: public List getProjects() {
128: List list = new ArrayList();
129: list.add(projectConfig);
130: return list;
131: }
132: });
133: Map projectsStatus = adaptor.getAllProjectsStatus();
134: assertEquals("now building since 20070420061700",
135: projectsStatus.get("test"));
136: }
137:
138: public void testShouldNotIncludeBuildStartTimeInWaitingStatus()
139: throws Exception {
140: final ProjectConfig projectConfig = new ProjectConfig() {
141: private static final long serialVersionUID = 1L;
142:
143: public String getName() {
144: return "test";
145: }
146:
147: public String getStatus() {
148: return "waiting for next time to build";
149: }
150:
151: public String getBuildStartTime() {
152: return "20070420061700";
153: }
154:
155: public boolean isPaused() {
156: return false;
157: }
158: };
159: adaptor = new CruiseControlControllerJMXAdaptor(
160: new CruiseControlController() {
161: public List getProjects() {
162: List list = new ArrayList();
163: list.add(projectConfig);
164: return list;
165: }
166: });
167: Map projectsStatus = adaptor.getAllProjectsStatus();
168: assertEquals(projectConfig.getStatus(), projectsStatus
169: .get("test"));
170: }
171:
172: public void testInvalid() throws Exception {
173: try {
174: StringBuffer sb = new StringBuffer();
175: sb.append("<cruisecontrol>");
176: sb.append("<project name=\"test\" foo=\"foo\"></project>");
177: sb.append("</cruisecontrol>");
178:
179: adaptor.validateConfig(sb.toString());
180: fail("No exception found");
181: } catch (CruiseControlException cce) {
182: // expected
183: }
184: }
185:
186: public void testValid() throws Exception {
187: try {
188: StringBuffer sb = new StringBuffer();
189: sb.append("<cruisecontrol>");
190: sb.append("<project name=\"test\">");
191: sb
192: .append("<modificationset><cvs localworkingcopy=\".\"/></modificationset>");
193: sb.append("<schedule><ant/></schedule>");
194: sb.append("</project>");
195: sb.append("</cruisecontrol>");
196:
197: filesToDelete
198: .add(new File(TestUtil.getTargetDir(), "logs"));
199:
200: adaptor.validateConfig(sb.toString());
201:
202: } catch (CruiseControlException cce) {
203: fail("Validation failed on valid config, reason: "
204: + cce.getMessage());
205: }
206: }
207: }
|