tim@52: /** tim@52: * tim@52: */ tim@52: package de.intevation.gnv.artifacts; tim@52: tim@58: import java.io.File; tim@58: import java.io.IOException; tim@54: import java.io.StringWriter; tim@54: tim@58: import javax.xml.parsers.DocumentBuilder; tim@52: import javax.xml.parsers.DocumentBuilderFactory; tim@52: import javax.xml.parsers.ParserConfigurationException; tim@54: import javax.xml.transform.Transformer; tim@54: import javax.xml.transform.TransformerConfigurationException; tim@54: import javax.xml.transform.TransformerException; tim@54: import javax.xml.transform.TransformerFactory; tim@54: import javax.xml.transform.TransformerFactoryConfigurationError; tim@54: import javax.xml.transform.dom.DOMSource; tim@54: import javax.xml.transform.stream.StreamResult; tim@52: tim@52: import org.apache.log4j.BasicConfigurator; tim@52: import org.apache.log4j.Logger; tim@52: import org.w3c.dom.Document; tim@58: import org.xml.sax.SAXException; tim@52: tim@52: import de.intevation.artifactdatabase.Config; tim@52: import de.intevation.artifactdatabase.FactoryBootstrap; tim@52: import de.intevation.artifacts.Artifact; tim@52: import de.intevation.artifacts.ArtifactFactory; tim@52: import de.intevation.gnv.artifacts.context.GNVArtifactContext; tim@52: import de.intevation.gnv.artifacts.context.GNVArtifactContextFactory; tim@52: import junit.framework.TestCase; tim@52: tim@52: /** tim@52: * @author Tim Englich tim@52: * tim@52: */ tim@52: public class GNVArtifactsTestCase extends TestCase { tim@52: tim@52: /** tim@52: * the logger, used to log exceptions and additonaly information tim@52: */ tim@52: private static Logger log = null; tim@52: tim@52: static { tim@52: BasicConfigurator.configure(); tim@52: log = Logger.getLogger(GNVArtifactContext.class); tim@52: } tim@52: tim@52: private String configurationFile = "src/test/ressources/GNVArtifactsTestCase_Configuration.xml"; tim@52: tim@52: private FactoryBootstrap bootstrap = null; tim@52: /** tim@52: * Constructor tim@52: * @param name tim@52: */ tim@52: public GNVArtifactsTestCase(String name) { tim@52: super(name); tim@52: } tim@52: tim@52: /** tim@52: * @see junit.framework.TestCase#setUp() tim@52: */ tim@52: protected void setUp() throws Exception { tim@52: log.debug("GNVArtifactsTestCase.setUp"); tim@52: super.setUp(); tim@52: log.info(Config.CONFIG_PROPERTY + " ==> "+configurationFile); tim@52: System.setProperty(Config.CONFIG_PROPERTY, configurationFile); tim@52: log.info("Bootstrap wird initialisiert."); tim@52: bootstrap = new FactoryBootstrap(); tim@52: bootstrap.boot(); tim@52: } tim@52: tim@52: public void testTimeSeriesArtifact(){ tim@52: log.debug("GNVArtifactsTestCase.testTimeSeriesArtifact"); tim@52: String artefactName = "timeSeries"; tim@52: ArtifactFactory artifactFactory = this.getArtifactFactory(artefactName); tim@52: assertNotNull(artifactFactory); tim@52: log.debug("TimeSeries-ArtifactFactory is available"); tim@52: Artifact artifact = artifactFactory.createArtifact(""+System.currentTimeMillis(),bootstrap.getContext()); tim@52: assertNotNull(artifact); tim@52: log.debug("TimeSeries-Artifact is available"); tim@52: tim@60: // Erster Schritt tim@54: Document outputData = artifact.describe(bootstrap.getContext()); tim@54: this.writeDocument2Log(outputData); tim@58: artifact.feed(this.readDocument("src/test/ressources/timeseries/timeseries_step_01_feed.xml"), bootstrap.getContext()); tim@58: outputData = artifact.advance(this.readDocument("src/test/ressources/timeseries/timeseries_step_01_advance.xml"), bootstrap.getContext()); tim@58: this.writeDocument2Log(outputData); tim@60: tim@60: // Zweiter Schritt tim@54: outputData = artifact.describe(bootstrap.getContext()); tim@58: this.writeDocument2Log(outputData); tim@60: artifact.feed(this.readDocument("src/test/ressources/timeseries/timeseries_step_02_feed.xml"), bootstrap.getContext()); tim@60: outputData = artifact.advance(this.readDocument("src/test/ressources/timeseries/timeseries_step_02_advance.xml"), bootstrap.getContext()); tim@60: this.writeDocument2Log(outputData); tim@60: tim@60: // Dritter Schritt tim@60: outputData = artifact.describe(bootstrap.getContext()); tim@60: this.writeDocument2Log(outputData); tim@60: artifact.feed(this.readDocument("src/test/ressources/timeseries/timeseries_step_03_feed.xml"), bootstrap.getContext()); tim@60: outputData = artifact.advance(this.readDocument("src/test/ressources/timeseries/timeseries_step_03_advance.xml"), bootstrap.getContext()); tim@60: this.writeDocument2Log(outputData); tim@52: tim@52: tim@52: } tim@52: private Document getDocument(){ tim@52: return this.createOutputDocument(); tim@52: } tim@52: tim@52: private Document createOutputDocument(){ tim@52: try { tim@52: return DocumentBuilderFactory tim@52: .newInstance() tim@52: .newDocumentBuilder() tim@52: .newDocument(); tim@52: } tim@52: catch (ParserConfigurationException e) { tim@52: log.error(e,e); tim@52: } tim@52: return null; tim@52: } tim@52: tim@52: /** tim@52: * @param artefactName tim@52: */ tim@52: private ArtifactFactory getArtifactFactory(String artefactName){ tim@52: log.debug("GNVArtifactsTestCase.getArtifactFactory"); tim@52: ArtifactFactory[] artifactFactories = bootstrap.getArtifactFactories(); tim@52: for (int i = 0; i < artifactFactories.length; i++){ tim@52: if (artifactFactories[i].getName().equals(artefactName)){ tim@52: log.debug("ArtifactFactory wurde gefunden."); tim@52: return artifactFactories[i]; tim@52: } tim@52: } tim@52: return null; tim@52: } tim@54: tim@54: protected void writeDocument2Log(Document document){ tim@54: try { tim@54: TransformerFactory transformerFactory = TransformerFactory.newInstance(); tim@54: Transformer transformer = transformerFactory.newTransformer(); tim@54: DOMSource source = new DOMSource(document); tim@54: StringWriter sw = new StringWriter(); tim@54: StreamResult result = new StreamResult(sw); tim@54: transformer.transform(source, result); tim@54: log.debug(sw.getBuffer().toString()); tim@54: } catch (TransformerConfigurationException e) { tim@54: log.error(e,e); tim@54: } catch (TransformerFactoryConfigurationError e) { tim@54: log.error(e,e); tim@54: } catch (TransformerException e) { tim@54: log.error(e,e); tim@54: } tim@54: } tim@54: tim@58: tim@58: protected Document readDocument(String fileName){ tim@58: Document returnValue = null; tim@58: try { tim@58: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); tim@58: DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); tim@58: returnValue = docBuilder.parse (new File(fileName)); tim@58: } catch (ParserConfigurationException e) { tim@58: log.error(e,e); tim@58: } catch (SAXException e) { tim@58: log.error(e,e); tim@58: } catch (IOException e) { tim@58: log.error(e,e); tim@58: } tim@58: return returnValue; tim@58: } tim@58: tim@52: tim@52: }