001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: <<<<<<< TestFindLiterals.java
005: $Id: TestFindLiterals.java,v 1.10 2008/01/02 12:05:33 andy_seaborne Exp $
006: =======
007: $Id: TestFindLiterals.java,v 1.10 2008/01/02 12:05:33 andy_seaborne Exp $
008: >>>>>>> 1.4
009: */
010:
011: package com.hp.hpl.jena.graph.test;
012:
013: import java.util.*;
014:
015: import com.hp.hpl.jena.graph.*;
016: import com.hp.hpl.jena.graph.impl.LiteralLabel;
017: import com.hp.hpl.jena.util.iterator.Map1;
018:
019: import junit.framework.TestSuite;
020:
021: public class TestFindLiterals extends GraphTestBase {
022: public TestFindLiterals(String name) {
023: super (name);
024: }
025:
026: static final Map1 getObject = new Map1() {
027: public Object map1(Object o) {
028: return ((Triple) o).getObject();
029: }
030: };
031:
032: public static TestFindLiterals aTest(final String graph,
033: final String size, final String search, final String results) {
034: return new TestFindLiterals("TestFindLiterals: graph {" + graph
035: + "} size " + size + " search " + search
036: + " expecting {" + results + "}") {
037: public void runBare() {
038: Graph g = graphWith(graph);
039: int n = Integer.parseInt(size);
040: Node literal = NodeCreateUtils.create(search);
041: //
042: assertEquals("graph has wrong size", n, g.size());
043: Set got = iteratorToSet(g.find(Node.ANY, Node.ANY,
044: literal).mapWith(getObject));
045: assertEquals(nodeSet(results), got);
046: }
047: };
048: }
049:
050: public static TestSuite suite() {
051: TestSuite result = new TestSuite(TestFindLiterals.class);
052: //
053: result.addTest(aTest("a P 'simple'", "1", "'simple'",
054: "'simple'"));
055: result.addTest(aTest("a P 'simple'xsd:string", "1", "'simple'",
056: "'simple'xsd:string"));
057: result.addTest(aTest("a P 'simple'", "1", "'simple'xsd:string",
058: "'simple'"));
059: result.addTest(aTest("a P 'simple'xsd:string", "1",
060: "'simple'xsd:string", "'simple'xsd:string"));
061: //
062: result.addTest(aTest("a P 'simple'; a P 'simple'xsd:string",
063: "2", "'simple'", "'simple' 'simple'xsd:string"));
064: result.addTest(aTest("a P 'simple'; a P 'simple'xsd:string",
065: "2", "'simple'xsd:string",
066: "'simple' 'simple'xsd:string"));
067: //
068: result.addTest(aTest("a P 1", "1", "1", "1"));
069: result.addTest(aTest("a P '1'xsd:float", "1", "'1'xsd:float",
070: "'1'xsd:float"));
071: result.addTest(aTest("a P '1'xsd:double", "1", "'1'xsd:double",
072: "'1'xsd:double"));
073: result.addTest(aTest("a P '1'xsd:float", "1", "'1'xsd:float",
074: "'1'xsd:float"));
075: result.addTest(aTest("a P '1.1'xsd:float", "1", "'1'xsd:float",
076: ""));
077: result
078: .addTest(aTest("a P '1'xsd:double", "1", "'1'xsd:int",
079: ""));
080: //
081: result.addTest(aTest("a P 'abc'rdf:XMLLiteral", "1", "'abc'",
082: ""));
083: result.addTest(aTest("a P 'abc'", "1", "'abc'rdf:XMLLiteral",
084: ""));
085: //
086: // floats & doubles are not compatible
087: //
088: result.addTest(aTest("a P '1'xsd:float", "1", "'1'xsd:double",
089: ""));
090: result.addTest(aTest("a P '1'xsd:double", "1", "'1'xsd:float",
091: ""));
092: //
093: result.addTest(aTest("a P 1", "1", "'1'", ""));
094: result.addTest(aTest("a P 1", "1", "'1'xsd:integer",
095: "'1'xsd:integer"));
096: result.addTest(aTest("a P 1", "1", "'1'", ""));
097: result.addTest(aTest("a P '1'xsd:short", "1", "'1'xsd:integer",
098: "'1'xsd:short"));
099: result.addTest(aTest("a P '1'xsd:int", "1", "'1'xsd:integer",
100: "'1'xsd:int"));
101: return result;
102: }
103:
104: public void testFloatVsDouble() {
105: Node A = NodeCreateUtils.create("'1'xsd:float");
106: Node B = NodeCreateUtils.create("'1'xsd:double");
107: assertFalse(A.equals(B));
108: assertFalse(A.sameValueAs(B));
109: assertFalse(B.sameValueAs(A));
110: assertFalse(A.matches(B));
111: assertFalse(B.matches(A));
112: }
113:
114: public void testProgrammaticValues() {
115: Node ab = Node.createLiteral(new LiteralLabel(new Byte(
116: (byte) 42)));
117: Node as = Node.createLiteral(new LiteralLabel(new Short(
118: (short) 42)));
119: Node ai = Node.createLiteral(new LiteralLabel(new Integer(42)));
120: Node al = Node.createLiteral(new LiteralLabel(new Long(42)));
121: Graph g = graphWith("");
122: Node SB = NodeCreateUtils.create("SB");
123: Node SS = NodeCreateUtils.create("SS");
124: Node SI = NodeCreateUtils.create("SI");
125: Node SL = NodeCreateUtils.create("SL");
126: Node P = NodeCreateUtils.create("P");
127: g.add(Triple.create(SB, P, ab));
128: g.add(Triple.create(SS, P, as));
129: g.add(Triple.create(SI, P, ai));
130: g.add(Triple.create(SL, P, al));
131: assertEquals(4, iteratorToSet(
132: g.find(Node.ANY, P, NodeCreateUtils.create("42")))
133: .size());
134: }
135: }
136:
137: /*
138: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
139: * All rights reserved.
140: *
141: * Redistribution and use in source and binary forms, with or without
142: * modification, are permitted provided that the following conditions
143: * are met:
144: * 1. Redistributions of source code must retain the above copyright
145: * notice, this list of conditions and the following disclaimer.
146: * 2. Redistributions in binary form must reproduce the above copyright
147: * notice, this list of conditions and the following disclaimer in the
148: * documentation and/or other materials provided with the distribution.
149: * 3. The name of the author may not be used to endorse or promote products
150: * derived from this software without specific prior written permission.
151: *
152: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
154: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
155: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
156: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
157: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
158: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
159: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
160: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
161: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
162: */
|