001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved.
004: $Id: TestEntityOutput.java,v 1.12 2008/01/02 12:06:48 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.xmloutput.test;
008:
009: import java.io.*;
010:
011: import com.hp.hpl.jena.rdf.model.*;
012: import com.hp.hpl.jena.rdf.model.test.ModelTestBase;
013: import com.hp.hpl.jena.vocabulary.RDF;
014: import com.hp.hpl.jena.xmloutput.impl.BaseXMLWriter;
015:
016: /**
017: Tests for entities being created corresponding to prefixes.
018: @author kers
019: */
020: public class TestEntityOutput extends ModelTestBase {
021: public TestEntityOutput(String name) {
022: super (name);
023: }
024:
025: public void testSettingWriterEntityProperty() {
026: FakeBaseWriter w = new FakeBaseWriter();
027: assertEquals(false, w.getShowDoctypeDeclaration());
028: assertEquals("false", w.setProperty("showDoctypeDeclaration",
029: "true"));
030: assertEquals(true, w.getShowDoctypeDeclaration());
031: assertEquals("true", w.setProperty("showDoctypeDeclaration",
032: "false"));
033: assertEquals(false, w.getShowDoctypeDeclaration());
034: //
035: assertEquals("false", w.setProperty("showDoctypeDeclaration",
036: Boolean.TRUE));
037: assertEquals(true, w.getShowDoctypeDeclaration());
038: assertEquals("true", w.setProperty("showDoctypeDeclaration",
039: Boolean.FALSE));
040: assertEquals(false, w.getShowDoctypeDeclaration());
041: }
042:
043: public void testKnownEntityNames() {
044: BaseXMLWriter w = new FakeBaseWriter();
045: assertEquals(true, w.isPredefinedEntityName("lt"));
046: assertEquals(true, w.isPredefinedEntityName("gt"));
047: assertEquals(true, w.isPredefinedEntityName("amp"));
048: assertEquals(true, w.isPredefinedEntityName("apos"));
049: assertEquals(true, w.isPredefinedEntityName("quot"));
050: //
051: assertEquals(false, w.isPredefinedEntityName("alt"));
052: assertEquals(false, w.isPredefinedEntityName("amper"));
053: assertEquals(false, w.isPredefinedEntityName("tapost"));
054: assertEquals(false, w.isPredefinedEntityName("gte"));
055: //
056: assertEquals(false, w.isPredefinedEntityName("rdf"));
057: assertEquals(false, w.isPredefinedEntityName("smerp"));
058: assertEquals(false, w.isPredefinedEntityName("nl"));
059: assertEquals(false, w.isPredefinedEntityName("acute"));
060: }
061:
062: public void testRDFNamespaceMissing() {
063: Model m = createMemModel();
064: modelAdd(m, "x R fake:uri#bogus");
065: m.setNsPrefix("spoo", "fake:uri#");
066: m.setNsPrefix("eh", "eh:/");
067: String s = checkedModelToString(m);
068: assertMatches("<!DOCTYPE rdf:RDF \\[", s);
069: assertMatches("<!ENTITY spoo 'fake:uri#'>", s);
070: assertMatches("rdf:resource=\"&spoo;bogus\"", s);
071: }
072:
073: public void testUsesEntityForPrefix() {
074: Model m = modelWithStatements("x R fake:uri#bogus");
075: m.setNsPrefix("spoo", "fake:uri#");
076: m.setNsPrefix("eh", "eh:/");
077: String s = checkedModelToString(m);
078: assertMatches("<!DOCTYPE rdf:RDF \\[", s);
079: assertMatches("<!ENTITY spoo 'fake:uri#'>", s);
080: assertMatches("rdf:resource=\"&spoo;bogus\"", s);
081: }
082:
083: public void testCatchesBadEntities() {
084: testCatchesBadEntity("amp");
085: testCatchesBadEntity("lt");
086: testCatchesBadEntity("gt");
087: testCatchesBadEntity("apos");
088: testCatchesBadEntity("quot");
089: }
090:
091: /* Old code produced:
092: <!DOCTYPE rdf:RDF [
093: <!ENTITY dd 'http://www.example.org/a"b#'>
094: <!ENTITY ampersand 'http://www.example.org/a?a&b#'>
095: <!ENTITY espace 'http://www.example.org/a%20space#'>
096: <!ENTITY zz 'http://www.example.org/a'b#'>
097: <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>]>
098: *
099: */
100: /**
101: * See
102: * http://www.w3.org/TR/xml/#NT-EntityValue
103: * " & and % ' are all legal URI chars, but illegal
104: * in entity defn.
105: * @throws IOException
106: */
107: public void testDifficultChars() throws IOException {
108: Model m = createMemModel();
109: m.read("file:testing/abbreviated/entities.rdf");
110: StringWriter w = new StringWriter();
111: RDFWriter wr = m.getWriter();
112: wr.setProperty("showDoctypeDeclaration", "true");
113: wr.write(m, w, "http://example.org/");
114: w.close();
115: // System.err.println(w.toString());
116: Reader r = new StringReader(w.toString());
117: Model m2 = createMemModel();
118: m2.read(r, "http://example.org/");
119: assertIsoModels("showDoctypeDeclaration problem", m, m2);
120: }
121:
122: private void testCatchesBadEntity(String bad) {
123: Model m = modelWithStatements("ampsersand spelt '&'; x R goo:spoo/noo");
124: m.setNsPrefix("rdf", RDF.getURI());
125: m.setNsPrefix(bad, "goo:spoo");
126: m.setNsPrefix("eh", "eh:/");
127: String s = checkedModelToString(m);
128: //assertTrue( s.toString().contains( "<!DOCTYPE rdf:RDF [" ) ); // java5-ism
129: assertTrue(s.toString().indexOf("<!DOCTYPE rdf:RDF [") >= 0);
130: assertMismatches("<!ENTITY " + bad + " ", s);
131: assertMismatches("rdf:resource=\"&" + bad + ";noo\"", s);
132: }
133:
134: private void checkModelFromXML(Model shouldBe, String s) {
135: Model m = createMemModel();
136: m.read(new StringReader(s), null, "RDF/XML");
137: assertIsoModels("model should be read back correctly",
138: shouldBe, m);
139: }
140:
141: private String checkedModelToString(Model m) {
142: String result = modelToString(m);
143: checkModelFromXML(m, result);
144: return result;
145: }
146:
147: private String modelToString(Model m) {
148: StringWriter s = new StringWriter();
149: RDFWriter w = m.getWriter("RDF/XML-ABBREV");
150: w.setProperty("showDoctypeDeclaration", Boolean.TRUE);
151: w.write(m, s, null);
152: return s.toString();
153: }
154:
155: private void assertMatches(String pattern, String x) {
156: if (!x.matches("(?s).*(" + pattern + ").*"))
157: fail("pattern {" + pattern + "} does not match string {"
158: + x + "}");
159: }
160:
161: private void assertMismatches(String pattern, String x) {
162: if (x.matches("(?s).*(" + pattern + ").*"))
163: fail("pattern {" + pattern + "} should not match string {"
164: + x + "}");
165: }
166:
167: private final static class FakeBaseWriter extends BaseXMLWriter {
168: protected void unblockAll() {
169: }
170:
171: protected void blockRule(Resource r) {
172: }
173:
174: protected void writeBody(Model mdl, PrintWriter pw,
175: String baseUri, boolean inclXMLBase) {
176: }
177:
178: protected boolean getShowDoctypeDeclaration() {
179: return showDoctypeDeclaration.booleanValue();
180: }
181: }
182: }
183:
184: /*
185: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
186: * All rights reserved.
187: *
188: * Redistribution and use in source and binary forms, with or without
189: * modification, are permitted provided that the following conditions
190: * are met:
191: * 1. Redistributions of source code must retain the above copyright
192: * notice, this list of conditions and the following disclaimer.
193: * 2. Redistributions in binary form must reproduce the above copyright
194: * notice, this list of conditions and the following disclaimer in the
195: * documentation and/or other materials provided with the distribution.
196: * 3. The name of the author may not be used to endorse or promote products
197: * derived from this software without specific prior written permission.
198: *
199: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
200: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
204: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
205: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
206: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
207: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
208: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
209: */
|