| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.felix.gogo.runtime.threadio; |
| 20 |
|
| 21 |
import junit.framework.TestCase; |
| 22 |
|
| 23 |
import java.io.ByteArrayOutputStream; |
| 24 |
import java.io.PrintStream; |
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
public class TestThreadIO extends TestCase |
| 29 |
{ |
| 30 |
|
| 31 |
/** |
| 32 |
* Test if the threadio works in a nested fashion. We first push |
| 33 |
* ten markers on the stack and print a message for each, capturing |
| 34 |
* the output in a ByteArrayOutputStream. Then we pop them, also printing |
| 35 |
* a message identifying the level. Then we verify the output for each level. |
| 36 |
*/ |
| 37 |
public void testNested() |
| 38 |
{ |
| 39 |
ThreadIOImpl tio = new ThreadIOImpl(); |
| 40 |
tio.start(); |
| 41 |
List<ByteArrayOutputStream> list = new ArrayList<ByteArrayOutputStream>(); |
| 42 |
for (int i = 0; i < 10; i++) |
| 43 |
{ |
| 44 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 45 |
list.add(out); |
| 46 |
tio.setStreams(System.in, new PrintStream(out), System.err); |
| 47 |
System.out.print("b" + i); |
| 48 |
} |
| 49 |
for (int i = 9; i >= 0; i--) |
| 50 |
{ |
| 51 |
System.out.println("e" + i); |
| 52 |
tio.close(); |
| 53 |
} |
| 54 |
tio.stop(); |
| 55 |
for (int i = 0; i < 10; i++) |
| 56 |
{ |
| 57 |
String message = list.get(i).toString().trim(); |
| 58 |
assertEquals("b" + i + "e" + i, message); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Simple test too see if the basics work. |
| 64 |
*/ |
| 65 |
public void testSimple() |
| 66 |
{ |
| 67 |
ThreadIOImpl tio = new ThreadIOImpl(); |
| 68 |
tio.start(); |
| 69 |
System.out.println("Hello World"); |
| 70 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 71 |
ByteArrayOutputStream err = new ByteArrayOutputStream(); |
| 72 |
tio.setStreams(System.in, new PrintStream(out), new PrintStream(err)); |
| 73 |
try |
| 74 |
{ |
| 75 |
System.out.println("Simple Normal Message"); |
| 76 |
System.err.println("Simple Error Message"); |
| 77 |
} |
| 78 |
finally |
| 79 |
{ |
| 80 |
tio.close(); |
| 81 |
} |
| 82 |
tio.stop(); |
| 83 |
String normal = out.toString().trim(); |
| 84 |
//String error = err.toString().trim(); |
| 85 |
assertEquals("Simple Normal Message", normal); |
| 86 |
//assertEquals("Simple Error Message", error ); |
| 87 |
System.out.println("Goodbye World"); |
| 88 |
} |
| 89 |
} |