1 6 7 package com.hp.hpl.jena.rdf.model.test; 8 9 import com.hp.hpl.jena.graph.*; 10 import com.hp.hpl.jena.graph.test.*; 11 import com.hp.hpl.jena.rdf.model.*; 12 import com.hp.hpl.jena.rdf.model.impl.*; 13 import com.hp.hpl.jena.shared.*; 14 import com.hp.hpl.jena.util.CollectionFactory; 15 16 import java.util.*; 17 18 24 25 public class ModelTestBase extends GraphTestBase 26 { 27 public ModelTestBase(String name) 28 { super(name); } 29 30 protected static Model aModel = extendedModel(); 31 32 protected static final Model empty = ModelFactory.createDefaultModel(); 33 34 protected static Model extendedModel() 35 { 36 Model result = ModelFactory.createDefaultModel(); 37 result.setNsPrefixes( PrefixMapping.Extended ); 38 return result; 39 } 40 41 protected static String nice( RDFNode n ) 42 { return nice( n.asNode() ); } 43 44 51 public static Statement statement( Model m, String fact ) 52 { 53 StringTokenizer st = new StringTokenizer( fact ); 54 Resource sub = resource( m, st.nextToken() ); 55 Property pred = property( m, st.nextToken() ); 56 RDFNode obj = rdfNode( m, st.nextToken() ); 57 return m.createStatement( sub, pred, obj ); 58 } 59 60 public static Statement statement( String fact ) 61 { return statement( aModel, fact ); } 62 63 public static RDFNode rdfNode( Model m, String s ) 64 { 65 Node n = Node.create( m, s ); 66 return (RDFNode) ((ModelCom) m).getNodeAs( n, (n.isLiteral() ? Literal.class : Resource.class) ); 67 } 68 69 protected static Resource resource() 70 { return ResourceFactory.createResource(); } 71 72 public static Resource resource( String s ) 73 { return resource( aModel, s ); } 74 75 public static Resource resource( Model m, String s ) 76 { return (Resource) rdfNode( m, s ); } 77 78 public static Property property( String s ) 79 { return property( aModel, s ); } 80 81 public static Property property( Model m, String s ) 82 { return (Property) rdfNode( m, s, Property.class ); } 83 84 public static Literal literal( Model m, String s ) 85 { return (Literal) rdfNode( m, s, Literal.class ); } 86 87 public static RDFNode rdfNode( Model m, String s, Class c ) 88 { 89 Node n = Node.create( m, s ); 90 return (RDFNode) ((ModelCom) m).getNodeAs( n, c ); 91 } 92 93 100 public static Statement [] statements( Model m, String facts ) 101 { 102 ArrayList sl = new ArrayList(); 103 StringTokenizer st = new StringTokenizer( facts, ";" ); 104 while (st.hasMoreTokens()) sl.add( statement( m, st.nextToken() ) ); 105 return (Statement []) sl.toArray( new Statement[sl.size()] ); 106 } 107 108 115 public static Resource [] resources( Model m, String items ) 116 { 117 ArrayList rl = new ArrayList(); 118 StringTokenizer st = new StringTokenizer( items ); 119 while (st.hasMoreTokens()) rl.add( resource( m, st.nextToken() ) ); 120 return (Resource []) rl.toArray( new Resource[rl.size()] ); 121 } 122 123 130 public static Model modelAdd( Model m, String facts ) 131 { 132 StringTokenizer semis = new StringTokenizer( facts, ";" ); 133 while (semis.hasMoreTokens()) m.add( statement( m, semis.nextToken() ) ); 134 return m; 135 } 136 137 143 public static Model modelWithStatements( String facts ) 144 { return modelWithStatements( ReificationStyle.Standard, facts ); } 145 146 154 public static Model modelWithStatements( ReificationStyle style, String facts ) 155 { return modelAdd( createModel( style ), facts ); } 156 157 160 public static Model createModel( ReificationStyle style ) 161 { 162 Model result = ModelFactory.createDefaultModel( style ); 163 result.setNsPrefixes( PrefixMapping.Extended ); 164 return result; 165 } 166 167 173 public static Model createMemModel() 174 { return ModelFactory.createDefaultModel(); } 175 176 184 public void assertIsoModels( String title, Model wanted, Model got ) 185 { 186 if (wanted.isIsomorphicWith( got ) == false) 187 { 188 Map map = CollectionFactory.createHashedMap(); 189 fail( title + ": expected " + nice( wanted.getGraph(), map ) + "\n but had " + nice( got.getGraph(), map ) ); 190 } 191 } 192 193 196 public void assertIsoModels( Model wanted, Model got ) 197 { assertIsoModels( "models must be isomorphic", wanted, got ); } 198 199 } 200 201 202 | Popular Tags |