1 6 7 package com.hp.hpl.jena.graph.test; 8 9 import java.util.Set ; 10 11 import com.hp.hpl.jena.graph.*; 12 import com.hp.hpl.jena.shared.*; 13 14 26 27 public abstract class AbstractTestGraphMaker extends GraphTestBase 28 { 29 public AbstractTestGraphMaker( String name ) 30 { super( name ); } 31 32 public abstract GraphMaker getGraphMaker(); 33 34 private GraphMaker gf; 35 36 public void setUp() 37 { gf = getGraphMaker(); } 38 39 public void tearDown() 40 { gf.close(); } 41 42 46 public void testGetGraph() 47 { 48 Graph g1 = gf.getGraph(); 49 assertFalse( "should deliver a Graph", g1 == null ); 50 assertSame( g1, gf.getGraph() ); 51 g1.close(); 52 } 53 54 public void testCreateGraph() 55 { 56 assertDiffer( "each created graph must differ", gf.createGraph(), gf.createGraph() ); 57 } 58 59 public void testAnyName() 60 { 61 gf.createGraph( "plain" ).close(); 62 gf.createGraph( "with.dot" ).close(); 63 gf.createGraph( "http://electric-hedgehog.net/topic#marker" ).close(); 64 } 65 66 69 public void testCannotCreateTwice() 70 { 71 String name = jName( "bonsai" ); 72 gf.createGraph( name, true ); 73 try 74 { 75 gf.createGraph( name, true ); 76 fail( "should not be able to create " + name + " twice" ); 77 } 78 catch (AlreadyExistsException e) 79 {} 80 } 81 82 private String jName( String name ) 83 { return "jena-test-AbstractTestGraphMaker-" + name; } 84 85 public void testCanCreateTwice() 86 { 87 String name = jName( "bridge" ); 88 Graph g1 = gf.createGraph( name, true ); 89 Graph g2 = gf.createGraph( name, false ); 90 assertTrue( "graphs should be the same", sameGraph( g1, g2 ) ); 91 Graph g3 = gf.createGraph( name ); 92 assertTrue( "graphs should be the same", sameGraph( g1, g3 ) ); 93 } 94 95 98 public void testCannotOpenUncreated() 99 { 100 String name = jName( "noSuchGraph" ); 101 try { gf.openGraph( name, true ); fail( name + " should not exist" ); } 102 catch (DoesNotExistException e) { } 103 } 104 105 108 public void testCanOpenUncreated() 109 { 110 String name = jName( "willBeCreated" ); 111 Graph g1 = gf.openGraph( name ); 112 g1.close(); 113 gf.openGraph( name, true ); 114 } 115 116 119 private void testExists( String name ) 120 { assertTrue( name + " should exist", gf.hasGraph( name ) ); } 121 122 123 126 private void testDoesNotExist( String name ) 127 { assertFalse( name + " should exist", gf.hasGraph( name ) ); } 128 129 136 public void testCanFindCreatedGraph() 137 { 138 String alpha = jName( "alpha" ), beta = jName( "beta" ); 139 Graph g1 = gf.createGraph( alpha, true ); 140 Graph h1 = gf.createGraph( beta, true ); 141 Graph g2 = gf.openGraph( alpha, true ); 142 Graph h2 = gf.openGraph( beta, true ); 143 assertTrue( "should find alpha", sameGraph( g1, g2 ) ); 144 assertTrue( "should find beta", sameGraph( h1, h2 ) ); 145 } 146 147 152 private boolean sameGraph( Graph g1, Graph g2 ) 153 { 154 Node S = node( "S" ), P = node( "P" ), O = node( "O" ); 155 g1.add( Triple.create( S, P, O ) ); 156 g2.add( Triple.create( O, P, S ) ); 157 return g2.contains( S, P, O ) && g1.contains( O, P, S ); 158 } 159 160 164 public void testCanRemoveGraph() 165 { 166 String alpha = jName( "bingo" ), beta = jName( "brillo" ); 167 gf.createGraph( alpha, true ); 168 gf.createGraph( beta, true ); 169 testExists( alpha ); 170 testExists( beta ); 171 gf.removeGraph( alpha ); 172 testExists( beta ); 173 testDoesNotExist( alpha ); 174 } 175 176 public void testHasnt() 177 { 178 assertFalse( "no such graph", gf.hasGraph( "john" ) ); 179 assertFalse( "no such graph", gf.hasGraph( "paul" ) ); 180 assertFalse( "no such graph", gf.hasGraph( "george" ) ); 181 182 gf.createGraph( "john", true ); 183 assertTrue( "john now exists", gf.hasGraph( "john" ) ); 184 assertFalse( "no such graph", gf.hasGraph( "paul" ) ); 185 assertFalse( "no such graph", gf.hasGraph( "george" ) ); 186 187 gf.createGraph( "paul", true ); 188 assertTrue( "john still exists", gf.hasGraph( "john" ) ); 189 assertTrue( "paul now exists", gf.hasGraph( "paul" ) ); 190 assertFalse( "no such graph", gf.hasGraph( "george" ) ); 191 192 gf.removeGraph( "john" ); 193 assertFalse( "john has been removed", gf.hasGraph( "john" ) ); 194 assertTrue( "paul still exists", gf.hasGraph( "paul" ) ); 195 assertFalse( "no such graph", gf.hasGraph( "george" ) ); 196 } 197 198 public void testCarefulClose() 199 { 200 Graph x = gf.createGraph( "x" ); 201 Graph y = gf.openGraph( "x" ); 202 x.add( triple( "a BB c" ) ); 203 x.close(); 204 y.add( triple( "p RR q" ) ); 205 y.close(); 206 } 207 208 211 public void testListNoGraphs() 212 { 213 Set s = iteratorToSet( gf.listGraphs() ); 214 if (s.size() > 0) 215 fail( "found names from 'empty' graph maker: " + s ); 216 } 217 218 223 public void testListThreeGraphs() 224 { String x = "x", y = "y/sub", z = "z:boo"; 225 gf.createGraph( x ).close(); 226 gf.createGraph( y ).close(); 227 gf.createGraph( z ).close(); 228 Set s = iteratorToSet( gf.listGraphs() ); 229 assertEquals( 3, s.size() ); 230 assertTrue( s.contains( x ) ); 231 assertTrue( s.contains( y ) ); 232 assertTrue( s.contains( z ) ); } 233 234 238 public void testListAfterDelete() 239 { String x = "x_y", y = "y//zub", z = "a:b/c"; 240 gf.createGraph( x ).close(); 241 gf.createGraph( y ).close(); 242 gf.createGraph( z ).close(); 243 gf.removeGraph( x ); 244 Set s = iteratorToSet( gf.listGraphs() ); 245 assertEquals( 2, s.size() ); 246 assertTrue( s.contains( y ) ); 247 assertTrue( s.contains( z ) ); } 248 249 } 250 251 | Popular Tags |