KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > test > TestUtil


1 /******************************************************************
2  * File: UtilForTests.java
3  * Created by: Dave Reynolds
4  * Created on: 14-Jan-03
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: TestUtil.java,v 1.16 2005/02/21 12:18:17 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.test;
11
12 import junit.framework.TestCase;
13 import java.util.Iterator JavaDoc;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 import com.hp.hpl.jena.rdf.model.*;
18 import com.hp.hpl.jena.rdf.model.Resource;
19
20 /**
21  * Collection of utilities to assist with unit testing.
22  *
23  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
24  * @version $Revision: 1.16 $ on $Date: 2005/02/21 12:18:17 $
25  */

26 public class TestUtil {
27     
28     /**
29      * Helper method to test an iterator against a list of objects - order independent
30      * @param testCase The JUnit test case that is invoking this helper
31      * @param it The iterator to test
32      * @param vals The expected values of the iterator
33      */

34     public static void assertIteratorValues(TestCase testCase, Iterator JavaDoc it, Object JavaDoc[] vals) {
35         assertIteratorValues( testCase, it, vals, 0 );
36     }
37     
38     /**
39      * Helper method to test an iterator against a list of objects - order independent, and
40      * can optionally check the count of anonymous resources. This allows us to test a
41      * iterator of resource values which includes both URI nodes and bNodes.
42      * @param testCase The JUnit test case that is invoking this helper
43      * @param it The iterator to test
44      * @param vals The expected values of the iterator
45      * @param anonCount If non zero, count the number of anonymous resources returned by <code>it</code>,
46      * and don't check these resources against the expected <code>vals</code>.
47      */

48     public static void assertIteratorValues(TestCase testCase, Iterator JavaDoc it, Object JavaDoc[] vals, int countAnon ) {
49         Log logger = LogFactory.getLog( testCase.getClass() );
50         
51         boolean[] found = new boolean[vals.length];
52         int anonFound = 0;
53         
54         for (int i = 0; i < vals.length; i++) found[i] = false;
55         
56         
57         while (it.hasNext()) {
58             Object JavaDoc n = it.next();
59             boolean gotit = false;
60             
61             // do bNodes separately
62
if (countAnon > 0 && isAnonValue( n )) {
63                 anonFound++;
64                 continue;
65             }
66             
67             for (int i = 0; i < vals.length; i++) {
68                 if (n.equals(vals[i])) {
69                     gotit = true;
70                     found[i] = true;
71                 }
72             }
73             if (!gotit) {
74                 logger.debug( testCase.getName() + " found unexpected iterator value: " + n);
75             }
76             TestCase.assertTrue( testCase.getName() + " found unexpected iterator value: " + n, gotit);
77         }
78         
79         // check that no expected values were unfound
80
for (int i = 0; i < vals.length; i++) {
81             if (!found[i]) {
82 // for (int j = 0; j < vals.length; j += 1) System.err.println( "#" + j + ": " + vals[j] );
83
logger.debug( testCase.getName() + " failed to find expected iterator value: " + vals[i]);
84             }
85             TestCase.assertTrue(testCase.getName() + " failed to find expected iterator value: " + vals[i], found[i]);
86         }
87         
88         // check we got the right no. of anons
89
TestCase.assertEquals( testCase.getName() + " iterator test did not find the right number of anon. nodes", countAnon, anonFound );
90     }
91     
92
93     /**
94      * Replace all blocks of white space by a single space character, just
95      * used for creating test cases.
96      *
97      * @param src the original string
98      * @return normalized version of src
99      */

100     public static String JavaDoc normalizeWhiteSpace(String JavaDoc src) {
101         StringBuffer JavaDoc result = new StringBuffer JavaDoc(src.length());
102         boolean inWhitespaceBlock = false;
103         for (int i = 0; i < src.length(); i++) {
104             char c = src.charAt(i);
105             if (Character.isWhitespace(c)) {
106                 if (!inWhitespaceBlock) {
107                     result.append(" ");
108                     inWhitespaceBlock = true;
109                 }
110             } else {
111                 inWhitespaceBlock = false;
112                 result.append(c);
113             }
114         }
115         return result.toString();
116     }
117     
118     /**
119      * Check the length of an iterator.
120      */

121     public static void assertIteratorLength(Iterator JavaDoc it, int expectedLength) {
122         int length = 0;
123         while (it.hasNext()) {
124             it.next();
125             length++;
126         }
127         TestCase.assertEquals(expectedLength, length);
128     }
129     
130     
131     /**
132      * For the purposes of counting, a value is anonymous if (a) it is an anonymous resource,
133      * or (b) it is a statement with a bNode subject or (c) it is a statement with a bNode
134      * object. This is because we cannot check bNode identity against fixed expected data values.
135      * @param n A value
136      * @return True if n is anonymous
137      */

138     protected static boolean isAnonValue( Object JavaDoc n ) {
139         return ((n instanceof Resource) && ((Resource) n).isAnon()) ||
140                ((n instanceof Statement) && ((Statement) n).getSubject().isAnon()) ||
141                ((n instanceof Statement) && isAnonValue( ((Statement) n).getObject() ));
142     }
143 }
144
145 /*
146     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
147     All rights reserved.
148
149     Redistribution and use in source and binary forms, with or without
150     modification, are permitted provided that the following conditions
151     are met:
152
153     1. Redistributions of source code must retain the above copyright
154        notice, this list of conditions and the following disclaimer.
155
156     2. Redistributions in binary form must reproduce the above copyright
157        notice, this list of conditions and the following disclaimer in the
158        documentation and/or other materials provided with the distribution.
159
160     3. The name of the author may not be used to endorse or promote products
161        derived from this software without specific prior written permission.
162
163     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
164     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
165     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
166     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
167     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
168     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
169     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
170     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
171     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
172     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
173 */

174
175
Popular Tags