001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package test.org.mandarax.rdf;
020:
021: import java.net.URL;
022: import java.util.Collection;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import org.mandarax.kernel.Fact;
026: import org.mandarax.rdf.RDFClauseSet;
027: import org.mandarax.util.ClauseIterator;
028:
029: /**
030: * Test case for RDF clause sets.
031: * @see org.mandarax.rdf.RDFClauseSet
032: * @author <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A> <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
033: * @version 1.1 <01 August 2004>
034: * @since 0.1
035: */
036:
037: public class RDFClauseSetTestCase extends AbstractRDFTestCase {
038: /**
039: * Create a new container used to compare imported and expected clauses.
040: * We use a Set here indicating that the order does not matter.
041: * Serialisation mechanisms seem to indicate that statements are ordered, but remember
042: * that a order would not make sense for the graph model.
043: * @return a container
044: */
045: private Collection createContainer() {
046: return new HashSet();
047: }
048:
049: /**
050: * Test 1. All objects are literals.
051: * @throws an exception (indicating that the test case has failed)
052: */
053: public void test1() throws Exception {
054: log("test1");
055: String testData = TEST_DATA_ROOT + "chap0301.rdf";
056: Collection container = createContainer();
057: container.add(buildFactWithLiteral(PSTCN, "author",
058: "http://burningbird.net/articles/monster3.htm",
059: "Shelley Powers"));
060: container.add(buildFactWithLiteral(PSTCN, "title",
061: "http://burningbird.net/articles/monster3.htm",
062: "Architeuthis Dux"));
063: test(testData, container);
064: }
065:
066: /**
067: * Test 2. Objects are literals or resources.
068: * @throws an exception (indicating that the test case has failed)
069: */
070: public void test2() throws Exception {
071: String testData = TEST_DATA_ROOT + "chap0302.rdf";
072:
073: Collection container = createContainer();
074: container.add(buildFactWithLiteral(PSTCN, "author",
075: "http://burningbird.net/articles/monster3.htm",
076: "Shelley Powers"));
077: container.add(buildFactWithLiteral(PSTCN, "title",
078: "http://burningbird.net/articles/monster3.htm",
079: "Architeuthis Dux"));
080: container.add(buildFactWithObject(PSTCN, "series",
081: "http://burningbird.net/articles/monster3.htm",
082: "http://burningbird.net/articles/monsters.htm"));
083: container.add(buildFactWithLiteral(PSTCN, "contains",
084: "http://burningbird.net/articles/monster3.htm",
085: "Physical description of giant squids"));
086: container.add(buildFactWithLiteral(PSTCN, "alsoContains",
087: "http://burningbird.net/articles/monster3.htm",
088: "Tale of the Legendary Kraken"));
089: container.add(buildFactWithLiteral(PSTCN, "seriesTitle",
090: "http://burningbird.net/articles/monsters.htm",
091: "A Tale of Two Monsters"));
092: test(testData, container);
093: }
094:
095: /**
096: * Tests the RDFClauseSet by building an instance from the test data imported from the
097: * url, iterating over it and comparing each clause with the clauses implicitely defined
098: * the arrays.
099: * @param testDataLocation the test data location (a relative path)
100: * @param expectedFacts the expected clauses
101: * @exception thrown if something goes wrong
102: */
103: private void test(String testDataLocation, Collection expectedFacts)
104: throws Exception {
105: URL url = this .getClass().getResource(testDataLocation);
106: RDFClauseSet clauseSet = new RDFClauseSet(url);
107: Collection importedFacts = createContainer();
108: for (ClauseIterator clauses = clauseSet.clauses(); clauses
109: .hasMoreClauses();) {
110: Fact nextClause = (Fact) clauses.nextClause();
111: importedFacts.add(nextClause);
112: }
113: // log
114: log("Expected facts:");
115: for (Iterator iter = expectedFacts.iterator(); iter.hasNext();)
116: log(iter.next().toString());
117: log("Imported facts:");
118: for (Iterator iter = importedFacts.iterator(); iter.hasNext();)
119: log(iter.next().toString());
120:
121: // compare
122: if (importedFacts.size() > expectedFacts.size())
123: assertTrue("More clauses imported than expected", false);
124: if (importedFacts.size() < expectedFacts.size())
125: assertTrue("Less clauses importedthan expected", false);
126: assertEquals(importedFacts, expectedFacts);
127: }
128: }
|