/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.store.filesystem.internal;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.xwiki.component.phase.InitializationException;
import org.xwiki.environment.Environment;
import org.xwiki.test.LogLevel;
import org.xwiki.test.junit5.LogCaptureExtension;
import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import org.xwiki.test.junit5.mockito.MockComponent;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Unit tests for {@link FilesystemStoreTools}.
 *
 * @version $Id$
 */
@ComponentTest
public class FilesystemStoreToolsTest
{
    @InjectMockComponents
    private FilesystemStoreTools filesystemStoreTools;

    @MockComponent
    private FilesystemAttachmentsConfiguration config;

    @MockComponent
    private Environment environment;

    @RegisterExtension
    LogCaptureExtension logCapture = new LogCaptureExtension(LogLevel.INFO);

    @Test
    public void initialize() throws IOException, InitializationException
    {
        // initialize() method is triggered by the test framework first for injecting the component
        assertEquals(new File(new File("."), "store/file").getCanonicalFile(),
            filesystemStoreTools.getStoreRootDirectory());
        verify(this.environment, times(1)).getPermanentDirectory();

        File configDirectory = Files.createTempDirectory("xwikitest").toFile();

        when(config.getDirectory()).thenReturn(configDirectory);
        when(config.cleanOnStartup()).thenReturn(true);

        filesystemStoreTools.initialize();
        assertEquals(configDirectory, filesystemStoreTools.getStoreRootDirectory());
        verify(this.environment, times(1)).getPermanentDirectory();
        verify(this.config, times(2)).cleanOnStartup();
        assertEquals(String.format("Using filesystem store directory [%s]", configDirectory.toString()),
            logCapture.getMessage(0));
    }
}

@@ -986,36 +986,47 @@
 on;%0A
+%0A
 import 
-java.nio.file.Files;%0A
+org.junit.jupiter.api.AfterAll;
 %0Aimp
@@ -1289,32 +1289,75 @@
 ptureExtension;%0A
+import org.xwiki.test.junit5.XWikiTempDir;%0A
 import org.xwiki
@@ -2061,16 +2061,23 @@
 ion%0A    
+static 
 LogCaptu
@@ -2156,23 +2156,16 @@
 est%0A    
-public 
 void ini
@@ -2172,16 +2172,50 @@
 tialize(
+@XWikiTempDir File configDirectory
 ) throws
@@ -2269,16 +2269,114 @@
      // 
+Use Case 1: Null store directory and cleanOnStartup = false%0A        // Note: FilesystemStoreTools.
 initiali
@@ -2390,16 +2390,22 @@
 thod is 
+first 
 triggere
@@ -2432,17 +2432,12 @@
 ork 
-first for
+when
  inj
@@ -2446,16 +2446,27 @@
 ting the
+%0A        //
  compone
@@ -2463,24 +2463,26 @@
 // component
+.%0A
 %0A        ass
@@ -2689,79 +2689,73 @@
     
-File configDirectory = Files.createTempDirectory(%22xwikitest%22).toFile();
+// Use Case 2: Non-null store directory and cleanOnStartup = true
 %0A%0A  
@@ -2913,24 +2913,25 @@
 itialize();%0A
+%0A
         asse
@@ -3243,16 +3243,326 @@
         
+ logCapture.getMessage(1));%0A    %7D%0A%0A    @AfterAll%0A    static void verifyLog() throws Exception%0A    %7B%0A        // Assert log happening in the first initialize() call.%0A        assertEquals(String.format(%22Using filesystem store directory %5B%25s%5D%22,%0A            new File(new File(%22.%22), %22store/file%22).getCanonicalFile()),
  logCapt

Fix test (tmp dir outside of target + missing capture of log happening before the test) + improved comments to make test easier to understand