1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package org.openoffice.test.common; 23 24 import java.io.ByteArrayInputStream; 25 import java.io.File; 26 import java.io.InputStream; 27 import java.text.MessageFormat; 28 import java.util.logging.Level; 29 import java.util.logging.LogManager; 30 31 import org.junit.internal.AssumptionViolatedException; 32 import org.junit.rules.TestRule; 33 import org.junit.runner.Description; 34 import org.junit.runners.model.Statement; 35 36 37 public class Logger extends java.util.logging.Logger implements TestRule { 38 private static final String SCREENSHOT_DIR = "output/screenshot"; 39 40 private static String curClassName; 41 42 private boolean screenshotEnabled = true; 43 44 static { 45 File logDir = Testspace.getFile("log"); logDir.mkdirs()46 logDir.mkdirs(); 47 String loggingProperties = 48 "java.util.logging.ConsoleHandler.level=INFO\n" 49 + "java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter\n" 50 + "java.util.logging.FileHandler.level=INFO\n" 51 + "java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter\n" 52 + "java.util.logging.FileHandler.limit=1024000\n" 53 + "java.util.logging.FileHandler.count=1\n" 54 + "java.util.logging.FileHandler.pattern=" + logDir.getAbsolutePath().replace("\\", "/") + "/%u.log\n" 55 + "java.util.logging.FileHandler.append=true\n" 56 + "handlers= java.util.logging.ConsoleHandler,java.util.logging.FileHandler "; 57 InputStream inputStream = new ByteArrayInputStream(loggingProperties.getBytes()); 58 LogManager logManager = LogManager.getLogManager(); 59 try { 60 logManager.readConfiguration(inputStream); 61 } catch (Exception e) { 62 // ignore 63 } 64 65 } 66 Logger(String name)67 private Logger(String name) { 68 super(name, null); 69 } 70 getLogger(Object obj, boolean screenshotEnabled)71 public static Logger getLogger(Object obj, boolean screenshotEnabled) { 72 Logger logger = getLogger(obj); 73 logger.setScreenshotEnabled(screenshotEnabled); 74 return logger; 75 } 76 getLogger(String name, boolean screenshotEnabled)77 public static Logger getLogger(String name, boolean screenshotEnabled) { 78 Logger logger = getLogger(name); 79 logger.setScreenshotEnabled(screenshotEnabled); 80 return logger; 81 } 82 83 @SuppressWarnings("rawtypes") getLogger(Object obj)84 public static Logger getLogger(Object obj) { 85 if (obj == null) 86 return getLogger("global"); 87 return getLogger(obj instanceof Class ? ((Class) obj).getName() : obj.getClass().getName()); 88 } 89 getLogger(String name)90 public static synchronized Logger getLogger(String name) { 91 LogManager manager = LogManager.getLogManager(); 92 Logger result = (Logger) manager.getLogger(name); 93 if (result == null) { 94 result = new Logger(name); 95 manager.addLogger(result); 96 result = (Logger) manager.getLogger(name); 97 } 98 return result; 99 } 100 start(Description description)101 protected void start(Description description) { 102 if (!description.getClassName().equals(curClassName)) { 103 curClassName = description.getClassName(); 104 log(Level.INFO, "Start running test class [" + curClassName + "]"); 105 File temp = Testspace.getFile("classtemp"); 106 FileUtil.deleteFile(temp); 107 log(Level.INFO, "Clean up temp directory for test class [" + temp.getAbsolutePath() + "]"); 108 temp.mkdirs(); 109 } 110 log(Level.INFO, "Start running test method [" + description.getMethodName() + "]"); 111 File temp = Testspace.getFile("temp"); 112 FileUtil.deleteFile(temp); 113 log(Level.INFO, "Clean up temp directory for test method [" + temp.getAbsolutePath() + "]"); 114 temp.mkdirs(); 115 } 116 fail(Throwable e, Description description)117 protected void fail(Throwable e, Description description) { 118 String screenshotPath = ""; 119 if (screenshotEnabled) { 120 screenshotPath = Testspace.getPath(SCREENSHOT_DIR + "/" + description.getDisplayName() + ".png"); 121 GraphicsUtil.screenShot(screenshotPath); 122 } 123 124 log(Level.SEVERE, MessageFormat.format("[{0}] is failed. Screenshot: {1}", description.getMethodName(), screenshotPath), e); 125 // Check if crash occurs! 126 // if (e instanceof CommunicationException) { 127 // logger.severe("Pay attention! OpenOffice maybe crashed or freezed. "); 128 // // If testcase is failed, kill AOO to avoid impacting the following 129 // // test cases. 130 // OpenOffice.killAll(); 131 // } 132 133 } 134 isScreenshotEnabled()135 public boolean isScreenshotEnabled() { 136 return screenshotEnabled; 137 } 138 setScreenshotEnabled(boolean screenshotEnabled)139 public void setScreenshotEnabled(boolean screenshotEnabled) { 140 this.screenshotEnabled = screenshotEnabled; 141 } 142 finish(Description description)143 private void finish(Description description) { 144 log(Level.INFO, "Finish running test method [" + description.getMethodName() + "]"); 145 } 146 147 @Override apply(final Statement base, final Description description)148 public Statement apply(final Statement base, final Description description) { 149 return new Statement() { 150 @Override 151 public void evaluate() throws Throwable { 152 start(description); 153 try { 154 base.evaluate(); 155 // succeeded(description); 156 } catch (AssumptionViolatedException e) { 157 throw e; 158 } catch (Throwable t) { 159 fail(t, description); 160 throw t; 161 } finally { 162 finish(description); 163 } 164 } 165 }; 166 167 } 168 } 169