View Javadoc

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