/*
 * Copyright (C) 2018 AXON Ivy AG
 *
 * Licensed 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 ch.ivyteam.ivy.maven;

import java.io.File;
import java.io.IOException;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import ch.ivyteam.ivy.maven.util.ClasspathJar;
import ch.ivyteam.ivy.maven.util.CompilerResult;
import ch.ivyteam.ivy.maven.util.MavenProperties;
import ch.ivyteam.ivy.maven.util.SharedFile;

/**
 * Shares the classpath of the built ivy project and it's engine as public property 
 * and tries to auto-configure maven-surefire-plugin to use this classpath.
 * 
 * @author Reguel Wermelinger
 * @since 6.0.2
 */
@Mojo(name = SetupIvyTestPropertiesMojo.GOAL)
public class SetupIvyTestPropertiesMojo extends AbstractMojo
{
  public static final String GOAL = "ivy-test-properties";
  
  public static final String IVY_ENGINE_CLASSPATH_PROPERTY = "ivy.engine.classpath";
  public static final String IVY_PROJECT_IAR_CLASSPATH_PROPERTY = "ivy.project.iar.classpath";
  public static final String MAVEN_TEST_ADDITIONAL_CLASSPATH_PROPERTY = "maven.test.additionalClasspath";

  @Parameter(property = "project", required = true, readonly = true)
  MavenProject project;

  /** 
   * Set to <code>true</code> to bypass property set-up. 
   * @since 6.1.0
   */
  @Parameter(defaultValue="false", property="maven.test.skip")
  boolean skipTest;

  @Override
  public void execute() throws MojoExecutionException, MojoFailureException
  {
    if (skipTest)
    {
      return;
    }
    
    SharedFile shared = new SharedFile(project);
    
    File engineCp = shared.getEngineClasspathJar();
    
    MavenProperties properties = new MavenProperties(project, getLog());
    
    if (engineCp.exists())
    {
      properties.setMavenProperty(IVY_ENGINE_CLASSPATH_PROPERTY, getClasspath(engineCp));
    }

    File iarCp = shared.getIarDependencyClasspathJar();
    if (iarCp.exists())
    {
      properties.setMavenProperty(IVY_PROJECT_IAR_CLASSPATH_PROPERTY, getClasspath(iarCp));
    }
    
    configureMavenTestProperties();
  }
  
  /**
   * defines properties that are interpreted by maven-surefire.
   */
  private void configureMavenTestProperties()
  {
    String surefireClasspath = "${"+IVY_ENGINE_CLASSPATH_PROPERTY+"}, ${"+IVY_PROJECT_IAR_CLASSPATH_PROPERTY+"}";
    new MavenProperties(project, getLog()).setMavenProperty(MAVEN_TEST_ADDITIONAL_CLASSPATH_PROPERTY, surefireClasspath);
    
    try
    {
      String testOutputDirectory = CompilerResult.load(project).getTestOutputDirectory();
      if (testOutputDirectory != null)
      {
        project.getBuild().setTestOutputDirectory(
                new File(project.getBasedir(), testOutputDirectory).getAbsolutePath());
      }
    }
    catch (IOException ex)
    {
      getLog().warn("Failed to set up ${project.build.testOutputDirectory}", ex);
    }
  }

  private static String getClasspath(File jar)
  {
    return new ClasspathJar(jar).getClasspathFiles();
  }

}

@@ -668,16 +668,75 @@
 ception;
+%0Aimport java.util.List;%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -2319,192 +2319,356 @@
     
-SharedFile shared = new SharedFile(project);%0A    %0A    File engineCp = shared.getEngineClasspathJar
+MavenProperties properties = new MavenProperties(project, getLog());%0A    setIvyProperties(properties);%0A    configureMavenTestProperties(properties);%0A    setTestOutputDirectory
 ();%0A  
-  %0A    MavenProperties properties = new MavenProperties(project, getLog());%0A    
+%7D%0A%0A  private void setIvyProperties(MavenProperties properties)%0A  %7B%0A    SharedFile shared = new SharedFile(project);%0A    File engineCp = shared.getEngineClasspathJar();
 %0A   
@@ -2982,49 +2982,8 @@
   %7D%0A
-    %0A    configureMavenTestProperties();%0A
   %7D%0A
@@ -3105,16 +3105,42 @@
 perties(
+MavenProperties properties
 )%0A  %7B%0A  
@@ -3145,40 +3145,41 @@
     
+List%3C
 String
- surefireClasspath = %22$%7B%22+
+%3E IVY_PROPS = List.of(
 IVY_
@@ -3207,17 +3207,10 @@
 ERTY
-+%22%7D, $%7B%22+
+, 
 IVY_
@@ -3243,56 +3243,120 @@
 ERTY
-+%22%7D%22
+)
 ;%0A    
-new MavenProperties(project, getLog())
+String surefireClasspath = IVY_PROPS.stream().collect(Collectors.joining(%22,%22, %22$%7B%22, %22%7D%22));%0A    properties
 .set
@@ -3433,18 +3433,62 @@
 ath);%0A  
-  
+%7D%0A%0A  private void setTestOutputDirectory()%0A  %7B
 %0A    try

simplify exec flow in test