1 5 6 package com.hp.hpl.jena.rdql.test; 7 8 11 import junit.framework.* ; 12 13 import java.util.* ; 14 15 import com.hp.hpl.jena.rdf.model.* ; 16 import com.hp.hpl.jena.rdql.*; 17 18 19 23 24 public class QueryTestProgrammatic extends TestSuite 25 { 26 static final String testSetName = "RDQL - Query - Programmatic" ; 27 28 public static boolean dumpModel = false ; 29 public static boolean verbose = false ; 30 31 public static TestSuite suite() 32 { 33 return new QueryTestProgrammatic(testSetName) ; 34 } 35 36 37 private QueryTestProgrammatic(String name) 38 { 39 super(name) ; 40 41 try { 42 Model model1 = makeModel1() ; 43 Model model2 = makeModel2() ; 44 addTest(new TestProgrammatic("RDQL-Test-Prog-1", model1, "SELECT ?x, ?z WHERE (?x, ?a, ?b), (?b, ?y, ?z)")) ; 46 addTest(new TestProgrammatic("RDQL-Test-Prog-2", model1, "SELECT ?z WHERE (?x, ?y, ?z) AND ?z == 1")) ; 47 addTest(new TestProgrammatic("RDQL-Test-Prog-3", model1, "SELECT ?z WHERE (<http://never/r-1>, <http://never/p-0>, ?z)")) ; 48 49 addTest(new TestProgrammatic("RDQL-Test-Prog-4", model2, "SELECT ?x, ?a, ?b, ?y, ?z WHERE (?x, ?a, ?b), (?b, ?y, ?z)")) ; 51 addTest(new TestProgrammatic("RDQL-Test-Prog-5", model2, "SELECT ?z WHERE (?x, ?y, ?z) AND ?z == 1")) ; 52 addTest(new TestProgrammatic("RDQL-Test-Prog-6", model2, "SELECT ?z WHERE (<http://never/r-1>, <http://never/p-0>, ?z)")) ; 53 54 57 ResultBindingImpl rb = new ResultBindingImpl() ; 58 rb.add("x", model1.createResource("http://never/r-1")) ; 59 rb.add("y", model1.createResource("http://never/p-1")) ; 60 addTest(new TestQueryTemplate("RDQL-Test-Template-1", model1, null, "SELECT * WHERE (?x, ?y, ?z)", model1.size())) ; 62 addTest(new TestQueryTemplate("RDQL-Test-Template-2", model1, rb, "SELECT * WHERE (?x, ?y, ?z)", 1)) ; 64 } catch (Exception ex) 65 { 66 System.err.println("Problems making RDQL test") ; 67 ex.printStackTrace(System.err) ; 68 return ; 69 } 70 } 72 73 static class TestProgrammatic extends TestCase 74 { 75 Model model ; 76 String queryString ; 77 78 TestProgrammatic(String testName, Model m, String q) 79 { 80 super(testName) ; 81 model = m ; 82 queryString = q ; 83 } 84 85 protected void runTest() throws Throwable 86 { 87 if ( verbose ) 88 { 89 System.out.println() ; 90 System.out.println("Query:") ; 91 System.out.println(queryString) ; 92 } 93 Query query = new Query(queryString) ; 95 query.setSource(model); 96 QueryExecution qe = new QueryEngine(query) ; 97 QueryResults results = qe.exec() ; 98 99 100 for ( ; results.hasNext() ; ) 101 { 102 ResultBindingImpl rb = (ResultBindingImpl)results.next() ; 103 for ( Iterator iter = rb.getTriples().iterator() ; iter.hasNext() ; ) 104 { 105 Statement s = (Statement)iter.next() ; 106 assertTrue("Statement not in model", model.contains(s)) ; 107 } 108 } 109 results.close() ; 110 } 111 } 112 113 static class TestQueryTemplate extends TestCase 114 { 115 Model model ; 116 String queryString ; 117 ResultBindingImpl binding ; 118 long numResults ; 119 120 TestQueryTemplate(String testName, Model m, ResultBindingImpl b, String q, long num) 121 { 122 super(testName) ; 123 model = m ; 124 queryString = q ; 125 binding = b ; 126 numResults = num ; 127 } 128 129 protected void runTest() throws Throwable 130 { 131 if ( verbose ) 132 { 133 System.out.println() ; 134 System.out.println("Query:") ; 135 System.out.println(queryString) ; 136 } 137 Query query = new Query(queryString) ; 139 query.setSource(model); 140 QueryEngine _qe = new QueryEngine(query) ; 142 QueryResults results = _qe.exec(binding) ; 143 QueryExecution qe = _qe ; 144 145 long count = 0 ; 146 for ( ; results.hasNext() ; ) 147 { 148 ResultBinding rb = (ResultBinding)results.next() ; 149 if ( rb == null ) 150 throw new Exception ("TestQueryTemplate: found null result binding") ; 151 for ( Iterator iter = query.getResultVars().iterator() ; 153 iter.hasNext() ; ) 154 { 155 String varName = (String )iter.next() ; 156 Object obj = rb.get(varName) ; 157 assertNotNull("Variable: "+varName, obj) ; 158 if ( binding != null ) 159 { 160 Object original = binding.get(varName) ; 161 if ( original != null ) 162 assertTrue("Variable: "+varName+" = "+original+" / "+obj, original.equals(obj)) ; 163 } 164 } 165 166 count++ ; 167 } 168 results.close() ; 169 170 if ( count != numResults ) 171 { 172 throw new Exception ("TestQueryTemplate: mismatch in counts. Expected "+numResults+". Got "+count+" Query: "+queryString) ; 173 } 174 175 } 176 } 177 178 static public Model makeModel1() throws Exception 179 { 180 Model model = ModelFactory.createDefaultModel() ; 181 182 for ( int i = 0 ; i < 2 ; i++ ) 184 { 185 for ( int j = 0 ; j < 2 ; j++ ) 187 { 188 model.add(model.createResource("http://never/r-"+i) , 189 model.createProperty("http://never/p-"+j) , 190 i+j) ; 192 } 193 } 194 195 Bag bag = model.createBag("http://never/bag") ; 197 bag.add("11") ; 198 bag.add("22") ; 199 200 model.add(model.createResource("http://never/path"), 202 model.createProperty("http://never/path") , 203 model.createResource("http://never/r-0")) ; 204 return model ; 205 } 206 207 208 static public Model makeModel2() throws Exception 209 { 210 Model model = makeModel1() ; 211 212 Resource anon1 = model.createResource() ; 214 Resource anon2 = model.createResource() ; 215 216 model.add(anon1, 217 model.createProperty("http://never/p-anon-1") , 218 "p-anon-1") ; 219 220 model.add(anon2, 221 model.createProperty("http://never/p-anon-2") , 222 "p-anon-2") ; 223 224 model.add(anon1, 226 model.createProperty("http://never/p-anon-1-2") , 227 anon2) ; 228 229 return model ; 230 } 231 } 232 233 259 260 | Popular Tags |