1 package cz.cuni.amis.tests;
2
3
4 import java.io.PrintWriter;
5 import java.io.StringWriter;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.List;
10 import java.util.Properties;
11 import java.util.logging.Level;
12 import java.util.logging.Logger;
13
14 import org.junit.BeforeClass;
15
16 import cz.cuni.amis.utils.simple_logging.SimpleLogging;
17
18 public class BaseTest {
19
20 public static final String NEW_LINE = System.getProperty("line.separator");
21
22 protected static Logger log;
23
24 @BeforeClass
25 public static void baseTestBeforeClass() {
26 SimpleLogging.initLogging();
27 log = Logger.getLogger("Test");
28 log.setLevel(Level.ALL);
29
30 log.info("BaseTest.baseTestBeforeClass() BEGIN");
31
32 Properties props = System.getProperties();
33 log.info(" Logging initialized.");
34 log.info(" System.getProperties():");
35 List<Object> keys = new ArrayList<Object>(props.keySet());
36 Collections.sort(keys, new Comparator() {
37 @Override
38 public int compare(Object o1, Object o2) {
39 if (o1 == null && o2 == null) return 0;
40 if (o1 == null) return -1;
41 if (o2 == null) return -2;
42 if (o1 instanceof String && o2 instanceof String) {
43 return ((String)o1).compareTo((String)o2);
44 }
45 return o1.hashCode() - o2.hashCode();
46 }
47 });
48 for (Object key : keys) {
49 if (key == null) continue;
50 log.info(" " + key + " = " + props.getProperty(key.toString()));
51 }
52 log.info(" -------");
53 log.info(" BaseTest.isWindows(): " + isWindows());
54 log.info(" BaseTest.isLinux(): " + isLinux());
55 log.info(" BaseTest.is32Bit(): " + is32Bit());
56 log.info(" BaseTest.is64Bit(): " + is64Bit());
57 if (isWindows() && isLinux()) {
58 RuntimeException e = new RuntimeException("Environment not recognized, isWindows() == true, isLinux() == true!");
59 log.severe(process(e));
60 throw e;
61 }
62 if (is32Bit() && is64Bit()) {
63 RuntimeException e = new RuntimeException("Environment not recognized, is32Bit() == true, is64Bit() == true!");
64 log.severe(process(e));
65 throw e;
66 }
67 if (!isWindows() && !isLinux()) {
68 RuntimeException e = new RuntimeException("Environment not recognized, isWindows() == false, isLinux() == false!");
69 log.severe(process(e));
70 throw e;
71 }
72 if (!is32Bit() && !is64Bit()) {
73 RuntimeException e = new RuntimeException("Environment not recognized, is32Bit() == false, is64Bit() == false!");
74 log.severe(process(e));
75 throw e;
76 }
77 log.info("BaseTest.baseTestBeforeClass() END");
78 }
79
80 protected Level getLogLevel() {
81 if (log.getLevel() == Level.ALL) return Level.FINEST;
82 if (log.getLevel() == Level.OFF) return Level.WARNING;
83 if (log.getLevel() == Level.CONFIG) return Level.WARNING;
84 return log.getLevel();
85 }
86
87 protected void assertTrue(String msg, boolean cnd) {
88 if (!cnd) testFailed(msg);
89 }
90
91 protected void assertFalse(String msg, boolean cnd) {
92 assertTrue(msg, !cnd);
93 }
94
95 protected void assertFail(String msg) {
96 RuntimeException e = new RuntimeException(msg);
97 log.severe(process(e));
98 throw e;
99 }
100
101 protected void log(String msg) {
102 log.info(msg);
103 }
104
105 protected void testOk() {
106 log.log(getLogLevel(), "---/// TEST OK ///---");
107 }
108
109 protected void testFailed() {
110 testFailed("TEST FAILED!");
111 }
112
113 public static boolean isWindows() {
114 return System.getProperty("os.name").contains("Windows");
115 }
116
117 public static boolean isLinux() {
118 return System.getProperty("os.name").contains("Linux");
119 }
120
121 public static boolean is32Bit() {
122 if (System.getProperty("sun.arch.data.model") != null) {
123 return System.getProperty("sun.arch.data.model").contains("32");
124 }
125 return !System.getProperty("java.vm.name").contains("64");
126 }
127
128 public static boolean is64Bit() {
129 if (System.getProperty("sun.arch.data.model") != null) {
130 return System.getProperty("sun.arch.data.model").contains("64");
131 }
132 return System.getProperty("java.vm.name").contains("64");
133 }
134
135 protected void testFailed(String msg) {
136 RuntimeException e = new RuntimeException(msg);
137 log.severe(process("TEST FAILED", e));
138 throw e;
139 }
140
141 public static String process(String message, Throwable e) {
142 StringBuffer sb = new StringBuffer();
143 if (message != null) {
144 sb.append(message);
145 sb.append(NEW_LINE);
146 }
147 Throwable cur = e;
148 if (cur != null) {
149 sb.append(cur.getClass().getName() + ": " + cur.getMessage() +
150 cur.getStackTrace() == null || cur.getStackTrace().length == 0 ?
151 " (at UNAVAILABLE)"
152 : " (at " + cur.getStackTrace()[0].toString() + ")"
153 );
154 cur = cur.getCause();
155 while (cur != null) {
156 sb.append(NEW_LINE);
157 sb.append("caused by: ");
158 sb.append(cur.getClass().getName() + ": " + cur.getMessage() +
159 cur.getStackTrace() == null || cur.getStackTrace().length == 0 ?
160 " (at UNAVAILABLE)"
161 : " (at " + cur.getStackTrace()[0].toString() + ")"
162 );
163 cur = cur.getCause();
164 }
165 sb.append(NEW_LINE);
166 sb.append("Stack trace:");
167 sb.append(NEW_LINE);
168 StringWriter stringError = new StringWriter();
169 PrintWriter printError = new PrintWriter(stringError);
170 e.printStackTrace(printError);
171 sb.append(stringError.toString());
172 }
173 return sb.toString();
174 }
175
176 public static String process(Throwable e) {
177 return process(null, e);
178 }
179
180 }