/*
 * Copyright (C) 2016 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 static org.fest.assertions.api.Assertions.assertThat;

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

import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;

public class TestShareEngineClasspathMojo
{
  @Test
  public void engineClasspathIsSharedAsProperty() throws Exception
  {
    ShareEngineCoreClasspathMojo mojo = rule.getMojo();
    assertThat(getEngineClasspathProperty())
            .as("used classpath has not been evaluated.")
            .isNullOrEmpty();

    mojo.execute();

    assertThat(getEngineClasspathProperty())
            .as("used classpath must be shared as property so that other mojos can access it")
            .contains("dummy-ivy.jar");
  }

  private String getEngineClasspathProperty()
  {
    return (String) rule.getMojo().project.getProperties()
            .get(ShareEngineCoreClasspathMojo.IVY_ENGINE_CORE_CLASSPATH_PROPERTY);
  }

  @Rule
  public ProjectMojoRule<ShareEngineCoreClasspathMojo> rule = new ProjectMojoRule<ShareEngineCoreClasspathMojo>(
          new File("src/test/resources/base"), ShareEngineCoreClasspathMojo.GOAL)
    {
      @Override
      protected void before() throws Throwable
      {
        super.before();
        writeEngineLibDir();
      }

      private void writeEngineLibDir()
      {
        File engineDirectory = rule.getMojo().getEngineDirectory();
        try
        {
          FileUtils.touch(new File(engineDirectory, "lib/shared/dummy-shared.jar"));
          FileUtils.touch(new File(engineDirectory, "/lib/patch/dummy-patch.jar"));
          FileUtils.touch(new File(engineDirectory, "/lib/ivy/dummy-ivy.jar"));
        }
        catch (IOException ex)
        {
          throw new RuntimeException("Cannot create server jars", ex);
        }
        
      }
    };
}

@@ -730,16 +730,44 @@
 ception;
+%0Aimport java.nio.file.Files;
 %0A%0Aimport
@@ -800,16 +800,71 @@
 eUtils;%0A
+import org.apache.maven.plugin.MojoExecutionException;%0A
 import o
@@ -1918,24 +1918,58 @@
 r.before();%0A
+        configureMojo(getMojo());%0A
         writ
@@ -1994,16 +1994,243 @@
      %7D%0A%0A
+      protected void configureMojo(AbstractEngineMojo newMojo) throws IOException%0A      %7B%0A        File engineDir = Files.createTempDirectory(%22tmpEngineDir%22).toFile();%0A        newMojo.engineDirectory = engineDir;%0A      %7D%0A      %0A
       pr
@@ -2260,32 +2260,56 @@
 ibDir()%0A      %7B%0A
+        try%0A        %7B%0A  
         File eng
@@ -2338,17 +2338,28 @@
 tMojo().
-g
+identifyAndG
 etEngine
@@ -2371,38 +2371,16 @@
 tory();%0A
-        try%0A        %7B%0A
         
@@ -2505,25 +2505,24 @@
 Directory, %22
-/
 lib/patch/du
@@ -2592,17 +2592,16 @@
 ctory, %22
-/
 lib/ivy/
@@ -2655,16 +2655,41 @@
 ception 
+%7C MojoExecutionException 
 ex)%0A    

fix test setup