diff gnv-artifacts/src/test/java/de/intevation/gnv/artifacts/GNVArtifactsTestCaseBase.java @ 345:c16c622ba2f3

Split all Unittestcases in separat Classes. Now it is easier to uses the UnitTests with Maven. gnv-artifacts/trunk@412 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 11 Dec 2009 10:02:10 +0000
parents
children 147d1e46b239
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/test/java/de/intevation/gnv/artifacts/GNVArtifactsTestCaseBase.java	Fri Dec 11 10:02:10 2009 +0000
@@ -0,0 +1,176 @@
+/**
+ *
+ */
+package de.intevation.gnv.artifacts;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.BasicConfigurator;
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+import de.intevation.artifactdatabase.Config;
+import de.intevation.artifactdatabase.DefaultCallMeta;
+import de.intevation.artifactdatabase.DefaultPreferredLocale;
+import de.intevation.artifactdatabase.FactoryBootstrap;
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactFactory;
+import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.PreferredLocale;
+import de.intevation.gnv.utils.ArtifactXMLUtilities;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public abstract class GNVArtifactsTestCaseBase extends TestCase {
+
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = null;
+
+    static {
+        BasicConfigurator.configure();
+        log = Logger.getLogger(GNVArtifactsTestCaseBase.class);
+    }
+    
+    private String configurationDir = "doc/conf";
+
+    protected FactoryBootstrap bootstrap = null;
+    
+    /**
+     * Constructor
+     */
+    public GNVArtifactsTestCaseBase() {
+    }
+
+    /**
+     * Constructor
+     * @param name
+     */
+    public GNVArtifactsTestCaseBase(String name) {
+        super(name);
+    }
+    
+    public abstract void testArtifact();
+    /**
+     * @see junit.framework.TestCase#setUp()
+     */
+    protected void setUp() throws Exception {
+        log.debug("GNVArtifactsTestCase.setUp");
+        super.setUp();
+        log.info(Config.CONFIG_DIR + " ==> " + configurationDir);
+        System.setProperty(Config.CONFIG_DIR, configurationDir);
+        log.info("Bootstrap wird initialisiert.");
+        bootstrap = new FactoryBootstrap();
+        bootstrap.boot();
+    }
+    
+    protected void writeDocument2Log(Document document) {
+        log.debug(new ArtifactXMLUtilities().writeDocument2String(document));
+    }
+
+    protected Document readDocument(String fileName) {
+        Document returnValue = null;
+        try {
+            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
+                    .newInstance();
+            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
+            returnValue = docBuilder.parse(new File(fileName));
+        } catch (ParserConfigurationException e) {
+            log.error(e, e);
+        } catch (SAXException e) {
+            log.error(e, e);
+        } catch (IOException e) {
+            log.error(e, e);
+        }
+        return returnValue;
+    }
+
+    protected void check4ExceptionReport(Document document) throws Exception {
+        document = new ArtifactXMLUtilities().reInitDocument(document);
+        String message = Config.getStringXPath(document,
+                "/exceptionreport/exception");
+        if (message != null) {
+            throw new Exception(message);
+        }
+    }
+    
+    /**
+     * @return
+     */
+    protected CallContext createCallContext() {
+        CallMeta callMeta = new DefaultCallMeta(
+                new PreferredLocale[] { new DefaultPreferredLocale("de_DE",
+                        1.0f) });
+        CallContext cc = new TestCallContext(bootstrap.getContext(), callMeta);
+        return cc;
+    }
+
+    /**
+     * @param artifact
+     * @param cc
+     * @param describeDocument TODO
+     * @throws Exception
+     */
+    protected void doNextStep(Artifact artifact, CallContext cc,
+                            String feedDocument, String advanceDocument, Document describeDocument)
+                                                                        throws Exception {
+        Document outputData = artifact.describe(describeDocument,cc);
+//        this.writeDocument2Log(outputData);
+        outputData = artifact.feed(this.readDocument(feedDocument), cc);
+        this.check4ExceptionReport(outputData);
+        outputData = artifact.advance(this.readDocument(advanceDocument), cc);
+//        this.writeDocument2Log(outputData);
+        this.check4ExceptionReport(outputData);
+
+    }
+    
+    protected void createFile(byte[] content, String fileName) {
+        try {
+            FileOutputStream fos = new FileOutputStream(new File(fileName));
+            ByteArrayInputStream bis = new ByteArrayInputStream(content);
+            byte[] buf = new byte[4096];
+            while (bis.read(buf) > 0) {
+                fos.write(buf);
+            }
+            fos.flush();
+            fos.close();
+        } catch (FileNotFoundException e) {
+            log.error(e, e);
+        } catch (IOException e) {
+            log.error(e, e);
+        }
+    }
+
+    /**
+     * @param artefactName
+     */
+    protected ArtifactFactory getArtifactFactory(String artefactName) {
+        log.debug("GNVArtifactsTestCase.getArtifactFactory");
+        ArtifactFactory[] artifactFactories = bootstrap.getArtifactFactories();
+        for (int i = 0; i < artifactFactories.length; i++) {
+            if (artifactFactories[i].getName().equals(artefactName)) {
+                log.debug("ArtifactFactory wurde gefunden.");
+                return artifactFactories[i];
+            }
+        }
+        return null;
+    }
+
+
+
+}

http://dive4elements.wald.intevation.org