001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: NewRegressionListSubjects.java,v 1.11 2008/01/02 12:07:04 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.regression;
008:
009: import java.util.*;
010:
011: import junit.framework.*;
012:
013: import com.hp.hpl.jena.rdf.model.*;
014: import com.hp.hpl.jena.rdf.model.test.ModelTestBase;
015: import com.hp.hpl.jena.regression.Regression.*;
016: import com.hp.hpl.jena.shared.PropertyNotFoundException;
017: import com.hp.hpl.jena.vocabulary.RDF;
018:
019: public class NewRegressionListSubjects extends ModelTestBase {
020: public NewRegressionListSubjects(String name) {
021: super (name);
022: }
023:
024: public static Test suite() {
025: return new TestSuite(NewRegressionListSubjects.class);
026: }
027:
028: protected Model getModel() {
029: return ModelFactory.createDefaultModel();
030: }
031:
032: protected Model m;
033:
034: public void setUp() {
035: m = getModel();
036: fillModel();
037: }
038:
039: public void tearDown() {
040: m = null;
041: }
042:
043: static final String subjectPrefix = "http://aldabaran/test8/s";
044:
045: static final String predicatePrefix = "http://aldabaran/test8/";
046:
047: Resource[] subjects;
048: Property[] predicates;
049: RDFNode[] objects;
050: Literal[] tvLitObjs;
051: Resource[] tvResObjs;
052:
053: boolean[] tvBooleans = { false, true };
054: long[] tvLongs = { 123, 321 };
055: char[] tvChars = { '@', ';' };
056: float[] tvFloats = { 456.789f, 789.456f };
057: double[] tvDoubles = { 123.456, 456.123 };
058: String[] tvStrings = { "test8 testing string 1",
059: "test8 testing string 2" };
060: String[] langs = { "en", "fr" };
061:
062: protected Set subjectsTo(String prefix, int limit) {
063: Set result = new HashSet();
064: for (int i = 0; i < limit; i += 1)
065: result.add(resource(prefix + i));
066: return result;
067: }
068:
069: public void testListResourcesOnObject() {
070: Object d = new Date();
071: Model m = modelWithStatements("");
072: m.addLiteral(resource("S"), property("P"), d);
073: m.addLiteral(resource("X"), property("P"), new Object());
074: List answers = m.listResourcesWithProperty(property("P"), d)
075: .toList();
076: assertEquals(listOfOne(resource("S")), answers);
077: }
078:
079: public void test8() {
080: assertEquiv(subjectsTo(subjectPrefix, 5), m
081: .listResourcesWithProperty(predicates[4]));
082:
083: assertEquiv(subjectsTo(subjectPrefix, 2), m
084: .listResourcesWithProperty(predicates[0]));
085:
086: assertEquiv(subjectsTo(subjectPrefix, 2),
087: m.listResourcesWithProperty(predicates[0],
088: tvBooleans[0]));
089:
090: assertEquiv(subjectsTo(subjectPrefix, 0),
091: m.listResourcesWithProperty(predicates[0],
092: tvBooleans[1]));
093:
094: assertEquiv(subjectsTo(subjectPrefix, 2), m
095: .listResourcesWithProperty(predicates[0], tvChars[0]));
096:
097: assertEquiv(subjectsTo(subjectPrefix, 0), m
098: .listResourcesWithProperty(predicates[0], tvChars[1]));
099:
100: assertEquiv(subjectsTo(subjectPrefix, 2), m
101: .listResourcesWithProperty(predicates[0], tvLongs[0]));
102:
103: assertEquiv(subjectsTo(subjectPrefix, 0), m
104: .listResourcesWithProperty(predicates[0], tvLongs[1]));
105:
106: assertEquiv(subjectsTo(subjectPrefix, 2), m
107: .listResourcesWithProperty(predicates[0], tvFloats[0]));
108:
109: assertEquiv(subjectsTo(subjectPrefix, 0), m
110: .listResourcesWithProperty(predicates[0], tvFloats[1]));
111:
112: assertEquiv(subjectsTo(subjectPrefix, 2), m
113: .listResourcesWithProperty(predicates[0], tvDoubles[0]));
114:
115: assertEquiv(subjectsTo(subjectPrefix, 0), m
116: .listResourcesWithProperty(predicates[0], tvDoubles[1]));
117:
118: assertEquiv(subjectsTo(subjectPrefix, 2), m
119: .listResourcesWithProperty(predicates[0],
120: (byte) tvLongs[0]));
121:
122: assertEquiv(subjectsTo(subjectPrefix, 0), m
123: .listResourcesWithProperty(predicates[0],
124: (byte) tvLongs[1]));
125:
126: assertEquiv(subjectsTo(subjectPrefix, 2), m
127: .listResourcesWithProperty(predicates[0],
128: (short) tvLongs[0]));
129:
130: assertEquiv(subjectsTo(subjectPrefix, 0), m
131: .listResourcesWithProperty(predicates[0],
132: (short) tvLongs[1]));
133:
134: assertEquiv(subjectsTo(subjectPrefix, 2), m
135: .listResourcesWithProperty(predicates[0],
136: (int) tvLongs[0]));
137:
138: assertEquiv(subjectsTo(subjectPrefix, 0), m
139: .listResourcesWithProperty(predicates[0],
140: (int) tvLongs[1]));
141:
142: assertEquiv(subjectsTo(subjectPrefix, 2), m
143: .listSubjectsWithProperty(predicates[0], tvStrings[0]));
144:
145: assertEquiv(subjectsTo(subjectPrefix, 0), m
146: .listSubjectsWithProperty(predicates[0], tvStrings[1]));
147:
148: assertEquiv(subjectsTo(subjectPrefix, 2), m
149: .listSubjectsWithProperty(predicates[0], tvStrings[0],
150: langs[0]));
151:
152: assertEquiv(subjectsTo(subjectPrefix, 0), m
153: .listSubjectsWithProperty(predicates[0], tvStrings[1],
154: langs[0]));
155:
156: assertEquiv(subjectsTo(subjectPrefix, 0), m
157: .listSubjectsWithProperty(predicates[0], tvStrings[0],
158: langs[1]));
159:
160: assertEquiv(subjectsTo(subjectPrefix, 0), m
161: .listSubjectsWithProperty(predicates[0], tvStrings[1],
162: langs[1]));
163:
164: assertEquiv(subjectsTo(subjectPrefix, 2), m
165: .listResourcesWithProperty(predicates[0], tvLitObjs[0]));
166:
167: assertEquiv(subjectsTo(subjectPrefix, 0), m
168: .listResourcesWithProperty(predicates[0], tvLitObjs[1]));
169:
170: assertEquiv(subjectsTo(subjectPrefix, 2), m
171: .listResourcesWithProperty(predicates[0], tvResObjs[0]));
172:
173: assertEquiv(subjectsTo(subjectPrefix, 0), m
174: .listResourcesWithProperty(predicates[0], tvResObjs[1]));
175:
176: // assertEquiv( new HashSet( Arrays.asList( objects ) ), m.listObjectsOfProperty( predicates[1] ) );
177: }
178:
179: protected void assertEquiv(Set set, Iterator iterator) {
180: List L = iteratorToList(iterator);
181: assertEquals(set.size(), L.size());
182: assertEquals(set, new HashSet(L));
183: }
184:
185: public void testGetRequiredProperty() {
186: Statement s = m.getRequiredProperty(subjects[1], predicates[1]);
187: try {
188: m.getRequiredProperty(subjects[1], RDF.value);
189: fail("should not find absent property");
190: } catch (PropertyNotFoundException e) {
191: pass();
192: }
193: }
194:
195: protected void fillModel() {
196: final int num = 5;
197: tvLitObjs = new Literal[] {
198: m.createTypedLiteral(new LitTestObjF()),
199: m.createTypedLiteral(new LitTestObjF()) };
200:
201: tvResObjs = new Resource[] {
202: m.createResource(new ResTestObjF()),
203: m.createResource(new ResTestObjF()) };
204:
205: objects = new RDFNode[] { m.createTypedLiteral(tvBooleans[1]),
206: m.createTypedLiteral(tvLongs[1]),
207: m.createTypedLiteral(tvChars[1]),
208: m.createTypedLiteral(tvFloats[1]),
209: m.createTypedLiteral(tvDoubles[1]),
210: m.createLiteral(tvStrings[1]),
211: m.createLiteral(tvStrings[1], langs[1]), tvLitObjs[1],
212: tvResObjs[1] };
213:
214: subjects = new Resource[num];
215: predicates = new Property[num];
216:
217: for (int i = 0; i < num; i++) {
218: subjects[i] = m.createResource(subjectPrefix + i);
219: predicates[i] = m.createProperty(predicatePrefix + i, "p");
220: }
221:
222: for (int i = 0; i < num; i += 1)
223: m.addLiteral(subjects[i], predicates[4], false);
224:
225: for (int i = 0; i < 2; i += 1) {
226: for (int j = 0; j < 2; j += 1) {
227: m.add(subjects[i], predicates[j], m
228: .createTypedLiteral(tvBooleans[j]));
229: m.addLiteral(subjects[i], predicates[j], tvLongs[j]);
230: m.addLiteral(subjects[i], predicates[j], tvChars[j]);
231: m.add(subjects[i], predicates[j], m
232: .createTypedLiteral(tvFloats[j]));
233: m.add(subjects[i], predicates[j], m
234: .createTypedLiteral(tvDoubles[j]));
235: m.add(subjects[i], predicates[j], tvStrings[j]);
236: m.add(subjects[i], predicates[j], tvStrings[j],
237: langs[j]);
238: m.add(subjects[i], predicates[j], tvLitObjs[j]);
239: m.add(subjects[i], predicates[j], tvResObjs[j]);
240: }
241: }
242: }
243: }
244:
245: /*
246: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
247: * All rights reserved.
248: *
249: * Redistribution and use in source and binary forms, with or without
250: * modification, are permitted provided that the following conditions
251: * are met:
252: * 1. Redistributions of source code must retain the above copyright
253: * notice, this list of conditions and the following disclaimer.
254: * 2. Redistributions in binary form must reproduce the above copyright
255: * notice, this list of conditions and the following disclaimer in the
256: * documentation and/or other materials provided with the distribution.
257: * 3. The name of the author may not be used to endorse or promote products
258: * derived from this software without specific prior written permission.
259: *
260: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
261: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
262: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
263: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
264: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
265: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
266: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
267: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270: */
|