KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: TestNodeCache.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     TestNodeCache - make some (minimal, driving) tests for the NodeCache used
15     to reduce store turnover for repeated Node construction.
16
17     @author kers
18 */

19 public class TestNodeCache extends GraphTestBase
20     {
21     public TestNodeCache( String JavaDoc name )
22         { super( name ); }
23     
24     public static TestSuite suite()
25         { return new TestSuite( TestNodeCache.class ); }
26         
27     /**
28          Utility to find short strings with the same hashcode, which can be used as
29          the basis for constructing nodes with the same hashcode - this is what
30          was used to find the clashing strings used below.
31     */

32     public static void main( String JavaDoc [] ignoredArguments )
33         {
34         String JavaDoc alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
35         String JavaDoc [] strings = new String JavaDoc[32000];
36         for (int i = 0; i < alphabet.length(); i += 1)
37             for (int j = 0; j < alphabet.length(); j += 1)
38                 {
39                 String JavaDoc s = "" + alphabet.charAt( i ) + alphabet.charAt( j );
40                 if (i == 10 && j == 10) System.err.println( s );
41                 int hash = s.hashCode();
42                 if (strings[hash] == null) strings[hash] = s;
43                 else System.out.println( ">> " + strings[hash] + " and " + s + " both hash to " + hash );
44                 }
45         }
46     
47     /**
48          Visible evidence that the pairs of strings used have the same hashcode.
49     */

50     public void testClashettes()
51         {
52         assertEquals( "eg:aa".hashCode(), "eg:bB".hashCode() );
53         assertEquals( "eg:ab".hashCode(), "eg:bC".hashCode() );
54         assertEquals( "eg:ac".hashCode(), "eg:bD".hashCode() );
55         assertEquals( "eg:Yv".hashCode(), "eg:ZW".hashCode() );
56         assertEquals( "eg:Yx".hashCode(), "eg:ZY".hashCode() );
57         }
58     
59     /**
60          An array of distinct URIs as ad-hoc probes into the cache under test.
61      */

62     protected static String JavaDoc [] someURIs = inventURIs();
63     
64     protected static String JavaDoc [] inventURIs()
65         {
66         String JavaDoc [] a = { "a", "mig", "spoo-", "gilbert", "124c41+" };
67         String JavaDoc [] b = { "b", "class", "procedure", "spindizzy", "rake" };
68         String JavaDoc [] c = { "c", "bucket", "42", "+1", "#mark" };
69         int here = 0;
70         String JavaDoc [] result = new String JavaDoc[a.length * b.length * c.length];
71         for (int i = 0; i < a.length; i += 1)
72             for (int j = 0; j < b.length; j += 1)
73                 for (int k = 0; k < c.length; k += 1)
74                     result[here++] = "eg:" + a[i] + b[j] + c[k];
75         return result;
76         }
77     
78     /**
79         test that a new cache is empty - none of the proble URIs are bound.
80     */

81     public void testNewCacheEmpty()
82         {
83         NodeCache c = new NodeCache();
84         for (int i = 0; i < someURIs.length; i += 1) assertEquals( null, c.get( someURIs[i] ) );
85         }
86         
87     /**
88         test that an element put into the cache is immediately retrievable
89     */

90     public void testNewCacheUpdates()
91         {
92         NodeCache c = new NodeCache();
93         for (int i = 0; i < someURIs.length; i += 1)
94             {
95             Node it = Node.createURI( someURIs[i] );
96             c.put( someURIs[i], it );
97             assertEquals( it, c.get( someURIs[i] ) );
98             }
99         }
100     
101     /**
102         test that labels with the same hashcode are not confused.
103     */

104     public void testClashing()
105         {
106         String JavaDoc A = "eg:aa", B = "eg:bB";
107         assertEquals( A.hashCode(), B.hashCode() );
108     /* */
109         NodeCache c = new NodeCache();
110         c.put( A, Node.createURI( A ) );
111         assertEquals( null, c.get( B ) );
112         c.put( B, Node.createURI( B ) );
113         assertEquals( Node.createURI( B ), c.get( B ) );
114         }
115     
116     }
117
118 /*
119     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
120     All rights reserved.
121
122     Redistribution and use in source and binary forms, with or without
123     modification, are permitted provided that the following conditions
124     are met:
125
126     1. Redistributions of source code must retain the above copyright
127        notice, this list of conditions and the following disclaimer.
128
129     2. Redistributions in binary form must reproduce the above copyright
130        notice, this list of conditions and the following disclaimer in the
131        documentation and/or other materials provided with the distribution.
132
133     3. The name of the author may not be used to endorse or promote products
134        derived from this software without specific prior written permission.
135
136     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
137     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
138     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
139     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
140     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
141     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
142     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
143     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
145     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 */
Popular Tags