1 6 7 package com.hp.hpl.jena.rdf.model.test; 8 9 import com.hp.hpl.jena.rdf.model.*; 10 11 import com.hp.hpl.jena.util.iterator.*; 12 import com.hp.hpl.jena.util.*; 13 import com.hp.hpl.jena.graph.*; 14 import com.hp.hpl.jena.graph.test.*; 15 16 import java.util.*; 17 18 import junit.framework.*; 19 20 28 public class TestQuery extends ModelTestBase 29 { 30 public TestQuery( String name ) 31 { super(name); } 32 33 public static TestSuite suite() 34 { return new TestSuite( TestQuery.class ); } 35 36 public void testAAA() 37 { 38 testQueryTranslates( "", "" ); 39 testQueryTranslates( "x R y", "x R y" ); 40 testQueryTranslates( "x R _y; p S 99", "x R _y; p S 99" ); 41 testQueryTranslates( "x R ?y", "x R ?y" ); 42 testQueryTranslates( "jqv:x jqv:H y", "?x ?H y" ); 43 } 44 45 public void testBBB() 46 { 47 Model m = ModelFactory.createDefaultModel(); 48 String [][] tests = 49 { 50 {"x", "x"}, 51 {"jqv:x", "?x"} 52 }; 53 for (int i = 0; i < tests.length; i += 1) 54 testVariablesTranslate( resources( m, tests[i][0] ), nodes( tests[i][1] ) ); 55 } 56 57 private void testQueryTranslates( String model, String graph ) 58 { 59 String title = "must translate <" + model + "> to <" + graph + ">"; 60 Model m = modelWithStatements( model ); 61 Graph g = GraphTestBase.graphWith( graph ); 62 QueryMapper qm = new QueryMapper( m, new Resource[0] ); 63 GraphTestBase.assertIsomorphic( title, g, qm.getGraph() ); 64 } 65 66 public void testVariablesTranslate( Resource [] vIn, Node [] vOut ) 67 { 68 assertEquals( "broken test", vIn.length, vOut.length ); 69 QueryMapper qm = new QueryMapper( modelWithStatements( "" ), vIn ); 70 Node [] result = qm.getVariables(); 71 assertEquals( vOut.length, result.length ); 72 for (int i = 0; i < result.length; i += 1) 73 assertEquals( "variable did not convert", vOut[i], result[i] ); 74 } 75 76 public void testModelQuery() 77 { 78 Model m = modelWithStatements( "a R b; b S c; a R p; p T d" ); 79 Model q = modelWithStatements( "jqv:x R jqv:y; jqv:y S jqv:z" ); 80 ExtendedIterator it = ModelQueryUtil.queryBindingsWith( m, q, resources( q, "jqv:x jqv:z") ); 81 assertTrue( it.hasNext() ); 82 assertEquals( Arrays.asList( resources( m, "a c b" ) ), it.next() ); 83 assertFalse( it.hasNext() ); 84 } 85 } 86 87 88 | Popular Tags |