001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestResourceFactory.java,v 1.12 2008/01/02 12:04:43 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.test;
008:
009: import java.util.Calendar;
010: import java.util.GregorianCalendar;
011: import java.util.TimeZone;
012:
013: import com.hp.hpl.jena.datatypes.RDFDatatype;
014: import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
015: import com.hp.hpl.jena.rdf.model.*;
016:
017: import junit.framework.*;
018:
019: public class TestResourceFactory extends TestCase {
020:
021: static final String uri1 = "http://example.org/example#a1";
022: static final String uri2 = "http://example.org/example#a2";
023:
024: public static TestSuite suite() {
025: return new TestSuite(TestResourceFactory.class);
026: }
027:
028: public TestResourceFactory(String name) {
029: super (name);
030: }
031:
032: public void testCreateResource() {
033: Resource r1 = ResourceFactory.createResource();
034: assertTrue(r1.isAnon());
035: Resource r2 = ResourceFactory.createResource();
036: assertTrue(r2.isAnon());
037: assertTrue(!r1.equals(r2));
038:
039: r1 = ResourceFactory.createResource(uri1);
040: assertTrue(r1.getURI().equals(uri1));
041: }
042:
043: public void testCreateProperty() {
044: Property p1 = ResourceFactory.createProperty(uri1);
045: assertTrue(p1.getURI().equals(uri1));
046: Property p2 = ResourceFactory.createProperty(uri1, "2");
047: assertTrue(p2.getURI().equals(uri1 + "2"));
048: }
049:
050: public void testCreateLiteral() {
051: Literal l = ResourceFactory.createPlainLiteral("lex");
052: assertTrue(l.getLexicalForm().equals("lex"));
053: assertTrue(l.getLanguage().equals(""));
054: assertNull(l.getDatatype());
055: assertNull(l.getDatatypeURI());
056: }
057:
058: public void testCreateTypedLiteral() {
059: Literal l = ResourceFactory.createTypedLiteral("22",
060: XSDDatatype.XSDinteger);
061: assertTrue(l.getLexicalForm().equals("22"));
062: assertTrue(l.getLanguage().equals(""));
063: assertTrue(l.getDatatype() == XSDDatatype.XSDinteger);
064: assertTrue(l.getDatatypeURI().equals(
065: XSDDatatype.XSDinteger.getURI()));
066:
067: }
068:
069: public void testCreateTypedLiteralObject() {
070: Literal l = ResourceFactory.createTypedLiteral(new Integer(22));
071: assertEquals("22", l.getLexicalForm());
072: assertEquals("", l.getLanguage());
073: assertEquals(XSDDatatype.XSDint, l.getDatatype());
074: }
075:
076: public void testCreateTypedLiteralOverload() {
077: Calendar testCal = new GregorianCalendar(TimeZone
078: .getTimeZone("GMT"));
079: testCal.set(1999, 4, 30, 15, 9, 32);
080: testCal.set(Calendar.MILLISECOND, 0); // ms field can be undefined on Linux
081: Literal lc = ResourceFactory.createTypedLiteral(testCal);
082: assertEquals("calendar overloading test", ResourceFactory
083: .createTypedLiteral("1999-05-30T15:09:32Z",
084: XSDDatatype.XSDdateTime), lc);
085:
086: }
087:
088: public void testCreateStatement() {
089: Resource s = ResourceFactory.createResource();
090: Property p = ResourceFactory.createProperty(uri2);
091: Resource o = ResourceFactory.createResource();
092: Statement stmt = ResourceFactory.createStatement(s, p, o);
093: assertTrue(stmt.getSubject().equals(s));
094: assertTrue(stmt.getPredicate().equals(p));
095: assertTrue(stmt.getObject().equals(o));
096: }
097:
098: public void testGetInstance() {
099: ResourceFactory.Interface factory = ResourceFactory
100: .getInstance();
101: Resource r1 = ResourceFactory.createResource();
102: assertTrue(r1.isAnon());
103: Resource r2 = ResourceFactory.createResource();
104: assertTrue(r2.isAnon());
105: assertTrue(!r1.equals(r2));
106: }
107:
108: public void testSetInstance() {
109: Resource r = ResourceFactory.createResource();
110: ResourceFactory.Interface factory = new TestFactory(r);
111: ResourceFactory.setInstance(factory);
112: assertTrue(factory.equals(ResourceFactory.getInstance()));
113: assertTrue(ResourceFactory.createResource() == r);
114: }
115:
116: class TestFactory implements ResourceFactory.Interface {
117:
118: Resource resource;
119:
120: TestFactory(Resource r) {
121: resource = r;
122: }
123:
124: public Resource createResource() {
125: return resource;
126: }
127:
128: public Resource createResource(String uriref) {
129: return null;
130: }
131:
132: public Literal createPlainLiteral(String string) {
133: return null;
134: }
135:
136: public Literal createTypedLiteral(String string,
137: RDFDatatype datatype) {
138: return null;
139: }
140:
141: public Literal createTypedLiteral(Object value) {
142: return null;
143: }
144:
145: public Property createProperty(String uriref) {
146: return null;
147: }
148:
149: public Property createProperty(String namespace,
150: String localName) {
151: return null;
152: }
153:
154: public Statement createStatement(Resource subject,
155: Property predicate, RDFNode object) {
156: return null;
157: }
158:
159: }
160: }
161:
162: /*
163: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
164: All rights reserved.
165:
166: Redistribution and use in source and binary forms, with or without
167: modification, are permitted provided that the following conditions
168: are met:
169:
170: 1. Redistributions of source code must retain the above copyright
171: notice, this list of conditions and the following disclaimer.
172:
173: 2. Redistributions in binary form must reproduce the above copyright
174: notice, this list of conditions and the following disclaimer in the
175: documentation and/or other materials provided with the distribution.
176:
177: 3. The name of the author may not be used to endorse or promote products
178: derived from this software without specific prior written permission.
179:
180: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
181: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
183: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
184: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
185: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
186: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
187: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
188: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
189: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
190: */
|