View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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      // load new log4j
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        // do nothing
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  }