KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2004, 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 com.jpeterson.x10.module.CM11A;
42 import com.jpeterson.x10.module.CM17A;
43
44 public class X10PublisherTest extends TestCase {
45
46 // These tests are only useful if you actually have a x10 computer interface connected to the computer.
47
// PJ - Sept 1, 2004
48
//
49
// public void testHandleBuild()
50
// throws CruiseControlException {
51
// X10Publisher x10Publisher = new X10Publisher();
52
// x10Publisher.setDeviceCode("3");
53
// x10Publisher.setHouseCode("A");
54
// x10Publisher.setPort("COM1");
55
//
56
// x10Publisher.handleBuild(true); //light should turn on
57
// x10Publisher.handleBuild(false); //light should turn off
58
//
59
// x10Publisher.setOnWhenBroken(false);
60
// x10Publisher.handleBuild(true); //light should turn off
61
// x10Publisher.handleBuild(false); //light should turn on
62
//
63
// }
64
//
65
// public void testHandleBuildWithAlternateBehavior()
66
// throws CruiseControlException {
67
// X10Publisher x10Publisher = new X10Publisher();
68
// x10Publisher.setDeviceCode("3");
69
// x10Publisher.setHouseCode("A");
70
// x10Publisher.setPort("COM1");
71
// x10Publisher.setOnWhenBroken(false);
72
//
73
// x10Publisher.handleBuild(true); //light should turn off
74
// x10Publisher.handleBuild(false); //light should turn on
75
// }
76

77     public void testSettingAPort() {
78         X10Publisher x10Publisher = new X10Publisher();
79         x10Publisher.setDeviceCode("3");
80         x10Publisher.setHouseCode("A");
81         x10Publisher.setPort("COM1");
82         //Shouldn't get an exception, even if the port doesn't exist,
83
// publish will just fail.
84
x10Publisher.setPort("THIS_ISN'T_A_REAL_PORT");
85     }
86
87     public void testRequiredFieldValidation() throws CruiseControlException {
88         X10Publisher x10Publisher = new X10Publisher();
89
90         try {
91             x10Publisher.validate();
92             fail("Should have gotten an exception when "
93                     + "required fields left blank.");
94         } catch (CruiseControlException e) {
95             assertTrue("Expected this exception.", true);
96         }
97
98         x10Publisher.setHouseCode("A");
99         x10Publisher.setDeviceCode("3");
100
101         x10Publisher.validate();
102     }
103
104     public void testHouseCodeValidation() throws CruiseControlException {
105         X10Publisher x10Publisher = new X10Publisher();
106
107         x10Publisher.setDeviceCode("3"); //Legal
108
x10Publisher.setHouseCode(null); //Not legal
109
try {
110             x10Publisher.validate();
111             fail("Should have gotten an exception when house code set to null");
112         } catch (CruiseControlException e) {
113             assertTrue("Expected this exception.", true);
114         }
115
116         x10Publisher.setHouseCode(""); //Not legal
117
try {
118             x10Publisher.validate();
119             fail("Should have gotten an exception when "
120                     + "house code set to blank");
121         } catch (CruiseControlException e) {
122             assertTrue("Expected this exception.", true);
123         }
124
125         x10Publisher.setHouseCode("1"); //Not legal
126
try {
127             x10Publisher.validate();
128             fail("Should have gotten an exception when house code"
129                     + " set to a number");
130         } catch (CruiseControlException e) {
131             assertTrue("Expected this exception.", true);
132         }
133
134         x10Publisher.setHouseCode("AA"); //Not legal
135
try {
136             x10Publisher.validate();
137             fail("Should have gotten an exception when house code set "
138                     + "to more than one character");
139         } catch (CruiseControlException e) {
140             assertTrue("Expected this exception.", true);
141         }
142
143         String JavaDoc[] illegalHouseCodes = {"Q", "R", "S", "T", "U", "V", "W", "X",
144                                       "Y", "Z"};
145         for (int i = 0; i < illegalHouseCodes.length; i++) {
146             String JavaDoc nextHouseCode = illegalHouseCodes[ i ];
147             x10Publisher.setHouseCode(nextHouseCode); //Not legal
148
try {
149                 x10Publisher.validate();
150                 fail("Should have gotten an exception when house code set to "
151                         + nextHouseCode);
152             } catch (CruiseControlException e) {
153                 assertTrue("Expected this exception.", true);
154             }
155         }
156
157         String JavaDoc[] legalHouseCodes = {"A", "B", "C", "D", "E", "F", "G", "H",
158                                     "I", "J", "K", "L", "M", "N", "O", "P"};
159         for (int i = 0; i < legalHouseCodes.length; i++) {
160             String JavaDoc nextHouseCode = legalHouseCodes[ i ];
161             x10Publisher.setHouseCode(nextHouseCode); //Legal!!
162
x10Publisher.validate();
163         }
164
165         String JavaDoc[] lowerCaseLegalHouseCodes = {"a", "b", "c", "d", "e", "f", "g",
166                                              "h", "i", "j", "k", "l", "m", "n",
167                                              "o", "p"};
168         for (int i = 0; i < lowerCaseLegalHouseCodes.length; i++) {
169             String JavaDoc nextHouseCode = lowerCaseLegalHouseCodes[ i ];
170             x10Publisher.setHouseCode(nextHouseCode); //Legal!!
171
x10Publisher.validate();
172         }
173     }
174
175     public void testDeviceCodeValidation() throws CruiseControlException {
176         X10Publisher x10Publisher = new X10Publisher();
177
178         x10Publisher.setHouseCode("A"); //Legal
179
x10Publisher.setDeviceCode(null); //Not legal
180
try {
181             x10Publisher.validate();
182             fail("Should have gotten an exception when device"
183                     + " code set to null");
184         } catch (CruiseControlException e) {
185             assertTrue("Expected this exception.", true);
186         }
187
188         x10Publisher.setDeviceCode(""); //Not legal
189
try {
190             x10Publisher.validate();
191             fail("Should have gotten an exception when device"
192                     + " code set to blank");
193         } catch (CruiseControlException e) {
194             assertTrue("Expected this exception.", true);
195         }
196
197         x10Publisher.setDeviceCode("A"); //Not legal
198
try {
199             x10Publisher.validate();
200             fail("Should have gotten an exception when device code"
201                     + " set to an alphabetic character");
202         } catch (CruiseControlException e) {
203             assertTrue("Expected this exception.", true);
204         }
205
206         String JavaDoc[] legalDeviceCodes = {"1", "2", "3", "4", "5", "6", "7", "8",
207                                      "9", "10", "11", "12", "13", "14", "15",
208                                      "16"};
209         for (int i = 0; i < legalDeviceCodes.length; i++) {
210             String JavaDoc nextDeviceCode = legalDeviceCodes[ i ];
211             x10Publisher.setDeviceCode(nextDeviceCode); //Legal!!
212
x10Publisher.validate();
213         }
214
215         String JavaDoc[] illegalDeviceCodes = {"-1", "17", "0", "-100", "1.1", "1.56",
216                                        "13.00000000000000000001"};
217         for (int i = 0; i < illegalDeviceCodes.length; i++) {
218             String JavaDoc nextDeviceCode = illegalDeviceCodes[ i ];
219             x10Publisher.setDeviceCode(nextDeviceCode); //Legal!!
220
try {
221                 x10Publisher.validate();
222                 fail("Should have gotten an exception when"
223                         + " the device code is set to "
224                         + nextDeviceCode);
225             } catch (CruiseControlException e) {
226                 assertTrue("Expected this exception", true);
227             }
228         }
229     }
230
231     public void testInterfaceModelValidation() throws CruiseControlException {
232         X10Publisher x10Publisher = new X10Publisher();
233
234         x10Publisher.setHouseCode("A"); //Legal
235
x10Publisher.setDeviceCode("3"); //Legal
236
x10Publisher.setInterfaceModel(null); //Legal
237
x10Publisher.validate();
238
239         x10Publisher.setInterfaceModel("CM11A"); //Legal
240
x10Publisher.validate();
241
242         x10Publisher.setInterfaceModel("CM17A"); //Legal
243
x10Publisher.validate();
244
245         x10Publisher.setInterfaceModel(""); //Legal
246
x10Publisher.validate();
247
248         x10Publisher.setInterfaceModel("cm11a"); //Legal
249
x10Publisher.validate();
250
251         x10Publisher.setInterfaceModel("cm17a"); //Legal
252
x10Publisher.validate();
253
254         x10Publisher.setInterfaceModel("cM11A"); //Legal
255
x10Publisher.validate();
256
257         x10Publisher.setInterfaceModel("cM17A"); //Legal
258
x10Publisher.validate();
259
260         x10Publisher.setInterfaceModel("jibberish"); //NOT Legal
261
try {
262             x10Publisher.validate();
263             fail("Expected an exception");
264         } catch (CruiseControlException e) {
265             assertTrue("Expected this exception", true);
266         }
267
268         x10Publisher.setInterfaceModel("firecraker"); //NOT Legal
269
try {
270             x10Publisher.validate();
271             fail("Expected an exception");
272         } catch (CruiseControlException e) {
273             assertTrue("Expected this exception", true);
274         }
275     }
276
277     public void testGetTransmitter() throws CruiseControlException {
278         X10Publisher x10Publisher = new X10Publisher();
279
280         x10Publisher.setHouseCode("A"); //Legal
281
x10Publisher.setDeviceCode("3"); //Legal
282

283         x10Publisher.setInterfaceModel(null); //Legal
284
assertTrue(x10Publisher.getTransmitter() instanceof CM11A);
285
286         x10Publisher.setInterfaceModel(""); //Legal
287
assertTrue(x10Publisher.getTransmitter() instanceof CM11A);
288
289         x10Publisher.setInterfaceModel("cm11a"); //Legal
290
assertTrue(x10Publisher.getTransmitter() instanceof CM11A);
291
292         x10Publisher.setInterfaceModel("cm11A"); //Legal
293
assertTrue(x10Publisher.getTransmitter() instanceof CM11A);
294
295         x10Publisher.setInterfaceModel("cm17a"); //Legal
296
assertTrue(x10Publisher.getTransmitter() instanceof CM17A);
297
298         x10Publisher.setInterfaceModel("cm17A"); //Legal
299
assertTrue(x10Publisher.getTransmitter() instanceof CM17A);
300     }
301 }
302
Popular Tags