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