001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: TestNodeCache.java,v 1.5 2008/01/02 12:05:31 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.test;
008:
009: import junit.framework.*;
010:
011: import com.hp.hpl.jena.graph.*;
012:
013: /**
014: TestNodeCache - make some (minimal, driving) tests for the NodeCache used
015: to reduce store turnover for repeated Node construction.
016:
017: @author kers
018: */
019: public class TestNodeCache extends GraphTestBase {
020: public TestNodeCache(String name) {
021: super (name);
022: }
023:
024: public static TestSuite suite() {
025: return new TestSuite(TestNodeCache.class);
026: }
027:
028: /**
029: Utility to find short strings with the same hashcode, which can be used as
030: the basis for constructing nodes with the same hashcode - this is what
031: was used to find the clashing strings used below.
032: */
033: public static void main(String[] ignoredArguments) {
034: String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
035: String[] strings = new String[32000];
036: for (int i = 0; i < alphabet.length(); i += 1)
037: for (int j = 0; j < alphabet.length(); j += 1) {
038: String s = "" + alphabet.charAt(i) + alphabet.charAt(j);
039: if (i == 10 && j == 10)
040: System.err.println(s);
041: int hash = s.hashCode();
042: if (strings[hash] == null)
043: strings[hash] = s;
044: else
045: System.out.println(">> " + strings[hash] + " and "
046: + s + " both hash to " + hash);
047: }
048: }
049:
050: /**
051: Visible evidence that the pairs of strings used have the same hashcode.
052: */
053: public void testClashettes() {
054: assertEquals("eg:aa".hashCode(), "eg:bB".hashCode());
055: assertEquals("eg:ab".hashCode(), "eg:bC".hashCode());
056: assertEquals("eg:ac".hashCode(), "eg:bD".hashCode());
057: assertEquals("eg:Yv".hashCode(), "eg:ZW".hashCode());
058: assertEquals("eg:Yx".hashCode(), "eg:ZY".hashCode());
059: }
060:
061: /**
062: An array of distinct URIs as ad-hoc probes into the cache under test.
063: */
064: protected static String[] someURIs = inventURIs();
065:
066: protected static String[] inventURIs() {
067: String[] a = { "a", "mig", "spoo-", "gilbert", "124c41+" };
068: String[] b = { "b", "class", "procedure", "spindizzy", "rake" };
069: String[] c = { "c", "bucket", "42", "+1", "#mark" };
070: int here = 0;
071: String[] result = new String[a.length * b.length * c.length];
072: for (int i = 0; i < a.length; i += 1)
073: for (int j = 0; j < b.length; j += 1)
074: for (int k = 0; k < c.length; k += 1)
075: result[here++] = "eg:" + a[i] + b[j] + c[k];
076: return result;
077: }
078:
079: /**
080: test that a new cache is empty - none of the proble URIs are bound.
081: */
082: public void testNewCacheEmpty() {
083: NodeCache c = new NodeCache();
084: for (int i = 0; i < someURIs.length; i += 1)
085: assertEquals(null, c.get(someURIs[i]));
086: }
087:
088: /**
089: test that an element put into the cache is immediately retrievable
090: */
091: public void testNewCacheUpdates() {
092: NodeCache c = new NodeCache();
093: for (int i = 0; i < someURIs.length; i += 1) {
094: Node it = Node.createURI(someURIs[i]);
095: c.put(someURIs[i], it);
096: assertEquals(it, c.get(someURIs[i]));
097: }
098: }
099:
100: /**
101: test that labels with the same hashcode are not confused.
102: */
103: public void testClashing() {
104: String A = "eg:aa", B = "eg:bB";
105: assertEquals(A.hashCode(), B.hashCode());
106: /* */
107: NodeCache c = new NodeCache();
108: c.put(A, Node.createURI(A));
109: assertEquals(null, c.get(B));
110: c.put(B, Node.createURI(B));
111: assertEquals(Node.createURI(B), c.get(B));
112: }
113:
114: }
115:
116: /*
117: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
118: All rights reserved.
119:
120: Redistribution and use in source and binary forms, with or without
121: modification, are permitted provided that the following conditions
122: are met:
123:
124: 1. Redistributions of source code must retain the above copyright
125: notice, this list of conditions and the following disclaimer.
126:
127: 2. Redistributions in binary form must reproduce the above copyright
128: notice, this list of conditions and the following disclaimer in the
129: documentation and/or other materials provided with the distribution.
130:
131: 3. The name of the author may not be used to endorse or promote products
132: derived from this software without specific prior written permission.
133:
134: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
135: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
136: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
137: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
138: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
139: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
140: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
141: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
142: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
143: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
144: */
|