001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.rio;
007:
008: import java.io.IOException;
009: import java.io.StringReader;
010: import java.io.StringWriter;
011: import java.util.Collection;
012:
013: import junit.framework.TestCase;
014:
015: import org.openrdf.model.Statement;
016: import org.openrdf.model.URI;
017: import org.openrdf.model.ValueFactory;
018: import org.openrdf.model.impl.ValueFactoryImpl;
019: import org.openrdf.rio.helpers.StatementCollector;
020:
021: /**
022: * @author Arjohn Kampman
023: */
024: public abstract class RDFWriterTest extends TestCase {
025:
026: protected RDFWriterFactory rdfWriterFactory;
027:
028: protected RDFParserFactory rdfParserFactory;
029:
030: protected RDFWriterTest(RDFWriterFactory writerF,
031: RDFParserFactory parserF) {
032: rdfWriterFactory = writerF;
033: rdfParserFactory = parserF;
034: }
035:
036: public void testPrefixRedefinition() throws RDFHandlerException,
037: RDFParseException, IOException {
038: String ns1 = "a:";
039: String ns2 = "b:";
040: String ns3 = "c:";
041:
042: ValueFactory vf = new ValueFactoryImpl();
043: URI uri1 = vf.createURI(ns1, "r1");
044: URI uri2 = vf.createURI(ns2, "r2");
045: URI uri3 = vf.createURI(ns3, "r3");
046: Statement st = vf.createStatement(uri1, uri2, uri3);
047:
048: StringWriter writer = new StringWriter();
049: RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer);
050: rdfWriter.handleNamespace("", ns1);
051: rdfWriter.handleNamespace("", ns2);
052: rdfWriter.handleNamespace("", ns3);
053: rdfWriter.startRDF();
054: rdfWriter.handleStatement(st);
055: rdfWriter.endRDF();
056:
057: StringReader reader = new StringReader(writer.toString());
058: RDFParser rdfParser = rdfParserFactory.getParser();
059: rdfParser.setValueFactory(vf);
060: StatementCollector stCollector = new StatementCollector();
061: rdfParser.setRDFHandler(stCollector);
062:
063: try {
064: rdfParser.parse(reader, "foo:bar");
065: } catch (RDFParseException e) {
066: System.err
067: .println("Failed to parse generated RDF document:");
068: System.err.println(writer.toString());
069: throw e;
070: }
071:
072: Collection<Statement> statements = stCollector.getStatements();
073: assertEquals("Unexpected number of statements", 1, statements
074: .size());
075:
076: Statement parsedSt = statements.iterator().next();
077: assertEquals("Written and parsed statements are not equal", st,
078: parsedSt);
079: }
080:
081: public void testIllegalPrefix() throws RDFHandlerException,
082: RDFParseException, IOException {
083: String ns1 = "a:";
084: String ns2 = "b:";
085: String ns3 = "c:";
086:
087: ValueFactory vf = new ValueFactoryImpl();
088: URI uri1 = vf.createURI(ns1, "r1");
089: URI uri2 = vf.createURI(ns2, "r2");
090: URI uri3 = vf.createURI(ns3, "r3");
091: Statement st = vf.createStatement(uri1, uri2, uri3);
092:
093: StringWriter writer = new StringWriter();
094: RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer);
095: rdfWriter.handleNamespace("1", ns1);
096: rdfWriter.handleNamespace("_", ns2);
097: rdfWriter.handleNamespace("a%", ns3);
098: rdfWriter.startRDF();
099: rdfWriter.handleStatement(st);
100: rdfWriter.endRDF();
101:
102: StringReader reader = new StringReader(writer.toString());
103: RDFParser rdfParser = rdfParserFactory.getParser();
104: rdfParser.setValueFactory(vf);
105: StatementCollector stCollector = new StatementCollector();
106: rdfParser.setRDFHandler(stCollector);
107:
108: try {
109: rdfParser.parse(reader, "foo:bar");
110: } catch (RDFParseException e) {
111: System.err
112: .println("Failed to parse generated RDF document:");
113: System.err.println(writer.toString());
114: throw e;
115: }
116:
117: Collection<Statement> statements = stCollector.getStatements();
118: assertEquals("Unexpected number of statements", 1, statements
119: .size());
120:
121: Statement parsedSt = statements.iterator().next();
122: assertEquals("Written and parsed statements are not equal", st,
123: parsedSt);
124: }
125: }
|