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.core.appender;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertTrue;
19  
20  import java.io.File;
21  import java.util.List;
22  
23  import org.junit.Test;
24  
25  import ch.qos.logback.core.Appender;
26  import ch.qos.logback.core.FileAppender;
27  import ch.qos.logback.core.encoder.DummyEncoder;
28  import ch.qos.logback.core.encoder.NopEncoder;
29  import ch.qos.logback.core.status.Status;
30  import ch.qos.logback.core.status.StatusManager;
31  import ch.qos.logback.core.testUtil.CoreTestConstants;
32  import ch.qos.logback.core.testUtil.RandomUtil;
33  import ch.qos.logback.core.testUtil.StatusChecker;
34  import ch.qos.logback.core.util.StatusPrinter;
35  
36  public class FileAppenderTest extends AbstractAppenderTest<Object> {
37  
38      int diff = RandomUtil.getPositiveInt();
39  
40      protected Appender<Object> getAppender() {
41          return new FileAppender<Object>();
42      }
43  
44      protected Appender<Object> getConfiguredAppender() {
45          FileAppender<Object> appender = new FileAppender<Object>();
46          appender.setEncoder(new NopEncoder<Object>());
47          appender.setFile(CoreTestConstants.OUTPUT_DIR_PREFIX + "temp.log");
48          appender.setName("test");
49          appender.setContext(context);
50          appender.start();
51          return appender;
52      }
53  
54      @Test
55      public void smoke() {
56          String filename = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fat-smoke.log";
57  
58          FileAppender<Object> appender = new FileAppender<Object>();
59          appender.setEncoder(new DummyEncoder<Object>());
60          appender.setAppend(false);
61          appender.setFile(filename);
62          appender.setName("smoke");
63          appender.setContext(context);
64          appender.start();
65          appender.doAppend(new Object());
66          appender.stop();
67  
68          File file = new File(filename);
69          assertTrue(file.exists());
70          assertTrue("failed to delete " + file.getAbsolutePath(), file.delete());
71      }
72  
73      @Test
74      public void testCreateParentFolders() {
75          String filename = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fat-testCreateParentFolders-" + diff + "/testCreateParentFolders.txt";
76          File file = new File(filename);
77          assertFalse(file.getParentFile().exists());
78          assertFalse(file.exists());
79  
80          FileAppender<Object> appender = new FileAppender<Object>();
81          appender.setEncoder(new DummyEncoder<Object>());
82          appender.setAppend(false);
83          appender.setFile(filename);
84          appender.setName("testCreateParentFolders");
85          appender.setContext(context);
86          appender.start();
87          appender.doAppend(new Object());
88          appender.stop();
89          assertTrue(file.getParentFile().exists());
90          assertTrue(file.exists());
91  
92          // cleanup
93          assertTrue("failed to delete " + file.getAbsolutePath(), file.delete());
94          File parent = file.getParentFile();
95          assertTrue("failed to delete " + parent.getAbsolutePath(), parent.delete());
96      }
97  
98      @Test
99      public void testPrudentModeLogicalImplications() {
100         String filename = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "fat-testPrudentModeLogicalImplications.txt";
101         File file = new File(filename);
102         FileAppender<Object> appender = new FileAppender<Object>();
103         appender.setEncoder(new DummyEncoder<Object>());
104         appender.setFile(filename);
105         appender.setName("testPrudentModeLogicalImplications");
106         appender.setContext(context);
107 
108         appender.setAppend(false);
109         appender.setPrudent(true);
110         appender.start();
111 
112         assertTrue(appender.isAppend());
113 
114         StatusManager sm = context.getStatusManager();
115         // StatusPrinter.print(context);
116         StatusChecker statusChecker = new StatusChecker(context);
117         assertEquals(Status.WARN, statusChecker.getHighestLevel(0));
118         List<Status> statusList = sm.getCopyOfStatusList();
119         assertTrue("Expecting status list size to be 2 or larger, but was " + statusList.size(), statusList.size() >= 2);
120         String msg1 = statusList.get(1).getMessage();
121 
122         assertTrue("Got message [" + msg1 + "]", msg1.startsWith("Setting \"Append\" property"));
123 
124         appender.doAppend(new Object());
125         appender.stop();
126         assertTrue(file.exists());
127         assertTrue("failed to delete " + file.getAbsolutePath(), file.delete());
128     }
129 
130     @Test
131     public void fileNameCollision() {
132         String fileName = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "fileNameCollision";
133 
134         FileAppender<Object> appender0 = new FileAppender<Object>();
135         appender0.setName("FA0");
136         appender0.setFile(fileName);
137         appender0.setContext(context);
138         appender0.setEncoder(new DummyEncoder<Object>());
139         appender0.start();
140         assertTrue(appender0.isStarted());
141 
142         FileAppender<Object> appender1 = new FileAppender<Object>();
143         appender1.setName("FA1");
144         appender1.setFile(fileName);
145         appender1.setContext(context);
146         appender1.setEncoder(new DummyEncoder<Object>());
147         appender1.start();
148 
149         assertFalse(appender1.isStarted());
150 
151         StatusPrinter.print(context);
152         StatusChecker checker = new StatusChecker(context);
153         checker.assertContainsMatch(Status.ERROR, "'File' option has the same value");
154 
155     }
156 }