KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > listeners > CurrentBuildStatusFTPListenerTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, 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.listeners;
38
39 import junit.framework.TestCase;
40 import junit.framework.Assert;
41 import net.sourceforge.cruisecontrol.CruiseControlException;
42 import net.sourceforge.cruisecontrol.ProjectState;
43 import net.sourceforge.cruisecontrol.util.Util;
44
45 import java.io.File JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.text.SimpleDateFormat JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.Date JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52
53 /**
54  * @author jerome@coffeebreaks.org
55  */

56 public class CurrentBuildStatusFTPListenerTest extends TestCase {
57     private static final String JavaDoc TEST_DIR = "tmp";
58     private final List JavaDoc filesToClear = new ArrayList JavaDoc();
59     private MockCurrentBuildStatusFTPListener listener;
60
61     class MockCurrentBuildStatusFTPListener extends CurrentBuildStatusFTPListener {
62         private String JavaDoc text;
63         private String JavaDoc expectedText;
64         private String JavaDoc path;
65         private String JavaDoc expectedPath;
66
67         public void setExpectedText(String JavaDoc expectedText) {
68             this.expectedText = expectedText;
69         }
70
71         public void setExpectedPath(String JavaDoc expectedPath) {
72             this.expectedPath = expectedPath;
73         }
74
75         protected void sendFileToFTPPath(String JavaDoc text, String JavaDoc path) throws CruiseControlException {
76             this.text = text;
77             this.path = path;
78         }
79
80         void ensureFileSent() {
81             Assert.assertEquals(expectedPath, path);
82             Assert.assertEquals(expectedText, text);
83         }
84     }
85
86     protected void setUp() throws Exception JavaDoc {
87         listener = new MockCurrentBuildStatusFTPListener();
88     }
89
90     protected void tearDown() {
91         listener = null;
92         for (Iterator JavaDoc iterator = filesToClear.iterator(); iterator.hasNext();) {
93             File JavaDoc file = (File JavaDoc) iterator.next();
94             if (file.exists()) {
95                 file.delete();
96             }
97         }
98         filesToClear.clear();
99     }
100
101     public void testValidate() throws CruiseControlException {
102         try {
103             listener.validate();
104             fail("'file' should be a required attribute");
105         } catch (CruiseControlException cce) {
106         }
107
108         listener.setFile("somefile");
109         try {
110             listener.validate();
111             fail("'destdir' should be a required attribute");
112         } catch (CruiseControlException cce) {
113         }
114
115         listener.setDestDir("destdir");
116         listener.validate();
117     }
118
119     public void testWritingStatus() throws CruiseControlException, IOException JavaDoc {
120         final String JavaDoc fileName = TEST_DIR + File.separator + "_testCurrentBuildStatus.txt";
121         listener.setFile(fileName);
122         listener.setDestDir("/pub");
123         filesToClear.add(new File JavaDoc(fileName));
124
125         checkResultForState(fileName, ProjectState.WAITING);
126         checkResultForState(fileName, ProjectState.IDLE);
127         checkResultForState(fileName, ProjectState.QUEUED);
128         checkResultForState(fileName, ProjectState.BOOTSTRAPPING);
129         checkResultForState(fileName, ProjectState.MODIFICATIONSET);
130         checkResultForState(fileName, ProjectState.BUILDING);
131         checkResultForState(fileName, ProjectState.MERGING_LOGS);
132         checkResultForState(fileName, ProjectState.PUBLISHING);
133         checkResultForState(fileName, ProjectState.PAUSED);
134         checkResultForState(fileName, ProjectState.STOPPED);
135     }
136
137     private void checkResultForState(final String JavaDoc fileName, ProjectState state)
138             throws CruiseControlException, IOException JavaDoc {
139         // This should be equivalent to the date used in listener at seconds precision
140
Date JavaDoc date = new Date JavaDoc();
141         listener.handleEvent(new ProjectStateChangedEvent("projName", state));
142         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("MM/dd/yyyy HH:mm:ss");
143         final String JavaDoc dateString = formatter.format(date);
144         final String JavaDoc description = state.getDescription();
145         String JavaDoc expected = description + " since\n" + dateString;
146         assertEquals(expected, Util.readFileToString(fileName));
147
148         listener.setExpectedPath("/pub" + File.separator + listener.getFileName());
149         listener.setExpectedText(expected);
150         listener.ensureFileSent();
151     }
152 }
153
Popular Tags