001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: NewRegressionLiterals.java,v 1.8 2008/01/02 12:07:04 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.regression;
008:
009: import junit.framework.TestSuite;
010:
011: import com.hp.hpl.jena.rdf.model.*;
012: import com.hp.hpl.jena.regression.Regression.*;
013:
014: public class NewRegressionLiterals extends NewRegressionBase {
015: public NewRegressionLiterals(String name) {
016: super (name);
017: }
018:
019: public static TestSuite suite() {
020: return new TestSuite(NewRegressionLiterals.class);
021: }
022:
023: protected Model getModel() {
024: return ModelFactory.createDefaultModel();
025: }
026:
027: public void testBooleans() {
028: Model m = getModel();
029: assertTrue(m.createTypedLiteral(true).getBoolean());
030: assertFalse(m.createTypedLiteral(false).getBoolean());
031: }
032:
033: public void testByteLiterals() {
034: Model m = getModel();
035: testByte(m, (byte) 0);
036: testByte(m, (byte) -1);
037: testByte(m, Byte.MIN_VALUE);
038: testByte(m, Byte.MAX_VALUE);
039: }
040:
041: public void testShortLiterals() {
042: Model m = getModel();
043: testShort(m, (short) 0);
044: testShort(m, (short) -1);
045: testShort(m, Short.MIN_VALUE);
046: testShort(m, Short.MAX_VALUE);
047: }
048:
049: public void testIntLiterals() {
050: Model m = getModel();
051: testInt(m, (int) 0);
052: testInt(m, (int) -1);
053: testInt(m, Integer.MIN_VALUE);
054: testInt(m, Integer.MAX_VALUE);
055: }
056:
057: public void testLongLiterals() {
058: Model m = getModel();
059: testLong(m, (long) 0);
060: testLong(m, (long) -1);
061: testLong(m, Long.MIN_VALUE);
062: testLong(m, Long.MAX_VALUE);
063: }
064:
065: public void testFloatLiterals() {
066: Model m = getModel();
067: testFloat(m, 0.0f);
068: testFloat(m, 1.0f);
069: testFloat(m, -1.0f);
070: testFloat(m, 12345.6789f);
071: testFloat(m, Float.MIN_VALUE);
072: testFloat(m, Float.MAX_VALUE);
073: }
074:
075: public void testDoubleLiterals() {
076: Model m = getModel();
077: testDouble(m, 0.0);
078: testDouble(m, 1.0);
079: testDouble(m, -1.0);
080: testDouble(m, 12345.678901);
081: testDouble(m, Double.MIN_VALUE);
082: testDouble(m, Double.MAX_VALUE);
083: }
084:
085: public void testCharacterLiterals() {
086: Model m = getModel();
087: testCharacter(m, 'A');
088: testCharacter(m, 'a');
089: testCharacter(m, '#');
090: testCharacter(m, '@');
091: testCharacter(m, '0');
092: testCharacter(m, '9');
093: testCharacter(m, '\u1234');
094: testCharacter(m, '\u5678');
095: }
096:
097: public void testPlainStringLiterals() {
098: Model m = getModel();
099: testPlainString(m, "");
100: testPlainString(m, "A test string");
101: testPlainString(m, "Another test string");
102: }
103:
104: public void testLanguagedStringLiterals() {
105: Model m = getModel();
106: testLanguagedString(m, "", "en");
107: testLanguagedString(m, "chat", "fr");
108: }
109:
110: public void testStringLiteralEquality() {
111: Model m = getModel();
112: assertEquals(m.createLiteral("A"), m.createLiteral("A"));
113: assertEquals(m.createLiteral("Alpha"), m.createLiteral("Alpha"));
114: assertDiffer(m.createLiteral("Alpha"), m.createLiteral("Beta"));
115: assertDiffer(m.createLiteral("A", "en"), m.createLiteral("A"));
116: assertDiffer(m.createLiteral("A"), m.createLiteral("A", "en"));
117: assertDiffer(m.createLiteral("A", "en"), m.createLiteral("A",
118: "fr"));
119: assertEquals(m.createLiteral("A", "en"), m.createLiteral("A",
120: "en"));
121: }
122:
123: public void testLiteralObjects() {
124: Model m = getModel();
125: testLiteralObject(m, 0);
126: testLiteralObject(m, 12345);
127: testLiteralObject(m, -67890);
128: }
129:
130: protected void testByte(Model m, byte tv) {
131: Literal l = m.createTypedLiteral(tv);
132: assertEquals(tv, l.getByte());
133: assertEquals(tv, l.getShort());
134: assertEquals(tv, l.getInt());
135: assertEquals(tv, l.getLong());
136: }
137:
138: protected void testShort(Model m, short tv) {
139: Literal l = m.createTypedLiteral(tv);
140: try {
141: assertEquals(tv, l.getByte());
142: assertInRange(Byte.MIN_VALUE, tv, Byte.MAX_VALUE);
143: } catch (NumberFormatException e) {
144: assertOutsideRange(Byte.MIN_VALUE, tv, Byte.MAX_VALUE);
145: } catch (IllegalArgumentException e) {
146: assertOutsideRange(Byte.MIN_VALUE, tv, Byte.MAX_VALUE);
147: }
148: assertEquals(tv, l.getShort());
149: assertEquals(tv, l.getInt());
150: assertEquals(tv, l.getLong());
151: }
152:
153: protected void testInt(Model m, int tv) {
154: Literal l = m.createTypedLiteral(tv);
155: try {
156: assertEquals(tv, l.getByte());
157: assertInRange(Byte.MIN_VALUE, tv, Byte.MAX_VALUE);
158: } catch (NumberFormatException e) {
159: assertOutsideRange(Byte.MIN_VALUE, tv, Byte.MAX_VALUE);
160: } catch (IllegalArgumentException e) {
161: assertOutsideRange(Byte.MIN_VALUE, tv, Byte.MAX_VALUE);
162: }
163: try {
164: assertEquals(tv, l.getShort());
165: assertInRange(Short.MIN_VALUE, tv, Short.MAX_VALUE);
166: } catch (NumberFormatException e) {
167: assertOutsideRange(Short.MIN_VALUE, tv, Short.MAX_VALUE);
168: } catch (IllegalArgumentException e) {
169: assertOutsideRange(Short.MIN_VALUE, tv, Short.MAX_VALUE);
170: }
171: assertEquals(tv, l.getInt());
172: assertEquals(tv, l.getLong());
173: }
174:
175: protected void testLong(Model m, long tv) {
176: Literal l = m.createTypedLiteral(tv);
177: try {
178: assertEquals(tv, l.getByte());
179: assertInRange(Byte.MIN_VALUE, tv, Byte.MAX_VALUE);
180: } catch (NumberFormatException e) {
181: assertOutsideRange(Byte.MIN_VALUE, tv, Byte.MAX_VALUE);
182: } catch (IllegalArgumentException e) {
183: assertOutsideRange(Byte.MIN_VALUE, tv, Byte.MAX_VALUE);
184: }
185: try {
186: assertEquals(tv, l.getShort());
187: assertInRange(Short.MIN_VALUE, tv, Short.MAX_VALUE);
188: } catch (NumberFormatException e) {
189: assertOutsideRange(Short.MIN_VALUE, tv, Short.MAX_VALUE);
190: } catch (IllegalArgumentException e) {
191: assertOutsideRange(Short.MIN_VALUE, tv, Short.MAX_VALUE);
192: }
193: try {
194: assertEquals(tv, l.getInt());
195: assertInRange(Integer.MIN_VALUE, tv, Integer.MAX_VALUE);
196: } catch (NumberFormatException e) {
197: assertOutsideRange(Integer.MIN_VALUE, tv, Integer.MAX_VALUE);
198: } catch (IllegalArgumentException e) {
199: assertOutsideRange(Integer.MIN_VALUE, tv, Integer.MAX_VALUE);
200: }
201: assertEquals(tv, l.getLong());
202: }
203:
204: protected void assertOutsideRange(long min, long x, long max) {
205: if (min <= x && x <= max)
206: fail("inside range: " + x + " min: " + min + " max: " + max);
207: }
208:
209: protected void assertInRange(long min, long x, long max) {
210: if (min <= x && x <= max)
211: return;
212: else
213: fail("outside range: " + x + " min: " + min + " max: "
214: + max);
215: }
216:
217: protected void testFloat(Model m, float tv) {
218: assertEquals(tv, m.createTypedLiteral(tv).getFloat(), fDelta);
219: }
220:
221: protected void testDouble(Model m, double tv) {
222: final double delta = 0.000000005;
223: assertEquals(tv, m.createTypedLiteral(tv).getDouble(), dDelta);
224: }
225:
226: protected void testCharacter(Model m, char tv) {
227: assertEquals(tv, m.createTypedLiteral(tv).getChar());
228: }
229:
230: protected void testLanguagedString(Model m, String tv, String lang) {
231: Literal l = m.createLiteral(tv, lang);
232: assertEquals(tv, l.getString());
233: assertEquals(tv, l.getLexicalForm());
234: assertEquals(lang, l.getLanguage());
235: }
236:
237: protected void testPlainString(Model m, String tv) {
238: Literal l = m.createLiteral(tv);
239: assertEquals(tv, l.getString());
240: assertEquals(tv, l.getLexicalForm());
241: assertEquals("", l.getLanguage());
242: }
243:
244: protected void testLiteralObject(Model m, int x) {
245: LitTestObj tv = new LitTestObj(x);
246: LitTestObjF factory = new LitTestObjF();
247: assertEquals(tv, m.createTypedLiteral(tv).getObject(factory));
248: }
249: }
250:
251: /*
252: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
253: * All rights reserved.
254: *
255: * Redistribution and use in source and binary forms, with or without
256: * modification, are permitted provided that the following conditions
257: * are met:
258: * 1. Redistributions of source code must retain the above copyright
259: * notice, this list of conditions and the following disclaimer.
260: * 2. Redistributions in binary form must reproduce the above copyright
261: * notice, this list of conditions and the following disclaimer in the
262: * documentation and/or other materials provided with the distribution.
263: * 3. The name of the author may not be used to endorse or promote products
264: * derived from this software without specific prior written permission.
265: *
266: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
267: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
268: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
269: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
270: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
271: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
272: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
273: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
274: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
275: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276: */
|