/*
 * 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.test.docker.junit5;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.utility.MountableFile;
import org.xwiki.text.StringUtils;

/**
 * Common code for container execution.
 *
 * @version $Id$
 * @since 10.10RC1
 */
public abstract class AbstractContainerExecutor
{
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractContainerExecutor.class);

    private static final boolean IN_A_CONTAINER = new File("/.dockerenv").exists();

    protected void start(GenericContainer container, TestConfiguration testConfiguration)
    {
        if (testConfiguration.isVerbose()) {
            LOGGER.info(String.format("Docker image used: [%s]", container.getDockerImageName()));
            container.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(this.getClass())));
        }

        // Get the latest image in case the tag has been updated on dockerhub.
        if (!testConfiguration.isOffline()) {
            container.getDockerClient().pullImageCmd(container.getDockerImageName());
        }

        DockerTestUtils.startContainer(container);

        // Display logs after the container has been started so that we can see problems happening in the containers
        DockerTestUtils.followOutput(container, getClass());
    }

    /**
     * Utility method to mount a directory from the host to the container with some optimization for different OS.
     *
     * @param container the container on which to mount the directory.
     * @param sourceDirectory the path of the directory on the host to mount in the container.
     * @param targetDirectory the target where to mount the directory in the container.
     * @throws IOException if the symlink creation fails
     * @since 10.11RC1
     */
    protected void mountFromHostToContainer(GenericContainer container, String sourceDirectory,
        String targetDirectory) throws IOException
    {
        // If we're inside a docker container already (docker on docker pattern), then the Docker daemon will be the
        // one from the HOST and hence, any volumes that are mounted is still referenced from the HOST, and not from
        // the Container.
        // Thus we need to map the source and target to the same path and we simulate this by creating a symlink
        // on the HOST to the target location.
        if (IN_A_CONTAINER) {
            Path existing = Paths.get(sourceDirectory);
            Path link = Paths.get(targetDirectory);
            Files.createDirectories(link.getParent());
            Files.createSymbolicLink(link, existing);
            container.withFileSystemBind(targetDirectory, targetDirectory);
        } else {
            // File mounting is awfully slow on Mac OSX. For example starting Tomcat with XWiki mounted takes
            // 45s+, while doing a COPY first and then starting Tomcat takes 8s (+5s for the copy).
            String osName = System.getProperty("os.name").toLowerCase();
            if (osName.startsWith("mac os x")) {
                MountableFile mountableDirectory = MountableFile.forHostPath(sourceDirectory);
                container.withCopyFileToContainer(mountableDirectory, targetDirectory);
            } else {
                container.withFileSystemBind(sourceDirectory, targetDirectory);
            }
        }
    }

    /**
     * Merge two set of properties and generate a String in the Docker command format.
     *
     * @param defaultCommands the default command that can be overridden
     * @param overrideCommands the command overrides
     * @return the string in the format {@code --key=value}
     */
    protected String mergeCommands(Properties defaultCommands, Properties overrideCommands)
    {
        Properties mergedCommands = new Properties();
        mergedCommands.putAll(defaultCommands);
        mergedCommands.putAll(overrideCommands);

        List<String> commands = new ArrayList<>();
        for (String key : mergedCommands.stringPropertyNames()) {
            StringBuilder builder = new StringBuilder();
            builder.append("--").append(key).append('=').append(mergedCommands.getProperty(key));
            commands.add(builder.toString());
        }

        return StringUtils.join(commands, ' ');
    }
}

@@ -930,140 +930,8 @@
 5;%0A%0A
-import java.io.File;%0Aimport java.io.IOException;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0A
 impo
@@ -1507,93 +1507,8 @@
 );%0A%0A
-    private static final boolean IN_A_CONTAINER = new File(%22/.dockerenv%22).exists();%0A%0A
     
@@ -2704,65 +2704,8 @@
 er.%0A
-     * @throws IOException if the symlink creation fails%0A
     
@@ -2862,798 +2862,15 @@
 ory)
- throws IOException%0A    %7B%0A        // If we're inside a docker container already (docker on docker pattern), then the Docker daemon will be the%0A        // one from the HOST and hence, any volumes that are mounted is still referenced from the HOST, and not from%0A        // the Container.%0A        // Thus we need to map the source and target to the same path and we simulate this by creating a symlink%0A        // on the HOST to the target location.%0A        if (IN_A_CONTAINER) %7B%0A            Path existing = Paths.get(sourceDirectory);%0A            Path link = Paths.get(targetDirectory);%0A            Files.createDirectories(link.getParent());%0A            Files.createSymbolicLink(link, existing);%0A            container.withFileSystemBind(targetDirectory, targetDirectory);%0A        %7D else %7B
 %0A    
+%7B%0A
     
@@ -2971,20 +2971,16 @@
 d takes%0A
-    
         
@@ -3071,28 +3071,24 @@
 y).%0A        
-    
 String osNam
@@ -3140,28 +3140,24 @@
 ();%0A        
-   
- 
 if (osName.s
@@ -3177,28 +3177,24 @@
 c os x%22)) %7B%0A
-    
             
@@ -3276,36 +3276,32 @@
 y);%0A            
- 
-   
 container.withCo
@@ -3348,36 +3348,32 @@
 rgetDirectory);%0A
-    
         %7D else %7B
@@ -3377,36 +3377,32 @@
 e %7B%0A            
-    
 container.withFi
@@ -3453,22 +3453,8 @@
 y);%0A
-            %7D%0A
     

Revert the try to make it work when inside a docker container since it cannot work this way