001: /******************************************************************
002: * File: UtilForTests.java
003: * Created by: Dave Reynolds
004: * Created on: 14-Jan-03
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: TestUtil.java,v 1.19 2008/01/02 12:08:31 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.test;
010:
011: import junit.framework.TestCase;
012: import java.util.Iterator;
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015:
016: import com.hp.hpl.jena.rdf.model.*;
017: import com.hp.hpl.jena.rdf.model.Resource;
018:
019: /**
020: * Collection of utilities to assist with unit testing.
021: *
022: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
023: * @version $Revision: 1.19 $ on $Date: 2008/01/02 12:08:31 $
024: */
025: public class TestUtil {
026:
027: /**
028: * Helper method to test an iterator against a list of objects - order independent
029: * @param testCase The JUnit test case that is invoking this helper
030: * @param it The iterator to test
031: * @param vals The expected values of the iterator
032: */
033: public static void assertIteratorValues(TestCase testCase,
034: Iterator it, Object[] vals) {
035: assertIteratorValues(testCase, it, vals, 0);
036: }
037:
038: /**
039: * Helper method to test an iterator against a list of objects - order independent, and
040: * can optionally check the count of anonymous resources. This allows us to test a
041: * iterator of resource values which includes both URI nodes and bNodes.
042: * @param testCase The JUnit test case that is invoking this helper
043: * @param it The iterator to test
044: * @param vals The expected values of the iterator
045: * @param anonCount If non zero, count the number of anonymous resources returned by <code>it</code>,
046: * and don't check these resources against the expected <code>vals</code>.
047: */
048: public static void assertIteratorValues(TestCase testCase,
049: Iterator it, Object[] vals, int countAnon) {
050: Log logger = LogFactory.getLog(testCase.getClass());
051:
052: boolean[] found = new boolean[vals.length];
053: int anonFound = 0;
054:
055: for (int i = 0; i < vals.length; i++)
056: found[i] = false;
057:
058: while (it.hasNext()) {
059: Object n = it.next();
060: boolean gotit = false;
061:
062: // do bNodes separately
063: if (countAnon > 0 && isAnonValue(n)) {
064: anonFound++;
065: continue;
066: }
067:
068: for (int i = 0; i < vals.length; i++) {
069: if (n.equals(vals[i])) {
070: gotit = true;
071: found[i] = true;
072: }
073: }
074: if (!gotit) {
075: logger.debug(testCase.getName()
076: + " found unexpected iterator value: " + n);
077: }
078: TestCase.assertTrue(testCase.getName()
079: + " found unexpected iterator value: " + n, gotit);
080: }
081:
082: // check that no expected values were unfound
083: for (int i = 0; i < vals.length; i++) {
084: if (!found[i]) {
085: // for (int j = 0; j < vals.length; j += 1) System.err.println( "#" + j + ": " + vals[j] );
086: logger.debug(testCase.getName()
087: + " failed to find expected iterator value: "
088: + vals[i]);
089: }
090: TestCase.assertTrue(testCase.getName()
091: + " failed to find expected iterator value: "
092: + vals[i], found[i]);
093: }
094:
095: // check we got the right no. of anons
096: TestCase
097: .assertEquals(
098: testCase.getName()
099: + " iterator test did not find the right number of anon. nodes",
100: countAnon, anonFound);
101: }
102:
103: /**
104: * Replace all blocks of white space by a single space character, just
105: * used for creating test cases.
106: *
107: * @param src the original string
108: * @return normalized version of src
109: */
110: public static String normalizeWhiteSpace(String src) {
111: StringBuffer result = new StringBuffer(src.length());
112: boolean inWhitespaceBlock = false;
113: for (int i = 0; i < src.length(); i++) {
114: char c = src.charAt(i);
115: if (Character.isWhitespace(c)) {
116: if (!inWhitespaceBlock) {
117: result.append(" ");
118: inWhitespaceBlock = true;
119: }
120: } else {
121: inWhitespaceBlock = false;
122: result.append(c);
123: }
124: }
125: return result.toString();
126: }
127:
128: /**
129: * Check the length of an iterator.
130: */
131: public static void assertIteratorLength(Iterator it,
132: int expectedLength) {
133: int length = 0;
134: while (it.hasNext()) {
135: it.next();
136: length++;
137: }
138: TestCase.assertEquals(expectedLength, length);
139: }
140:
141: /**
142: * For the purposes of counting, a value is anonymous if (a) it is an anonymous resource,
143: * or (b) it is a statement with a bNode subject or (c) it is a statement with a bNode
144: * object. This is because we cannot check bNode identity against fixed expected data values.
145: * @param n A value
146: * @return True if n is anonymous
147: */
148: protected static boolean isAnonValue(Object n) {
149: return ((n instanceof Resource) && ((Resource) n).isAnon())
150: || ((n instanceof Statement) && ((Statement) n)
151: .getSubject().isAnon())
152: || ((n instanceof Statement) && isAnonValue(((Statement) n)
153: .getObject()));
154: }
155: }
156:
157: /*
158: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
159: All rights reserved.
160:
161: Redistribution and use in source and binary forms, with or without
162: modification, are permitted provided that the following conditions
163: are met:
164:
165: 1. Redistributions of source code must retain the above copyright
166: notice, this list of conditions and the following disclaimer.
167:
168: 2. Redistributions in binary form must reproduce the above copyright
169: notice, this list of conditions and the following disclaimer in the
170: documentation and/or other materials provided with the distribution.
171:
172: 3. The name of the author may not be used to endorse or promote products
173: derived from this software without specific prior written permission.
174:
175: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
176: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
178: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
179: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
181: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
182: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
183: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
184: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
185: */
|