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