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.rolling;
015
016import ch.qos.logback.core.Context;
017import ch.qos.logback.core.ContextBase;
018import ch.qos.logback.core.encoder.EchoEncoder;
019import ch.qos.logback.core.encoder.Encoder;
020import ch.qos.logback.core.rolling.helper.RenameUtil;
021import ch.qos.logback.core.testUtil.CoreTestConstants;
022import ch.qos.logback.core.testUtil.RandomUtil;
023import ch.qos.logback.core.testUtil.StatusChecker;
024import ch.qos.logback.core.util.StatusPrinter;
025
026import org.junit.Before;
027import org.junit.Ignore;
028import org.junit.Test;
029
030import java.io.File;
031import java.io.FileInputStream;
032import java.io.FileNotFoundException;
033import java.io.FileOutputStream;
034import java.io.IOException;
035
036import static org.junit.Assert.assertEquals;
037import static org.junit.Assert.assertTrue;
038
039public class RenameUtilTest {
040
041    Encoder<Object> encoder;
042    Context context = new ContextBase();
043    StatusChecker statusChecker = new StatusChecker(context);
044
045    long currentTime = System.currentTimeMillis();
046    int diff = RandomUtil.getPositiveInt();
047    protected String randomOutputDirAsStr = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/";
048    protected File randomOutputDir = new File(randomOutputDirAsStr);
049
050    @Before
051    public void setUp() throws Exception {
052        encoder = new EchoEncoder<Object>();
053        // if this this the fist test run after 'build clean up' then the
054        // OUTPUT_DIR_PREFIX might be not yet created
055        randomOutputDir.mkdirs();
056    }
057
058    @Test
059    public void renameToNonExistingDirectory() throws IOException, RolloverFailure {
060        RenameUtil renameUtil = new RenameUtil();
061        renameUtil.setContext(context);
062
063        int diff2 = RandomUtil.getPositiveInt();
064        File fromFile = File.createTempFile("from" + diff, "test", randomOutputDir);
065
066        String randomTARGETDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff2;
067
068        renameUtil.rename(fromFile.toString(), new File(randomTARGETDir + "/to.test").toString());
069        StatusPrinter.printInCaseOfErrorsOrWarnings(context);
070        assertTrue(statusChecker.isErrorFree(0));
071    }
072
073    
074    @Test //  LOGBACK-1054 
075    public void renameLockedAbstractFile_LOGBACK_1054 () throws IOException, RolloverFailure {
076        RenameUtil renameUtil = new RenameUtil();
077        renameUtil.setContext(context);
078
079        String abstractFileName = "abstract_pathname-"+diff;
080        
081        String src = CoreTestConstants.OUTPUT_DIR_PREFIX+abstractFileName;
082        String target = abstractFileName + ".target";
083        
084        makeFile(src);
085        
086        FileInputStream fisLock = new FileInputStream(src);
087        renameUtil.rename(src,  target);
088        // release the lock
089        fisLock.close();
090        
091        StatusPrinter.print(context);
092        assertEquals(0, statusChecker.matchCount("Parent of target file ."+target+". is null"));
093    }
094
095    @Test
096    @Ignore
097    public void MANUAL_renamingOnDifferentVolumesOnLinux() throws IOException, RolloverFailure {
098        RenameUtil renameUtil = new RenameUtil();
099        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}