comparison gnv-artifacts/src/test/java/de/intevation/gnv/artifacts/GNVArtifactsTestCaseBase.java @ 875:5e9efdda6894

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

http://dive4elements.wald.intevation.org