001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestQueryNode.java,v 1.10 2008/01/02 12:08:56 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.graph.query.test;
007:
008: import java.util.HashSet;
009: import java.util.Set;
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: import junit.framework.TestSuite;
016:
017: public class TestQueryNode extends QueryTestBase {
018: public TestQueryNode(String name) {
019: super (name);
020: }
021:
022: public static TestSuite suite() {
023: return new TestSuite(TestQueryNode.class);
024: }
025:
026: public static final QueryNodeFactory F = QueryNode.factory;
027:
028: public void testNoIndex() {
029: assertTrue(QueryNode.NO_INDEX < 0);
030: }
031:
032: public void testFixed() {
033: Node fixed = NodeCreateUtils.create("fixed");
034: QueryNode n = new QueryNode.Fixed(fixed);
035: assertSame(fixed, n.node);
036: assertEquals(QueryNode.NO_INDEX, n.index);
037: assertEquals(false, n.mustMatch());
038: assertSame(fixed, n.finder(null));
039: }
040:
041: public void testBind() {
042: final int index = 7;
043: Node bind = NodeCreateUtils.create("?bind");
044: QueryNode n = new QueryNode.Bind(bind, index);
045: assertSame(bind, n.node);
046: assertEquals(index, n.index);
047: assertEquals(true, n.mustMatch());
048: assertSame(Node.ANY, n.finder(null));
049: }
050:
051: public void testBound() {
052: testBoundAt(0);
053: testBoundAt(3);
054: testBoundAt(7);
055: }
056:
057: protected void testBoundAt(final int index) {
058: Node bound = NodeCreateUtils.create("?bound");
059: QueryNode n = new QueryNode.Bound(bound, index);
060: assertSame(bound, n.node);
061: assertEquals(index, n.index);
062: assertEquals(false, n.mustMatch());
063: Domain d = new Domain(index + 1);
064: Node item = NodeCreateUtils.create("'anItem'");
065: d.setElement(index, item);
066: assertSame(item, n.finder(d));
067: }
068:
069: public void testJustBound() {
070: final int index = 1;
071: Node just = NodeCreateUtils.create("?jBound");
072: QueryNode n = new QueryNode.JustBound(just, index);
073: assertSame(just, n.node);
074: assertEquals(index, n.index);
075: assertEquals(true, n.mustMatch());
076: assertEquals(Node.ANY, n.finder(null));
077: }
078:
079: public void testAny() {
080: QueryNode n = new QueryNode.Any();
081: assertSame(Node.ANY, n.node);
082: assertEquals(QueryNode.NO_INDEX, n.index);
083: assertEquals(false, n.mustMatch());
084: assertSame(Node.ANY, n.finder(null));
085: }
086:
087: public void testClassifyFixed() {
088: Node fixed = NodeCreateUtils.create("someURI");
089: QueryNode n = QueryNode.classify(F, null, null, fixed);
090: assertInstanceOf(QueryNode.Fixed.class, n);
091: assertEquals(QueryNode.NO_INDEX, n.index);
092: assertSame(fixed, n.node);
093: }
094:
095: public void testClassifyAny() {
096: QueryNode n = QueryNode.classify(F, null, null, Node.ANY);
097: assertInstanceOf(QueryNode.Any.class, n);
098: assertEquals(QueryNode.NO_INDEX, n.index);
099: assertSame(Node.ANY, n.node);
100: }
101:
102: public void testClassifyFirstBind() {
103: Mapping m = new Mapping(new Node[0]);
104: testClassifyBind(NodeCreateUtils.create("?bind"), m, 0);
105: }
106:
107: public void testClassifySecondBind() {
108: Mapping m = new Mapping(new Node[0]);
109: m.newIndex(NodeCreateUtils.create("?other"));
110: testClassifyBind(NodeCreateUtils.create("?bind"), m, 1);
111: }
112:
113: protected void testClassifyBind(Node bind, Mapping m, int index) {
114: QueryNode n = QueryNode.classify(F, m, new HashSet(), bind);
115: assertInstanceOf(QueryNode.Bind.class, n);
116: assertSame(n.node, bind);
117: assertEquals(index, n.index);
118: }
119:
120: public void testClassifyBound() {
121: testClassifyBound(0);
122: testClassifyBound(1);
123: testClassifyBound(4);
124: testClassifyBound(17);
125: }
126:
127: protected void testClassifyBound(int index) {
128: Node bound = NodeCreateUtils.create("?bound");
129: Mapping m = getPreloadedMapping(index);
130: m.newIndex(bound);
131: QueryNode n = QueryNode.classify(F, m, new HashSet(), bound);
132: assertInstanceOf(QueryNode.Bound.class, n);
133: assertSame(n.node, bound);
134: assertEquals(index, n.index);
135: }
136:
137: public void testClassifyJustBound() {
138: testClassifyJustBound(0);
139: testClassifyJustBound(1);
140: testClassifyJustBound(17);
141: testClassifyJustBound(42);
142: }
143:
144: protected void testClassifyJustBound(int index) {
145: Node recent = NodeCreateUtils.create("?recent");
146: Mapping m = getPreloadedMapping(index);
147: m.newIndex(recent);
148: Set withRecent = new HashSet();
149: withRecent.add(recent);
150: QueryNode n = QueryNode.classify(F, m, withRecent, recent);
151: assertInstanceOf(QueryNode.JustBound.class, n);
152: assertSame(recent, n.node);
153: assertEquals(index, n.index);
154: }
155:
156: public void testBindingSetsJustBound() {
157: Node X = NodeCreateUtils.create("?X");
158: Mapping m = getPreloadedMapping(0);
159: Set s = new HashSet();
160: QueryNode n = QueryNode.classify(F, m, s, X);
161: assertTrue(s.contains(X));
162: }
163:
164: public void testBindingSetsJustBoundTwice() {
165: Node X = NodeCreateUtils.create("?X"), Y = NodeCreateUtils
166: .create("?Y");
167: Mapping m = getPreloadedMapping(0);
168: Set s = new HashSet();
169: QueryNode.classify(F, m, s, X);
170: QueryNode.classify(F, m, s, Y);
171: assertTrue(s.contains(X));
172: assertTrue(s.contains(Y));
173: }
174:
175: protected Mapping getPreloadedMapping(int count) {
176: Mapping m = new Mapping(new Node[0]);
177: for (int i = 0; i < count; i += 1)
178: m.newIndex(NodeCreateUtils.create("?bound-" + i));
179: return m;
180: }
181:
182: public void testMatchFixed() {
183: Node fixed = NodeCreateUtils.create("_anon");
184: QueryNode n = new QueryNode.Fixed(fixed);
185: try {
186: n.match(null, NodeCreateUtils.create("named"));
187: fail("Fixed should not be matching");
188: } catch (QueryNode.MustNotMatchException e) {
189: pass();
190: }
191: }
192:
193: public void testMatchBound() {
194: Node bound = NodeCreateUtils.create("?xx");
195: QueryNode n = new QueryNode.Bound(bound, 1);
196: try {
197: n.match(null, NodeCreateUtils.create("_anon"));
198: fail("Bound should not be matching");
199: } catch (QueryNode.MustNotMatchException e) {
200: pass();
201: }
202: }
203:
204: public void testMatchAny() {
205: QueryNode n = new QueryNode.Any();
206: try {
207: n.match(null, NodeCreateUtils.create("17"));
208: fail("Any should not be matching");
209: } catch (QueryNode.MustNotMatchException e) {
210: pass();
211: }
212: }
213:
214: public void testMatchBind() {
215: Node v = NodeCreateUtils.create("?v");
216: Node x = NodeCreateUtils.create("elephant"), y = NodeCreateUtils
217: .create("hedgehog");
218: QueryNode n = new QueryNode.Bind(v, 1);
219: Domain d = new Domain(3);
220: assertTrue(n.match(d, x));
221: assertSame(x, d.getElement(n.index));
222: assertTrue(n.match(d, y));
223: assertSame(y, d.getElement(n.index));
224: }
225:
226: public void testMatchJustBound() {
227: Node v = NodeCreateUtils.create("?who");
228: Node A = NodeCreateUtils.create("A"), B = NodeCreateUtils
229: .create("B");
230: QueryNode n = new QueryNode.JustBound(v, 1);
231: Domain d = new Domain(3);
232: d.setElement(n.index, A);
233: assertTrue(n.match(d, A));
234: assertFalse(n.match(d, B));
235: d.setElement(n.index, B);
236: assertFalse(n.match(d, A));
237: assertTrue(n.match(d, B));
238: }
239:
240: }
241: /*
242: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
243: * All rights reserved.
244: *
245: * Redistribution and use in source and binary forms, with or without
246: * modification, are permitted provided that the following conditions
247: * are met:
248: * 1. Redistributions of source code must retain the above copyright
249: * notice, this list of conditions and the following disclaimer.
250: * 2. Redistributions in binary form must reproduce the above copyright
251: * notice, this list of conditions and the following disclaimer in the
252: * documentation and/or other materials provided with the distribution.
253: * 3. The name of the author may not be used to endorse or promote products
254: * derived from this software without specific prior written permission.
255: *
256: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
257: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
258: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
259: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
260: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
261: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
262: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
263: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
265: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266: */
|