001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: RecordingModelListener.java,v 1.7 2008/01/02 12:04:44 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.rdf.model.test;
007:
008: import java.util.ArrayList;
009: import java.util.Arrays;
010: import java.util.List;
011:
012: import junit.framework.Assert;
013:
014: import com.hp.hpl.jena.rdf.model.Model;
015: import com.hp.hpl.jena.rdf.model.ModelChangedListener;
016: import com.hp.hpl.jena.rdf.model.Statement;
017: import com.hp.hpl.jena.rdf.model.StmtIterator;
018:
019: public class RecordingModelListener implements ModelChangedListener {
020: List history = new ArrayList();
021:
022: public void addedStatement(Statement s) {
023: record("add", s);
024: }
025:
026: public void addedStatements(Statement[] statements) {
027: record("add[]", Arrays.asList(statements));
028: }
029:
030: public void addedStatements(List statements) {
031: record("addList", statements);
032: }
033:
034: public void addedStatements(StmtIterator statements) {
035: record("addIterator", ModelTestBase.iteratorToList(statements));
036: }
037:
038: public void addedStatements(Model m) {
039: record("addModel", m);
040: }
041:
042: public void removedStatements(Statement[] statements) {
043: record("remove[]", Arrays.asList(statements));
044: }
045:
046: public void removedStatement(Statement s) {
047: record("remove", s);
048: }
049:
050: public void removedStatements(List statements) {
051: record("removeList", statements);
052: }
053:
054: public void removedStatements(StmtIterator statements) {
055: record("removeIterator", ModelTestBase
056: .iteratorToList(statements));
057: }
058:
059: public void removedStatements(Model m) {
060: record("removeModel", m);
061: }
062:
063: public void notifyEvent(Model m, Object event) {
064: record("someEvent", m, event);
065: }
066:
067: protected void record(String tag, Object x, Object y) {
068: history.add(tag);
069: history.add(x);
070: history.add(y);
071: }
072:
073: protected void record(String tag, Object info) {
074: history.add(tag);
075: history.add(info);
076: }
077:
078: public boolean has(Object[] things) {
079: return history.equals(Arrays.asList(things));
080: }
081:
082: public void assertHas(Object[] things) {
083: if (has(things) == false)
084: ModelTestBase.fail("expected " + Arrays.asList(things)
085: + " but got " + history);
086: }
087:
088: public boolean has(List things) {
089: return history.equals(things);
090: }
091:
092: public boolean hasStart(List L) {
093: return L.size() <= history.size()
094: && L.equals(history.subList(0, L.size()));
095: }
096:
097: public boolean hasEnd(List L) {
098: return L.size() <= history.size()
099: && L.equals(history.subList(history.size() - L.size(),
100: history.size()));
101: }
102:
103: public void assertHas(List things) {
104: if (has(things) == false)
105: Assert.fail("expected " + things + " but got " + history);
106: }
107:
108: public void assertHasStart(Object[] start) {
109: List L = Arrays.asList(start);
110: if (hasStart(L) == false)
111: Assert.fail("expected " + L + " at the beginning of "
112: + history);
113: }
114:
115: public void assertHasEnd(Object[] end) {
116: List L = Arrays.asList(end);
117: if (hasEnd(L) == false)
118: Assert.fail("expected " + L + " at the end of " + history);
119: }
120:
121: public void clear() {
122: history.clear();
123: }
124:
125: }
126:
127: /*
128: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
129: All rights reserved.
130:
131: Redistribution and use in source and binary forms, with or without
132: modification, are permitted provided that the following conditions
133: are met:
134:
135: 1. Redistributions of source code must retain the above copyright
136: notice, this list of conditions and the following disclaimer.
137:
138: 2. Redistributions in binary form must reproduce the above copyright
139: notice, this list of conditions and the following disclaimer in the
140: documentation and/or other materials provided with the distribution.
141:
142: 3. The name of the author may not be used to endorse or promote products
143: derived from this software without specific prior written permission.
144:
145: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
146: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
147: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
148: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
149: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
150: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
151: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
152: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
153: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
154: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
155: */
|