001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: NewRegressionObjects.java,v 1.6 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.vocabulary.RDF;
016:
017: public class NewRegressionObjects extends ModelTestBase {
018: public NewRegressionObjects(String name) {
019: super (name);
020: }
021:
022: public static TestSuite suite() {
023: return new TestSuite(NewRegressionObjects.class);
024: }
025:
026: protected Model getModel() {
027: return ModelFactory.createDefaultModel();
028: }
029:
030: protected Model m;
031: protected Resource S;
032: protected Property P;
033:
034: public void setUp() {
035: m = getModel();
036: S = m.createResource("http://nowhere.man/subject");
037: P = m.createProperty("http://nowhere.man/predicate");
038: }
039:
040: public void tearDown() {
041: m = null;
042: S = null;
043: P = null;
044: }
045:
046: protected static int numberSubjects = 7;
047: protected static int numberPredicates = 3;
048:
049: protected static final String subjectPrefix = "http://aldabaran/test6/s";
050: protected static final String predicatePrefix = "http://aldabaran/test6/";
051:
052: public void testListSubjects() {
053: Set statements = fill(m);
054: List L = iteratorToList(m.listSubjects());
055: assertEquals(numberSubjects, L.size());
056: Set wanted = subjectSet(numberSubjects);
057: assertEquals(wanted, iteratorToSet(L.iterator()));
058: }
059:
060: public void testListNamespaces() {
061: Set statements = fill(m);
062: List L = iteratorToList(m.listNameSpaces());
063: assertEquals(numberPredicates, L.size());
064: Set wanted = predicateSet(numberPredicates);
065: assertEquals(wanted, new HashSet(L));
066: }
067:
068: public void testListStatements() {
069: Set statements = fill(m);
070: List L = iteratorToList(m.listStatements());
071: assertEquals(statements.size(), L.size());
072: assertEquals(statements, new HashSet(L));
073: }
074:
075: public void testListObjectsOfPropertyByProperty() {
076: Set statements = fill(m);
077: List L = iteratorToList(m
078: .listObjectsOfProperty(property(predicatePrefix + "0/p")));
079: assertEquals(numberSubjects, L.size());
080: Set wanted = literalsFor(0);
081: assertEquals(wanted, new HashSet(L));
082: }
083:
084: public void testListObjectsOfPropertyBySubject() {
085: int size = 10;
086: Resource s = m.createResource();
087: for (int i = 0; i < size; i += 1)
088: m.addLiteral(s, RDF.value, i);
089: List L = iteratorToList(m.listObjectsOfProperty(s, RDF.value));
090: assertEquals(size, L.size());
091: Set wanted = literalsUpto(size);
092: assertEquals(wanted, new HashSet(L));
093: }
094:
095: public void testListObjects() {
096: fill(m);
097: Set wanted = literalsUpto(numberSubjects * numberPredicates);
098: assertEquals(wanted, iteratorToSet(m.listObjects()));
099: }
100:
101: protected Set subjectSet(int limit) {
102: Set result = new HashSet();
103: for (int i = 0; i < limit; i += 1)
104: result.add(resource(subjectPrefix + i));
105: return result;
106: }
107:
108: protected Set predicateSet(int limit) {
109: Set result = new HashSet();
110: for (int i = 0; i < limit; i += 1)
111: result.add(predicatePrefix + i + "/");
112: return result;
113: }
114:
115: protected Set literalsUpto(int limit) {
116: Set result = new HashSet();
117: for (int i = 0; i < limit; i += 1)
118: result.add(m.createTypedLiteral(i));
119: return result;
120: }
121:
122: protected Set literalsFor(int predicate) {
123: Set result = new HashSet();
124: for (int i = 0; i < numberSubjects; i += 1)
125: result.add(m.createTypedLiteral(i * numberPredicates
126: + predicate));
127: return result;
128: }
129:
130: protected Set fill(Model m) {
131: Set statements = new HashSet();
132: for (int i = 0; i < numberSubjects; i += 1)
133: for (int j = 0; j < numberPredicates; j += 1) {
134: Statement s = m.createLiteralStatement(
135: resource(subjectPrefix + i),
136: property(predicatePrefix + j + "/p"), i
137: * numberPredicates + j);
138: m.add(s);
139: statements.add(s);
140: }
141: assertEquals(numberSubjects * numberPredicates, m.size());
142: return statements;
143: }
144: }
145:
146: /*
147: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
148: * All rights reserved.
149: *
150: * Redistribution and use in source and binary forms, with or without
151: * modification, are permitted provided that the following conditions
152: * are met:
153: * 1. Redistributions of source code must retain the above copyright
154: * notice, this list of conditions and the following disclaimer.
155: * 2. Redistributions in binary form must reproduce the above copyright
156: * notice, this list of conditions and the following disclaimer in the
157: * documentation and/or other materials provided with the distribution.
158: * 3. The name of the author may not be used to endorse or promote products
159: * derived from this software without specific prior written permission.
160: *
161: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
163: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
164: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
165: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
166: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
167: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
168: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
169: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
170: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
171: */
|