001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: Test_rdfcat.java,v 1.4 2008/01/02 12:08:49 andy_seaborne Exp $
005: */
006:
007: package jena.test;
008:
009: import java.io.*;
010: import java.util.*;
011:
012: import jena.rdfcat;
013:
014: import com.hp.hpl.jena.rdf.model.Model;
015: import com.hp.hpl.jena.rdf.model.ModelFactory;
016:
017: import junit.framework.TestCase;
018:
019: public class Test_rdfcat extends TestCase {
020: public void testAbbreviationTable() {
021: assertEquals("RDF/XML", jena.rdfcat.unabbreviate.get("x"));
022: assertEquals("RDF/XML", jena.rdfcat.unabbreviate.get("rdf"));
023: assertEquals("RDF/XML", jena.rdfcat.unabbreviate.get("rdfxml"));
024: assertEquals("RDF/XML", jena.rdfcat.unabbreviate.get("xml"));
025: assertEquals("N3", jena.rdfcat.unabbreviate.get("n3"));
026: assertEquals("N3", jena.rdfcat.unabbreviate.get("n"));
027: assertEquals("N3", jena.rdfcat.unabbreviate.get("ttl"));
028: assertEquals("N-TRIPLE", jena.rdfcat.unabbreviate
029: .get("ntriples"));
030: assertEquals("N-TRIPLE", jena.rdfcat.unabbreviate
031: .get("ntriple"));
032: assertEquals("N-TRIPLE", jena.rdfcat.unabbreviate.get("t"));
033: assertEquals("RDF/XML-ABBREV", jena.rdfcat.unabbreviate
034: .get("owl"));
035: assertEquals("RDF/XML-ABBREV", jena.rdfcat.unabbreviate
036: .get("abbrev"));
037: }
038:
039: public void testExistingLanguage() {
040: assertEquals("RDF/XML", jena.rdfcat.getCheckedLanguage("x"));
041: assertEquals("RDF/XML", jena.rdfcat.getCheckedLanguage("xml"));
042: assertEquals("RDF/XML-ABBREV", jena.rdfcat
043: .getCheckedLanguage("owl"));
044: assertEquals("N3", jena.rdfcat.getCheckedLanguage("N3"));
045: assertEquals("N-TRIPLE", jena.rdfcat
046: .getCheckedLanguage("N-TRIPLE"));
047: }
048:
049: public void testNonexistantLanguage() {
050: try {
051: jena.rdfcat
052: .getCheckedLanguage("noSuchLanguageAsThisOneFruitcake");
053: fail("should trap non-existant language");
054: } catch (IllegalArgumentException e) {
055: assertTrue("message should mention bad language", e
056: .getMessage().indexOf("Fruitcake") > 0);
057: }
058: }
059:
060: /**
061: * Test the identity transform - RDF/XML to RDF/XML
062: */
063: public void testRdfcatIdentity() {
064: Model source = ModelFactory.createDefaultModel();
065: source.read("file:testing/cmd/rdfcat.xml", "RDF/XML");
066:
067: OutputStream so = new ByteArrayOutputStream();
068:
069: rdfcatFixture rc = new rdfcatFixture(so);
070: rc.testGo(new String[] { "file:testing/cmd/rdfcat.xml" });
071:
072: Model output = asModel(so, "RDF/XML");
073: assertTrue(output.isIsomorphicWith(source));
074: }
075:
076: /**
077: * Test the basic concatenation
078: */
079: public void testRdfcatConcat() {
080: Model source = ModelFactory.createDefaultModel();
081: source.read("file:testing/cmd/rdfcat.xml", "RDF/XML");
082:
083: OutputStream so = new ByteArrayOutputStream();
084:
085: rdfcatFixture rc = new rdfcatFixture(so);
086: rc.testGo(new String[] { "file:testing/cmd/rdfcat_1.xml",
087: "file:testing/cmd/rdfcat_2.xml" });
088:
089: Model output = asModel(so, "RDF/XML");
090: assertTrue(output.isIsomorphicWith(source));
091: }
092:
093: /**
094: * Change the default input langauge
095: */
096: public void testRdfcatConcat1() {
097: Model source = ModelFactory.createDefaultModel();
098: source.read("file:testing/cmd/rdfcat.xml", "RDF/XML");
099:
100: OutputStream so = new ByteArrayOutputStream();
101:
102: rdfcatFixture rc = new rdfcatFixture(so);
103: rc.testGo(new String[] { "-in", "N3",
104: "file:testing/cmd/rdfcat_1_n3",
105: "file:testing/cmd/rdfcat_2_n3" });
106:
107: Model output = asModel(so, "RDF/XML");
108: assertTrue(output.isIsomorphicWith(source));
109: }
110:
111: public void testRdfcatN3ToRDFXML_0() {
112: doTestRdfcatOutput("-n", "file:testing/cmd/rdfcat.n3",
113: "RDF/XML", "RDF/XML");
114: }
115:
116: public void testRdfcatN3ToRDFXML_1() {
117: doTestRdfcatOutput("-n3", "file:testing/cmd/rdfcat.n3",
118: "RDF/XML", "RDF/XML");
119: }
120:
121: public void testRdfcatN3ToRDFXML_2() {
122: doTestRdfcatOutput("-ttl", "file:testing/cmd/rdfcat.n3",
123: "RDF/XML", "RDF/XML");
124: }
125:
126: public void testRdfcatN3ToRDFXML_3() {
127: doTestRdfcatOutput("-N3", "file:testing/cmd/rdfcat.n3",
128: "RDF/XML", "RDF/XML");
129: }
130:
131: public void testRdfcatN3ToNtriple() {
132: doTestRdfcatOutput("-n", "file:testing/cmd/rdfcat.n3",
133: "N-TRIPLE", "N-TRIPLE");
134: }
135:
136: public void testRdfcatN3ToN3() {
137: doTestRdfcatOutput("-n", "file:testing/cmd/rdfcat.n3", "N3",
138: "N3");
139: }
140:
141: public void testRdfcatN3ToRDFXMLDefault() {
142: doTestRdfcatOutput(null, "file:testing/cmd/rdfcat.n3",
143: "RDF/XML", "RDF/XML");
144: }
145:
146: public void testRdfcatRDFXMLToRDFXML_0() {
147: doTestRdfcatOutput("-x", "file:testing/cmd/rdfcat.xml",
148: "RDF/XML", "RDF/XML");
149: }
150:
151: public void testRdfcatRDFXMLToRDFXML_1() {
152: doTestRdfcatOutput("-xml", "file:testing/cmd/rdfcat.xml",
153: "RDF/XML", "RDF/XML");
154: }
155:
156: public void testRdfcatRDFXMLToRDFXML_2() {
157: doTestRdfcatOutput("-rdfxml", "file:testing/cmd/rdfcat.xml",
158: "RDF/XML", "RDF/XML");
159: }
160:
161: public void testRdfcatRDFXMLToRDFXML_3() {
162: doTestRdfcatOutput("-rdf", "file:testing/cmd/rdfcat.xml",
163: "RDF/XML", "RDF/XML");
164: }
165:
166: public void testRdfcatRDFXMLToNtriple() {
167: doTestRdfcatOutput("-x", "file:testing/cmd/rdfcat.xml",
168: "N-TRIPLE", "N-TRIPLE");
169: }
170:
171: public void testRdfcatRDFXMLToN3() {
172: doTestRdfcatOutput("-x", "file:testing/cmd/rdfcat.xml", "N3",
173: "N3");
174: }
175:
176: public void testRdfcatRDFXMLToRDFXMLDefault() {
177: doTestRdfcatOutput(null, "file:testing/cmd/rdfcat.xml",
178: "RDF/XML", "RDF/XML");
179: }
180:
181: public void testRdfcatNtripleToRDFXML_0() {
182: doTestRdfcatOutput("-t", "file:testing/cmd/rdfcat.nt",
183: "RDF/XML", "RDF/XML");
184: }
185:
186: public void testRdfcatNtripleToRDFXML_1() {
187: doTestRdfcatOutput("-ntriple", "file:testing/cmd/rdfcat.nt",
188: "RDF/XML", "RDF/XML");
189: }
190:
191: public void testRdfcatNtripleToRDFXML_2() {
192: doTestRdfcatOutput("-ntriples", "file:testing/cmd/rdfcat.nt",
193: "RDF/XML", "RDF/XML");
194: }
195:
196: public void testRdfcatNtripleToRDFXML_3() {
197: doTestRdfcatOutput("-n-triple", "file:testing/cmd/rdfcat.nt",
198: "RDF/XML", "RDF/XML");
199: }
200:
201: public void testRdfcatNtripleToNtriple() {
202: doTestRdfcatOutput("-t", "file:testing/cmd/rdfcat.nt",
203: "N-TRIPLE", "N-TRIPLE");
204: }
205:
206: public void testRdfcatNtripleToN3() {
207: doTestRdfcatOutput("-t", "file:testing/cmd/rdfcat.nt", "N3",
208: "N3");
209: }
210:
211: public void testRdfcatNtripleToRDFXMLDefault() {
212: doTestRdfcatOutput(null, "file:testing/cmd/rdfcat.nt",
213: "RDF/XML", "RDF/XML");
214: }
215:
216: /**
217: * Utility to do a basic cat operation
218: */
219: public void doTestRdfcatOutput(String inFormArg, String inputArg,
220: String outFormArg, String parseAs) {
221: Model source = ModelFactory.createDefaultModel();
222: source.read("file:testing/cmd/rdfcat.xml");
223:
224: OutputStream so = new ByteArrayOutputStream();
225:
226: rdfcatFixture rc = new rdfcatFixture(so);
227:
228: List l = new ArrayList();
229: if (outFormArg != null) {
230: l.add("-out");
231: l.add(outFormArg);
232: }
233: if (inFormArg != null)
234: l.add(inFormArg);
235: l.add(inputArg);
236:
237: String[] args = new String[l.size()];
238: for (int i = 0; i < l.size(); i++) {
239: args[i] = (String) l.get(i);
240: }
241: // use file extension guessing
242: rc.testGo(args);
243:
244: Model output = asModel(so, parseAs);
245: assertTrue(output.isIsomorphicWith(source));
246: }
247:
248: /** Convert an output stream holding a model content to a model */
249: protected Model asModel(OutputStream so, String syntax) {
250: String out = so.toString();
251: Model output = ModelFactory.createDefaultModel();
252: output.read(new StringReader(out), "http://example.com/foo",
253: syntax);
254: return output;
255: }
256:
257: /**
258: * A minimal extension to rdfcat to provide a test fixture
259: */
260: protected class rdfcatFixture extends rdfcat {
261: private OutputStream m_so;
262:
263: protected rdfcatFixture(OutputStream so) {
264: m_so = so;
265: }
266:
267: protected OutputStream getOutputStream() {
268: return m_so;
269: }
270:
271: protected void testGo(String[] args) {
272: go(args);
273: }
274: }
275: }
276:
277: /*
278: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
279: * All rights reserved.
280: *
281: * Redistribution and use in source and binary forms, with or without
282: * modification, are permitted provided that the following conditions
283: * are met:
284: * 1. Redistributions of source code must retain the above copyright
285: * notice, this list of conditions and the following disclaimer.
286: * 2. Redistributions in binary form must reproduce the above copyright
287: * notice, this list of conditions and the following disclaimer in the
288: * documentation and/or other materials provided with the distribution.
289: * 3. The name of the author may not be used to endorse or promote products
290: * derived from this software without specific prior written permission.
291: *
292: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
293: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
294: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
295: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
296: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
297: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
298: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
299: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
300: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
301: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
302: */
|