001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: NewRegressionStatementMethods.java,v 1.10 2008/01/02 12:07:03 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.regression;
008:
009: import com.hp.hpl.jena.rdf.model.*;
010: import com.hp.hpl.jena.vocabulary.RDF;
011: import com.hp.hpl.jena.regression.Regression.*;
012:
013: import junit.framework.*;
014:
015: public class NewRegressionStatementMethods extends NewRegressionBase {
016: public NewRegressionStatementMethods(String name) {
017: super (name);
018: }
019:
020: public static Test suite() {
021: return new TestSuite(NewRegressionStatementMethods.class);
022: }
023:
024: protected Model getModel() {
025: return ModelFactory.createDefaultModel();
026: }
027:
028: protected Model m;
029: protected Resource r;
030:
031: public void setUp() {
032: m = getModel();
033: r = m.createResource();
034: }
035:
036: public void testGetResource() {
037: assertEquals(r, m.createStatement(r, RDF.value, r)
038: .getResource());
039: }
040:
041: public void testGetResourceFailure() {
042: try {
043: m.createLiteralStatement(r, RDF.value, false).getResource();
044: fail("should trap non-resource object");
045: } catch (ResourceRequiredException e) {
046: pass();
047: }
048: }
049:
050: public void testGetTrueBoolean() {
051: assertEquals(true, m.createLiteralStatement(r, RDF.value, true)
052: .getLiteral().getBoolean());
053: }
054:
055: public void testGetLiteralFailure() {
056: try {
057: m.createStatement(r, RDF.value, r).getLiteral();
058: fail("should trap non-literal object");
059: } catch (LiteralRequiredException e) {
060: pass();
061: }
062: }
063:
064: public void testBoolean() {
065: Statement s = m.createLiteralStatement(r, RDF.value, true);
066: assertEquals(m.createTypedLiteral(true), s.getObject());
067: assertEquals(true, s.getBoolean());
068: }
069:
070: public void testByte() {
071: Statement s = m.createLiteralStatement(r, RDF.value, tvByte);
072: assertEquals(m.createTypedLiteral(tvByte), s.getObject());
073: assertEquals(tvByte, s.getLong());
074: }
075:
076: public void testShort() {
077: Statement s = m.createLiteralStatement(r, RDF.value, tvShort);
078: assertEquals(m.createTypedLiteral(tvShort), s.getObject());
079: assertEquals(tvShort, s.getShort());
080: }
081:
082: public void testInt() {
083: Statement s = m.createLiteralStatement(r, RDF.value, tvInt);
084: assertEquals(m.createTypedLiteral(tvInt), s.getObject());
085: assertEquals(tvInt, s.getInt());
086: }
087:
088: public void testLong() {
089: Statement s = m.createLiteralStatement(r, RDF.value, tvLong);
090: assertEquals(m.createTypedLiteral(tvLong), s.getObject());
091: assertEquals(tvLong, s.getLong());
092: }
093:
094: public void testChar() {
095: Statement s = m.createLiteralStatement(r, RDF.value, tvChar);
096: assertEquals(m.createTypedLiteral(tvChar), s.getObject());
097: assertEquals(tvChar, s.getChar());
098: }
099:
100: public void testFloat() {
101: Statement s = m.createLiteralStatement(r, RDF.value, tvFloat);
102: assertEquals(m.createTypedLiteral(tvFloat), s.getObject());
103: assertEquals(tvFloat, s.getFloat(), fDelta);
104: }
105:
106: public void testDouble() {
107: Statement s = m.createLiteralStatement(r, RDF.value, tvDouble);
108: assertEquals(m.createTypedLiteral(tvDouble), s.getObject());
109: assertEquals(tvDouble, s.getDouble(), dDelta);
110: }
111:
112: public void testString() {
113: assertEquals(tvString, m
114: .createStatement(r, RDF.value, tvString).getString());
115: }
116:
117: public void testStringWithLanguage() {
118: String lang = "fr";
119: assertEquals(tvString, m.createStatement(r, RDF.value,
120: tvString, lang).getString());
121: assertEquals(lang, m.createStatement(r, RDF.value, tvString,
122: lang).getLanguage());
123: }
124:
125: public void testResObj() {
126: Resource tvResObj = m.createResource(new ResTestObjF());
127: assertEquals(tvResObj, m
128: .createStatement(r, RDF.value, tvResObj).getResource());
129: }
130:
131: public void testLitObj() {
132: assertEquals(tvLitObj, m.createLiteralStatement(r, RDF.value,
133: tvLitObj).getObject(new LitTestObjF()));
134: }
135:
136: public void testBag() {
137: Bag tvBag = m.createBag();
138: assertEquals(tvBag, m.createStatement(r, RDF.value, tvBag)
139: .getBag());
140: }
141:
142: public void testSeq() {
143: Seq tvSeq = m.createSeq();
144: assertEquals(tvSeq, m.createStatement(r, RDF.value, tvSeq)
145: .getSeq());
146: }
147:
148: public void testAlt() {
149: Alt tvAlt = m.createAlt();
150: assertEquals(tvAlt, m.createStatement(r, RDF.value, tvAlt)
151: .getAlt());
152: }
153:
154: public void testChangeObjectBoolean() {
155: Statement sTrue = loadInitialStatement();
156: Statement sFalse = sTrue.changeLiteralObject(false);
157: checkChangedStatementSP(sFalse);
158: assertEquals(m.createTypedLiteral(false), sFalse.getObject());
159: assertEquals(false, sFalse.getBoolean());
160: checkCorrectStatements(sTrue, sFalse);
161: assertTrue(m.containsLiteral(r, RDF.value, false));
162: }
163:
164: public void testChangeObjectByte() {
165: Statement sTrue = loadInitialStatement();
166: Statement changed = sTrue.changeLiteralObject(tvByte);
167: checkChangedStatementSP(changed);
168: assertEquals(m.createTypedLiteral(tvByte), changed.getObject());
169: assertEquals(tvByte, changed.getByte());
170: checkCorrectStatements(sTrue, changed);
171: assertTrue(m.containsLiteral(r, RDF.value, tvByte));
172: }
173:
174: public void testChangeObjectShort() {
175: Statement sTrue = loadInitialStatement();
176: Statement changed = sTrue.changeLiteralObject(tvShort);
177: checkChangedStatementSP(changed);
178: assertEquals(m.createTypedLiteral(tvShort), changed.getObject());
179: assertEquals(tvShort, changed.getShort());
180: checkCorrectStatements(sTrue, changed);
181: assertTrue(m.containsLiteral(r, RDF.value, tvShort));
182: }
183:
184: public void testChangeObjectInt() {
185: Statement sTrue = loadInitialStatement();
186: Statement changed = sTrue.changeLiteralObject(tvInt);
187: checkChangedStatementSP(changed);
188: assertEquals(m.createTypedLiteral(tvInt), changed.getObject());
189: assertEquals(tvInt, changed.getInt());
190: checkCorrectStatements(sTrue, changed);
191: assertTrue(m.containsLiteral(r, RDF.value, tvInt));
192: }
193:
194: public void testChangeObjectLong() {
195: Statement sTrue = loadInitialStatement();
196: Statement changed = sTrue.changeLiteralObject(tvLong);
197: checkChangedStatementSP(changed);
198: assertEquals(m.createTypedLiteral(tvLong), changed.getObject());
199: assertEquals(tvLong, changed.getLong());
200: checkCorrectStatements(sTrue, changed);
201: assertTrue(m.containsLiteral(r, RDF.value, tvLong));
202: }
203:
204: public void testChangeObjectChar() {
205: Statement sTrue = loadInitialStatement();
206: Statement changed = sTrue.changeLiteralObject(tvChar);
207: checkChangedStatementSP(changed);
208: assertEquals(tvChar, changed.getChar());
209: assertEquals(m.createTypedLiteral(tvChar), changed.getObject());
210: checkCorrectStatements(sTrue, changed);
211: assertTrue(m.containsLiteral(r, RDF.value, tvChar));
212: }
213:
214: public void testChangeObjectFloat() {
215: Statement sTrue = loadInitialStatement();
216: Statement changed = sTrue.changeLiteralObject(tvFloat);
217: checkChangedStatementSP(changed);
218: assertEquals(m.createTypedLiteral(tvFloat), changed.getObject());
219: assertEquals(tvFloat, changed.getFloat(), fDelta);
220: checkCorrectStatements(sTrue, changed);
221: assertTrue(m.containsLiteral(r, RDF.value, tvFloat));
222: }
223:
224: public void testChangeObjectDouble() {
225: Statement sTrue = loadInitialStatement();
226: Statement changed = sTrue.changeLiteralObject(tvDouble);
227: checkChangedStatementSP(changed);
228: assertEquals(m.createTypedLiteral(tvDouble), changed
229: .getObject());
230: assertEquals(tvDouble, changed.getDouble(), dDelta);
231: checkCorrectStatements(sTrue, changed);
232: assertTrue(m.containsLiteral(r, RDF.value, tvDouble));
233: }
234:
235: public void testChangeObjectString() {
236: Statement sTrue = loadInitialStatement();
237: Statement changed = sTrue.changeObject(tvString);
238: checkChangedStatementSP(changed);
239: assertEquals(tvString, changed.getString());
240: checkCorrectStatements(sTrue, changed);
241: assertTrue(m.contains(r, RDF.value, tvString));
242: }
243:
244: public void testChangeObjectStringWithLanguage() {
245: String lang = "en";
246: Statement sTrue = loadInitialStatement();
247: Statement changed = sTrue.changeObject(tvString, lang);
248: checkChangedStatementSP(changed);
249: assertEquals(tvString, changed.getString());
250: assertEquals(lang, changed.getLanguage());
251: checkCorrectStatements(sTrue, changed);
252: assertTrue(m.contains(r, RDF.value, tvString, lang));
253: }
254:
255: public void testChangeObjectResObject() {
256: Resource tvResObj = m.createResource(new ResTestObjF());
257: Statement sTrue = loadInitialStatement();
258: Statement changed = sTrue.changeObject(tvResObj);
259: checkChangedStatementSP(changed);
260: assertEquals(tvResObj, changed.getResource());
261: checkCorrectStatements(sTrue, changed);
262: assertTrue(m.contains(r, RDF.value, tvResObj));
263: }
264:
265: public void testChangeObjectLiteral() {
266: Statement sTrue = loadInitialStatement();
267: m.remove(sTrue);
268: assertFalse(m.contains(sTrue));
269: assertFalse(m.containsLiteral(r, RDF.value, true));
270: }
271:
272: public void testChangeObjectYByte() {
273: Statement sTrue = loadInitialStatement();
274: Statement changed = sTrue.changeLiteralObject(tvByte);
275: checkChangedStatementSP(changed);
276: assertEquals(tvByte, changed.getByte());
277: checkCorrectStatements(sTrue, changed);
278: assertTrue(m.containsLiteral(r, RDF.value, tvByte));
279: }
280:
281: protected void checkCorrectStatements(Statement sTrue,
282: Statement changed) {
283: assertFalse(m.contains(sTrue));
284: assertFalse(m.containsLiteral(r, RDF.value, true));
285: assertTrue(m.contains(changed));
286: }
287:
288: protected void checkChangedStatementSP(Statement changed) {
289: assertEquals(r, changed.getSubject());
290: assertEquals(RDF.value, changed.getPredicate());
291: }
292:
293: protected Statement loadInitialStatement() {
294: Statement sTrue = m.createLiteralStatement(r, RDF.value, true);
295: m.add(sTrue);
296: return sTrue;
297: }
298: }
299:
300: /*
301: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
302: * All rights reserved.
303: *
304: * Redistribution and use in source and binary forms, with or without
305: * modification, are permitted provided that the following conditions
306: * are met:
307: * 1. Redistributions of source code must retain the above copyright
308: * notice, this list of conditions and the following disclaimer.
309: * 2. Redistributions in binary form must reproduce the above copyright
310: * notice, this list of conditions and the following disclaimer in the
311: * documentation and/or other materials provided with the distribution.
312: * 3. The name of the author may not be used to endorse or promote products
313: * derived from this software without specific prior written permission.
314: *
315: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
316: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
317: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
318: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
319: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
320: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
321: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
322: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
323: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
324: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325: */
|