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

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

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

import org.apache.commons.io.FileUtils;
import org.dom4j.dom.DOMElement;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
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;

@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 mergeManager = mock(MergeManager.class);

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

		packager.writeManifest();

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

	}

	@Test
	public void guiPrefsWithSplash() throws IOException {
		PowerMockito.mockStatic(FileUtils.class);
		final File splashImage = new File("");
		when(FileUtils.toFile(null)).thenReturn(splashImage);

		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 mergeManager = mock(MergeManager.class);

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

		packager.writeManifest();

		verify(mergeManager, times(2)).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 mergeManager = mock(MergeManager.class);

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

		packager.writeManifest();

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

	}
}

@@ -378,16 +378,41 @@
 lement;%0A
+import org.junit.Before;%0A
 import o
@@ -893,80 +893,124 @@
 %7B%0A%0A%09
-@Test%0A%09public void guiPrefsWithNoSplash() throws IOException %7B%0A%09%09final R
+private ResourceFinder resourceFinder;%0A%09private MergeManager mergeManager;%0A%0A%09@Before%0A%09public void setUpMocks() %7B%0A%09%09r
 esou
@@ -1011,33 +1011,40 @@
 %09resourceFinder 
-r
+= mock(R
 esourceFinder = 
@@ -1044,39 +1044,127 @@
 nder
- = mock(ResourceFinder.class);%0A
+.class);%0A%09%09mergeManager = mock(MergeManager.class);%0A%09%7D%0A%0A%09@Test%0A%09public void guiPrefsWithNoSplash() throws IOException %7B
 %0A%09%09f
@@ -1354,71 +1354,8 @@
 );%0A%0A
-%09%09final MergeManager mergeManager = mock(MergeManager.class);%0A%0A
 %09%09fi
@@ -1798,76 +1798,8 @@
 );%0A%0A
-%09%09final ResourceFinder resourceFinder = mock(ResourceFinder.class);%0A
 %09%09fi
@@ -2082,71 +2082,8 @@
 );%0A%0A
-%09%09final MergeManager mergeManager = mock(MergeManager.class);%0A%0A
 %09%09fi
@@ -2389,76 +2389,8 @@
 n %7B%0A
-%09%09final ResourceFinder resourceFinder = mock(ResourceFinder.class);%0A
 %09%09wh
@@ -2495,71 +2495,8 @@
 );%0A%0A
-%09%09final MergeManager mergeManager = mock(MergeManager.class);%0A%0A
 %09%09fi

Create mocks in @Before method