001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestContains.java,v 1.13 2008/01/02 12:04:41 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.test;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.rdf.model.*;
011: import com.hp.hpl.jena.rdf.model.impl.ModelCom;
012:
013: import junit.framework.*;
014:
015: /**
016: @author kers
017: */
018: public class TestContains extends ModelTestBase {
019: public TestContains(String name) {
020: super (name);
021: }
022:
023: public static TestSuite suite() {
024: return new TestSuite(TestContains.class);
025: }
026:
027: public void testContains(boolean yes, String facts, String resource) {
028: Model m = modelWithStatements(facts);
029: RDFNode r = rdfNode(m, resource);
030: if (modelWithStatements(facts).containsResource(r) != yes)
031: fail("[" + facts + "] should" + (yes ? "" : " not")
032: + " contain " + resource);
033: }
034:
035: public void testContains() {
036: testContains(false, "", "x");
037: testContains(false, "a R b", "x");
038: testContains(false, "a R b; c P d", "x");
039: /* */
040: testContains(false, "a R b", "z");
041: /* */
042: testContains(true, "x R y", "x");
043: testContains(true, "a P b", "P");
044: testContains(true, "i Q j", "j");
045: testContains(true, "x R y; a P b; i Q j", "y");
046: /* */
047: testContains(true, "x R y; a P b; i Q j", "y");
048: testContains(true, "x R y; a P b; i Q j", "R");
049: testContains(true, "x R y; a P b; i Q j", "a");
050: }
051:
052: private Resource res(String uri) {
053: return ResourceFactory.createResource("eh:/" + uri);
054: }
055:
056: private Property prop(String uri) {
057: return ResourceFactory.createProperty("eh:/" + uri);
058: }
059:
060: public void testContainsWithNull() {
061: testCWN(false, "", null, null, null);
062: testCWN(true, "x R y", null, null, null);
063: testCWN(false, "x R y", null, null, res("z"));
064: testCWN(true, "x RR y", res("x"), prop("RR"), null);
065: testCWN(true, "a BB c", null, prop("BB"), res("c"));
066: testCWN(false, "a BB c", null, prop("ZZ"), res("c"));
067: }
068:
069: public void testCWN(boolean yes, String facts, Resource S,
070: Property P, RDFNode O) {
071: assertEquals(yes, modelWithStatements(facts).contains(S, P, O));
072: }
073:
074: public void testModelComContainsSPcallsContainsSPO() {
075: Graph g = Factory.createDefaultGraph();
076: final boolean[] wasCalled = { false };
077: Model m = new ModelCom(g) {
078: public boolean contains(Resource s, Property p, RDFNode o) {
079: wasCalled[0] = true;
080: return super .contains(s, p, o);
081: }
082: };
083: assertFalse(m.contains(resource("r"), property("p")));
084: assertTrue("contains(S,P) should call contains(S,P,O)",
085: wasCalled[0]);
086: }
087: }
088:
089: /*
090: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
091: All rights reserved.
092:
093: Redistribution and use in source and binary forms, with or without
094: modification, are permitted provided that the following conditions
095: are met:
096:
097: 1. Redistributions of source code must retain the above copyright
098: notice, this list of conditions and the following disclaimer.
099:
100: 2. Redistributions in binary form must reproduce the above copyright
101: notice, this list of conditions and the following disclaimer in the
102: documentation and/or other materials provided with the distribution.
103:
104: 3. The name of the author may not be used to endorse or promote products
105: derived from this software without specific prior written permission.
106:
107: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
108: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
109: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
110: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
111: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
112: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
113: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
114: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
115: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
116: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
117: */
|