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.is32Bit(): " + is32Bit()); 58 log.info(" BaseTest.is64Bit(): " + is64Bit()); 59 if (isWindows() && isLinux()) { 60 RuntimeException e = new RuntimeException("Environment not recognized, isWindows() == true, isLinux() == true!"); 61 log.severe(process(e)); 62 throw e; 63 } 64 if (is32Bit() && is64Bit()) { 65 RuntimeException e = new RuntimeException("Environment not recognized, is32Bit() == true, is64Bit() == true!"); 66 log.severe(process(e)); 67 throw e; 68 } 69 if (!isWindows() && !isLinux()) { 70 RuntimeException e = new RuntimeException("Environment not recognized, isWindows() == false, isLinux() == false!"); 71 log.severe(process(e)); 72 throw e; 73 } 74 if (!is32Bit() && !is64Bit()) { 75 RuntimeException e = new RuntimeException("Environment not recognized, is32Bit() == false, is64Bit() == false!"); 76 log.severe(process(e)); 77 throw e; 78 } 79 log.info("BaseTest.baseTestBeforeClass() END"); 80 } 81 82 private long testStart; 83 84 @Before 85 public void beforeTest() { 86 testStart = System.currentTimeMillis(); 87 } 88 89 @After 90 public void afterTest() { 91 log.finest("Test time: " + (System.currentTimeMillis() - testStart) + " ms"); 92 } 93 94 protected Level getLogLevel() { 95 if (log.getLevel() == Level.ALL) return Level.FINEST; 96 if (log.getLevel() == Level.OFF) return Level.WARNING; 97 if (log.getLevel() == Level.CONFIG) return Level.WARNING; 98 return log.getLevel(); 99 } 100 101 protected void assertTrue(String msg, boolean cnd) { 102 if (!cnd) testFailed(msg); 103 } 104 105 protected void assertFalse(String msg, boolean cnd) { 106 assertTrue(msg, !cnd); 107 } 108 109 protected void assertFail(String msg) { 110 RuntimeException e = new RuntimeException(msg); 111 log.severe(process(e)); 112 throw e; 113 } 114 115 protected void log(String msg) { 116 log.info(msg); 117 } 118 119 protected void testOk() { 120 log.log(getLogLevel(), "---/// TEST OK ///---"); 121 } 122 123 protected void testFailed() { 124 testFailed("TEST FAILED!"); 125 } 126 127 public static boolean isWindows() { 128 return System.getProperty("os.name").contains("Windows"); 129 } 130 131 public static boolean isLinux() { 132 return System.getProperty("os.name").contains("Linux"); 133 } 134 135 public static boolean is32Bit() { 136 if (System.getProperty("sun.arch.data.model") != null) { 137 return System.getProperty("sun.arch.data.model").contains("32"); 138 } 139 return !System.getProperty("java.vm.name").contains("64"); 140 } 141 142 public static boolean is64Bit() { 143 if (System.getProperty("sun.arch.data.model") != null) { 144 return System.getProperty("sun.arch.data.model").contains("64"); 145 } 146 return System.getProperty("java.vm.name").contains("64"); 147 } 148 149 protected void testFailed(String msg) { 150 RuntimeException e = new RuntimeException(msg); 151 log.severe(process("TEST FAILED", e)); 152 throw e; 153 } 154 155 public static String process(String message, Throwable e) { 156 StringBuffer sb = new StringBuffer(); 157 if (message != null) { 158 sb.append(message); 159 sb.append(NEW_LINE); 160 } 161 Throwable cur = e; 162 if (cur != null) { 163 sb.append(cur.getClass().getName() + ": " + cur.getMessage() + 164 cur.getStackTrace() == null || cur.getStackTrace().length == 0 ? 165 " (at UNAVAILABLE)" 166 : " (at " + cur.getStackTrace()[0].toString() + ")" 167 ); 168 cur = cur.getCause(); 169 while (cur != null) { 170 sb.append(NEW_LINE); 171 sb.append("caused by: "); 172 sb.append(cur.getClass().getName() + ": " + cur.getMessage() + 173 cur.getStackTrace() == null || cur.getStackTrace().length == 0 ? 174 " (at UNAVAILABLE)" 175 : " (at " + cur.getStackTrace()[0].toString() + ")" 176 ); 177 cur = cur.getCause(); 178 } 179 sb.append(NEW_LINE); 180 sb.append("Stack trace:"); 181 sb.append(NEW_LINE); 182 StringWriter stringError = new StringWriter(); 183 PrintWriter printError = new PrintWriter(stringError); 184 e.printStackTrace(printError); 185 sb.append(stringError.toString()); 186 } 187 return sb.toString(); 188 } 189 190 public static String process(Throwable e) { 191 return process(null, e); 192 } 193 194 }