001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestSimpleListStatements.java,v 1.21 2008/01/02 12:04:44 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.test;
008:
009: /**
010: @author bwm out of kers
011: */
012:
013: import java.util.List;
014:
015: import com.hp.hpl.jena.rdf.model.*;
016: import com.hp.hpl.jena.vocabulary.*;
017:
018: import junit.framework.*;
019:
020: public class TestSimpleListStatements extends ModelTestBase {
021:
022: public TestSimpleListStatements(String name) {
023: super (name);
024: }
025:
026: public static TestSuite suite() {
027: return new TestSuite(TestSimpleListStatements.class);
028: }
029:
030: Model model = null;
031:
032: static boolean booleanValue = true;
033: static char charValue = 'c';
034: static long longValue = 456;
035: static float floatValue = 5.67F;
036: static double doubleValue = 6.78;
037: static String stringValue = "stringValue";
038: static String langValue = "en";
039:
040: protected void setUp() throws java.lang.Exception {
041:
042: model = ModelFactory.createDefaultModel();
043: model.createResource("http://example.org/boolean").addLiteral(
044: RDF.value, booleanValue);
045: model.createResource("http://example.org/char").addLiteral(
046: RDF.value, charValue);
047: model.createResource("http://example.org/long").addLiteral(
048: RDF.value, longValue);
049: model.createResource("http://example.org/float").addLiteral(
050: RDF.value, floatValue);
051: model.createResource("http://example.org/double").addLiteral(
052: RDF.value, doubleValue);
053: model.createResource("http://example.org/string").addProperty(
054: RDF.value, stringValue);
055: model.createResource("http://example.org/langString")
056: .addProperty(RDF.value, stringValue, langValue);
057:
058: }
059:
060: protected void tearDown() throws java.lang.Exception {
061: model.close();
062: model = null;
063: }
064:
065: public void testBoolean() {
066: List got = model
067: .listLiteralStatements(null, null, booleanValue)
068: .toList();
069: assertEquals(1, got.size());
070: Statement it = (Statement) got.get(0);
071: assertEquals(resource("http://example.org/boolean"), it
072: .getSubject());
073: assertEquals(model.createTypedLiteral(booleanValue), it
074: .getObject());
075: }
076:
077: public void testChar() {
078: List got = model.listLiteralStatements(null, null, charValue)
079: .toList();
080: assertEquals(1, got.size());
081: Statement it = (Statement) got.get(0);
082: assertEquals(resource("http://example.org/char"), it
083: .getSubject());
084: assertEquals(model.createTypedLiteral(charValue), it
085: .getObject());
086: }
087:
088: public void testLong() {
089: List got = model.listLiteralStatements(null, null, longValue)
090: .toList();
091: assertEquals(1, got.size());
092: Statement it = (Statement) got.get(0);
093: assertEquals(resource("http://example.org/long"), it
094: .getSubject());
095: assertEquals(model.createTypedLiteral(longValue), it
096: .getObject());
097: }
098:
099: public void testFloat() {
100: List got = model.listlLiteralStatements(null, null, floatValue)
101: .toList();
102: assertEquals(1, got.size());
103: Statement it = (Statement) got.get(0);
104: assertEquals(resource("http://example.org/float"), it
105: .getSubject());
106: assertEquals(model.createTypedLiteral(floatValue), it
107: .getObject());
108: }
109:
110: public void testDouble() {
111: List got = model.listLiteralStatements(null, null, doubleValue)
112: .toList();
113: assertEquals(1, got.size());
114: Statement it = (Statement) got.get(0);
115: assertEquals(resource("http://example.org/double"), it
116: .getSubject());
117: assertEquals(model.createTypedLiteral(doubleValue), it
118: .getObject());
119: }
120:
121: public void testString() {
122: StmtIterator iter = model.listStatements(null, null,
123: stringValue);
124: int i = 0;
125: while (iter.hasNext()) {
126: i++;
127: assertEquals(iter.nextStatement().getSubject().getURI(),
128: "http://example.org/string");
129: }
130: assertEquals(1, i);
131: }
132:
133: public void testLangString() {
134: StmtIterator iter = model.listStatements(null, null,
135: stringValue, langValue);
136: int i = 0;
137: while (iter.hasNext()) {
138: i++;
139: assertEquals(iter.nextStatement().getSubject().getURI(),
140: "http://example.org/langString");
141: }
142: assertEquals(1, i);
143: }
144:
145: public void testAll() {
146: StmtIterator iter = model.listStatements(null, null,
147: (RDFNode) null);
148: int i = 0;
149: while (iter.hasNext()) {
150: i++;
151: iter.next();
152: }
153: assertEquals(7, i);
154: }
155:
156: public void testAllString() {
157: StmtIterator iter = model.listStatements(null, null,
158: (String) null);
159: int i = 0;
160: while (iter.hasNext()) {
161: i++;
162: iter.next();
163: }
164: assertEquals(7, i);
165: }
166:
167: public Model modelWithStatements(StmtIterator it) {
168: Model m = ModelFactory.createDefaultModel();
169: while (it.hasNext())
170: m.add(it.nextStatement());
171: return m;
172: }
173:
174: public void checkReturns(String things, StmtIterator it) {
175: Model wanted = modelWithStatements(things);
176: Model got = modelWithStatements(it);
177: if (wanted.isIsomorphicWith(got) == false)
178: fail("wanted " + wanted + " got " + got);
179: }
180:
181: public void testListStatementsSPO() {
182: Model m = ModelFactory.createDefaultModel();
183: Resource A = resource(m, "A"), X = resource(m, "X");
184: Property P = property(m, "P"), P1 = property(m, "P1");
185: RDFNode O = resource(m, "O"), Y = resource(m, "Y");
186: String S1 = "S P O; S1 P O; S2 P O";
187: String S2 = "A P1 B; A P1 B; A P1 C";
188: String S3 = "X P1 Y; X P2 Y; X P3 Y";
189: modelAdd(m, S1);
190: modelAdd(m, S2);
191: modelAdd(m, S3);
192: checkReturns(S1, m.listStatements(null, P, O));
193: checkReturns(S2, m.listStatements(A, P1, (RDFNode) null));
194: checkReturns(S3, m.listStatements(X, null, Y));
195: m.close();
196: }
197:
198: public void testListStatementsClever() {
199: Model m = ModelFactory.createDefaultModel();
200: modelAdd(m, "S P O; S P O2; S P2 O; S2 P O");
201: Selector sel = new SimpleSelector(null, null, (RDFNode) null) {
202: public boolean test(Statement st) {
203: return st.getSubject().toString().length()
204: + st.getPredicate().toString().length()
205: + st.getObject().toString().length() == 15; /* eh:/S + eh:/P + eh:/O */
206: }
207:
208: public boolean isSimple() {
209: return false;
210: }
211: };
212: checkReturns("S P O", m.listStatements(sel));
213: }
214: }
215:
216: /*
217: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
218: All rights reserved.
219:
220: Redistribution and use in source and binary forms, with or without
221: modification, are permitted provided that the following conditions
222: are met:
223:
224: 1. Redistributions of source code must retain the above copyright
225: notice, this list of conditions and the following disclaimer.
226:
227: 2. Redistributions in binary form must reproduce the above copyright
228: notice, this list of conditions and the following disclaimer in the
229: documentation and/or other materials provided with the distribution.
230:
231: 3. The name of the author may not be used to endorse or promote products
232: derived from this software without specific prior written permission.
233:
234: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
235: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
236: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
237: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
238: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
239: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
240: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
243: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
244: */
|