001: package org.ontoware.rdf2go;
002:
003: import static org.junit.Assert.fail;
004:
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.util.Properties;
008:
009: import junit.framework.Assert;
010:
011: import org.junit.Test;
012: import org.ontoware.rdf2go.exception.ModelRuntimeException;
013: import org.ontoware.rdf2go.impl.AbstractModelFactory;
014: import org.ontoware.rdf2go.model.Model;
015: import org.ontoware.rdf2go.model.ModelSet;
016: import org.ontoware.rdf2go.model.node.URI;
017: import org.ontoware.rdf2go.testdata.TestData;
018:
019: /**
020: * make some basic functionality tests with the model implementation
021: *
022: * assure the implementation is found by the factory,
023: * open and close the model, insert some elements, etc.
024: */
025: public class GenericTest {
026:
027: @Test
028: public void testFindImplementation() throws ModelRuntimeException,
029: IOException {
030:
031: //test basic model functionalities
032: //grab the model factory
033: //let it create a model
034: //check if the model is closed by default
035: //and truly open if it was opened
036: ModelFactory modelFactory = RDF2Go.getModelFactory();
037: Assert.assertNotNull(modelFactory);
038: Model model = modelFactory.createModel();
039: Assert.assertNotNull(model);
040: Assert.assertFalse(model.isOpen());
041: model.open();
042: Assert.assertTrue(model.isOpen());
043:
044: // use the model a bit
045: //load foaf test data
046: //make sure the file is loaded
047: //assure the model form above has no entries
048: //put the foaf file into the model and make sure the model now
049: //has elements
050: //finally close the model and find out if it truly is closed
051: InputStream foafStream = TestData.getFoafAsStream();
052: Assert.assertNotNull(foafStream);
053: Assert.assertEquals(0, model.size());
054: model.readFrom(foafStream);
055: Assert.assertTrue(model.size() > 0);
056: model.close();
057: Assert.assertFalse(model.isOpen());
058: }
059:
060: //the classes return null on purpose
061: //they are only mock implementations for testing purposes
062:
063: private static class RDF2GoTestAdapter extends AbstractModelFactory {
064:
065: public Model createModel(Properties p)
066: throws ModelRuntimeException {
067: return null;
068: }
069:
070: public ModelSet createModelSet(Properties p)
071: throws ModelRuntimeException {
072: return null;
073: }
074:
075: public Model createModel(URI contextURI)
076: throws ModelRuntimeException {
077: return null;
078: }
079: }
080:
081: private static class RDF2GoTestAdapterTwo extends
082: AbstractModelFactory {
083:
084: public Model createModel(Properties p)
085: throws ModelRuntimeException {
086: return null;
087: }
088:
089: public ModelSet createModelSet(Properties p)
090: throws ModelRuntimeException {
091: return null;
092: }
093:
094: public Model createModel(URI contextURI)
095: throws ModelRuntimeException {
096: return null;
097: }
098: }
099:
100: /**
101: * Testing the RDF2Go default class
102: *
103: * assures that the registration of different implementations of the
104: * rdf2go framework are made correctly
105: *
106: * create two inner classes that implement the missing parts
107: * form the abstract factory
108: *
109: * register those implementation with the RDF2Go framework
110: *
111: * @author sauermann
112: */
113: @Test
114: public void testRDF2GoFactory() {
115: // this is protected by design
116: RDF2Go.modelFactory = null;
117:
118: // register the same twice is allowed
119: ModelFactory adapterOne = new RDF2GoTestAdapter();
120: RDF2Go.register(adapterOne);
121: RDF2Go.register(adapterOne);
122:
123: // twice is only allowed with the same class but not with a different
124: // one
125: adapterOne = new RDF2GoTestAdapter();
126: RDF2Go.register(adapterOne);
127:
128: ModelFactory adapterTwo = new RDF2GoTestAdapterTwo();
129:
130: try {
131: RDF2Go.register(adapterTwo);
132: fail("cannot register two different RDF2Go classes");
133: } catch (RuntimeException e) {
134: // ok
135: } finally {
136: RDF2Go.modelFactory = null;
137: }
138:
139: }
140:
141: /**
142: * call this test from other code to check if the resource can be found from
143: * within a packaged jar
144: */
145: @Test
146: public void testTestData() {
147: InputStream in = TestData.getFoafAsStream();
148: Assert.assertNotNull(in);
149: try {
150: Assert.assertTrue(in.read() != -1);
151: } catch (IOException e) {
152: Assert.fail();
153: }
154: }
155:
156: }
|