package org.jerrymouse.gaes.mavenPlugin.pomPatch;

import java.io.IOException;
import java.io.StringWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.jerrymouse.gaes.mavenPlugin.archetype.ModelPatchImpl;
import org.jerrymouse.gaes.mavenPlugin.archetype.Patch;
import org.jerrymouse.gaes.mavenPlugin.archetype.PatchReader;
import org.junit.Before;
import org.junit.Test;

public class ModelPatchImplTest {
	Model model;
	PatchReader patchReader;
	ModelPatchImpl modelPatch;
	MavenXpp3Writer mavenXpp3Writer;
	Log log = LogFactory.getLog(getClass());

	@Before
	public void setUp() throws Exception {
		MavenXpp3Reader reader = new MavenXpp3Reader();
		mavenXpp3Writer = new MavenXpp3Writer();
		modelPatch = new ModelPatchImpl();
		patchReader = new PatchReader();
		model = reader.read(ClassLoader
				.getSystemResourceAsStream("pomOriginal.xml"));

	}

	@Test
	public void testAddParentPatch() throws IOException {
		Patch patch = patchReader.read(ClassLoader
				.getSystemResourceAsStream("testAddParent.xml"));
		model = modelPatch.patch(model, patch);
		StringWriter writer = new StringWriter();
		mavenXpp3Writer.write(writer, model);
		log.trace(writer.toString());
	}

	@Test
	public void testRemoveDistributionManagementPatch() throws IOException {
		Patch patch = patchReader
				.read(ClassLoader
						.getSystemResourceAsStream("testRemoveDistributionManagement.xml"));
		model = modelPatch.patch(model, patch);
		StringWriter writer = new StringWriter();
		mavenXpp3Writer.write(writer, model);
		log.trace(writer.toString());
	}
}
