001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: TestGraphExtract.java,v 1.9 2008/01/02 12:05:34 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.test;
008:
009: import com.hp.hpl.jena.graph.*;
010:
011: import junit.framework.TestSuite;
012:
013: /**
014: Tests for recursive sub-graph extraction.
015: @author hedgehog
016: */
017: public class TestGraphExtract extends GraphTestBase {
018: public TestGraphExtract(String name) {
019: super (name);
020: }
021:
022: public static TestSuite suite() {
023: return new TestSuite(TestGraphExtract.class);
024: }
025:
026: public void testExtractNothing() {
027: testExtract("", "x", "");
028: testExtract("", "x", "a R b");
029: testExtract("", "x", "a R x");
030: testExtract("", "x", "a x y");
031: }
032:
033: public void testExtractOneLevel() {
034: testExtract("a R b", "a", "a R b");
035: testExtract("a R b; a R c", "a", "a R b; a R c");
036: testExtract("a R b; a S d", "a", "a R b; a S d");
037: }
038:
039: public void testNoJunk() {
040: testExtract("a R b", "a", "a R b; x R y");
041: }
042:
043: public void testExtractTwoLevels() {
044: testExtract("a R b; b S c", "a", "a R b; b S c");
045: testExtract("a R b; b S c", "a", "a R b; b S c; x P y");
046: testExtract("a R b; b S c; b T d", "a", "a R b; b S c; b T d");
047: testExtract("a R b; b S c; a T d", "a", "a R b; a T d; b S c");
048: }
049:
050: public void testExtractSeveralLevels() {
051: testExtract("a R b; b S c; c T d; d U e", "a",
052: "a R b; b S c; c T d; d U e");
053: }
054:
055: public void testExtractNoLoop() {
056: testExtract("a R a", "a", "a R a");
057: testExtract("a R b; b R a", "a", "a R b; b R a; z P a");
058: testExtract("a R b; b S c; c T a", "a",
059: "a R b; b S c; c T a; junk P junk");
060: }
061:
062: public void testTripleFilter() {
063: assertTrue(TripleBoundary.stopAtAnonObject
064: .stopAt(triple("a R _b")));
065: assertFalse(TripleBoundary.stopAtAnonObject
066: .stopAt(triple("a R b")));
067: assertFalse(TripleBoundary.stopAtAnonObject
068: .stopAt(triple("a _R b")));
069: assertFalse(TripleBoundary.stopAtAnonObject
070: .stopAt(triple("_a R b")));
071: }
072:
073: public void testExtractBoundary() {
074: testExtract("a R b; b S _c", "a", "a R b; b S _c; _c T d",
075: TripleBoundary.stopAtAnonObject);
076: }
077:
078: /**
079: This test exposed that the update-existing-graph functionality was broken
080: if the target graph already contained any statements with a subject S
081: appearing as subject in the source graph - no further Spo statements were
082: added.
083: */
084: public void testPartialUpdate() {
085: Graph source = graphWith("a R b; b S e");
086: Graph dest = graphWith("b R d");
087: GraphExtract e = new GraphExtract(TripleBoundary.stopNowhere);
088: e.extractInto(dest, node("a"), source);
089: assertIsomorphic(graphWith("a R b; b S e; b R d"), dest);
090: }
091:
092: public void testExtract(String wanted, String node, String source) {
093: testExtract(wanted, node, source, TripleBoundary.stopNowhere);
094: }
095:
096: /**
097: */
098: private void testExtract(String wanted, String node, String source,
099: TripleBoundary b) {
100: assertIsomorphic(graphWith(wanted), extract(node(node), b,
101: graphWith(source)));
102: }
103:
104: public Graph extract(Node node, TripleBoundary b, Graph graph) {
105: return new GraphExtract(b).extract(node, graph);
106: }
107:
108: }
109:
110: /*
111: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
112: All rights reserved.
113:
114: Redistribution and use in source and binary forms, with or without
115: modification, are permitted provided that the following conditions
116: are met:
117:
118: 1. Redistributions of source code must retain the above copyright
119: notice, this list of conditions and the following disclaimer.
120:
121: 2. Redistributions in binary form must reproduce the above copyright
122: notice, this list of conditions and the following disclaimer in the
123: documentation and/or other materials provided with the distribution.
124:
125: 3. The name of the author may not be used to endorse or promote products
126: derived from this software without specific prior written permission.
127:
128: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
129: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
130: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
131: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
132: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
133: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
134: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
135: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
136: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
137: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
138: */
|