001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestModelEvents.java,v 1.21 2008/01/02 12:04:41 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.test;
008:
009: import com.hp.hpl.jena.rdf.listeners.*;
010: import com.hp.hpl.jena.rdf.model.*;
011: import com.hp.hpl.jena.rdf.model.impl.*;
012:
013: import java.util.*;
014: import junit.framework.*;
015:
016: /**
017: Tests for model events and listeners.
018: @author kers
019: */
020: public class TestModelEvents extends ModelTestBase {
021: public TestModelEvents(String name) {
022: super (name);
023: }
024:
025: public static TestSuite suite() {
026: return new TestSuite(TestModelEvents.class);
027: }
028:
029: protected Model model;
030: protected RecordingModelListener SL;
031:
032: public void setUp() {
033: model = ModelFactory.createDefaultModel();
034: SL = new RecordingModelListener();
035: }
036:
037: public void testRegistrationCompiles() {
038: assertSame(model, model.register(new RecordingModelListener()));
039: }
040:
041: public void testUnregistrationCompiles() {
042: model.unregister(new RecordingModelListener());
043: }
044:
045: public void testAddSingleStatements() {
046: Statement S1 = statement(model, "S P O");
047: Statement S2 = statement(model, "A B C");
048: assertFalse(SL.has(new Object[] { "add", S1 }));
049: model.register(SL);
050: model.add(S1);
051: SL.assertHas(new Object[] { "add", S1 });
052: model.add(S2);
053: SL.assertHas(new Object[] { "add", S1, "add", S2 });
054: model.add(S1);
055: SL.assertHas(new Object[] { "add", S1, "add", S2, "add", S1 });
056: }
057:
058: public void testTwoListeners() {
059: Statement S = statement(model, "S P O");
060: RecordingModelListener SL1 = new RecordingModelListener();
061: RecordingModelListener SL2 = new RecordingModelListener();
062: model.register(SL1).register(SL2);
063: model.add(S);
064: SL2.assertHas(new Object[] { "add", S });
065: SL1.assertHas(new Object[] { "add", S });
066: }
067:
068: public void testUnregisterWorks() {
069: model.register(SL);
070: model.unregister(SL);
071: model.add(statement(model, "X R Y"));
072: SL.assertHas(new Object[] {});
073: }
074:
075: public void testRemoveSingleStatements() {
076: Statement S = statement(model, "D E F");
077: model.register(SL);
078: model.add(S);
079: model.remove(S);
080: SL.assertHas(new Object[] { "add", S, "remove", S });
081: }
082:
083: public void testAddInPieces() {
084: model.register(SL);
085: model.add(resource(model, "S"), property(model, "P"), resource(
086: model, "O"));
087: SL.assertHas(new Object[] { "add", statement(model, "S P O") });
088: }
089:
090: public void testAddStatementArray() {
091: model.register(SL);
092: Statement[] s = statements(model, "a P b; c Q d");
093: model.add(s);
094: SL.assertHas(new Object[] { "add[]", Arrays.asList(s) });
095: }
096:
097: public void testDeleteStatementArray() {
098: model.register(SL);
099: Statement[] s = statements(model, "a P b; c Q d");
100: model.remove(s);
101: SL.assertHas(new Object[] { "remove[]", Arrays.asList(s) });
102: }
103:
104: public void testAddStatementList() {
105: model.register(SL);
106: List L = Arrays.asList(statements(model, "b I g; m U g"));
107: model.add(L);
108: SL.assertHas(new Object[] { "addList", L });
109: }
110:
111: public void testDeleteStatementList() {
112: model.register(SL);
113: List L = Arrays.asList(statements(model, "b I g; m U g"));
114: model.remove(L);
115: SL.assertHas(new Object[] { "removeList", L });
116: }
117:
118: public void testAddStatementIterator() {
119: model.register(SL);
120: Statement[] sa = statements(model, "x R y; a P b; x R y");
121: StmtIterator it = asIterator(sa);
122: model.add(it);
123: SL.assertHas(new Object[] { "addIterator", Arrays.asList(sa) });
124: }
125:
126: public void testDeleteStatementIterator() {
127: model.register(SL);
128: Statement[] sa = statements(model, "x R y; a P b; x R y");
129: StmtIterator it = asIterator(sa);
130: model.remove(it);
131: SL
132: .assertHas(new Object[] { "removeIterator",
133: Arrays.asList(sa) });
134: }
135:
136: protected StmtIterator asIterator(Statement[] statements) {
137: return new StmtIteratorImpl(Arrays.asList(statements)
138: .iterator());
139: }
140:
141: public void testAddModel() {
142: model.register(SL);
143: Model m = modelWithStatements("NT beats S; S beats H; H beats D");
144: model.add(m);
145: SL.assertHas(new Object[] { "addModel", m });
146: }
147:
148: public void testDeleteModel() {
149: model.register(SL);
150: Model m = modelWithStatements("NT beats S; S beats H; H beats D");
151: model.remove(m);
152: SL.assertHas(new Object[] { "removeModel", m });
153: }
154:
155: /**
156: Test that the null listener doesn't appear to do anything. Or at least
157: doesn't crash ....
158: */
159: public void testNullListener() {
160: ModelChangedListener NL = new NullListener();
161: model.register(NL);
162: model.add(statement(model, "S P O "));
163: model.remove(statement(model, "X Y Z"));
164: model.add(statements(model, "a B c; d E f"));
165: model.remove(statements(model, "g H i; j K l"));
166: model.add(asIterator(statements(model, "m N o; p Q r")));
167: model.remove(asIterator(statements(model, "s T u; v W x")));
168: model.add(modelWithStatements("leaves fall softly"));
169: model.remove(modelWithStatements("water drips endlessly"));
170: model.add(Arrays.asList(statements(model, "xx RR yy")));
171: model.remove(Arrays.asList(statements(model, "aa VV rr")));
172: }
173:
174: public void testChangedListener() {
175: ChangedListener CL = new ChangedListener();
176: model.register(CL);
177: assertFalse(CL.hasChanged());
178: model.add(statement(model, "S P O"));
179: assertTrue(CL.hasChanged());
180: assertFalse(CL.hasChanged());
181: model.remove(statement(model, "ab CD ef"));
182: assertTrue(CL.hasChanged());
183: model.add(statements(model, "gh IJ kl"));
184: assertTrue(CL.hasChanged());
185: model.remove(statements(model, "mn OP qr"));
186: assertTrue(CL.hasChanged());
187: model.add(asIterator(statements(model, "st UV wx")));
188: assertTrue(CL.hasChanged());
189: assertFalse(CL.hasChanged());
190: model.remove(asIterator(statements(model, "yz AB cd")));
191: assertTrue(CL.hasChanged());
192: model.add(modelWithStatements("ef GH ij"));
193: assertTrue(CL.hasChanged());
194: model.remove(modelWithStatements("kl MN op"));
195: assertTrue(CL.hasChanged());
196: model.add(Arrays.asList(statements(model, "rs TU vw")));
197: assertTrue(CL.hasChanged());
198: model.remove(Arrays.asList(statements(model, "xy wh q")));
199: assertTrue(CL.hasChanged());
200: }
201:
202: public void testGeneralEvent() {
203: model.register(SL);
204: Object e = new int[] {};
205: model.notifyEvent(e);
206: SL.assertHas(new Object[] { "someEvent", model, e });
207: }
208:
209: /**
210: Local test class to see that a StatementListener funnels all the changes through
211: add/remove a single statement
212: */
213: public static class WatchStatementListener extends
214: StatementListener {
215: List statements = new ArrayList();
216: String addOrRem = "<unset>";
217:
218: public List contents() {
219: try {
220: return statements;
221: } finally {
222: statements = new ArrayList();
223: }
224: }
225:
226: public String getAddOrRem() {
227: return addOrRem;
228: }
229:
230: public void addedStatement(Statement s) {
231: statements.add(s);
232: addOrRem = "add";
233: }
234:
235: public void removedStatement(Statement s) {
236: statements.add(s);
237: addOrRem = "rem";
238: }
239: }
240:
241: public void another(Map m, Object x) {
242: Integer n = (Integer) m.get(x);
243: if (n == null)
244: n = new Integer(0);
245: m.put(x, new Integer(n.intValue() + 1));
246: }
247:
248: public Map asBag(List l) {
249: Map result = new HashMap();
250: for (int i = 0; i < l.size(); i += 1)
251: another(result, l.get(i));
252: return result;
253: }
254:
255: public void assertSameBag(List wanted, List got) {
256: assertEquals(asBag(wanted), asBag(got));
257: }
258:
259: public void testGot(WatchStatementListener sl, String how,
260: String template) {
261: assertSameBag(Arrays.asList(statements(model, template)), sl
262: .contents());
263: assertEquals(how, sl.getAddOrRem());
264: assertTrue(sl.contents().size() == 0);
265: }
266:
267: public void testTripleListener() {
268: WatchStatementListener sl = new WatchStatementListener();
269: model.register(sl);
270: model.add(statement(model, "b C d"));
271: testGot(sl, "add", "b C d");
272: model.remove(statement(model, "e F g"));
273: testGot(sl, "rem", "e F g");
274: /* */
275: model.add(statements(model, "h I j; k L m"));
276: testGot(sl, "add", "h I j; k L m");
277: model.remove(statements(model, "n O p; q R s"));
278: testGot(sl, "rem", "n O p; q R s");
279: /* */
280: model.add(Arrays.asList(statements(model, "t U v; w X y")));
281: testGot(sl, "add", "t U v; w X y");
282: model.remove(Arrays.asList(statements(model, "z A b; c D e")));
283: testGot(sl, "rem", "z A b; c D e");
284: /* */
285: model.add(asIterator(statements(model, "f G h; i J k")));
286: testGot(sl, "add", "f G h; i J k");
287: model.remove(asIterator(statements(model, "l M n; o P q")));
288: testGot(sl, "rem", "l M n; o P q");
289: /* */
290: model.add(modelWithStatements("r S t; u V w; x Y z"));
291: testGot(sl, "add", "r S t; u V w; x Y z");
292: model.remove(modelWithStatements("a E i; o U y"));
293: testGot(sl, "rem", "a E i; o U y");
294: }
295:
296: static class OL extends ObjectListener {
297: private Object recorded;
298: private String how;
299:
300: public void added(Object x) {
301: recorded = x;
302: how = "add";
303: }
304:
305: public void removed(Object x) {
306: recorded = x;
307: how = "rem";
308: }
309:
310: private Object comparable(Object x) {
311: if (x instanceof Statement[])
312: return Arrays.asList((Statement[]) x);
313: if (x instanceof Iterator)
314: return iteratorToList((Iterator) x);
315: return x;
316: }
317:
318: public void recent(String wantHow, Object value) {
319: assertEquals(comparable(value), comparable(recorded));
320: assertEquals(wantHow, how);
321: recorded = how = null;
322: }
323: }
324:
325: public void testObjectListener() {
326: OL ll = new OL();
327: model.register(ll);
328: Statement s = statement(model, "aa BB cc"), s2 = statement(
329: model, "dd EE ff");
330: model.add(s);
331: ll.recent("add", s);
332: model.remove(s2);
333: ll.recent("rem", s2);
334: /* */
335: List sList = Arrays.asList(statements(model,
336: "gg HH ii; jj KK ll"));
337: model.add(sList);
338: ll.recent("add", sList);
339: List sList2 = Arrays.asList(statements(model,
340: "mm NN oo; pp QQ rr; ss TT uu"));
341: model.remove(sList2);
342: ll.recent("rem", sList2);
343: /* */
344: Model m1 = modelWithStatements("vv WW xx; yy ZZ aa");
345: model.add(m1);
346: ll.recent("add", m1);
347: Model m2 = modelWithStatements("a B g; d E z");
348: model.remove(m2);
349: ll.recent("rem", m2);
350: /* */
351: Statement[] sa1 = statements(model, "th i k; l m n");
352: model.add(sa1);
353: ll.recent("add", sa1);
354: Statement[] sa2 = statements(model, "x o p; r u ch");
355: model.remove(sa2);
356: ll.recent("rem", sa2);
357: /* */
358: Statement[] si1 = statements(model, "u ph ch; psi om eh");
359: model.add(asIterator(si1));
360: ll.recent("add", asIterator(si1));
361: Statement[] si2 = statements(model,
362: "at last the; end of these; tests ok guv");
363: model.remove(asIterator(si2));
364: ll.recent("rem", asIterator(si2));
365: }
366: }
367:
368: /*
369: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
370: All rights reserved.
371:
372: Redistribution and use in source and binary forms, with or without
373: modification, are permitted provided that the following conditions
374: are met:
375:
376: 1. Redistributions of source code must retain the above copyright
377: notice, this list of conditions and the following disclaimer.
378:
379: 2. Redistributions in binary form must reproduce the above copyright
380: notice, this list of conditions and the following disclaimer in the
381: documentation and/or other materials provided with the distribution.
382:
383: 3. The name of the author may not be used to endorse or promote products
384: derived from this software without specific prior written permission.
385:
386: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
387: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
388: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
389: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
390: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
391: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
392: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
393: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
394: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
395: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
396: */
|