comparison 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
comparison
equal deleted inserted replaced
344:44adf8918155 345:c16c622ba2f3
1 /**
2 *
3 */
4 package de.intevation.gnv.artifacts;
5
6 import java.io.ByteArrayInputStream;
7 import java.io.File;
8 import java.io.FileNotFoundException;
9 import java.io.FileOutputStream;
10 import java.io.IOException;
11
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.ParserConfigurationException;
15
16 import junit.framework.TestCase;
17
18 import org.apache.log4j.BasicConfigurator;
19 import org.apache.log4j.Logger;
20 import org.w3c.dom.Document;
21 import org.xml.sax.SAXException;
22
23 import de.intevation.artifactdatabase.Config;
24 import de.intevation.artifactdatabase.DefaultCallMeta;
25 import de.intevation.artifactdatabase.DefaultPreferredLocale;
26 import de.intevation.artifactdatabase.FactoryBootstrap;
27 import de.intevation.artifacts.Artifact;
28 import de.intevation.artifacts.ArtifactFactory;
29 import de.intevation.artifacts.CallContext;
30 import de.intevation.artifacts.CallMeta;
31 import de.intevation.artifacts.PreferredLocale;
32 import de.intevation.gnv.utils.ArtifactXMLUtilities;
33
34 /**
35 * @author Tim Englich <tim.englich@intevation.de>
36 *
37 */
38 public abstract class GNVArtifactsTestCaseBase extends TestCase {
39
40 /**
41 * the logger, used to log exceptions and additonaly information
42 */
43 private static Logger log = null;
44
45 static {
46 BasicConfigurator.configure();
47 log = Logger.getLogger(GNVArtifactsTestCaseBase.class);
48 }
49
50 private String configurationDir = "doc/conf";
51
52 protected FactoryBootstrap bootstrap = null;
53
54 /**
55 * Constructor
56 */
57 public GNVArtifactsTestCaseBase() {
58 }
59
60 /**
61 * Constructor
62 * @param name
63 */
64 public GNVArtifactsTestCaseBase(String name) {
65 super(name);
66 }
67
68 public abstract void testArtifact();
69 /**
70 * @see junit.framework.TestCase#setUp()
71 */
72 protected void setUp() throws Exception {
73 log.debug("GNVArtifactsTestCase.setUp");
74 super.setUp();
75 log.info(Config.CONFIG_DIR + " ==> " + configurationDir);
76 System.setProperty(Config.CONFIG_DIR, configurationDir);
77 log.info("Bootstrap wird initialisiert.");
78 bootstrap = new FactoryBootstrap();
79 bootstrap.boot();
80 }
81
82 protected void writeDocument2Log(Document document) {
83 log.debug(new ArtifactXMLUtilities().writeDocument2String(document));
84 }
85
86 protected Document readDocument(String fileName) {
87 Document returnValue = null;
88 try {
89 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
90 .newInstance();
91 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
92 returnValue = docBuilder.parse(new File(fileName));
93 } catch (ParserConfigurationException e) {
94 log.error(e, e);
95 } catch (SAXException e) {
96 log.error(e, e);
97 } catch (IOException e) {
98 log.error(e, e);
99 }
100 return returnValue;
101 }
102
103 protected void check4ExceptionReport(Document document) throws Exception {
104 document = new ArtifactXMLUtilities().reInitDocument(document);
105 String message = Config.getStringXPath(document,
106 "/exceptionreport/exception");
107 if (message != null) {
108 throw new Exception(message);
109 }
110 }
111
112 /**
113 * @return
114 */
115 protected CallContext createCallContext() {
116 CallMeta callMeta = new DefaultCallMeta(
117 new PreferredLocale[] { new DefaultPreferredLocale("de_DE",
118 1.0f) });
119 CallContext cc = new TestCallContext(bootstrap.getContext(), callMeta);
120 return cc;
121 }
122
123 /**
124 * @param artifact
125 * @param cc
126 * @param describeDocument TODO
127 * @throws Exception
128 */
129 protected void doNextStep(Artifact artifact, CallContext cc,
130 String feedDocument, String advanceDocument, Document describeDocument)
131 throws Exception {
132 Document outputData = artifact.describe(describeDocument,cc);
133 // this.writeDocument2Log(outputData);
134 outputData = artifact.feed(this.readDocument(feedDocument), cc);
135 this.check4ExceptionReport(outputData);
136 outputData = artifact.advance(this.readDocument(advanceDocument), cc);
137 // this.writeDocument2Log(outputData);
138 this.check4ExceptionReport(outputData);
139
140 }
141
142 protected void createFile(byte[] content, String fileName) {
143 try {
144 FileOutputStream fos = new FileOutputStream(new File(fileName));
145 ByteArrayInputStream bis = new ByteArrayInputStream(content);
146 byte[] buf = new byte[4096];
147 while (bis.read(buf) > 0) {
148 fos.write(buf);
149 }
150 fos.flush();
151 fos.close();
152 } catch (FileNotFoundException e) {
153 log.error(e, e);
154 } catch (IOException e) {
155 log.error(e, e);
156 }
157 }
158
159 /**
160 * @param artefactName
161 */
162 protected ArtifactFactory getArtifactFactory(String artefactName) {
163 log.debug("GNVArtifactsTestCase.getArtifactFactory");
164 ArtifactFactory[] artifactFactories = bootstrap.getArtifactFactories();
165 for (int i = 0; i < artifactFactories.length; i++) {
166 if (artifactFactories[i].getName().equals(artefactName)) {
167 log.debug("ArtifactFactory wurde gefunden.");
168 return artifactFactories[i];
169 }
170 }
171 return null;
172 }
173
174
175
176 }

http://dive4elements.wald.intevation.org