1
2import java.security.Policy;
3
4/**
5 *
6 *
7 * @author huizhe.wang@oracle.com
8 */
9public class TestBase {
10    public String filePath;
11    boolean hasSM;
12    String curdir;
13    Policy origPolicy;
14
15    String testName;
16    String errMsg;
17
18    int passed = 0, failed = 0;
19
20    /**
21     * Creates a new instance of StreamReader
22     */
23    public TestBase(String name) {
24        testName = name;
25    }
26
27    //junit @Override
28    protected void setUp() {
29        if (System.getSecurityManager() != null) {
30            hasSM = true;
31            System.setSecurityManager(null);
32        }
33
34        filePath = System.getProperty("test.src");
35        if (filePath == null) {
36            //current directory
37            filePath = System.getProperty("user.dir");
38        }
39        origPolicy = Policy.getPolicy();
40
41    }
42
43    //junit @Override
44    public void tearDown() {
45        // turn off security manager and restore policy
46        System.setSecurityManager(null);
47        Policy.setPolicy(origPolicy);
48        if (hasSM) {
49            System.setSecurityManager(new SecurityManager());
50        }
51        System.out.println("\nNumber of tests passed: " + passed);
52        System.out.println("Number of tests failed: " + failed + "\n");
53
54        if (errMsg != null ) {
55            throw new RuntimeException(errMsg);
56        }
57    }
58
59    void fail(String msg) {
60        if (errMsg == null) {
61            errMsg = msg;
62        } else {
63            errMsg = errMsg + "\n" + msg;
64        }
65        failed++;
66    }
67
68    void success(String msg) {
69        passed++;
70        System.out.println(msg);
71    }
72
73}
74