blob: 73548d888570a3aff77600e5fffdfc44aa29fb49 [file] [log] [blame]
package org.apache.hadoop.chukwa.inputtools.log4j;
import java.io.File;
import java.net.URL;
import java.util.Date;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.helpers.OptionConverter;
public class TestChukwaDailyRollingFileAppender extends TestCase {
@SuppressWarnings("deprecation")
public void testlateInit4ChukwaDailyRollingFileAppender() {
String folder = System.getProperty("test.build.data", "/tmp");
File tempDir = new File(folder);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
// load new log4j
String configuratorClassName = OptionConverter.getSystemProperty(
LogManager.CONFIGURATOR_CLASS_KEY, null);
URL url = TestChukwaDailyRollingFileAppender.class
.getResource("late-log4j.properties");
System.getProperties().setProperty("CHUKWA_TEST_LOG_LATE_INIT", folder);
if (url != null) {
LogLog
.debug("Using URL [" + url + "] for automatic log4j configuration.");
try {
OptionConverter.selectAndConfigure(url, configuratorClassName,
LogManager.getLoggerRepository());
} catch (NoClassDefFoundError e) {
LogLog.warn("Error during default initialization", e);
}
} else {
Assert.fail("URL should not be null");
}
File logFile = new File(folder + "/chukwaTestLateLogInit.log");
if (logFile.exists()) {
logFile.delete();
}
Assert.assertTrue("Log file should not be there", logFile.exists() == false);
Logger log = Logger.getLogger(ChukwaAgent.class);
try {
Thread.sleep(2000);
}catch (Exception e) {
// do nothing
}
Assert.assertTrue("Log file should not be there", logFile.exists() == false);
log.warn("test 123 " + new Date());
Assert.assertTrue("Log file should not be there", logFile.exists() == true);
logFile.delete();
}
}