View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.rolling;
15  
16  import org.junit.After;
17  import org.junit.Before;
18  import org.junit.Test;
19  
20  import ch.qos.logback.classic.ClassicTestConstants;
21  import ch.qos.logback.classic.Logger;
22  import ch.qos.logback.classic.LoggerContext;
23  import ch.qos.logback.classic.joran.JoranConfigurator;
24  import ch.qos.logback.core.joran.spi.JoranException;
25  import ch.qos.logback.core.rolling.testUtil.ScaffoldingForRollingTests;
26  import ch.qos.logback.core.testUtil.CoreTestConstants;
27  import ch.qos.logback.core.testUtil.RandomUtil;
28  import ch.qos.logback.core.testUtil.StatusChecker;
29  import ch.qos.logback.core.util.CachingDateFormatter;
30  
31  /**
32   * Test that we can create time-stamped log files with the help of
33   * the <timestamp> element in configuration files.
34   * 
35   * @author Ceki Gülcü
36   *
37   */
38  public class UniqueFileTest {
39      static String UNIK_DIFF = "UNIK_DIFF";
40  
41      LoggerContext lc = new LoggerContext();
42      StatusChecker sc = new StatusChecker(lc);
43      Logger logger = lc.getLogger(this.getClass());
44      int diff = RandomUtil.getPositiveInt() % 1000;
45      String diffAsStr = Integer.toString(diff);
46  
47      @Before
48      public void setUp() {
49          System.setProperty(UNIK_DIFF, diffAsStr);
50      }
51  
52      @After
53      public void tearDown() {
54          System.clearProperty(UNIK_DIFF);
55      }
56  
57      void loadConfig(String confifFile) throws JoranException {
58          JoranConfigurator jc = new JoranConfigurator();
59          jc.setContext(lc);
60          jc.doConfigure(confifFile);
61      }
62  
63      @Test
64      public void basic() throws Exception {
65          loadConfig(ClassicTestConstants.JORAN_INPUT_PREFIX + "unique.xml");
66          CachingDateFormatter sdf = new CachingDateFormatter("yyyyMMdd'T'HHmm");
67          String timestamp = sdf.format(System.currentTimeMillis());
68  
69          sc.assertIsErrorFree();
70  
71          Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
72          root.info("hello");
73  
74          ScaffoldingForRollingTests.existenceCheck(CoreTestConstants.OUTPUT_DIR_PREFIX + "UNIK_" + timestamp + diffAsStr + "log.txt");
75      }
76  }