Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/test/java/de/intevation/gnv/artifacts/GNVArtifactsTestCaseBase.java @ 657:af3f56758f59
merged gnv-artifacts/0.5
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:53 +0200 |
parents | e0d7b8a0bc42 |
children | 9a828e5a2390 |
comparison
equal
deleted
inserted
replaced
590:5f5f273c8566 | 657:af3f56758f59 |
---|---|
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 docBuilderFactory.setNamespaceAware(true); | |
92 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); | |
93 returnValue = docBuilder.parse(new File(fileName)); | |
94 } catch (ParserConfigurationException e) { | |
95 log.error(e, e); | |
96 } catch (SAXException e) { | |
97 log.error(e, e); | |
98 } catch (IOException e) { | |
99 log.error(e, e); | |
100 } | |
101 return returnValue; | |
102 } | |
103 | |
104 protected void check4ExceptionReport(Document document) throws Exception { | |
105 document = new ArtifactXMLUtilities().reInitDocument(document); | |
106 String message = Config.getStringXPath(document, | |
107 "/exceptionreport/exception"); | |
108 if (message != null) { | |
109 throw new Exception(message); | |
110 } | |
111 } | |
112 | |
113 /** | |
114 * @return | |
115 */ | |
116 protected CallContext createCallContext(ArtifactFactory artifactFactory) { | |
117 CallMeta callMeta = new DefaultCallMeta( | |
118 new PreferredLocale[] { new DefaultPreferredLocale("de_DE", | |
119 1.0f) }); | |
120 CallContext cc = new TestCallContext(bootstrap.getContext(), callMeta,artifactFactory); | |
121 return cc; | |
122 } | |
123 | |
124 /** | |
125 * @param artifact | |
126 * @param cc | |
127 * @param describeDocument TODO | |
128 * @throws Exception | |
129 */ | |
130 protected void doNextStep(Artifact artifact, CallContext cc, | |
131 String feedDocument, String advanceDocument, Document describeDocument) | |
132 throws Exception { | |
133 Document outputData = artifact.describe(describeDocument,cc); | |
134 // this.writeDocument2Log(outputData); | |
135 outputData = artifact.feed(this.readDocument(feedDocument), cc); | |
136 this.check4ExceptionReport(outputData); | |
137 outputData = artifact.advance(this.readDocument(advanceDocument), cc); | |
138 // this.writeDocument2Log(outputData); | |
139 this.check4ExceptionReport(outputData); | |
140 | |
141 } | |
142 | |
143 protected void createFile(byte[] content, String fileName) { | |
144 try { | |
145 FileOutputStream fos = new FileOutputStream(new File(fileName)); | |
146 ByteArrayInputStream bis = new ByteArrayInputStream(content); | |
147 byte[] buf = new byte[4096]; | |
148 while (bis.read(buf) > 0) { | |
149 fos.write(buf); | |
150 } | |
151 fos.flush(); | |
152 fos.close(); | |
153 } catch (FileNotFoundException e) { | |
154 log.error(e, e); | |
155 } catch (IOException e) { | |
156 log.error(e, e); | |
157 } | |
158 } | |
159 | |
160 /** | |
161 * @param artefactName | |
162 */ | |
163 protected ArtifactFactory getArtifactFactory(String artefactName) { | |
164 log.debug("GNVArtifactsTestCase.getArtifactFactory"); | |
165 ArtifactFactory[] artifactFactories = bootstrap.getArtifactFactories(); | |
166 for (int i = 0; i < artifactFactories.length; i++) { | |
167 if (artifactFactories[i].getName().equals(artefactName)) { | |
168 log.debug("ArtifactFactory wurde gefunden."); | |
169 return artifactFactories[i]; | |
170 } | |
171 } | |
172 return null; | |
173 } | |
174 | |
175 /** | |
176 * @param artifactFactory | |
177 * @return | |
178 */ | |
179 protected Artifact createArtifact(ArtifactFactory artifactFactory) { | |
180 Document setupData = null; | |
181 Artifact artifact = artifactFactory.createArtifact( | |
182 "" + System.currentTimeMillis(), | |
183 bootstrap.getContext(), | |
184 setupData); | |
185 assertNotNull(artifact); | |
186 log.debug("Artifact is available"); | |
187 return artifact; | |
188 } | |
189 | |
190 | |
191 } |