01: package abbot.util;
02:
03: import java.io.*;
04:
05: import junit.framework.*;
06: import junit.extensions.abbot.*;
07:
08: public class SystemStateTest extends TestCase {
09:
10: public void testPreserveProperties() throws Throwable {
11: final String nullProperty = "null.property";
12: final String knownProperty = "os.name";
13: String nullValue = System.getProperty(nullProperty);
14: String knownValue = System.getProperty(knownProperty);
15: SystemState state = new SystemState();
16:
17: System.setProperty(nullProperty, "non-null");
18: System.setProperty(knownProperty, "dummy value");
19:
20: state.restore();
21: assertEquals("Null property not restored", nullValue, System
22: .getProperty(nullProperty));
23: assertEquals("Known property not restored", knownValue, System
24: .getProperty(knownProperty));
25: }
26:
27: public void testPreserveStreams() throws Throwable {
28: PrintStream out = System.out;
29: PrintStream err = System.err;
30: SystemState state = new SystemState();
31:
32: System.out.close();
33: System.err.close();
34: System.setOut(new PrintStream(new OutputStream() {
35: public void write(int b) {
36: }
37: }));
38: System.setErr(new PrintStream(new OutputStream() {
39: public void write(int b) {
40: }
41: }));
42: System.out.println(getName());
43: System.err.println(getName());
44:
45: state.restore();
46:
47: assertEquals("System.out not preserved", out, System.out);
48: assertEquals("System.err not preserved", err, System.err);
49: assertTrue("System.out was closed", !System.out.checkError());
50: assertTrue("System.err was closed", !System.err.checkError());
51: }
52:
53: public static void main(String[] args) {
54: TestHelper.runTests(args, SystemStateTest.class);
55: }
56: }
|