KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > MockCommandline


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 /*
38  * The Apache Software License, Version 1.1
39  *
40  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
41  * reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  *
47  * 1. Redistributions of source code must retain the above copyright
48  * notice, this list of conditions and the following disclaimer.
49  *
50  * 2. Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in
52  * the documentation and/or other materials provided with the
53  * distribution.
54  *
55  * 3. The end-user documentation included with the redistribution, if
56  * any, must include the following acknowlegement:
57  * "This product includes software developed by the
58  * Apache Software Foundation (http://www.apache.org/)."
59  * Alternately, this acknowlegement may appear in the software itself,
60  * if and wherever such third-party acknowlegements normally appear.
61  *
62  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
63  * Foundation" must not be used to endorse or promote products derived
64  * from this software without prior written permission. For written
65  * permission, please contact apache@apache.org.
66  *
67  * 5. Products derived from this software may not be called "Apache"
68  * nor may "Apache" appear in their names without prior written
69  * permission of the Apache Group.
70  *
71  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
72  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
73  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
74  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
75  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
76  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
77  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
78  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
79  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
80  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
81  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82  * SUCH DAMAGE.
83  * ====================================================================
84  *
85  * This software consists of voluntary contributions made by many
86  * individuals on behalf of the Apache Software Foundation. For more
87  * information on the Apache Software Foundation, please see
88  * <http://www.apache.org/>.
89  */

90 package net.sourceforge.cruisecontrol.util;
91
92 import junit.framework.Assert;
93
94 import java.io.InputStream JavaDoc;
95 import java.io.OutputStream JavaDoc;
96 import java.util.Arrays JavaDoc;
97
98 /**
99  * Mocks Commandline objects.
100  *
101  * @author jerome@coffeebreaks.org
102  */

103 public class MockCommandline extends Commandline {
104
105     // private static final Logger LOG = Logger.getLogger(Commandline.class);
106

107     private String JavaDoc[] expectedCommandline;
108     private String JavaDoc expectedWorkingDirectory;
109     private InputStream JavaDoc processErrorStream = null;
110     private InputStream JavaDoc processInputStream = null;
111     private OutputStream JavaDoc processOutputStream = null;
112     private boolean assertCorrectCommandline = true;
113
114     public MockCommandline() {
115     }
116
117     /**
118      * @param expectedCommandline The expectedCommandline to set.
119      */

120     public void setExpectedCommandline(String JavaDoc[] expectedCommandline) {
121         this.expectedCommandline = expectedCommandline;
122     }
123
124     /**
125      * @param expectedWorkingDirectory The expectedWorkingDirectory to set.
126      */

127     public void setExpectedWorkingDirectory(String JavaDoc expectedWorkingDirectory) {
128         this.expectedWorkingDirectory = expectedWorkingDirectory;
129     }
130
131     /**
132      * @param processErrorStream The processErrorStream to set.
133      */

134     public void setProcessErrorStream(InputStream JavaDoc processErrorStream) {
135         this.processErrorStream = processErrorStream;
136     }
137
138     /**
139      * @param processInputStream The processInputStream to set.
140      */

141     public void setProcessInputStream(InputStream JavaDoc processInputStream) {
142         this.processInputStream = processInputStream;
143     }
144
145     /**
146      * @param processOutputStream The processOutputStream to set.
147      */

148     public void setProcessOutputStream(OutputStream JavaDoc processOutputStream) {
149         this.processOutputStream = processOutputStream;
150     }
151
152     /**
153      * Do not perform assertions.
154      * @param assertCorrectCommandline
155      */

156     public void setAssertCorrectCommandline(boolean assertCorrectCommandline) {
157         this.assertCorrectCommandline = assertCorrectCommandline;
158     }
159
160     public void ensureCommandline() {
161         if (!Arrays.equals(expectedCommandline, getCommandline())) {
162             Assert.fail("Command line error expected: "
163                 + buildString(expectedCommandline) + " - got: " + buildString(getCommandline()));
164         }
165     }
166
167     public void ensureWorkingDirectory() {
168         Assert.assertEquals("WorkingDirectory error", expectedWorkingDirectory, getWorkingDirectory());
169     }
170
171     private static final String JavaDoc buildString(String JavaDoc[] array) {
172         if (array == null) {
173             return "null";
174         }
175         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
176         for (int i = 0; i < array.length; i++) {
177             sb.append(array[i]).append(" ");
178         }
179         return sb.toString();
180     }
181     
182     /**
183      * Fakes the execution of the command.
184      * Checks that the command line and the working directory are correctly set / computed.
185      *
186      * @return a MockProcess that uses the streams registered with that instance.
187      */

188     public Process JavaDoc execute() {
189
190         if (assertCorrectCommandline) {
191             ensureCommandline();
192             ensureWorkingDirectory();
193         }
194
195         MockProcess process = getMockProcess();
196         process.setErrorStream(processErrorStream);
197         process.setInputStream(processInputStream);
198         process.setOutputStream(processOutputStream);
199
200         return process;
201     }
202
203     protected MockProcess getMockProcess() {
204         return new MockProcess();
205     }
206
207 }
208
Popular Tags