1 37 90 package net.sourceforge.cruisecontrol.util; 91 92 import junit.framework.Assert; 93 94 import java.io.InputStream ; 95 import java.io.OutputStream ; 96 import java.util.Arrays ; 97 98 103 public class MockCommandline extends Commandline { 104 105 107 private String [] expectedCommandline; 108 private String expectedWorkingDirectory; 109 private InputStream processErrorStream = null; 110 private InputStream processInputStream = null; 111 private OutputStream processOutputStream = null; 112 private boolean assertCorrectCommandline = true; 113 114 public MockCommandline() { 115 } 116 117 120 public void setExpectedCommandline(String [] expectedCommandline) { 121 this.expectedCommandline = expectedCommandline; 122 } 123 124 127 public void setExpectedWorkingDirectory(String expectedWorkingDirectory) { 128 this.expectedWorkingDirectory = expectedWorkingDirectory; 129 } 130 131 134 public void setProcessErrorStream(InputStream processErrorStream) { 135 this.processErrorStream = processErrorStream; 136 } 137 138 141 public void setProcessInputStream(InputStream processInputStream) { 142 this.processInputStream = processInputStream; 143 } 144 145 148 public void setProcessOutputStream(OutputStream processOutputStream) { 149 this.processOutputStream = processOutputStream; 150 } 151 152 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 buildString(String [] array) { 172 if (array == null) { 173 return "null"; 174 } 175 StringBuffer sb = new StringBuffer (); 176 for (int i = 0; i < array.length; i++) { 177 sb.append(array[i]).append(" "); 178 } 179 return sb.toString(); 180 } 181 182 188 public Process 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 |