001: /*
002: * LICENSE INFORMATION
003: * Copyright 2005-2007 by FZI (http://www.fzi.de).
004: * Licensed under a BSD license (http://www.opensource.org/licenses/bsd-license.php)
005: * <OWNER> = Max Völkel
006: * <ORGANIZATION> = FZI Forschungszentrum Informatik Karlsruhe, Karlsruhe, Germany
007: * <YEAR> = 2007
008: *
009: * Project information at http://semweb4j.org/rdf2go
010: */
011: package org.ontoware.rdf2go.testdata;
012:
013: import java.io.IOException;
014: import java.io.InputStream;
015:
016: import org.ontoware.rdf2go.ModelFactory;
017: import org.ontoware.rdf2go.exception.ModelRuntimeException;
018: import org.ontoware.rdf2go.model.Model;
019:
020: /**
021: * Class that loads test-data models for JUnit tests.
022: *
023: * @author sauermann
024: */
025: public class TestData {
026:
027: /**
028: * triples in foaf
029: */
030: public static final long FOAFSIZE = 536;
031:
032: /**
033: * triples in ical
034: */
035: public static final long ICALSIZE = 1317;
036:
037: private static Model foafBuffered = null;
038:
039: private static Model icalBuffered = null;
040:
041: /**
042: * loads foaf once, and then returns the model again and again. Use this if
043: * you need the same model over and over in JUnit tests.
044: *
045: * @param factory
046: * the factory to use to create the model
047: * @return the loaded model
048: */
049: public static Model loadFoafBuffered(ModelFactory factory)
050: throws IOException, ModelRuntimeException {
051: if (factory == null)
052: throw new IllegalArgumentException(
053: "Factory may not be null");
054: if (foafBuffered == null) {
055: foafBuffered = loadFoaf(factory);
056: }
057: return foafBuffered;
058: }
059:
060: /**
061: * load the foaf vocabulary from resource-stream.
062: *
063: * The consumer must close the model!
064: *
065: * @param factory
066: * factory to create model
067: * @return a loaded model
068: * @throws ModelRuntimeException
069: * @throws IOException
070: */
071: public static Model loadFoaf(ModelFactory factory)
072: throws IOException, ModelRuntimeException {
073: Model result = factory.createModel();
074: result.open();
075: InputStream in = getFoafAsStream();
076: try {
077: result.readFrom(in);
078: } finally {
079: in.close();
080: }
081: return result;
082: }
083:
084: /**
085: *
086: * @return as RDF/XML
087: */
088: public static InputStream getFoafAsStream() {
089:
090: ClassLoader cl = Thread.currentThread().getContextClassLoader();
091: InputStream in = cl
092: .getResourceAsStream("org/ontoware/rdf2go/testdata/foaf.xml");
093: return in;
094: }
095:
096: /**
097: * loads ICAL once, and then returns the model again and again. Use this if
098: * you need the same model over and over in JUnit tests.
099: *
100: * @param factory
101: * the factory to use to create the model
102: * @return the loaded model
103: */
104: public static Model loadICALBuffered(ModelFactory factory)
105: throws IOException, ModelRuntimeException {
106: if (icalBuffered == null) {
107: icalBuffered = loadICAL(factory);
108: }
109: return icalBuffered;
110: }
111:
112: /**
113: * load the icaltzd vocabulary from resource-stream
114: *
115: * @param factory
116: * factory to create model
117: * @return a loaded model
118: * @throws ModelRuntimeException
119: * @throws IOException
120: */
121: public static Model loadICAL(ModelFactory factory)
122: throws IOException, ModelRuntimeException {
123: Model result = factory.createModel();
124: result.open();
125: InputStream in = getICALAsStream();
126: try {
127: result.readFrom(in);
128: } finally {
129: in.close();
130: }
131: return result;
132: }
133:
134: /**
135: *
136: * @return icaltzd.rdfs as an InputStream
137: */
138: public static InputStream getICALAsStream() {
139: ClassLoader cl = Thread.currentThread().getContextClassLoader();
140: InputStream in = cl
141: .getResourceAsStream("org/ontoware/rdf2go/testdata/icaltzd.rdfs");
142: return in;
143: }
144:
145: /**
146: * A simple self-check for OSGi-debugging
147: *
148: * @param args
149: */
150: public static void main(String[] args) {
151: if (getFoafAsStream() == null)
152: throw new RuntimeException(
153: "Could not load FOAF from internally packaged file");
154:
155: //else
156: System.out.println("FOAF found.");
157: }
158:
159: }
|