package org.ligoj.app.plugin.prov;

import java.net.URL;
import java.net.URLClassLoader;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.eclipse.jetty.util.thread.ThreadClassLoaderScope;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

/**
 * Test class of {@link TerraformResource} for {@link ProcessBuilder} platform only.
 */
public class TerraformResourceCLTest {

	private String os = SystemUtils.OS_NAME;

	@Test
	public void newBuildWindows() throws ReflectiveOperationException {
		checkCommands("Windows", new String[] { "cmd.exe", "/c", "terraform" });
	}

	@Test
	public void newBuildOther() throws ReflectiveOperationException {
		checkCommands("FreeBSD", new String[] { "sh", "-c", "terraform" });
	}

	@SuppressWarnings("unchecked")
	private void checkCommands(final String os, final String... command)
			throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		final URL[] urLs = ((URLClassLoader) Main.class.getClassLoader()).getURLs();
		ThreadClassLoaderScope scope = null;
		try {
			System.setProperty("os.name", os);
			final URLClassLoader urlClassLoader = new URLClassLoader(urLs, null);
			scope = new ThreadClassLoaderScope(urlClassLoader);
			Assert.assertEquals(command,
					((Class<TerraformResource>) urlClassLoader.loadClass(TerraformResource.class.getName()))
							.newInstance().newBuilder("some").command());
		} finally {
			IOUtils.closeQuietly(scope);
		}
	}

	@After
	public void restoreOs() {
		System.setProperty("os.name", os);
	}
}

@@ -29,16 +29,68 @@
 .prov;%0A%0A
+import java.lang.reflect.InvocationTargetException;%0A
 import j
@@ -133,16 +133,41 @@
 sLoader;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -239,24 +239,129 @@
 ystemUtils;%0A
+import org.apache.commons.lang3.reflect.FieldUtils;%0Aimport org.apache.commons.lang3.reflect.MethodUtils;%0A
 import org.e
@@ -564,16 +564,19 @@
 platform
+%0A *
  only.%0A 
@@ -793,24 +793,29 @@
 xe%22, %22/c%22, %22
+null 
 terraform%22 %7D
@@ -950,16 +950,21 @@
  %22-c%22, %22
+null 
 terrafor
@@ -978,40 +978,8 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22unchecked%22)%0A
 %09pri
@@ -1043,20 +1043,17 @@
 command)
-%0A%09%09%09
+ 
 throws I
@@ -1098,17 +1098,20 @@
 ception,
- 
+%0A%09%09%09
 ClassNot
@@ -1120,24 +1120,119 @@
 undException
+, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
  %7B%0A%09%09final U
@@ -1521,171 +1521,574 @@
 %0A%09%09%09
-Assert.assertEquals(command,%0A%09%09%09%09%09((Class%3CTerraformResource%3E) urlClassLoader.loadClass(TerraformResource.class.getName()))
+final Object terra = urlClassLoader.loadClass(%22org.ligoj.app.plugin.prov.TerraformResource%22).newInstance();%0A%09%09%09final Object mock = MethodUtils.invokeStaticMethod(urlClassLoader.loadClass(%22org.mockito.Mockito%22), %22mock%22,%0A%09%09%09%09%09urlClassLoader.loadClass(%22org.ligoj.bootstrap.resource.system.configuration.ConfigurationResource%22));
 %0A%09%09%09
-%09%09%09%09.newInstance().newBuilder(%22some%22)
+FieldUtils.writeField(terra, %22configuration%22, mock, true);%0A%0A%09%09%09Assert.assertEquals(Arrays.asList(command),%0A%09%09%09%09%09((ProcessBuilder) MethodUtils.invokeMethod(terra, true, %22newBuilder%22, new Object%5B%5D %7B new String%5B%5D %7B %22terraform%22 %7D %7D))%0A%09%09%09%09%09%09%09
 .com

Fix Test with custom classLoader