001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: NewRegressionResources.java,v 1.12 2008/01/02 12:07:05 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.impl.*;
015: import com.hp.hpl.jena.regression.Regression.ResTestObjF;
016: import com.hp.hpl.jena.shared.*;
017: import com.hp.hpl.jena.util.iterator.Map1;
018: import com.hp.hpl.jena.vocabulary.RDF;
019: import com.hp.hpl.jena.regression.Regression.*;
020:
021: public class NewRegressionResources extends NewRegressionBase {
022: public NewRegressionResources(String name) {
023: super (name);
024: }
025:
026: public static TestSuite suite() {
027: return new TestSuite(NewRegressionResources.class);
028: }
029:
030: protected Model getModel() {
031: return ModelFactory.createDefaultModel();
032: }
033:
034: public void testCreateAnonResource() {
035: Model m = getModel();
036: Resource r = m.createResource();
037: assertTrue(r.isAnon());
038: assertNull(r.getURI());
039: assertNull(r.getNameSpace());
040: assertNull(r.getLocalName());
041: }
042:
043: public void testCreateAnonResourceWithNull() {
044: Model m = getModel();
045: Resource r = m.createResource((String) null);
046: assertTrue(r.isAnon());
047: assertNull(r.getURI());
048: assertNull(r.getNameSpace());
049: assertNull(r.getLocalName());
050: }
051:
052: public void testCreateNamedResource() {
053: Model m = getModel();
054: String uri = "http://aldabaran.hpl.hp.com/foo";
055: assertEquals(uri, m.createResource(uri).getURI());
056: }
057:
058: public void testCreateTypedAnonResource() {
059: Model m = getModel();
060: Resource r = m.createResource(RDF.Property);
061: assertTrue(r.isAnon());
062: assertTrue(m.contains(r, RDF.type, RDF.Property));
063: }
064:
065: public void testCreateTypedNamedresource() {
066: Model m = getModel();
067: String uri = "http://aldabaran.hpl.hp.com/foo";
068: Resource r = m.createResource(uri, RDF.Property);
069: assertEquals(uri, r.getURI());
070: assertTrue(m.contains(r, RDF.type, RDF.Property));
071: }
072:
073: public void testCreateAnonByFactory() {
074: Model m = getModel();
075: assertTrue(m.createResource(new ResTestObjF()).isAnon());
076: }
077:
078: public void testCreateResourceByFactory() {
079: Model m = getModel();
080: String uri = "http://aldabaran.hpl.hp.com/foo";
081: assertEquals(uri, m.createResource(uri, new ResTestObjF())
082: .getURI());
083: }
084:
085: public void testCreateNullPropertyFails() {
086: Model m = getModel();
087: try {
088: m.createProperty(null);
089: fail("should not create null property");
090: } catch (InvalidPropertyURIException e) {
091: pass();
092: }
093: }
094:
095: public void testCreatePropertyOneArg() {
096: Model m = getModel();
097: Property p = m.createProperty("abc/def");
098: assertEquals("abc/", p.getNameSpace());
099: assertEquals("def", p.getLocalName());
100: assertEquals("abc/def", p.getURI());
101: }
102:
103: public void testCreatePropertyTwoArgs() {
104: Model m = getModel();
105: Property p = m.createProperty("abc/", "def");
106: assertEquals("abc/", p.getNameSpace());
107: assertEquals("def", p.getLocalName());
108: assertEquals("abc/def", p.getURI());
109: }
110:
111: public void testCreatePropertyStrangeURI() {
112: Model m = getModel();
113: String uri = RDF.getURI() + "_345";
114: Property p = m.createProperty(uri);
115: assertEquals(RDF.getURI(), p.getNameSpace());
116: assertEquals("_345", p.getLocalName());
117: assertEquals(uri, p.getURI());
118: }
119:
120: public void testCreatePropertyStrangeURITwoArgs() {
121: Model m = getModel();
122: String local = "_345";
123: Property p = m.createProperty(RDF.getURI(), local);
124: assertEquals(RDF.getURI(), p.getNameSpace());
125: assertEquals(local, p.getLocalName());
126: assertEquals(RDF.getURI() + local, p.getURI());
127: }
128:
129: public void testEnhancedResources() {
130: Model m = getModel();
131: Resource r = new ResourceImpl((ModelCom) m);
132: testResource(m, r, 0);
133:
134: testResource(m, m.createBag(), 1);
135: testContainer(m, m.createBag(), m.createBag());
136:
137: testResource(m, m.createAlt(), 1);
138: testContainer(m, m.createAlt(), m.createAlt());
139:
140: testResource(m, m.createSeq(), 1);
141: testContainer(m, m.createSeq(), m.createSeq());
142: // testSeq( m, m.createSeq(), m.createSeq(), m.createSeq(),
143: // m.createSeq(), m.createSeq(), m.createSeq(),
144: // m.createSeq() );
145: }
146:
147: protected Set setOf(Object x) {
148: Set result = new HashSet();
149: result.add(x);
150: return result;
151: }
152:
153: public void testResource(Model m, Resource r, int numProps) {
154: Literal tvLiteral = m.createLiteral("test 12 string 2");
155: Resource tvResource = m.createResource();
156: String lang = "fr";
157: //
158: assertTrue(r.addLiteral(RDF.value, tvBoolean).hasLiteral(
159: RDF.value, tvBoolean));
160: assertTrue(r.addLiteral(RDF.value, tvByte).hasLiteral(
161: RDF.value, tvByte));
162: assertTrue(r.addLiteral(RDF.value, tvShort).hasLiteral(
163: RDF.value, tvShort));
164: assertTrue(r.addLiteral(RDF.value, tvInt).hasLiteral(RDF.value,
165: tvInt));
166: assertTrue(r.addLiteral(RDF.value, tvLong).hasLiteral(
167: RDF.value, tvLong));
168: assertTrue(r.addLiteral(RDF.value, tvChar).hasLiteral(
169: RDF.value, tvChar));
170: assertTrue(r.addLiteral(RDF.value, tvFloat).hasLiteral(
171: RDF.value, tvFloat));
172: assertTrue(r.addLiteral(RDF.value, tvDouble).hasLiteral(
173: RDF.value, tvDouble));
174: assertTrue(r.addProperty(RDF.value, tvString).hasProperty(
175: RDF.value, tvString));
176: assertTrue(r.addProperty(RDF.value, tvString, lang)
177: .hasProperty(RDF.value, tvString, lang));
178: assertTrue(r.addLiteral(RDF.value, tvObject).hasLiteral(
179: RDF.value, tvObject));
180: assertTrue(r.addProperty(RDF.value, tvLiteral).hasProperty(
181: RDF.value, tvLiteral));
182: assertTrue(r.addProperty(RDF.value, tvResource).hasProperty(
183: RDF.value, tvResource));
184: assertTrue(r.getRequiredProperty(RDF.value).getSubject()
185: .equals(r));
186: //
187: Property p = m.createProperty("foo/", "bar");
188: try {
189: r.getRequiredProperty(p);
190: fail("should detect missing property");
191: } catch (PropertyNotFoundException e) {
192: pass();
193: }
194: //
195: assertEquals(13, iteratorToSet(r.listProperties(RDF.value))
196: .size());
197: assertEquals(setOf(r), iteratorToSet(r
198: .listProperties(RDF.value).mapWith(
199: Statement.Util.getSubject)));
200: //
201: assertEquals(0, iteratorToSet(r.listProperties(p)).size());
202: assertEquals(new HashSet(), iteratorToSet(r.listProperties(p)
203: .mapWith(Statement.Util.getSubject)));
204: //
205: assertEquals(13 + numProps, iteratorToSet(r.listProperties())
206: .size());
207: assertEquals(setOf(r), iteratorToSet(r.listProperties()
208: .mapWith(Statement.Util.getSubject)));
209: //
210: r.removeProperties();
211: assertEquals(0, m.query(
212: new SimpleSelector(r, null, (RDFNode) null)).size());
213: }
214:
215: public void testContainer(Model m, Container cont1, Container cont2) {
216: NodeIterator nIter;
217: StmtIterator sIter;
218:
219: Literal tvLiteral = m.createLiteral("test 12 string 2");
220: Resource tvResObj = m.createResource(new ResTestObjF());
221: Object tvLitObj = new LitTestObj(1234);
222: Bag tvBag = m.createBag();
223: Alt tvAlt = m.createAlt();
224: Seq tvSeq = m.createSeq();
225: String lang = "en";
226: Statement stmt;
227: //
228: assertEquals(0, cont1.size());
229: assertEquals(0, cont2.size());
230: //
231: assertTrue(cont1.add(tvBoolean).contains(tvBoolean));
232: assertTrue(cont1.add(tvByte).contains(tvByte));
233: assertTrue(cont1.add(tvShort).contains(tvShort));
234: assertTrue(cont1.add(tvInt).contains(tvInt));
235: assertTrue(cont1.add(tvLong).contains(tvLong));
236: assertTrue(cont1.add(tvFloat).contains(tvFloat));
237: assertTrue(cont1.add(tvDouble).contains(tvDouble));
238: assertTrue(cont1.add(tvChar).contains(tvChar));
239: assertTrue(cont1.add(tvString).contains(tvString));
240: assertFalse(cont1.contains(tvString, lang));
241: assertTrue(cont1.add(tvString, lang).contains(tvString, lang));
242: assertTrue(cont1.add(tvLiteral).contains(tvLiteral));
243: assertTrue(cont1.add(tvResObj).contains(tvResObj));
244: assertTrue(cont1.add(tvLitObj).contains(tvLitObj));
245: assertEquals(13, cont1.size());
246: //
247: int num = 10;
248: for (int i = 0; i < num; i += 1)
249: cont2.add(i);
250: assertEquals(num, cont2.size());
251: checkNumericContent(cont2, num);
252: //
253: boolean[] found = new boolean[num];
254: boolean[] retain = { true, true, true, false, false, false,
255: false, false, true, true };
256: retainOnlySpecified(cont2, num, retain);
257: seeWhatsThere(cont2, found);
258: for (int i = 0; i < num; i += 1)
259: assertEquals(i + "th element of array", retain[i], found[i]);
260: }
261:
262: protected void seeWhatsThere(Container cont2, boolean[] found) {
263: NodeIterator nit = cont2.iterator();
264: while (nit.hasNext()) {
265: int v = ((Literal) nit.nextNode()).getInt();
266: assertFalse(found[v]);
267: found[v] = true;
268: }
269: }
270:
271: protected void retainOnlySpecified(Container cont2, int num,
272: boolean[] retain) {
273: NodeIterator nit = cont2.iterator();
274: for (int i = 0; i < num; i++) {
275: nit.nextNode();
276: if (retain[i] == false)
277: nit.remove();
278: }
279: assertFalse(nit.hasNext());
280: }
281:
282: protected void checkNumericContent(Container cont2, int num) {
283: NodeIterator nit = cont2.iterator();
284: for (int i = 0; i < num; i += 1)
285: assertEquals(i, ((Literal) nit.nextNode()).getInt());
286: assertFalse(nit.hasNext());
287: }
288: }
289:
290: /*
291: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
292: * reserved.
293: *
294: * Redistribution and use in source and binary forms, with or without
295: * modification, are permitted provided that the following conditions are met:
296: * 1. Redistributions of source code must retain the above copyright notice,
297: * this list of conditions and the following disclaimer. 2. Redistributions in
298: * binary form must reproduce the above copyright notice, this list of
299: * conditions and the following disclaimer in the documentation and/or other
300: * materials provided with the distribution. 3. The name of the author may not
301: * be used to endorse or promote products derived from this software without
302: * specific prior written permission.
303: *
304: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
305: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
306: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
307: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
308: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
309: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
310: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
311: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
312: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
313: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
314: */
|