001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.appender; 015 016import static org.junit.Assert.assertEquals; 017import static org.junit.Assert.assertFalse; 018import static org.junit.Assert.assertTrue; 019 020import java.io.File; 021import java.util.List; 022 023import org.junit.Test; 024 025import ch.qos.logback.core.Appender; 026import ch.qos.logback.core.FileAppender; 027import ch.qos.logback.core.encoder.DummyEncoder; 028import ch.qos.logback.core.encoder.NopEncoder; 029import ch.qos.logback.core.status.Status; 030import ch.qos.logback.core.status.StatusManager; 031import ch.qos.logback.core.testUtil.CoreTestConstants; 032import ch.qos.logback.core.testUtil.RandomUtil; 033import ch.qos.logback.core.testUtil.StatusChecker; 034import ch.qos.logback.core.util.StatusPrinter; 035 036public class FileAppenderTest extends AbstractAppenderTest<Object> { 037 038 int diff = RandomUtil.getPositiveInt(); 039 040 protected Appender<Object> getAppender() { 041 return new FileAppender<Object>(); 042 } 043 044 protected Appender<Object> getConfiguredAppender() { 045 FileAppender<Object> appender = new FileAppender<Object>(); 046 appender.setEncoder(new NopEncoder<Object>()); 047 appender.setFile(CoreTestConstants.OUTPUT_DIR_PREFIX + "temp.log"); 048 appender.setName("test"); 049 appender.setContext(context); 050 appender.start(); 051 return appender; 052 } 053 054 @Test 055 public void smoke() { 056 String filename = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fat-smoke.log"; 057 058 FileAppender<Object> appender = new FileAppender<Object>(); 059 appender.setEncoder(new DummyEncoder<Object>()); 060 appender.setAppend(false); 061 appender.setFile(filename); 062 appender.setName("smoke"); 063 appender.setContext(context); 064 appender.start(); 065 appender.doAppend(new Object()); 066 appender.stop(); 067 068 File file = new File(filename); 069 assertTrue(file.exists()); 070 assertTrue("failed to delete " + file.getAbsolutePath(), file.delete()); 071 } 072 073 @Test 074 public void testCreateParentFolders() { 075 String filename = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fat-testCreateParentFolders-" + diff + "/testCreateParentFolders.txt"; 076 File file = new File(filename); 077 assertFalse(file.getParentFile().exists()); 078 assertFalse(file.exists()); 079 080 FileAppender<Object> appender = new FileAppender<Object>(); 081 appender.setEncoder(new DummyEncoder<Object>()); 082 appender.setAppend(false); 083 appender.setFile(filename); 084 appender.setName("testCreateParentFolders"); 085 appender.setContext(context); 086 appender.start(); 087 appender.doAppend(new Object()); 088 appender.stop(); 089 assertTrue(file.getParentFile().exists()); 090 assertTrue(file.exists()); 091 092 // cleanup 093 assertTrue("failed to delete " + file.getAbsolutePath(), file.delete()); 094 File parent = file.getParentFile(); 095 assertTrue("failed to delete " + parent.getAbsolutePath(), parent.delete()); 096 } 097 098 @Test 099 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}