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

import static org.junit.Assert.fail;

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

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
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.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]));

			factory = DocumentBuilderFactory.newInstance();
			SchemaFactory schemaFactory = SchemaFactory
					.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
			Schema oldSchema = schemaFactory.newSchema(ClassLoader
					.getSystemResource("updateSchema.xsd"));
			factory.setNamespaceAware(true);
			factory.setSchema(oldSchema);
			builder = factory.newDocumentBuilder();
			
			document = builder.parse(testFile);

			Updater.getInstance().update(document, currentVersion);

			Schema schema = SchemaFactory.newInstance(
					XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(
					ClassLoader.getSystemResource("schema.xsd"));
			schema.newValidator().validate(new DOMSource(document));
			
			document.normalizeDocument();
			schema.newValidator().validate(new DOMSource(document));
		} catch (ParserConfigurationException e) {
			fail(e.getMessage());
		} catch (SAXException e) {
			fail(e.getMessage());
		} catch (IOException e) {
			fail(e.getMessage());
		} catch (VersionTooOldException e) {
			fail(e.getMessage());
		}
	}
}
@@ -351,21 +351,267 @@
 orm.
-dom.DOMSource
+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
 ;%0Aim
@@ -2557,24 +2557,352 @@
 ment));%0A%09%09%09%0A
+%09%09%09TransformerFactory tFactory =%0A%09                TransformerFactory.newInstance();%0A%09            Transformer transformer = tFactory.newTransformer();%0A%09            DOMSource source = new DOMSource(document);%0A%09            StreamResult result = new StreamResult(System.out);%0A%09            transformer.transform(source, result);%0A%09%09%09%0A
 %09%09%09document.
@@ -3223,16 +3223,153 @@
 age());%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%09%7D%0A%7D

debug output