1 6 7 package com.hp.hpl.jena.graph.test; 8 9 13 14 import com.hp.hpl.jena.util.CollectionFactory; 15 import com.hp.hpl.jena.util.IteratorCollection; 16 import com.hp.hpl.jena.util.iterator.*; 17 import com.hp.hpl.jena.graph.*; 18 import com.hp.hpl.jena.graph.impl.GraphBase; 19 import com.hp.hpl.jena.mem.*; 20 import com.hp.hpl.jena.rdf.model.*; 21 import com.hp.hpl.jena.shared.*; 22 import com.hp.hpl.jena.test.*; 23 24 import java.lang.reflect.Constructor ; 25 import java.util.*; 26 27 public class GraphTestBase extends JenaTestBase 28 { 29 public GraphTestBase( String name ) 30 { super( name ); } 31 32 public static Node node( String x ) 33 { return Node.create( x ); } 34 35 public static Model modelFor( Graph g ) 36 { return ModelFactory.createModelForGraph( g ); } 37 38 public static Set iteratorToSet( Iterator L ) 39 { return IteratorCollection.iteratorToSet( L ); } 40 41 public static List iteratorToList( Iterator it ) 42 { return IteratorCollection.iteratorToList( it ); } 43 44 public Set nodeSet( String nodes ) 45 { 46 Set result = CollectionFactory.createHashedSet(); 47 StringTokenizer st = new StringTokenizer( nodes ); 48 while (st.hasMoreTokens()) result.add( node( st.nextToken() ) ); 49 return result; 50 } 51 52 public Set arrayToSet( Object [] elements ) 53 { return CollectionFactory.createHashedSet( Arrays.asList( elements ) ); } 54 55 public static Triple triple( String fact ) 56 { return Triple.create( fact ); } 57 58 public static Triple triple( PrefixMapping pm, String fact ) 59 { return Triple.create( pm, fact ); } 60 61 public static Triple [] tripleArray( String facts ) 62 { 63 ArrayList al = new ArrayList(); 64 StringTokenizer semis = new StringTokenizer( facts, ";" ); 65 while (semis.hasMoreTokens()) al.add( triple( PrefixMapping.Extended, semis.nextToken() ) ); 66 return (Triple []) al.toArray( new Triple [al.size()] ); 67 } 68 69 public static Set tripleSet( String facts ) 70 { 71 Set result = new HashSet(); 72 StringTokenizer semis = new StringTokenizer( facts, ";" ); 73 while (semis.hasMoreTokens()) result.add( triple( semis.nextToken() ) ); 74 return result; 75 } 76 77 public static Node [] nodes( String items ) 78 { 79 ArrayList nl = new ArrayList(); 80 StringTokenizer nodes = new StringTokenizer( items ); 81 while (nodes.hasMoreTokens()) nl.add( node( nodes.nextToken() ) ); 82 return (Node []) nl.toArray( new Node [nl.size()] ); 83 } 84 85 public static Graph graphAdd( Graph g, String s ) 86 { 87 StringTokenizer semis = new StringTokenizer( s, ";" ); 88 while (semis.hasMoreTokens()) g.add( triple( PrefixMapping.Extended, semis.nextToken() ) ); 89 return g; 90 } 91 92 public static Graph newGraph() 93 { 94 Graph result = new GraphMem(); 95 result.getPrefixMapping().setNsPrefixes( PrefixMapping.Extended ); 96 return result; 97 } 98 99 public static Graph graphWith( String s ) 100 { 101 return graphAdd( newGraph(), s ); 102 } 103 104 public static void assertEqualsTemplate( String title, Graph g, String template ) 105 { 106 assertIsomorphic( title, graphWith( template ), g ); 108 } 109 110 public static void assertIsomorphic( String title, Graph expected, Graph got ) 111 { 112 if (!expected.isIsomorphicWith( got )) 113 { 114 Map map = CollectionFactory.createHashedMap(); 115 fail( title + ": wanted " + nice( expected, map ) + "\nbut got " + nice( got, map ) ); 116 } 117 } 118 119 public static String nice( Graph g, Map bnodes ) 120 { 121 StringBuffer b = new StringBuffer ( g.size() * 100 ); 122 ExtendedIterator it = GraphUtil.findAll( g ); 123 while (it.hasNext()) niceTriple( b, bnodes, (Triple) it.next() ); 124 return b.toString(); 125 } 126 127 protected static void niceTriple( StringBuffer b, Map bnodes, Triple t ) 128 { 129 b.append( "\n " ); 130 appendNode( b, bnodes, t.getSubject() ); 131 appendNode( b, bnodes, t.getPredicate() ); 132 appendNode( b, bnodes, t.getObject() ); 133 } 134 135 static int bnc = 1000; 136 137 protected static void appendNode( StringBuffer b, Map bnodes, Node n ) 138 { 139 b.append( ' ' ); 140 if (n.isBlank()) 141 { 142 Object already = bnodes.get( n ); 143 if (already == null) bnodes.put( n, already = "_b" + bnc++ ); 144 b.append( already ); 145 } 146 else 147 b.append( nice( n ) ); 148 } 149 150 protected static String nice( Node n ) 151 { return n.toString( PrefixMapping.Extended, true ); } 152 153 public static void assertIsomorphic( Graph expected, Graph got ) 154 { assertIsomorphic( "graphs must be isomorphic", expected, got ); } 155 156 public static void assertContains( String name, String s, Graph g ) 157 { 158 assertTrue( name + " must contain " + s, g.contains( triple( s ) ) ); 159 } 160 161 public static void assertContainsAll( String name, Graph g, String s ) 162 { 163 StringTokenizer semis = new StringTokenizer( s, ";" ); 164 while (semis.hasMoreTokens()) assertContains( name, semis.nextToken(), g ); 165 } 166 167 public void assertOmits( String name, Graph g, String s ) 168 { 169 assertFalse( name + " must not contain " + s, g.contains( triple( s ) ) ); 170 } 171 172 public void assertOmitsAll( String name, Graph g, String s ) 173 { 174 StringTokenizer semis = new StringTokenizer( s, ";" ); 175 while (semis.hasMoreTokens()) assertOmits( name, g, semis.nextToken() ); 176 } 177 178 public static boolean contains( Graph g, String fact ) 179 { 180 return g.contains( triple( fact ) ); 181 } 182 183 public void testContains( Graph g, Triple [] triples ) 184 { 185 for (int i = 0; i < triples.length; i += 1) 186 assertTrue( "contains " + triples[i], g.contains( triples[i] ) ); 187 } 188 189 public void testContains( Graph g, List triples ) 190 { 191 for (int i = 0; i < triples.size(); i += 1) 192 assertTrue( g.contains( (Triple) triples.get(i) ) ); 193 } 194 195 public void testContains( Graph g, Iterator it ) 196 { while (it.hasNext()) assertTrue( g.contains( (Triple) it.next() ) ); } 197 198 public void testContains( Graph g, Graph other ) 199 { testContains( g, GraphUtil.findAll( other ) ); } 200 201 public void testOmits( Graph g, Triple [] triples ) 202 { for (int i = 0; i < triples.length; i += 1) assertFalse( "", g.contains( triples[i] ) ); } 203 204 public void testOmits( Graph g, List triples ) 205 { 206 for (int i = 0; i < triples.size(); i += 1) 207 assertFalse( "", g.contains( (Triple) triples.get(i) ) ); 208 } 209 210 public void testOmits( Graph g, Iterator it ) 211 { while (it.hasNext()) assertFalse( "", g.contains( (Triple) it.next() ) ); } 212 213 public void testOmits( Graph g, Graph other ) 214 { testOmits( g, GraphUtil.findAll( other ) ); } 215 216 public static void show( String title, Graph g ) 217 { 218 ClosableIterator it = GraphUtil.findAll( g ); 219 System.out.println( title ); 220 while (it.hasNext()) 221 { 222 Triple t = (Triple) it.next(); 223 System.out.println( " " + t.getSubject() + " @" + t.getPredicate() + " " + t.getObject() ); 224 } 225 } 226 227 242 public static Graph getGraph( Object wrap, Class graphClass, ReificationStyle style ) 243 { 244 try 245 { 246 Constructor cons = getConstructor( graphClass, new Class [] {ReificationStyle.class} ); 247 if (cons != null) return (Graph) cons.newInstance( new Object [] { style } ); 248 Constructor cons2 = getConstructor( graphClass, new Class [] {wrap.getClass(), ReificationStyle.class} ); 249 if (cons2 != null) return (Graph) cons2.newInstance( new Object [] { wrap, style } ); 250 throw new JenaException( "no suitable graph constructor found for " + graphClass ); 251 } 252 catch (RuntimeException e) 253 { throw e; } 254 catch (Exception e) 255 { throw new JenaException( e ); } 256 } 257 258 protected static Graph getReificationTriples( final Reifier r ) 259 { 260 return new GraphBase() 261 { 262 public ExtendedIterator graphBaseFind( TripleMatch m ) { return r.find( m ); } 263 }; 264 } 265 266 267 } 268 269 298 | Popular Tags |