001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved. [See end of file]
004: $Id: XMLOutputTestBase.java,v 1.6 2008/01/02 12:06:48 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.xmloutput.test;
007:
008: import java.io.*;
009: import java.util.Properties;
010: import java.util.regex.Pattern;
011:
012: import com.hp.hpl.jena.rdf.model.Model;
013: import com.hp.hpl.jena.rdf.model.RDFWriter;
014: import com.hp.hpl.jena.rdf.model.test.ModelTestBase;
015: import com.hp.hpl.jena.xmloutput.impl.BaseXMLWriter;
016: import com.hp.hpl.jena.xmloutput.impl.SimpleLogger;
017:
018: public class XMLOutputTestBase extends ModelTestBase {
019: protected final String lang;
020:
021: public XMLOutputTestBase(String name, String lang) {
022: super (name);
023: this .lang = lang;
024: }
025:
026: static SimpleLogger realLogger;
027:
028: static boolean sawErrors;
029:
030: static SimpleLogger falseLogger = new SimpleLogger() {
031: public void warn(String s) {
032: sawErrors = true;
033: }
034:
035: public void warn(String s, Exception e) {
036: sawErrors = true;
037: }
038: };
039:
040: static void blockLogger() {
041: realLogger = BaseXMLWriter.setLogger(falseLogger);
042: sawErrors = false;
043: }
044:
045: static boolean unblockLogger() {
046: BaseXMLWriter.setLogger(realLogger);
047: return sawErrors;
048: }
049:
050: static protected class Change {
051: public void modify(RDFWriter w) {
052: }
053:
054: public void modify(Model m) {
055: }
056:
057: public void modify(Model m, RDFWriter w) {
058: modify(m);
059: modify(w);
060: }
061:
062: public static Change none() {
063: return new Change();
064: }
065:
066: public static Change setProperty(final String property,
067: final String value) {
068: return new Change() {
069: public void modify(RDFWriter writer) {
070: writer.setProperty(property, value);
071: }
072: };
073: }
074:
075: public static Change setProperty(final String property,
076: final boolean value) {
077: return new Change() {
078: public void modify(RDFWriter writer) {
079: writer
080: .setProperty(property, Boolean
081: .valueOf(value));
082: }
083: };
084: }
085:
086: public static Change setPrefix(final String prefix,
087: final String URI) {
088: return new Change() {
089: public void modify(Model m) {
090: m.setNsPrefix(prefix, URI);
091: }
092: };
093: }
094:
095: public static Change blockRules(String ruleName) {
096: return setProperty("blockrules", ruleName);
097: }
098:
099: public Change andSetPrefix(String prefix, String URI) {
100: return and(Change.setPrefix(prefix, URI));
101: }
102:
103: private Change and(final Change change) {
104: return new Change() {
105: public void modify(Model m, RDFWriter w) {
106: Change.this .modify(m, w);
107: change.modify(m, w);
108: }
109: };
110: }
111: }
112:
113: /**
114: * @param code Stuff to do to the writer.
115: * @param filename Read this file, write it out, read it in.
116: * @param regex Written file must match this.
117: */
118: protected void check(String filename, String regex, Change code)
119: throws IOException {
120: check(filename, regex, null, code);
121: }
122:
123: protected void check(String filename, String regexPresent,
124: String regexAbsent, Change code) throws IOException {
125: check(filename, null, regexPresent, regexAbsent, false, code);
126: }
127:
128: protected void check(String filename, String encoding,
129: String regexPresent, String regexAbsent, Change code)
130: throws IOException {
131: check(filename, encoding, regexPresent, regexAbsent, false,
132: code);
133: }
134:
135: protected void check(String filename, String regexAbsent,
136: Change code, String base) throws IOException {
137: check(filename, null, regexAbsent, null, false, Change.none(),
138: base);
139: check(filename, null, null, regexAbsent, false, code, base);
140: }
141:
142: protected void check(String filename, String encoding,
143: String regexPresent, String regexAbsent, boolean errs,
144: Change code) throws IOException {
145: check(filename, encoding, regexPresent, regexAbsent, errs,
146: code, "file:" + filename);
147: }
148:
149: protected void check(String filename, String encoding,
150: String regexPresent, String regexAbsent,
151: boolean errorExpected, Change code, String base)
152: throws IOException {
153: //TestLogger tl = new TestLogger(BaseXMLWriter.class);
154: blockLogger();
155: boolean errorsFound;
156: Model m = createMemModel();
157: InputStream in = new FileInputStream(filename);
158: m.read(in, base);
159: in.close();
160: //m.read(filename);
161: Writer sw;
162: ByteArrayOutputStream bos = null;
163: if (encoding == null)
164: sw = new StringWriter();
165: else {
166: bos = new ByteArrayOutputStream();
167: sw = new OutputStreamWriter(bos, encoding);
168: }
169: Properties p = (Properties) System.getProperties().clone();
170: RDFWriter writer = m.getWriter(lang);
171: code.modify(m, writer);
172: writer.write(m, sw, base);
173: sw.close();
174:
175: String contents;
176: if (encoding == null)
177: contents = sw.toString();
178: else {
179: contents = bos.toString(encoding);
180: }
181: try {
182: Model m2 = createMemModel();
183: m2.read(new StringReader(contents), base);
184: assertTrue("Data got changed.", m.isIsomorphicWith(m2));
185: if (regexPresent != null)
186: assertTrue("Should find /" + regexPresent + "/",
187: Pattern.compile(regexPresent, Pattern.DOTALL)
188: .matcher(contents).find()
189: // matcher.contains(contents, awk.compile(regexPresent))
190: );
191: if (regexAbsent != null)
192: assertTrue("Should not find /" + regexAbsent + "/",
193: !Pattern.compile(regexAbsent, Pattern.DOTALL)
194: .matcher(contents).find()
195: // !matcher.contains(contents, awk.compile(regexAbsent))
196: );
197: contents = null;
198: } finally {
199: errorsFound = unblockLogger();
200: System.setProperties(p);
201: if (contents != null) {
202: System.err.println("===================");
203: System.err.println("Offending content - " + toString());
204: System.err.println("===================");
205: System.err.println(contents);
206: System.err.println("===================");
207: }
208: }
209: assertEquals("Errors (not) detected.", errorExpected,
210: errorsFound);
211:
212: }
213:
214: }
215:
216: /*
217: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
218: * All rights reserved.
219: *
220: * Redistribution and use in source and binary forms, with or without
221: * modification, are permitted provided that the following conditions
222: * are met:
223: * 1. Redistributions of source code must retain the above copyright
224: * notice, this list of conditions and the following disclaimer.
225: * 2. Redistributions in binary form must reproduce the above copyright
226: * notice, this list of conditions and the following disclaimer in the
227: * documentation and/or other materials provided with the distribution.
228: * 3. The name of the author may not be used to endorse or promote products
229: * derived from this software without specific prior written permission.
230:
231: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
232: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
233: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
234: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
235: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
236: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
238: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
239: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
240: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241: *
242: */
|