KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > test > RecordingListener


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: RecordingListener.java,v 1.13 2005/02/21 11:52:37 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.test;
8
9 import com.hp.hpl.jena.graph.*;
10
11 import junit.framework.*;
12 import java.util.*;
13
14 /**
15     This testing listener records the event names and data, and provides
16     a method for comparing the actual with the expected history.
17 */

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