KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package com.hp.hpl.jena.graph.test;
8
9 import junit.framework.*;
10
11 import com.hp.hpl.jena.graph.*;
12
13 /**
14  TestTripleCache
15
16  @author kers
17 */

18 public class TestTripleCache extends GraphTestBase
19     {
20     public TestTripleCache( String JavaDoc name )
21         { super( name ); }
22     
23     public static TestSuite suite()
24         { return new TestSuite( TestTripleCache.class ); }
25     
26     protected static Triple [] someTriples = makeTriples();
27     
28     protected static Triple [] makeTriples()
29         {
30         Node [] S = { node( "x" ), node( "gooseberry" ), node( "_who" ) };
31         Node [] P = { node( "p" ), node( "ramshackle" ), node( "_what" ) };
32         Node [] O = { node( "o" ), node( "42" ), node( "'alpha'greek" ), node( "_o" ) };
33         int here = 0;
34         Triple [] result = new Triple[S.length * P.length * O.length];
35         for (int i = 0; i < S.length; i += 1)
36             for (int j = 0; j < P.length; j += 1)
37                 for (int k = 0; k < O.length; k += 1)
38                     result[here++] = new Triple( S[i], P[j], O[k] );
39         return result;
40         }
41     
42     public void testEmptyTripleCache()
43         {
44         TripleCache tc = new TripleCache();
45         for (int i = 0; i < someTriples.length; i += 1)
46             {
47             Triple t = someTriples[i];
48             assertEquals( null, tc.get( t.getSubject(), t.getPredicate(), t.getObject() ) );
49             }
50         }
51     
52     public void testPutReturnsTriple()
53         {
54         TripleCache tc = new TripleCache();
55         for (int i = 0; i < someTriples.length; i += 1)
56             assertSame( someTriples[i], tc.put( someTriples[i] ) );
57         }
58     
59     public void testPutinTripleCache()
60         {
61         TripleCache tc = new TripleCache();
62         for (int i = 0; i < someTriples.length; i += 1)
63             {
64             Triple t = someTriples[i];
65             tc.put( t );
66             assertEquals( t, tc.get( t.getSubject(), t.getPredicate(), t.getObject() ) );
67             }
68         }
69     
70     public void testCacheClash()
71         {
72         TripleCache tc = new TripleCache();
73         Triple A = new Triple( node( "eg:Yx" ), node( "p" ), node( "o" ) );
74         Triple B = new Triple( node( "eg:ZY" ), node( "p" ), node( "o" ) );
75         assertEquals( A.hashCode(), B.hashCode() );
76         tc.put( A );
77         assertEquals( null, tc.get( B.getSubject(), B.getPredicate(), B.getObject() ) );
78         }
79     }
80
81 /*
82     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
83     All rights reserved.
84
85     Redistribution and use in source and binary forms, with or without
86     modification, are permitted provided that the following conditions
87     are met:
88
89     1. Redistributions of source code must retain the above copyright
90        notice, this list of conditions and the following disclaimer.
91
92     2. Redistributions in binary form must reproduce the above copyright
93        notice, this list of conditions and the following disclaimer in the
94        documentation and/or other materials provided with the distribution.
95
96     3. The name of the author may not be used to endorse or promote products
97        derived from this software without specific prior written permission.
98
99     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
100     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
101     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
102     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
103     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
104     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
105     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
106     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
107     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
108     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
109 */
Popular Tags