package com.izforge.izpack.compiler.packager.impl;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;

import org.dom4j.dom.DOMElement;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.izforge.izpack.api.adaptator.impl.XMLElementImpl;
import com.izforge.izpack.compiler.resource.ResourceFinder;
import com.izforge.izpack.merge.MergeManager;
import com.izforge.izpack.util.file.FileUtils;

@RunWith(PowerMockRunner.class)
@PrepareForTest(FileUtils.class)
public class PackagerTest {

	@Test
	public void guiPrefsWithNoSplash() throws IOException {
		final ResourceFinder resourceFinder = mock(ResourceFinder.class);

		final DOMElement rootNode = new DOMElement("installation");
		rootNode.add(new DOMElement("guiprefs"));
		when(resourceFinder.getXMLTree()).thenReturn(
				new XMLElementImpl(rootNode));

		final MergeManager mockMergeManager = mock(MergeManager.class);

		final Packager packager = new Packager(null, null, null, null, null,
				null, null, mockMergeManager, null, null, null, resourceFinder);

		packager.writeManifest();

		verify(mockMergeManager).addResourceToMerge(anyString(), anyString());

	}

	@Test
	public void guiPrefsWithSplash() throws IOException {
		final ResourceFinder resourceFinder = mock(ResourceFinder.class);
		final DOMElement rootNode = new DOMElement("installation");
		final DOMElement guiPrefsNode = new DOMElement("guiprefs");
		guiPrefsNode.add(new DOMElement("splash"));
		rootNode.add(guiPrefsNode);
		when(resourceFinder.getXMLTree()).thenReturn(
				new XMLElementImpl(rootNode));

		final MergeManager mockMergeManager = mock(MergeManager.class);

		final Packager packager = new Packager(null, null, null, null, null,
				null, null, mockMergeManager, null, null, null, resourceFinder);

		packager.writeManifest();

		verify(mockMergeManager).addResourceToMerge(anyString(), anyString());

	}

	@Test
	public void noGuiPrefs() throws IOException {
		final ResourceFinder resourceFinder = mock(ResourceFinder.class);
		when(resourceFinder.getXMLTree()).thenReturn(
				new XMLElementImpl(new DOMElement("installation")));

		final MergeManager mockMergeManager = mock(MergeManager.class);

		final Packager packager = new Packager(null, null, null, null, null,
				null, null, mockMergeManager, null, null, null, resourceFinder);

		packager.writeManifest();

		verify(mockMergeManager).addResourceToMerge(anyString(), anyString());

	}
}

@@ -131,16 +131,57 @@
 o.mock;%0A
+import static org.mockito.Mockito.times;%0A
 import s
@@ -214,16 +214,16 @@
 verify;%0A
-
 import s
@@ -251,24 +251,45 @@
 kito.when;%0A%0A
+import java.io.File;%0A
 import java.
@@ -301,24 +301,64 @@
 Exception;%0A%0A
+import org.apache.commons.io.FileUtils;%0A
 import org.d
@@ -434,16 +434,63 @@
 unWith;%0A
+import org.powermock.api.mockito.PowerMockito;%0A
 import o
@@ -771,55 +771,8 @@
 ger;
-%0Aimport com.izforge.izpack.util.file.FileUtils;
 %0A%0A@R
@@ -1555,32 +1555,174 @@
 s IOException %7B%0A
+%09%09PowerMockito.mockStatic(FileUtils.class);%0A%09%09final File splashImage = new File(%22%22);%0A%09%09when(FileUtils.toFile(null)).thenReturn(splashImage);%0A%0A
 %09%09final Resource
@@ -2311,32 +2311,42 @@
 mockMergeManager
+, times(2)
 ).addResourceToM
@@ -2354,33 +2354,37 @@
 rge(anyString(),
- 
+%0A%09%09%09%09
 anyString());%0A%0A%09
@@ -2373,33 +2373,32 @@
 %09%09anyString());%0A
-%0A
 %09%7D%0A%0A%09@Test%0A%09publ

Verify number of calls to MergeManager