001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestStageElements.java,v 1.5 2008/01/02 12:08:56 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query.test;
008:
009: import junit.framework.TestSuite;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.graph.query.*;
013: import com.hp.hpl.jena.graph.test.NodeCreateUtils;
014:
015: public class TestStageElements extends QueryTestBase {
016: public TestStageElements(String name) {
017: super (name);
018: }
019:
020: public static TestSuite suite() {
021: return new TestSuite(TestStageElements.class);
022: }
023:
024: protected static final class MockElement extends StageElement {
025: public boolean wasRun = false;
026:
027: public Domain lastDomain = null;
028:
029: public void run(Domain current) {
030: wasRun = true;
031: lastDomain = current;
032: }
033: }
034:
035: protected static final class ControlledValuator extends ValuatorSet {
036: protected boolean result;
037:
038: public ControlledValuator(boolean result) {
039: this .result = result;
040: }
041:
042: public boolean evalBool(IndexValues vv) {
043: return result;
044: }
045: }
046:
047: protected static final class Pipelet implements Pipe {
048: public Domain lastPut;
049:
050: public boolean hasNext() {
051: return false;
052: }
053:
054: public Domain get() {
055: return null;
056: }
057:
058: public void put(Domain d) {
059: lastPut = d;
060: }
061:
062: public void close() {
063: }
064:
065: public void close(Exception e) {
066: }
067: }
068:
069: protected Pipelet p = new Pipelet();
070:
071: public void testPutBindings() {
072: StageElement.PutBindings b = new StageElement.PutBindings(p);
073: Domain d = makeDomain(Node.ANY);
074: b.run(d);
075: assertEquals(d, p.lastPut);
076: assertNotSame(d, p.lastPut);
077: }
078:
079: public void testPutBindingsTwice() {
080: StageElement.PutBindings b = new StageElement.PutBindings(p);
081: Domain d1 = makeDomain(Node.ANY);
082: Domain d2 = makeDomain(NodeCreateUtils.create("_blank"),
083: NodeCreateUtils.create("17"));
084: b.run(d1);
085: b.run(d2);
086: assertEquals(d2, p.lastPut);
087: assertNotSame(d2, p.lastPut);
088: }
089:
090: public void testValuatorStageTrue() {
091: ValuatorSet vs = new ControlledValuator(true);
092: MockElement next = new MockElement();
093: StageElement.RunValuatorSet r = new StageElement.RunValuatorSet(
094: vs, next);
095: Domain d = makeDomain(NodeCreateUtils.create("_blank"),
096: NodeCreateUtils.create("17"));
097: r.run(d);
098: assertTrue(next.wasRun);
099: assertSame(d, next.lastDomain);
100: }
101:
102: public void testValuatorStageFalse() {
103: ValuatorSet vs = new ControlledValuator(false);
104: MockElement next = new MockElement();
105: StageElement.RunValuatorSet r = new StageElement.RunValuatorSet(
106: vs, next);
107: Domain d = makeDomain(NodeCreateUtils.create("_blank"),
108: NodeCreateUtils.create("17"));
109: r.run(d);
110: assertFalse(next.wasRun);
111: assertNull(next.lastDomain);
112: }
113:
114: protected Domain makeDomain(Node X) {
115: return makeDomain(X, null);
116: }
117:
118: protected Domain makeDomain(Node X, Node Y) {
119: Domain d = new Domain(3);
120: d.setElement(1, X);
121: d.setElement(2, Y);
122: return d;
123: }
124: }
125:
126: /*
127: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
128: * All rights reserved.
129: *
130: * Redistribution and use in source and binary forms, with or without
131: * modification, are permitted provided that the following conditions
132: * are met:
133: * 1. Redistributions of source code must retain the above copyright
134: * notice, this list of conditions and the following disclaimer.
135: * 2. Redistributions in binary form must reproduce the above copyright
136: * notice, this list of conditions and the following disclaimer in the
137: * documentation and/or other materials provided with the distribution.
138: * 3. The name of the author may not be used to endorse or promote products
139: * derived from this software without specific prior written permission.
140: *
141: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
142: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
143: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
144: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
145: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
146: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
147: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
148: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
149: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
150: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
151: */
|