01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.tools.ant.taskdefs;
19:
20: import java.util.Random;
21:
22: import org.apache.tools.ant.BuildException;
23: import org.apache.tools.ant.Task;
24:
25: /**
26: * A simple task that prints to System.out and System.err and then catches
27: * the output which it then checks. If the output does not match, an
28: * exception is thrown
29: *
30: * @since 1.5
31: * @created 21 February 2002
32: */
33: public class DemuxOutputTask extends Task {
34: private String randomOutValue;
35: private String randomErrValue;
36: private boolean outputReceived = false;
37: private boolean errorReceived = false;
38:
39: public void execute() {
40: Random generator = new Random();
41: randomOutValue = "Output Value is " + generator.nextInt();
42: randomErrValue = "Error Value is " + generator.nextInt();
43:
44: System.out.println(randomOutValue);
45: System.err.println(randomErrValue);
46: if (!outputReceived) {
47: throw new BuildException("Did not receive output");
48: }
49:
50: if (!errorReceived) {
51: throw new BuildException("Did not receive error");
52: }
53: }
54:
55: protected void handleOutput(String line) {
56: line = line.trim();
57: if (line.length() != 0 && !line.equals(randomOutValue)) {
58: String message = "Received = [" + line + "], expected = ["
59: + randomOutValue + "]";
60: throw new BuildException(message);
61: }
62: outputReceived = true;
63: }
64:
65: protected void handleErrorOutput(String line) {
66: line = line.trim();
67: if (line.length() != 0 && !line.equals(randomErrValue)) {
68: String message = "Received = [" + line + "], expected = ["
69: + randomErrValue + "]";
70: throw new BuildException(message);
71: }
72: errorReceived = true;
73: }
74: }
|