1 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 22 public class TestNodeToTriplesMap extends GraphTestBase 23 { 24 public TestNodeToTriplesMap( String 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 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 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 216 protected void addTriples( NodeToTriplesMap nt, String 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 x ) 223 { 224 Set result = new HashSet(); 225 result.add( x ); 226 return result; 227 } 228 229 protected static Set both( Object x, Object y ) 230 { 231 Set result = just( x ); 232 result.add( y ); 233 return result; 234 } 235 236 } 237 238 239 | Popular Tags |