001: /******************************************************************
002: * File: TestMonitorGraph.java
003: * Created by: Dave Reynolds
004: * Created on: 12-May-2005
005: *
006: * (c) Copyright 2005, Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: TestMonitors.java,v 1.6 2008/01/02 12:08:35 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.util.test;
010:
011: import java.util.*;
012:
013: import com.hp.hpl.jena.util.MonitorGraph;
014: import com.hp.hpl.jena.util.MonitorModel;
015: import com.hp.hpl.jena.graph.*;
016: import com.hp.hpl.jena.graph.test.*;
017: import com.hp.hpl.jena.rdf.model.*;
018: import com.hp.hpl.jena.rdf.model.test.RecordingModelListener;
019: import com.hp.hpl.jena.reasoner.test.TestUtil;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: /**
025: * Tests for MonitorGraph implementation.
026: *
027: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
028: * @version $Revision: 1.6 $
029: */
030:
031: public class TestMonitors extends TestCase {
032:
033: /**
034: * Boilerplate for junit
035: */
036: public TestMonitors(String name) {
037: super (name);
038: }
039:
040: /**
041: * Boilerplate for junit.
042: * This is its own test suite
043: */
044: public static TestSuite suite() {
045: return new TestSuite(TestMonitors.class);
046: }
047:
048: // constants used in the tests
049: String NS = "http://jena.hpl.hp.com/test#";
050: Node a = NodeCreateUtils.create(NS + "a");
051: Node p = NodeCreateUtils.create(NS + "p");
052: Triple t1 = new Triple(a, p, NodeCreateUtils.create(NS + "v1"));
053: Triple t2 = new Triple(a, p, NodeCreateUtils.create(NS + "v2"));
054: Triple t3 = new Triple(a, p, NodeCreateUtils.create(NS + "v3"));
055: Triple t4 = new Triple(a, p, NodeCreateUtils.create(NS + "v4"));
056: Triple t5 = new Triple(a, p, NodeCreateUtils.create(NS + "v5"));
057: Triple t6 = new Triple(a, p, NodeCreateUtils.create(NS + "v6"));
058:
059: /**
060: * Basic graph level test, no monitoring
061: */
062: public void testBasics() {
063: Graph base = Factory.createGraphMem();
064: MonitorGraph monitor = new MonitorGraph(base);
065:
066: // base data
067: base.add(t1);
068: base.add(t2);
069: base.add(t3);
070:
071: // Test changes from empty
072: List additions = new ArrayList();
073: List deletions = new ArrayList();
074: monitor.snapshot(additions, deletions);
075: TestUtil.assertIteratorValues(this , additions.iterator(),
076: new Object[] { t1, t2, t3 });
077: TestUtil.assertIteratorValues(this , deletions.iterator(),
078: new Object[] {});
079:
080: // Make some new changes
081: base.add(t4);
082: base.add(t5);
083: base.delete(t1);
084: base.delete(t2);
085:
086: additions.clear();
087: deletions.clear();
088: monitor.snapshot(additions, deletions);
089: TestUtil.assertIteratorValues(this , additions.iterator(),
090: new Object[] { t4, t5 });
091: TestUtil.assertIteratorValues(this , deletions.iterator(),
092: new Object[] { t1, t2 });
093: TestUtil.assertIteratorValues(this , monitor.find(Node.ANY,
094: Node.ANY, Node.ANY), new Object[] { t3, t4, t5 });
095: }
096:
097: /**
098: * Monitoring test.
099: */
100: public void testListener() {
101: Graph base = Factory.createGraphMem();
102: MonitorGraph monitor = new MonitorGraph(base);
103: RecordingListener listener = new RecordingListener();
104: monitor.getEventManager().register(listener);
105: // base data
106: base.add(t1);
107: base.add(t2);
108: base.add(t3);
109:
110: listener.has(new Object[] {});
111:
112: // Test changes from empty
113: List additions = new ArrayList();
114: List deletions = new ArrayList();
115: monitor.snapshot(additions, deletions);
116: TestUtil.assertIteratorValues(this , additions.iterator(),
117: new Object[] { t1, t2, t3 });
118: TestUtil.assertIteratorValues(this , deletions.iterator(),
119: new Object[] {});
120:
121: listener.assertHas(new Object[] { "addList", monitor,
122: additions, "deleteList", monitor, deletions });
123: listener.clear();
124:
125: // Make some new changes
126: base.add(t4);
127: base.add(t5);
128: base.delete(t1);
129: base.delete(t2);
130:
131: additions.clear();
132: deletions.clear();
133: monitor.snapshot(additions, deletions);
134: TestUtil.assertIteratorValues(this , additions.iterator(),
135: new Object[] { t4, t5 });
136: TestUtil.assertIteratorValues(this , deletions.iterator(),
137: new Object[] { t1, t2 });
138: TestUtil.assertIteratorValues(this , monitor.find(Node.ANY,
139: Node.ANY, Node.ANY), new Object[] { t3, t4, t5 });
140:
141: listener.assertHas(new Object[] { "addList", monitor,
142: additions, "deleteList", monitor, deletions });
143: listener.clear();
144: }
145:
146: /**
147: * Test model level access
148: */
149: public void testModelMonitor() {
150: Model base = ModelFactory.createDefaultModel();
151: // Constants for model level test
152: Resource ar = base.createResource(NS + "a");
153: Property pr = base.createProperty(NS + "p");
154: Statement s1 = base.createStatement(ar, pr, "1");
155: Statement s2 = base.createStatement(ar, pr, "2");
156: Statement s3 = base.createStatement(ar, pr, "3");
157: Statement s4 = base.createStatement(ar, pr, "4");
158: Statement s5 = base.createStatement(ar, pr, "5");
159:
160: MonitorModel monitor = new MonitorModel(base);
161: RecordingModelListener listener = new RecordingModelListener();
162: monitor.register(listener);
163:
164: // base data
165: base.add(s1);
166: base.add(s2);
167: base.add(s3);
168:
169: // Test changes from empty
170: List additions = new ArrayList();
171: List deletions = new ArrayList();
172: monitor.snapshot(additions, deletions);
173: TestUtil.assertIteratorValues(this , additions.iterator(),
174: new Object[] { s1, s2, s3 });
175: TestUtil.assertIteratorValues(this , deletions.iterator(),
176: new Object[] {});
177: listener.assertHas(new Object[] { "addList", additions,
178: "removeList", deletions });
179: listener.clear();
180:
181: // Make some new changes
182: base.add(s4);
183: base.add(s5);
184: base.remove(s1);
185: base.remove(s2);
186:
187: additions.clear();
188: deletions.clear();
189: monitor.snapshot(additions, deletions);
190: TestUtil.assertIteratorValues(this , additions.iterator(),
191: new Object[] { s4, s5 });
192: TestUtil.assertIteratorValues(this , deletions.iterator(),
193: new Object[] { s1, s2 });
194: TestUtil.assertIteratorValues(this , monitor.listStatements(),
195: new Object[] { s3, s4, s5 });
196:
197: listener.assertHas(new Object[] { "addList", additions,
198: "removeList", deletions });
199: listener.clear();
200: }
201:
202: }
203:
204: /*
205: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
206: All rights reserved.
207:
208: Redistribution and use in source and binary forms, with or without
209: modification, are permitted provided that the following conditions
210: are met:
211:
212: 1. Redistributions of source code must retain the above copyright
213: notice, this list of conditions and the following disclaimer.
214:
215: 2. Redistributions in binary form must reproduce the above copyright
216: notice, this list of conditions and the following disclaimer in the
217: documentation and/or other materials provided with the distribution.
218:
219: 3. The name of the author may not be used to endorse or promote products
220: derived from this software without specific prior written permission.
221:
222: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
223: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
224: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
225: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
226: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
227: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
228: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
229: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
230: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
232: */
|