KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > test > RecordingModelListener


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: RecordingModelListener.java,v 1.3 2005/02/21 12:15:00 andy_seaborne Exp $
5 */

6 package com.hp.hpl.jena.rdf.model.test;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.List JavaDoc;
11
12 import junit.framework.Assert;
13
14 import com.hp.hpl.jena.rdf.model.Model;
15 import com.hp.hpl.jena.rdf.model.ModelChangedListener;
16 import com.hp.hpl.jena.rdf.model.Statement;
17 import com.hp.hpl.jena.rdf.model.StmtIterator;
18
19
20 class RecordingModelListener implements ModelChangedListener
21     {
22     List JavaDoc history = new ArrayList JavaDoc();
23     
24     public void addedStatement( Statement s )
25         { record( "add", s ); }
26         
27     public void addedStatements( Statement [] statements )
28         { record( "add[]", Arrays.asList( statements ) ); }
29         
30     public void addedStatements( List JavaDoc statements )
31         { record( "addList", statements ); }
32         
33     public void addedStatements( StmtIterator statements )
34         { record( "addIterator", ModelTestBase.iteratorToList( statements ) ); }
35         
36     public void addedStatements( Model m )
37         { record( "addModel", m ); }
38         
39     public void removedStatements( Statement [] statements )
40         { record( "remove[]", Arrays.asList( statements ) ); }
41     
42    public void removedStatement( Statement s )
43         { record( "remove", s ); }
44         
45     public void removedStatements( List JavaDoc statements )
46         { record( "removeList", statements ); }
47         
48     public void removedStatements( StmtIterator statements )
49         { record( "removeIterator", ModelTestBase.iteratorToList( statements ) ); }
50         
51     public void removedStatements( Model m )
52         { record( "removeModel", m ); }
53     
54     public void notifyEvent( Model m, Object JavaDoc event )
55         { record( "someEvent", m, event ); }
56     
57     protected void record( String JavaDoc tag, Object JavaDoc x, Object JavaDoc y )
58         { history.add( tag ); history.add( x ); history.add( y ); }
59         
60     protected void record( String JavaDoc tag, Object JavaDoc info )
61         { history.add( tag ); history.add( info ); }
62         
63     boolean has( Object JavaDoc [] things )
64         { return history.equals( Arrays.asList( things ) ); }
65         
66     void assertHas( Object JavaDoc [] things )
67         {
68         if (has( things ) == false)
69             ModelTestBase.fail( "expected " + Arrays.asList( things ) + " but got " + history );
70         }
71     
72     public boolean has( List JavaDoc things )
73             { return history.equals( things ); }
74         
75     public boolean hasStart( List JavaDoc L )
76         { return L.size() <= history.size() && L.equals( history.subList( 0, L.size() ) ); }
77     
78     public boolean hasEnd( List JavaDoc L )
79         { return L.size() <= history.size() && L.equals( history.subList( history.size() - L.size(), history.size() ) ); }
80     
81     public void assertHas( List JavaDoc things )
82         { if (has( things ) == false) Assert.fail( "expected " + things + " but got " + history ); }
83     
84     public void assertHasStart( Object JavaDoc [] start )
85         {
86         List JavaDoc L = Arrays.asList( start );
87         if (hasStart( L ) == false) Assert.fail( "expected " + L + " at the beginning of " + history );
88         }
89     
90     public void assertHasEnd( Object JavaDoc [] end )
91         {
92         List JavaDoc L = Arrays.asList( end );
93         if (hasEnd( L ) == false) Assert.fail( "expected " + L + " at the end of " + history );
94         }
95     }
96
97 /*
98 (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
99 All rights reserved.
100
101 Redistribution and use in source and binary forms, with or without
102 modification, are permitted provided that the following conditions
103 are met:
104
105 1. Redistributions of source code must retain the above copyright
106    notice, this list of conditions and the following disclaimer.
107
108 2. Redistributions in binary form must reproduce the above copyright
109    notice, this list of conditions and the following disclaimer in the
110    documentation and/or other materials provided with the distribution.
111
112 3. The name of the author may not be used to endorse or promote products
113    derived from this software without specific prior written permission.
114
115 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
116 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
117 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
118 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
119 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
120 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
121 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
122 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
123 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
124 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
125 */
Popular Tags