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.testUtil.StatusChecker;
24  import ch.qos.logback.core.util.StatusPrinter;
25  
26  import org.junit.Before;
27  import org.junit.Ignore;
28  import org.junit.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.Assert.assertEquals;
37  import static org.junit.Assert.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      @Before
51      public void setUp() throws Exception {
52          encoder = new EchoEncoder<Object>();
53          // if this 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      
74      @Test //  LOGBACK-1054 
75      public void renameLockedAbstractFile_LOGBACK_1054 () throws IOException, RolloverFailure {
76          RenameUtil renameUtil = new RenameUtil();
77          renameUtil.setContext(context);
78  
79          String abstractFileName = "abstract_pathname-"+diff;
80          
81          String src = CoreTestConstants.OUTPUT_DIR_PREFIX+abstractFileName;
82          String target = abstractFileName + ".target";
83          
84          makeFile(src);
85          
86          FileInputStream fisLock = new FileInputStream(src);
87          renameUtil.rename(src,  target);
88          // release the lock
89          fisLock.close();
90          
91          StatusPrinter.print(context);
92          assertEquals(0, statusChecker.matchCount("Parent of target file ."+target+". is null"));
93      }
94  
95      @Test
96      @Ignore
97      public void MANUAL_renamingOnDifferentVolumesOnLinux() throws IOException, RolloverFailure {
98          RenameUtil renameUtil = new RenameUtil();
99          renameUtil.setContext(context);
100 
101         String src = "/tmp/ramdisk/foo.txt";
102         makeFile(src);
103 
104         renameUtil.rename(src, "/tmp/foo" + diff + ".txt");
105         StatusPrinter.print(context);
106     }
107 
108 
109     @Test
110     @Ignore
111     public void MANUAL_renamingOnDifferentVolumesOnWindows() throws IOException, RolloverFailure {
112         RenameUtil renameUtil = new RenameUtil();
113         renameUtil.setContext(context);
114 
115         String src = "c:/tmp/foo.txt"; 
116         makeFile(src);
117         
118         renameUtil.rename(src, "d:/tmp/foo" + diff + ".txt");
119         StatusPrinter.print(context);
120         assertTrue(statusChecker.isErrorFree(0));
121     }
122 
123     private void makeFile(String src) throws FileNotFoundException, IOException {
124         
125         FileOutputStream fos = new FileOutputStream(src);
126         fos.write(("hello" + diff).getBytes());
127         fos.close();
128     }
129 
130    
131 }