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  
19  package org.apache.hadoop.chukwa.inputtools.log4j;
20  
21  import java.io.File;
22  import java.net.URL;
23  import java.util.Date;
24  
25  import junit.framework.Assert;
26  import junit.framework.TestCase;
27  
28  import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
29  import org.apache.log4j.LogManager;
30  import org.apache.log4j.Logger;
31  import org.apache.log4j.helpers.LogLog;
32  import org.apache.log4j.helpers.OptionConverter;
33  
34  public class TestTaskLogAppender extends TestCase {
35  
36    @SuppressWarnings("deprecation")
37    public void testTaskLogAppender() {
38  
39      String folder = System.getProperty("test.build.classes");
40      File logFile = new File(folder + "/userlogs/job_200905220200_13470/attempt_200905220200_13470_r_000000_0/syslog");
41      if (logFile.exists()) {
42        logFile.delete();
43      }
44      Assert.assertTrue("Log file should not be there", logFile.exists() == false);
45      File tempDir = new File(folder);
46      if (!tempDir.exists()) {
47        tempDir.mkdirs();
48      }
49      // load new log4j
50      String configuratorClassName = OptionConverter.getSystemProperty(
51          LogManager.CONFIGURATOR_CLASS_KEY, null);
52  
53      URL url = TestTaskLogAppender.class
54          .getResource("/tasklog-log4j.properties");
55  
56      System.getProperties().setProperty("CHUKWA_LOG_DIR", folder);
57  
58      if (url != null) {
59        LogLog
60            .debug("Using URL [" + url + "] for automatic log4j configuration.");
61        try {
62          OptionConverter.selectAndConfigure(url, configuratorClassName,
63              LogManager.getLoggerRepository());
64        } catch (NoClassDefFoundError e) {
65          LogLog.warn("Error during default initialization", e);
66        }
67      } else {
68        Assert.fail("URL should not be null");
69      }
70      Logger log = Logger.getLogger(TestTaskLogAppender.class);
71      try {
72        Thread.sleep(2000);
73      }catch (Exception e) {
74        // do nothing
75      }
76      log.warn("test 123 " + new Date());
77      Assert.assertTrue("Log file should exist", logFile.exists() == true);
78      logFile.delete();
79    }
80  
81  }