/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package org.apache.logging.log4j.core.appender.rolling.action;

import java.io.File;
import java.io.PrintStream;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 *
 */
public class FileRenameActionTest {

    private static final String DIR = "target/fileRename";

    @BeforeClass
    public static void beforeClass() throws Exception {
        final File file = new File(DIR);
        file.mkdirs();
    }

    @AfterClass
    public static void afterClass() {
        deleteDir();
    }

    @After
    public void after() {
        deleteFiles();
    }

    @Test
    public void testRename1() throws Exception {
        final File file = new File("target/fileRename/fileRename.log");
        final PrintStream pos = new PrintStream(file);
        for (int i = 0; i < 100; ++i) {
            pos.println("This is line " + i);
        }
        pos.close();

        final File dest = new File("target/fileRename/newFile.log");
        final FileRenameAction action = new FileRenameAction(file, dest, false);
        action.execute();
        assertTrue("Renamed file does not exist", dest.exists());
        assertTrue("Old file exists", !file.exists());
    }

    @Test
    public void testEmpty() throws Exception {
        final File file = new File("target/fileRename/fileRename.log");
        final PrintStream pos = new PrintStream(file);
        pos.close();

        final File dest = new File("target/fileRename/newFile.log");
        final FileRenameAction action = new FileRenameAction(file, dest, false);
        action.execute();
        assertTrue("Renamed file does not exist", !dest.exists());
        assertTrue("Old file does not exist", !file.exists());
    }


    @Test
    public void testNoParent() throws Exception {
        final File file = new File("fileRename.log");
        final PrintStream pos = new PrintStream(file);
        for (int i = 0; i < 100; ++i) {
            pos.println("This is line " + i);
        }
        pos.close();

        final File dest = new File("newFile.log");
        try {
            final FileRenameAction action = new FileRenameAction(file, dest, false);
            action.execute();
            assertTrue("Renamed file does not exist", dest.exists());
            assertTrue("Old file exists", !file.exists());
        } finally {
            try {
                dest.delete();
                file.delete();
            } catch (final Exception ex) {
                System.out.println("Unable to cleanup files written to main directory");
            }
        }
    }


    private static void deleteDir() {
        final File dir = new File(DIR);
        if (dir.exists()) {
            final File[] files = dir.listFiles();
            for (final File file : files) {
                file.delete();
            }
            dir.delete();
        }
    }

    private static void deleteFiles() {
        final File dir = new File(DIR);
        if (dir.exists()) {
            final File[] files = dir.listFiles();
            for (final File file : files) {
                file.delete();
            }
        }
    }
}

@@ -1582,32 +1582,37 @@
 .log%22);%0A        
+try (
 final PrintStrea
@@ -1632,33 +1632,35 @@
 rintStream(file)
-;
+) %7B
 %0A        for (in
@@ -1644,32 +1644,36 @@
 ile)) %7B%0A        
+    
 for (int i = 0; 
@@ -1680,32 +1680,36 @@
 i %3C 100; ++i) %7B%0A
+    
             pos.
@@ -1738,32 +1738,36 @@
 %22 + i);%0A        
+    
 %7D%0A        pos.cl
@@ -1756,36 +1756,25 @@
   %7D%0A        
-pos.close();
+%7D
 %0A%0A        fi
@@ -2196,32 +2196,37 @@
 .log%22);%0A        
+try (
 final PrintStrea
@@ -2246,33 +2246,35 @@
 rintStream(file)
-;
+) %7B
 %0A        pos.clo
@@ -2262,36 +2262,51 @@
 ) %7B%0A        
-pos.close();
+    // do nothing%0A        %7D
 %0A%0A        fi
@@ -2727,24 +2727,29 @@
 %22);%0A        
+try (
 final PrintS
@@ -2781,17 +2781,19 @@
 am(file)
-;
+) %7B
 %0A       
@@ -2785,32 +2785,36 @@
 ile)) %7B%0A        
+    
 for (int i = 0; 
@@ -2821,32 +2821,36 @@
 i %3C 100; ++i) %7B%0A
+    
             pos.
@@ -2879,32 +2879,36 @@
 %22 + i);%0A        
+    
 %7D%0A        pos.cl
@@ -2905,20 +2905,9 @@
     
-pos.close();
+%7D
 %0A%0A  

Use try-with-resources.