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:
012: package org.ontoware.rdf2go.model;
013:
014: import java.util.Iterator;
015:
016: import org.ontoware.rdf2go.ModelFactory;
017: import org.ontoware.rdf2go.RDF2Go;
018: import org.ontoware.rdf2go.exception.ModelRuntimeException;
019: import org.ontoware.rdf2go.model.impl.DiffImpl;
020: import org.ontoware.rdf2go.model.impl.NotifyingModelLayer;
021: import org.ontoware.rdf2go.model.impl.StatementImpl;
022: import org.ontoware.rdf2go.model.impl.TriplePatternImpl;
023: import org.ontoware.rdf2go.model.node.Variable;
024: import org.ontoware.rdf2go.model.node.impl.PlainLiteralImpl;
025:
026: public class NotifyingModelTest extends AbstractModelTest {
027: // abstract superclass for ModelchangeListeners in this test. By default,
028: // no method of the changelistener should be called, if a method is expected
029: // to be called, this method has to be overwritten.
030:
031: NotifyingModelLayer model;
032:
033: @Override
034: public ModelFactory getModelFactory() {
035: return RDF2Go.getModelFactory();
036: }
037:
038: @Override
039: public void setUp() throws ModelRuntimeException, Exception {
040: super .setUp();
041: Model plainModel = getModelFactory().createModel();
042: assertNotNull(plainModel);
043: this .model = new NotifyingModelLayer(plainModel);
044: }
045:
046: public void testModelConnection() {
047: assertNotNull(this .model);
048: assertNotNull(this .model.getDelegatedModel());
049: assertFalse(this .model.isOpen());
050: this .model.open();
051: assertTrue(this .model.isOpen());
052: this .model.close();
053: assertFalse(this .model.isOpen());
054: }
055:
056: public void testAddStatement() {
057: this .model.open();
058: this .model.addStatement(predicate, object, subject); // this statement should
059: // not cause a
060: // notification
061: this .model
062: .addModelChangedListener(new AbstractModelChangeListener() {
063:
064: @Override
065: public void addedStatement(Statement statement) {
066: assertEquals(new StatementImpl(null, subject,
067: predicate, object), statement);
068: }
069:
070: @Override
071: public void addedStatements(
072: Iterator<? extends Statement> statements) {
073: assertTrue(statements.hasNext());
074: assertEquals(new StatementImpl(null, subject,
075: predicate, object), statements.next());
076: assertFalse(statements.hasNext());
077: }
078: });
079: this .model.addStatement(subject, predicate, object);
080: this .model.close();
081: }
082:
083: public void testRemoveStatement() {
084: this .model.open();
085: this .model.addStatement(subject, predicate, object);
086:
087: this .model
088: .addModelChangedListener(new AbstractModelChangeListener() {
089:
090: @Override
091: public void removedStatement(Statement statement) {
092: assertEquals(new StatementImpl(null, subject,
093: predicate, object), statement);
094: }
095:
096: @Override
097: public void removedStatements(
098: Iterator<? extends Statement> statements) {
099: assertTrue(statements.hasNext());
100: assertEquals(new StatementImpl(null, subject,
101: predicate, object), statements.next());
102: assertFalse(statements.hasNext());
103: }
104: });
105: this .model.close();
106: }
107:
108: public void testChangeStatement() {
109: this .model
110: .addModelChangedListener(new AbstractModelChangeListener() {
111: @Override
112: public void performedUpdate(DiffReader diff) {
113: int i = 0;
114: for (Statement s : diff.getAdded()) {
115: i++;
116: assertEquals(new StatementImpl(null,
117: subject, predicate, object), s);
118: }
119: assertEquals(1, i);
120: assertFalse(diff.getRemoved().iterator()
121: .hasNext());
122: }
123: });
124: this .model.open();
125: Diff diff = new DiffImpl();
126: diff.addStatement(subject, predicate, object);
127: this .model.update(diff);
128: this .model.close();
129: }
130:
131: public void testNotificationOnSubject() {
132: this .model.open();
133: ModelChangedListener listener = new AbstractModelChangeListener() {
134: @Override
135: public void addedStatement(Statement statement) {
136: assertEquals(subject, statement.getSubject());
137: assertEquals(predicate, statement.getPredicate());
138: }
139:
140: @Override
141: public void addedStatements(
142: Iterator<? extends Statement> statements) {
143: assertTrue(statements.hasNext());
144: Statement next = statements.next();
145: assertEquals(subject, next.getSubject());
146: assertEquals(predicate, next.getPredicate());
147: assertFalse(statements.hasNext());
148: }
149: };
150: this .model.addModelChangedListener(listener,
151: new TriplePatternImpl(subject, Variable.ANY,
152: Variable.ANY));
153:
154: this .model.addStatement(subject, predicate, "Test1");
155: this .model.addStatement(predicate, object, "Test2");
156: this .model.addStatement(predicate, subject, "Test3");
157: this .model.removeModelChangedListener(listener);
158: listener = new AbstractModelChangeListener() {
159: @Override
160: public void removedStatement(Statement statement) {
161: assertEquals(subject, statement.getSubject());
162: assertEquals(predicate, statement.getPredicate());
163: }
164:
165: @Override
166: public void removedStatements(
167: Iterator<? extends Statement> statements) {
168: assertTrue(statements.hasNext());
169: Statement next = statements.next();
170: assertEquals(subject, next.getSubject());
171: assertEquals(predicate, next.getPredicate());
172: assertFalse(statements.hasNext());
173: }
174: };
175: this .model.removeStatement(subject, predicate, "Test1");
176: this .model.removeStatement(predicate, object, "Test2");
177: this .model.removeStatement(predicate, subject, "Test3");
178: this .model.close();
179: }
180:
181: public void testNotificationOnLiteralObject() {
182: this .model.open();
183: ModelChangedListener listener = new AbstractModelChangeListener() {
184: @Override
185: public void addedStatement(Statement statement) {
186: assertEquals(new PlainLiteralImpl("Sebastian"),
187: statement.getObject());
188: }
189: };
190: this .model.addModelChangedListener(listener,
191: new TriplePatternImpl(Variable.ANY, Variable.ANY,
192: new PlainLiteralImpl("Sebastian")));
193: this .model.addStatement(subject, predicate, "Sebastian Gerke");
194: this .model.addStatement(predicate, object, "Sebastian");
195: this .model.close();
196: }
197:
198: public void testNotificationOnPredicateAndObject() {
199: this .model.open();
200: ModelChangedListener listener = new AbstractModelChangeListener() {
201: @Override
202: public void addedStatement(Statement statement) {
203: System.out.println(statement.getObject().getClass());
204: assertEquals(new PlainLiteralImpl("Sebastian"),
205: statement.getObject());
206: assertEquals(predicate, statement.getPredicate());
207: }
208: };
209: this .model.addModelChangedListener(listener,
210: new TriplePatternImpl(Variable.ANY, predicate,
211: new PlainLiteralImpl("Sebastian")));
212: this .model.addStatement(subject, predicate, "Sebastian Gerke");
213: this .model.addStatement(subject, predicate, "Sebastian");
214: this .model.addStatement(predicate, object, "Sebastian");
215: this.model.close();
216: }
217: }
|