package de.ptb.epics.eve.data.scandescription.updater.tests;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import de.ptb.epics.eve.data.scandescription.updater.Modification;
import de.ptb.epics.eve.data.scandescription.updater.Patch;
import de.ptb.epics.eve.data.scandescription.updater.Updater;
import de.ptb.epics.eve.data.scandescription.updater.VersionTooOldException;
import de.ptb.epics.eve.util.data.Version;

/**
 * @author Marcus Michalsky
 * @since 1.18
 */
public class UpdaterTest {

	/**
	 * Tests whether all included Updates are applied successfully by validating
	 * the result with the XML schema.
	 */
	@Test
	public void testUpdate() {
		try {
			File testFile = new File(ClassLoader.getSystemResource("test.scml")
					.getFile());

			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(new File(ClassLoader
					.getSystemResource("schema.xsd").getFile()));
			Node node = document.getElementsByTagName("schema").item(0);
			String versionString = node.getAttributes().getNamedItem("version")
					.getNodeValue();
			Version currentVersion = new Version(Integer.parseInt(versionString
					.split("\\.")[0]), Integer.parseInt(versionString
					.split("\\.")[1]));

			SchemaFactory schemaFactory = SchemaFactory
					.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
			Schema schema = schemaFactory.newSchema(ClassLoader
					.getSystemResource("schema.xsd"));
			
			factory.setNamespaceAware(true);
			factory.setSchema(schema);
			
			builder.reset();
			document = builder.parse(testFile);
			
			List<Patch> patches = Updater.getInstance().update(document, 
					currentVersion);
			for (Patch p : patches) {
				for (Modification m : p.getModifications()) {
					System.out.println(m.getChangeLog());
				}
			}
			
			schema.newValidator().validate(new DOMSource(document));
			
			DOMSource domSource = new DOMSource(document);
		       StringWriter writer = new StringWriter();
		       StreamResult result = new StreamResult(writer);
		       TransformerFactory tf = TransformerFactory.newInstance();
		       Transformer transformer = tf.newTransformer();
		       transformer.transform(domSource, result);
		       System.out.println(writer.toString());
		} catch (ParserConfigurationException e) {
			fail(e.getMessage());
		} catch (SAXException e) {
			fail(e.getMessage());
		} catch (IOException e) {
			fail(e.getMessage());
		} catch (VersionTooOldException e) {
			fail(e.getMessage());
		} catch (TransformerConfigurationException e) {
			fail(e.getMessage());
		} catch (TransformerException e) {
			fail(e.getMessage());
		}
	}
}
@@ -145,60 +145,8 @@
 ion;
-%0Aimport java.io.StringWriter;%0Aimport java.util.List;
 %0A%0Aim
@@ -351,267 +351,21 @@
 orm.
-Transformer;%0Aimport javax.xml.transform.TransformerConfigurationException;%0Aimport javax.xml.transform.TransformerException;%0Aimport javax.xml.transform.TransformerFactory;%0Aimport javax.xml.transform.dom.DOMSource;%0Aimport javax.xml.transform.stream.StreamResult
+dom.DOMSource
 ;%0Aim
@@ -557,135 +557,8 @@
 n;%0A%0A
-import de.ptb.epics.eve.data.scandescription.updater.Modification;%0Aimport de.ptb.epics.eve.data.scandescription.updater.Patch;%0A
 impo
@@ -1148,24 +1148,288 @@
 Instance();%0A
+%09%09%09SchemaFactory schemaFactory = SchemaFactory%0A%09%09%09%09%09.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);%0A%09%09%09Schema schema = schemaFactory.newSchema(ClassLoader%0A%09%09%09%09%09.getSystemResource(%22schema.xsd%22));%0A%09%09%09factory.setNamespaceAware(true);%0A%09%09%09factory.setSchema(schema);%0A%0A
 %09%09%09DocumentB
@@ -1897,279 +1897,8 @@
 );%0A%0A
-%09%09%09SchemaFactory schemaFactory = SchemaFactory%0A%09%09%09%09%09.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);%0A%09%09%09Schema schema = schemaFactory.newSchema(ClassLoader%0A%09%09%09%09%09.getSystemResource(%22schema.xsd%22));%0A%09%09%09%0A%09%09%09factory.setNamespaceAware(true);%0A%09%09%09factory.setSchema(schema);%0A%09%09%09%0A
 %09%09%09b
@@ -1913,16 +1913,17 @@
 eset();%0A
+%0A
 %09%09%09docum
@@ -1957,37 +1957,12 @@
 e);%0A
-%09%09%09
 %0A%09%09%09
-List%3CPatch%3E patches = 
 Upda
@@ -1996,22 +1996,16 @@
 cument, 
-%0A%09%09%09%09%09
 currentV
@@ -2017,585 +2017,65 @@
 n);%0A
-%09%09%09for (Patch p : patches) %7B%0A%09%09%09%09for (Modification m : p.getModifications()) %7B%0A%09%09%09%09%09System.out.println(m.getChangeLog());%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09schema.newValidator().validate(new DOMSource(document));%0A%09%09%09%0A%09%09%09DOMSource domSource = new DOMSource(document);%0A%09%09       StringWriter writer = new StringWriter();%0A%09%09       StreamResult result = new StreamResult(writer);%0A%09%09       TransformerFactory tf = TransformerFactory.newInstance();%0A%09%09       Transformer transformer = tf.newTransformer();%0A%09%09       transformer.transform(domSource, result);%0A%09%09       System.out.println(writer.toString(
+%0A%09%09%09schema.newValidator().validate(new DOMSource(document
 ));%0A
@@ -2319,145 +2319,8 @@
 ));%0A
-%09%09%7D catch (TransformerConfigurationException e) %7B%0A%09%09%09fail(e.getMessage());%0A%09%09%7D catch (TransformerException e) %7B%0A%09%09%09fail(e.getMessage());%0A
 %09%09%7D%0A

set document factory namespace awareness to true