001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved.
004: $Id: TestLiteralEncoding.java,v 1.7 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.Model;
012: import com.hp.hpl.jena.rdf.model.ModelFactory;
013: import com.hp.hpl.jena.rdf.model.impl.Util;
014: import com.hp.hpl.jena.rdf.model.test.ModelTestBase;
015: import com.hp.hpl.jena.shared.CannotEncodeCharacterException;
016: import com.hp.hpl.jena.vocabulary.RDF;
017:
018: /**
019: Tests to ensure that certain literals are either encoded properly or reported
020: as exceptions.
021: @author kers
022: */
023: public class TestLiteralEncoding extends ModelTestBase {
024: public TestLiteralEncoding(String name) {
025: super (name);
026: }
027:
028: public void testX() {
029: assertEquals("", Util.substituteEntitiesInElementContent(""));
030: assertEquals("abc", Util
031: .substituteEntitiesInElementContent("abc"));
032: assertEquals("a<b", Util
033: .substituteEntitiesInElementContent("a<b"));
034: assertEquals("a>b", Util
035: .substituteEntitiesInElementContent("a>b"));
036: assertEquals("a&b", Util
037: .substituteEntitiesInElementContent("a&b"));
038: assertEquals("a;b", Util
039: .substituteEntitiesInElementContent("a;b"));
040: assertEquals("a b", Util
041: .substituteEntitiesInElementContent("a b"));
042: assertEquals("a\nb", Util
043: .substituteEntitiesInElementContent("a\nb"));
044: assertEquals("a'b", Util
045: .substituteEntitiesInElementContent("a'b"));
046: //
047: assertEquals("a<b<c", Util
048: .substituteEntitiesInElementContent("a<b<c"));
049: assertEquals("a<b>c", Util
050: .substituteEntitiesInElementContent("a<b>c"));
051: assertEquals("a<b&c", Util
052: .substituteEntitiesInElementContent("a<b&c"));
053: assertEquals("a&b&c", Util
054: .substituteEntitiesInElementContent("a&b&c"));
055: assertEquals("a&b>c", Util
056: .substituteEntitiesInElementContent("a&b>c"));
057: assertEquals("a&b<c", Util
058: .substituteEntitiesInElementContent("a&b<c"));
059: //
060: assertEquals("", Util.substituteStandardEntities(""));
061: assertEquals("<", Util.substituteStandardEntities("<"));
062: assertEquals(">", Util.substituteStandardEntities(">"));
063: assertEquals("&", Util.substituteStandardEntities("&"));
064: assertEquals("'", Util.substituteStandardEntities("\'"));
065: assertEquals(""", Util.substituteStandardEntities("\""));
066: assertEquals("
", Util.substituteStandardEntities("\n"));
067: assertEquals("
", Util.substituteStandardEntities("\r"));
068: assertEquals("	", Util.substituteStandardEntities("\t"));
069: //
070: assertEquals("a<b&c>d", Util
071: .substituteStandardEntities("a<b&c>d"));
072: assertEquals("", Util.substituteStandardEntities(""));
073: assertEquals("", Util.substituteStandardEntities(""));
074: assertEquals("", Util.substituteStandardEntities(""));
075: assertEquals("", Util.substituteStandardEntities(""));
076: assertEquals("", Util.substituteStandardEntities(""));
077: assertEquals("", Util.substituteStandardEntities(""));
078: assertEquals("", Util.substituteStandardEntities(""));
079: assertEquals("", Util.substituteStandardEntities(""));
080: assertEquals("", Util.substituteStandardEntities(""));
081: assertEquals("", Util.substituteStandardEntities(""));
082: }
083:
084: public void testLexicalEncodingException(String lang) {
085: for (char ch = 0; ch < 32; ch += 1)
086: if (ch != '\n' && ch != '\t' && ch != '\r')
087: testThrowsBadCharacterException(ch, lang);
088: testThrowsBadCharacterException((char) 0xFFFF, lang);
089: testThrowsBadCharacterException((char) 0xFFFE, lang);
090:
091: }
092:
093: public void testBasicLexicalEncodingException() {
094: testLexicalEncodingException("RDF/XML");
095: }
096:
097: // TODO: add test for bad char in property attribute.
098: public void testPrettyLexicalEncodingException() {
099: testLexicalEncodingException("RDF/XML-ABBREV");
100: }
101:
102: private void testThrowsBadCharacterException(char badChar,
103: String lang) {
104: String badString = "" + badChar;
105:
106: Model m = ModelFactory.createDefaultModel();
107: m.createResource().addProperty(RDF.value, badString);
108: Writer w = new Writer() {
109: public void close() throws IOException {
110: }
111:
112: public void flush() throws IOException {
113: }
114:
115: public void write(char[] arg0, int arg1, int arg2)
116: throws IOException {
117: }
118: };
119: try {
120: m.write(w, lang);
121: // Util.substituteEntitiesInElementContent( badString );
122: fail("should trap bad character: (char)" + (int) badChar);
123: } catch (CannotEncodeCharacterException e) {
124: assertEquals(badChar, e.getBadChar());
125: assertEquals("XML", e.getEncodingContext());
126: }
127: }
128:
129: public void testNoApparentCData() {
130: Model m = modelWithStatements("a R ']]>'");
131: StringWriter s = new StringWriter();
132: m.write(s, "RDF/XML-ABBREV");
133: Model m2 = modelWithStatements("");
134: m2.read(new StringReader(s.toString()), null, "RDF/XML");
135: assertIsoModels(m, m2);
136: //assertTrue( s.toString().contains( "]]>" ) ); // Java 1.5-ism
137: //assertFalse( s.toString().contains( "]]>" ) );
138: assertTrue(s.toString().indexOf("]]>") >= 0);
139: assertFalse(s.toString().indexOf("]]>") >= 0);
140: }
141: }
142:
143: /*
144: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
145: * All rights reserved.
146: *
147: * Redistribution and use in source and binary forms, with or without
148: * modification, are permitted provided that the following conditions
149: * are met:
150: * 1. Redistributions of source code must retain the above copyright
151: * notice, this list of conditions and the following disclaimer.
152: * 2. Redistributions in binary form must reproduce the above copyright
153: * notice, this list of conditions and the following disclaimer in the
154: * documentation and/or other materials provided with the distribution.
155: * 3. The name of the author may not be used to endorse or promote products
156: * derived from this software without specific prior written permission.
157: *
158: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
159: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
161: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
162: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
163: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
164: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
165: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
166: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
167: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168: */
|