KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: TestNodeToTriplesMap.java,v 1.6 2005/02/21 11:52:48 andy_seaborne Exp $
5 */

6 package com.hp.hpl.jena.graph.test;
7
8 import java.util.*;
9
10 import com.hp.hpl.jena.graph.*;
11 import com.hp.hpl.jena.mem.NodeToTriplesMap;
12
13 import junit.framework.TestSuite;
14
15 /**
16     TestNodeToTriplesMap: added, post-hoc, by kers once NTM got
17     rather complicated. So these tests may be (are, at the moment)
18     incomplete.
19     
20     @author kers
21 */

22 public class TestNodeToTriplesMap extends GraphTestBase
23     {
24     public TestNodeToTriplesMap( String JavaDoc name )
25         { super( name ); }
26     
27     public static TestSuite suite()
28         { return new TestSuite( TestNodeToTriplesMap.class ); }
29     
30     protected NodeToTriplesMap ntS = new NodeToTriplesMap()
31         { public Node getIndexNode( Triple t ) { return t.getSubject(); } };
32         
33     protected NodeToTriplesMap ntP = new NodeToTriplesMap()
34         { public Node getIndexNode( Triple t ) { return t.getPredicate(); } };
35         
36     protected NodeToTriplesMap ntO = new NodeToTriplesMap()
37         { public Node getIndexNode( Triple t ) { return t.getObject(); } };
38
39     protected static final Node x = node( "x" );
40     
41     protected static final Node y = node( "y" );
42     
43     public void testZeroSize()
44         {
45         testZeroSize( "fresh NTM", ntS );
46         }
47     
48     protected void testZeroSize( String JavaDoc title, NodeToTriplesMap nt )
49         {
50         assertEquals( title + " should have size 0", 0, nt.size() );
51         assertEquals( title + " should be isEmpty()", true, nt.isEmpty() );
52         assertEquals( title + " should have empty domain", false, nt.domain().hasNext() );
53         }
54     
55     public void testAddOne()
56         {
57         ntS.add( x, triple( "x P y" ) );
58         testJustOne( x, ntS );
59         }
60     
61     public void testAddOneTwice()
62         {
63         addTriples( ntS, "x P y; x P y" );
64         testJustOne( x, ntS );
65         }
66     
67     protected void testJustOne( Node x, NodeToTriplesMap nt )
68         {
69         assertEquals( 1, nt.size() );
70         assertEquals( false, nt.isEmpty() );
71         assertEquals( just( x ), iteratorToSet( nt.domain() ) );
72         }
73     
74     public void testAddTwoUnshared()
75         {
76         addTriples( ntS, "x P a; y Q b" );
77         assertEquals( 2, ntS.size() );
78         assertEquals( false, ntS.isEmpty() );
79         assertEquals( both( x, y ), iteratorToSet( ntS.domain() ) );
80         }
81     
82     public void testAddTwoShared()
83         {
84         addTriples( ntS, "x P a; x Q b" );
85         assertEquals( 2, ntS.size() );
86         assertEquals( false, ntS.isEmpty() );
87         assertEquals( just( x ), iteratorToSet( ntS.domain() ) );
88         }
89     
90     public void testClear()
91         {
92         addTriples( ntS, "x P a; x Q b; y R z" );
93         ntS.clear();
94         testZeroSize( "cleared NTM", ntS );
95         }
96     
97     public void testAllIterator()
98         {
99         String JavaDoc triples = "x P b; y P d; y P f";
100         addTriples( ntS, triples );
101         assertEquals( tripleSet( triples ), iteratorToSet( ntS.iterator() ) );
102         }
103     
104     public void testOneIterator()
105         {
106         addTriples( ntS, "x P b; y P d; y P f" );
107         assertEquals( tripleSet( "x P b" ), iteratorToSet( ntS.iterator( x ) ) );
108         assertEquals( tripleSet( "y P d; y P f" ), iteratorToSet( ntS.iterator( y ) ) );
109         }
110     
111     public void testRemove()
112         {
113         addTriples( ntS, "x P b; y P d; y R f" );
114         ntS.remove( y, triple( "y P d" ) );
115         assertEquals( 2, ntS.size() );
116         assertEquals( tripleSet( "x P b; y R f" ), iteratorToSet( ntS.iterator() ) );
117         }
118     
119     public void testRemoveByIterator()
120         {
121         addTriples( ntS, "x nice a; a nasty b; x nice c" );
122         addTriples( ntS, "y nice d; y nasty e; y nice f" );
123         Iterator it = ntS.iterator();
124         while (it.hasNext())
125             {
126             Triple t = (Triple) it.next();
127             if (t.getPredicate().equals( node( "nasty") )) it.remove();
128             }
129         assertEquals( tripleSet( "x nice a; x nice c; y nice d; y nice f" ), iteratorToSet( ntS.iterator() ) );
130         }
131     
132     public void testIteratorWIthPatternOnEmpty()
133         {
134         assertEquals( tripleSet( "" ), iteratorToSet( ntS.iterator( triple( "a P b" ) ) ) );
135         }
136
137     public void testIteratorWIthPatternOnSomething()
138         {
139         addTriples( ntS, "x P a; y P b; y R c" );
140         assertEquals( tripleSet( "x P a" ), iteratorToSet( ntS.iterator( triple( "x P ??" ) ) ) );
141         assertEquals( tripleSet( "y P b; y R c" ), iteratorToSet( ntS.iterator( triple( "y ?? ??" ) ) ) );
142         assertEquals( tripleSet( "x P a; y P b" ), iteratorToSet( ntS.iterator( triple( "?? P ??" ) ) ) );
143         assertEquals( tripleSet( "y R c" ), iteratorToSet( ntS.iterator( triple( "?? ?? c" ) ) ) );
144         }
145     
146     public void testSpecificIteratorWithPatternOnEmpty()
147         {
148         assertEquals( tripleSet( "" ), iteratorToSet( ntS.iterator( x, triple( "x P b" ) ) ) );
149         }
150     
151     public void testSpecificIteratorWithPatternOnSomething()
152         {
153         addTriples( ntS, "x P a; y P b; y R c" );
154         assertEquals( tripleSet( "x P a" ), iteratorToSet( ntS.iterator( x, triple( "x P ??" ) ) ) );
155         assertEquals( tripleSet( "y P b; y R c" ), iteratorToSet( ntS.iterator( y, triple( "y ?? ??" ) ) ) );
156         assertEquals( tripleSet( "x P a" ), iteratorToSet( ntS.iterator( x, triple( "?? P ??" ) ) ) );
157         assertEquals( tripleSet( "y R c" ), iteratorToSet( ntS.iterator( y, triple( "?? ?? c" ) ) ) );
158         }
159
160     public void testUnspecificRemoveS()
161         {
162         addTriples( ntS, "x P a; y Q b; z R c" );
163         ntS.remove( triple( "x P a" ) );
164         assertEquals( tripleSet( "y Q b; z R c" ), iteratorToSet( ntS.iterator() ) );
165         }
166     
167     public void testUnspecificRemoveP()
168         {
169         addTriples( ntP, "x P a; y Q b; z R c" );
170         ntP.remove( triple( "y Q b" ) );
171         assertEquals( tripleSet( "x P a; z R c" ), iteratorToSet( ntP.iterator() ) );
172         }
173     
174     public void testUnspecificRemoveO()
175         {
176         addTriples( ntO, "x P a; y Q b; z R c" );
177         ntO.remove( triple( "z R c" ) );
178         assertEquals( tripleSet( "x P a; y Q b" ), iteratorToSet( ntO.iterator() ) );
179         }
180     
181     public void testAddBooleanResult()
182         {
183         assertEquals( true, ntS.add( x, triple( "x P y" ) ) );
184         assertEquals( false, ntS.add( x, triple( "x P y" ) ) );
185     /* */
186         assertEquals( true, ntS.add( y, triple( "y Q z" ) ) );
187         assertEquals( false, ntS.add( y, triple( "y Q z" ) ) );
188     /* */
189         assertEquals( true, ntS.add( y, triple( "y R s" ) ) );
190         assertEquals( false, ntS.add( y, triple( "y R s" ) ) );
191         }
192     
193     public void testRemoveBooleanResult()
194         {
195         assertEquals( false, ntS.remove( triple( "x P y" ) ) );
196         ntS.add( x, triple( "x P y" ) );
197         assertEquals( false, ntS.remove( triple( "x Q y" ) ) );
198         assertEquals( true, ntS.remove( triple( "x P y" ) ) );
199         assertEquals( false, ntS.remove( triple( "x P y" ) ) );
200         }
201     
202     public void testContains()
203         {
204         addTriples( ntS, "x P y; a P b" );
205         assertTrue( ntS.contains( triple( "x P y" ) ) );
206         assertTrue( ntS.contains( triple( "a P b" ) ) );
207         assertFalse( ntS.contains( triple( "x P z" ) ) );
208         assertFalse( ntS.contains( triple( "y P y" ) ) );
209         assertFalse( ntS.contains( triple( "x R y" ) ) );
210         assertFalse( ntS.contains( triple( "e T f" ) ) );
211         assertFalse( ntS.contains( triple( "_x F 17" ) ) );
212         }
213     
214     // TODO more here
215

216     protected void addTriples( NodeToTriplesMap nt, String JavaDoc facts )
217         {
218         Triple [] t = tripleArray( facts );
219         for (int i = 0; i < t.length; i += 1) nt.add( nt.getIndexNode( t[i] ), t[i] );
220         }
221     
222     protected static Set just( Object JavaDoc x )
223         {
224         Set result = new HashSet();
225         result.add( x );
226         return result;
227         }
228
229     protected static Set both( Object JavaDoc x, Object JavaDoc y )
230         {
231         Set result = just( x );
232         result.add( y );
233         return result;
234         }
235     
236     }
237
238
239 /*
240     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
241     All rights reserved.
242     
243     Redistribution and use in source and binary forms, with or without
244     modification, are permitted provided that the following conditions
245     are met:
246     
247     1. Redistributions of source code must retain the above copyright
248        notice, this list of conditions and the following disclaimer.
249     
250     2. Redistributions in binary form must reproduce the above copyright
251        notice, this list of conditions and the following disclaimer in the
252        documentation and/or other materials provided with the distribution.
253     
254     3. The name of the author may not be used to endorse or promote products
255        derived from this software without specific prior written permission.
256     
257     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
258     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
259     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
260     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
261     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
262     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
263     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
264     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
265     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267 */
Popular Tags