summaryrefslogtreecommitdiffstats
path: root/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/PKIJUnitTest.java
blob: 0ce46c7522b5aefee976fde7c226ac2cd7ed46dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.netscape.beakertests;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * Super class for all the test classes.
 * Provides common functionality for logging messages and
 * providing asserts and getting the test environment parameters
 * @author akoneru
 *
 */
public class PKIJUnitTest {

    public static String INFO = "rlLogInfo";
    public static String DEBUG = "rlLogDebug";
    public static String WARNING = "rlLogWarning";
    public static String ERROR = "rlLogError";
    public static String CRITICAL = "rlLogFatal";

    private BeakerScript beakerScript;
    String logLevel;
    Properties properties;
    private boolean run_with_beaker = false;

    public PKIJUnitTest() {
        String runWithBeaker = System.getenv("RUNNING_WITH_BEAKER");
        if (runWithBeaker == null || (! runWithBeaker.toLowerCase().equals("true"))) {
            properties = new Properties();
            try {
                properties.load(new BufferedReader(new FileReader("tests/dogtag/dev_java_tests/conf/test.cfg")));
            } catch (IOException e) {
                e.printStackTrace();
                System.err.println("Cannot read the configuration file");
                System.exit(-1);
            }
        } else {
            run_with_beaker = true;
            beakerScript = BeakerScript.getInstance();
            logLevel = INFO;
        }
    }

    public void setLogLevel(String logLevel) {
        this.logLevel = logLevel;
    }

    public void log(String message) {
        if (run_with_beaker) {
            beakerScript.addBeakerCommand(new String[] { logLevel, message });
            return;
        }
        System.out.println(message);
    }

    /**
     * Use this method to add asserts to the beaker output.
     *
     * @param hasPassed
     * @param comment
     */
    public void beakerAssert(boolean hasPassed, String comment) {
        if (comment == null) {
            comment = "";
        }
        if (hasPassed) {
            beakerScript.addBeakerCommand(new String[] { "rlPass", comment });
        } else {
            beakerScript.addBeakerCommand(new String[] { "rlFail", comment });
        }
    }

    /**
     * All the configuration entries are set as environment variables when run in beaker.
     * Similar key-value pairs are found in the configuration file when running the tests in eclipse
     * on a local setup.
     * @param key
     * @return
     */
    public String getParameter(String key) {
        if (run_with_beaker) {
            return System.getenv(key);
        }
        return properties.getProperty(key);
    }

}