1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.inputtools.log4j;
19
20 import java.io.File;
21 import java.net.URL;
22 import java.util.Date;
23
24 import junit.framework.Assert;
25 import junit.framework.TestCase;
26
27 import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
28 import org.apache.log4j.LogManager;
29 import org.apache.log4j.Logger;
30 import org.apache.log4j.helpers.LogLog;
31 import org.apache.log4j.helpers.OptionConverter;
32
33 public class TestChukwaDailyRollingFileAppender extends TestCase {
34
35 @SuppressWarnings("deprecation")
36 public void testlateInit4ChukwaDailyRollingFileAppender() {
37
38 String folder = System.getProperty("test.build.data", "/tmp");
39 File tempDir = new File(folder);
40 if (!tempDir.exists()) {
41 tempDir.mkdirs();
42 }
43
44 String configuratorClassName = OptionConverter.getSystemProperty(
45 LogManager.CONFIGURATOR_CLASS_KEY, null);
46
47 URL url = TestChukwaDailyRollingFileAppender.class
48 .getResource("/late-log4j.properties");
49
50 System.getProperties().setProperty("CHUKWA_TEST_LOG_LATE_INIT", folder);
51
52 if (url != null) {
53 LogLog
54 .debug("Using URL [" + url + "] for automatic log4j configuration.");
55 try {
56 OptionConverter.selectAndConfigure(url, configuratorClassName,
57 LogManager.getLoggerRepository());
58 } catch (NoClassDefFoundError e) {
59 LogLog.warn("Error during default initialization", e);
60 }
61 } else {
62 Assert.fail("URL should not be null");
63 }
64 File logFile = new File(folder + "/chukwaTestLateLogInit.log");
65 if (logFile.exists()) {
66 logFile.delete();
67 }
68 Assert.assertTrue("Log file should not be there", logFile.exists() == false);
69 Logger log = Logger.getLogger(ChukwaAgent.class);
70 try {
71 Thread.sleep(2000);
72 }catch (Exception e) {
73
74 }
75 Assert.assertTrue("Log file should not be there", logFile.exists() == false);
76 log.warn("test 123 " + new Date());
77 Assert.assertTrue("Log file should not be there", logFile.exists() == true);
78 logFile.delete();
79 }
80
81 }