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.rolling;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.ContextBase;
18  import ch.qos.logback.core.encoder.EchoEncoder;
19  import ch.qos.logback.core.encoder.Encoder;
20  import ch.qos.logback.core.rolling.helper.RenameUtil;
21  import ch.qos.logback.core.testUtil.CoreTestConstants;
22  import ch.qos.logback.core.testUtil.RandomUtil;
23  import ch.qos.logback.core.status.testUtil.StatusChecker;
24  import ch.qos.logback.core.util.StatusPrinter;
25  
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Disabled;
28  import org.junit.jupiter.api.Test;
29  
30  import java.io.File;
31  import java.io.FileInputStream;
32  import java.io.FileNotFoundException;
33  import java.io.FileOutputStream;
34  import java.io.IOException;
35  
36  import static org.junit.jupiter.api.Assertions.assertEquals;
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  public class RenameUtilTest {
40  
41      Encoder<Object> encoder;
42      Context context = new ContextBase();
43      StatusChecker statusChecker = new StatusChecker(context);
44  
45      long currentTime = System.currentTimeMillis();
46      int diff = RandomUtil.getPositiveInt();
47      protected String randomOutputDirAsStr = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/";
48      protected File randomOutputDir = new File(randomOutputDirAsStr);
49  
50      @BeforeEach
51      public void setUp() throws Exception {
52          encoder = new EchoEncoder<Object>();
53          // if this the fist test run after 'build clean up' then the
54          // OUTPUT_DIR_PREFIX might be not yet created
55          randomOutputDir.mkdirs();
56      }
57  
58      @Test
59      public void renameToNonExistingDirectory() throws IOException, RolloverFailure {
60          RenameUtil renameUtil = new RenameUtil();
61          renameUtil.setContext(context);
62  
63          int diff2 = RandomUtil.getPositiveInt();
64          File fromFile = File.createTempFile("from" + diff, "test", randomOutputDir);
65  
66          String randomTARGETDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff2;
67  
68          renameUtil.rename(fromFile.toString(), new File(randomTARGETDir + "/to.test").toString());
69          StatusPrinter.printInCaseOfErrorsOrWarnings(context);
70          assertTrue(statusChecker.isErrorFree(0));
71      }
72  
73      @Test // LOGBACK-1054
74      public void renameLockedAbstractFile_LOGBACK_1054() throws IOException, RolloverFailure {
75          RenameUtil renameUtil = new RenameUtil();
76          renameUtil.setContext(context);
77  
78          String abstractFileName = "abstract_pathname-" + diff;
79  
80          String src = CoreTestConstants.OUTPUT_DIR_PREFIX + abstractFileName;
81          String target = abstractFileName + ".target";
82  
83          makeFile(src);
84  
85          FileInputStream fisLock = new FileInputStream(src);
86          renameUtil.rename(src, target);
87          // release the lock
88          fisLock.close();
89  
90          StatusPrinter.print(context);
91          assertEquals(0, statusChecker.matchCount("Parent of target file ." + target + ". is null"));
92      }
93  
94      @Test
95      @Disabled
96      public void MANUAL_renamingOnDifferentVolumesOnLinux() throws IOException, RolloverFailure {
97          RenameUtil renameUtil = new RenameUtil();
98          renameUtil.setContext(context);
99  
100         String src = "/tmp/ramdisk/foo.txt";
101         makeFile(src);
102 
103         renameUtil.rename(src, "/tmp/foo" + diff + ".txt");
104         StatusPrinter.print(context);
105     }
106 
107     @Test
108     @Disabled
109     public void MANUAL_renamingOnDifferentVolumesOnWindows() throws IOException, RolloverFailure {
110         RenameUtil renameUtil = new RenameUtil();
111         renameUtil.setContext(context);
112 
113         String src = "c:/tmp/foo.txt";
114         makeFile(src);
115 
116         renameUtil.rename(src, "d:/tmp/foo" + diff + ".txt");
117         StatusPrinter.print(context);
118         assertTrue(statusChecker.isErrorFree(0));
119     }
120 
121     private void makeFile(String src) throws FileNotFoundException, IOException {
122 
123         FileOutputStream fos = new FileOutputStream(src);
124         fos.write(("hello" + diff).getBytes());
125         fos.close();
126     }
127 
128 }