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