1 6 7 package com.hp.hpl.jena.rdf.model.test; 8 9 12 13 import com.hp.hpl.jena.rdf.model.*; 14 import com.hp.hpl.jena.vocabulary.*; 15 16 17 import junit.framework.*; 18 19 public class TestSimpleListStatements extends ModelTestBase 20 { 21 22 public TestSimpleListStatements( String name ) 23 { super( name ); } 24 25 public static TestSuite suite() 26 { return new TestSuite( TestSimpleListStatements.class ); } 27 28 Model model = null; 29 30 static boolean booleanValue = true; 31 static char charValue = 'c'; 32 static long longValue = 456; 33 static float floatValue = 5.67F; 34 static double doubleValue = 6.78; 35 static String stringValue ="stringValue"; 36 static String langValue = "en"; 37 38 protected void setUp() throws java.lang.Exception { 39 40 model = ModelFactory.createDefaultModel(); 41 model.createResource("http://example.org/boolean") 42 .addProperty(RDF.value, booleanValue); 43 model.createResource("http://example.org/char") 44 .addProperty(RDF.value, charValue); 45 model.createResource("http://example.org/long") 46 .addProperty(RDF.value, longValue); 47 model.createResource("http://example.org/float") 48 .addProperty(RDF.value, floatValue); 49 model.createResource("http://example.org/double") 50 .addProperty(RDF.value, doubleValue); 51 model.createResource("http://example.org/string") 52 .addProperty(RDF.value, stringValue); 53 model.createResource("http://example.org/langString") 54 .addProperty(RDF.value, stringValue, langValue); 55 56 } 57 58 protected void tearDown() throws java.lang.Exception { 59 model.close(); 60 model = null; 61 } 62 63 public void testBoolean() { 64 StmtIterator iter = model.listStatements(null, null, booleanValue); 65 int i =0; 66 while (iter.hasNext()) { 67 i++; 68 assertEquals(iter.nextStatement().getSubject().getURI(), 69 "http://example.org/boolean"); 70 } 71 assertEquals(1, i); 72 } 73 74 public void testChar() { 75 StmtIterator iter = model.listStatements(null, null, charValue); 76 int i =0; 77 while (iter.hasNext()) { 78 i++; 79 assertEquals(iter.nextStatement().getSubject().getURI(), 80 "http://example.org/char"); 81 } 82 assertEquals(1, i); 83 } 84 85 public void testLong() { 86 StmtIterator iter = model.listStatements(null, null, longValue); 87 int i =0; 88 while (iter.hasNext()) { 89 i++; 90 assertEquals(iter.nextStatement().getSubject().getURI(), 91 "http://example.org/long"); 92 } 93 assertEquals(1, i); 94 } 95 96 public void testFloat() { 97 StmtIterator iter = model.listStatements(null, null, floatValue); 98 int i =0; 99 while (iter.hasNext()) { 100 i++; 101 assertEquals(iter.nextStatement().getSubject().getURI(), 102 "http://example.org/float"); 103 } 104 assertEquals(1, i); 105 } 106 107 public void testDouble() { 108 StmtIterator iter = model.listStatements(null, null, doubleValue); 109 int i =0; 110 while (iter.hasNext()) { 111 i++; 112 assertEquals(iter.nextStatement().getSubject().getURI(), 113 "http://example.org/double"); 114 } 115 assertEquals(1, i); 116 } 117 118 public void testString() { 119 StmtIterator iter = model.listStatements(null, null, stringValue); 120 int i =0; 121 while (iter.hasNext()) { 122 i++; 123 assertEquals(iter.nextStatement().getSubject().getURI(), 124 "http://example.org/string"); 125 } 126 assertEquals(1, i); 127 } 128 129 public void testLangString() { 130 StmtIterator iter = model.listStatements(null, null, 131 stringValue, langValue); 132 int i =0; 133 while (iter.hasNext()) { 134 i++; 135 assertEquals(iter.nextStatement().getSubject().getURI(), 136 "http://example.org/langString"); 137 } 138 assertEquals(1, i); 139 } 140 141 142 public void testAll() { 143 StmtIterator iter = model.listStatements(null, null, (RDFNode) null); 144 int i =0; 145 while (iter.hasNext()) { 146 i++; 147 iter.next(); 148 } 149 assertEquals(7, i); 150 } 151 152 153 public Model modelWithStatements( StmtIterator it ) 154 { 155 Model m = ModelFactory.createDefaultModel(); 156 while (it.hasNext()) m.add( it.nextStatement() ); 157 return m; 158 } 159 160 public void checkReturns( String things, StmtIterator it ) 161 { 162 Model wanted = modelWithStatements( things ); 163 Model got = modelWithStatements( it ); 164 if (wanted.isIsomorphicWith( got ) == false) 165 fail( "wanted " + wanted + " got " + got ); 166 } 167 168 public void testListStatementsSPO() 169 { 170 Model m = ModelFactory.createDefaultModel(); 171 Resource A = resource( m, "A" ), X = resource( m, "X" ); 172 Property P = property( m, "P" ), P1 = property( m, "P1" ); 173 RDFNode O = resource( m, "O" ), Y = resource( m, "Y" ); 174 String S1 = "S P O; S1 P O; S2 P O"; 175 String S2 = "A P1 B; A P1 B; A P1 C"; 176 String S3 = "X P1 Y; X P2 Y; X P3 Y"; 177 modelAdd( m, S1 ); 178 modelAdd( m, S2 ); 179 modelAdd( m, S3 ); 180 checkReturns( S1, m.listStatements( null, P, O ) ); 181 checkReturns( S2, m.listStatements( A, P1, (RDFNode) null ) ); 182 checkReturns( S3, m.listStatements( X, null, Y ) ); 183 m.close(); 184 } 185 186 public void testListStatementsClever() 187 { 188 Model m = ModelFactory.createDefaultModel(); 189 modelAdd( m, "S P O; S P O2; S P2 O; S2 P O" ); 190 Selector sel = new SimpleSelector( null, null, (RDFNode) null ) 191 { 192 public boolean test( Statement st ) 193 { return 194 st.getSubject().toString().length() 195 + st.getPredicate().toString().length() 196 + st.getObject().toString().length() 197 == 15; 198 } 199 200 public boolean isSimple() 201 { return false; } 202 }; 203 checkReturns( "S P O", m.listStatements( sel ) ); 204 } 205 } 206 207 208 237 | Popular Tags |