001: /*
002: (c) Copyright 2001, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved.
004: [See end of file]
005: $Id: PrettyWriterTest.java,v 1.16 2008/02/11 11:10:30 jeremy_carroll Exp $
006: */
007:
008: // Package
009: ///////////////
010: package com.hp.hpl.jena.xmloutput.test;
011:
012: // Imports
013: ///////////////
014:
015: import java.io.ByteArrayInputStream;
016: import java.io.IOException;
017: import java.io.OutputStream;
018: import java.io.StringReader;
019: import java.io.StringWriter;
020: import java.util.regex.Pattern;
021:
022: import com.hp.hpl.jena.ontology.OntModel;
023: import com.hp.hpl.jena.ontology.OntModelSpec;
024: import com.hp.hpl.jena.rdf.model.Model;
025: import com.hp.hpl.jena.rdf.model.ModelFactory;
026: import com.hp.hpl.jena.rdf.model.test.ModelTestBase;
027:
028: /**
029: * JUnit regression tests for the Jena DAML model.
030: *
031: * @author Jeremy Carroll
032: * @version CVS info: $Id: PrettyWriterTest.java,v 1.16 2008/02/11 11:10:30 jeremy_carroll Exp $,
033: */
034:
035: public class PrettyWriterTest extends ModelTestBase {
036:
037: /**
038: * Constructor requires that all tests be named
039: *
040: * @param name The name of this test
041: */
042: public PrettyWriterTest(String name) {
043: super (name);
044: }
045:
046: // Test cases
047: /////////////
048: // static AwkCompiler awk = new AwkCompiler();
049: // static AwkMatcher matcher = new AwkMatcher();
050:
051: /**
052: * @param filename Read this file, write it out, read it in.
053: * @param regex Written file must match this.
054: */
055: private void check(String filename, String regex)
056: throws IOException {
057: check(filename, regex, true);
058: }
059:
060: private void checkNoMatch(String filename, String regex)
061: throws IOException {
062: check(filename, regex, false);
063:
064: }
065:
066: private void check(String filename, String regex, boolean match)
067: throws IOException {
068: String contents = null;
069: try {
070: Model m = createMemModel();
071: m.read(filename);
072: StringWriter sw = new StringWriter();
073: m.write(sw, "RDF/XML-ABBREV", filename);
074: sw.close();
075: contents = sw.toString();
076: Model m2 = createMemModel();
077: m2.read(new StringReader(contents), filename);
078: assertTrue(m.isIsomorphicWith(m2));
079:
080: assertTrue("Looking for /" + regex + "/ ",
081: // +contents,
082: match == Pattern.compile(regex, Pattern.DOTALL)
083: .matcher(contents).find()
084: // matcher.contains(contents, awk.compile(regex))
085: );
086: contents = null;
087: } finally {
088: if (contents != null) {
089: System.err.println("Incorrect contents:");
090: System.err.println(contents);
091: }
092: }
093: }
094:
095: public void testConsistency() throws IOException {
096: checkNoMatch("file:testing/abbreviated/consistency.rdf",
097: "rdf:resource");
098: }
099:
100: public void testAnonDamlClass() throws IOException {
101: check("file:testing/abbreviated/daml.rdf",
102: "rdf:parseType=[\"']daml:collection[\"']");
103: }
104:
105: public void testRDFCollection() throws IOException {
106: check("file:testing/abbreviated/collection.rdf",
107: "rdf:parseType=[\"']Collection[\"']");
108: }
109:
110: public void testOWLPrefix() throws IOException {
111: // check(
112: // "file:testing/abbreviated/collection.rdf",
113: // "xmlns:owl=[\"']http://www.w3.org/2002/07/owl#[\"']");
114: }
115:
116: public void testLi() throws IOException {
117: check("file:testing/abbreviated/container.rdf",
118: "<rdf:li.*<rdf:li.*<rdf:li.*<rdf:li");
119: }
120:
121: public void test803804() {
122: String sourceT = "<rdf:RDF "
123: + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"
124: + " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'"
125: + " xmlns:owl=\"http://www.w3.org/2002/07/owl#\">"
126: + " <owl:ObjectProperty rdf:about="
127: + "'http://example.org/foo#p'>"
128: + " </owl:ObjectProperty>" + "</rdf:RDF>";
129:
130: OntModel m = ModelFactory.createOntologyModel(
131: OntModelSpec.OWL_MEM_RULE_INF, null);
132: m.read(new ByteArrayInputStream(sourceT.getBytes()),
133: "http://example.org/foo");
134:
135: Model m0 = ModelFactory.createModelForGraph(m.getGraph());
136: /*
137: Set copyOfm0 = new HashSet();
138: Set blankNodes = new HashSet();
139: Iterator it = m0.listStatements();
140: while (it.hasNext()) {
141: Statement st = (Statement)it.next();
142: copyOfm0.add(st);
143: Resource subj = st.getSubject();
144: if (subj.isAnon())
145: blankNodes.add(subj);
146: }
147:
148: it = blankNodes.iterator();
149: while (it.hasNext()) {
150: Resource b = (Resource)it.next();
151: Statement st = m0.createStatement(b,OWL.sameAs,b);
152: // assertEquals(m0.contains(st),copyOfm0.contains(st));
153: }
154: */
155: TestXMLFeatures.blockLogger();
156: try {
157: m0.write(new OutputStream() {
158: public void write(int b) throws IOException {
159: }
160: }, "RDF/XML-ABBREV");
161:
162: } finally {
163: // This will need to change when the bug is finally fixed.
164:
165: assertTrue(TestXMLFeatures.unblockLogger());
166: }
167: }
168: }
169:
170: /*****************************************************************************
171: * Source code information
172: * -----------------------
173: * Original author Jeremy Carroll, HP Labs Bristol
174: * Author email jjc@hpl.hp.com
175: * Package Jena
176: * Created 10 Nov 2000
177: * Filename $RCSfile: PrettyWriterTest.java,v $
178: * Revision $Revision: 1.16 $
179: *
180: * Last modified on $Date: 2008/02/11 11:10:30 $
181: * by $Author: jeremy_carroll $
182: *
183: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
184: * All rights reserved.
185: *
186: * Redistribution and use in source and binary forms, with or without
187: * modification, are permitted provided that the following conditions
188: * are met:
189: * 1. Redistributions of source code must retain the above copyright
190: * notice, this list of conditions and the following disclaimer.
191: * 2. Redistributions in binary form must reproduce the above copyright
192: * notice, this list of conditions and the following disclaimer in the
193: * documentation and/or other materials provided with the distribution.
194: * 3. The name of the author may not be used to endorse or promote products
195: * derived from this software without specific prior written permission.
196: *
197: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
200: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
203: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
204: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
205: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
206: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
207: *****************************************************************************/
|