1 6 7 package com.hp.hpl.jena.rdf.model.test; 8 9 import com.hp.hpl.jena.rdf.model.*; 10 11 import java.util.*; 12 13 import junit.framework.*; 14 15 20 public class TestRDFNodes extends ModelTestBase 21 { 22 public TestRDFNodes(String name) 23 { super(name); } 24 25 public static TestSuite suite() 26 { return new TestSuite( TestRDFNodes.class ); } 27 28 public void testRDFVisitor() 29 { 30 final List strings = new ArrayList(); 31 Model m = ModelFactory.createDefaultModel(); 32 final RDFNode S = m.createResource(); 33 final RDFNode P = m.createProperty( "eh:PP" ); 34 final RDFNode O = m.createLiteral( "LL" ); 35 36 RDFVisitor rv = new RDFVisitor() 37 { 38 public Object visitBlank( Resource R, AnonId id ) 39 { 40 strings.add( "blank" ); 41 assertTrue( "must visit correct node", R == S ); 42 assertEquals( "must have correct field", R.getId(), id ); 43 return "blank result"; 44 } 45 public Object visitURI( Resource R, String uri ) 46 { 47 strings.add( "uri" ); 48 assertTrue( "must visit correct node", R == P ); 49 assertEquals( "must have correct field", R.getURI(), uri ); 50 return "uri result"; 51 } 52 public Object visitLiteral( Literal L ) 53 { 54 strings.add( "literal" ); 55 assertTrue( "must visit correct node", L == O ); 56 return "literal result"; 57 } 58 }; 59 60 assertEquals( "blank result", S.visitWith( rv ) ); 61 assertEquals( "uri result", P.visitWith( rv ) ); 62 assertEquals( "literal result", O.visitWith( rv ) ); 63 64 assertEquals( strings.get(0), "blank" ); 65 assertEquals( strings.get(1), "uri" ); 66 assertEquals( strings.get(2), "literal" ); 67 } 68 69 public void testRemoveAllRemoves() 70 { 71 String ps = "x P a; x P b", rest = "x Q c; y P a; y Q b"; 72 Model m = modelWithStatements( ps + "; " + rest ); 73 Resource r = resource( m, "x" ); 74 Resource r2 = r.removeAll( property( m, "P" ) ); 75 assertSame( "removeAll should deliver its receiver", r, r2 ); 76 assertIsoModels( "x's P-values should go", modelWithStatements( rest ), m ); 77 } 78 79 public void testRemoveAllBoring() 80 { 81 Model m1 = modelWithStatements( "x P a; y Q b" ); 82 Model m2 = modelWithStatements( "x P a; y Q b" ); 83 resource( m2, "x" ).removeAll( property( m2, "Z" ) ); 84 assertIsoModels( "m2 should be unchanged", m1, m2 ); 85 } 86 87 public void testInModel() 88 { 89 Model m1 = modelWithStatements( "" ); 90 Model m2 = modelWithStatements( "" ); 91 Resource r1 = resource( m1, "r1" ); 92 Resource r2 = resource( m1, "_r2" ); 93 94 assertTrue( r1.getModel() == m1 ); 95 assertTrue( r2.getModel() == m1 ); 96 assertFalse( r1.isAnon() ); 97 assertTrue( r2.isAnon() ); 98 99 assertTrue( ((Resource) r1.inModel( m2 )).getModel() == m2 ); 100 assertTrue( ((Resource) r2.inModel( m2 )).getModel() == m2 ); 101 102 assertEquals( r1, r1.inModel( m2 ) ); 103 assertEquals( r2, r2.inModel( m2 ) ); 104 } 105 } 106 107 | Popular Tags |