1 10 package com.hp.hpl.jena.reasoner.test; 11 12 import junit.framework.TestCase; 13 import java.util.Iterator ; 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 26 public class TestUtil { 27 28 34 public static void assertIteratorValues(TestCase testCase, Iterator it, Object [] vals) { 35 assertIteratorValues( testCase, it, vals, 0 ); 36 } 37 38 48 public static void assertIteratorValues(TestCase testCase, Iterator it, Object [] 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 n = it.next(); 59 boolean gotit = false; 60 61 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 for (int i = 0; i < vals.length; i++) { 81 if (!found[i]) { 82 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 TestCase.assertEquals( testCase.getName() + " iterator test did not find the right number of anon. nodes", countAnon, anonFound ); 90 } 91 92 93 100 public static String normalizeWhiteSpace(String src) { 101 StringBuffer result = new StringBuffer (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 121 public static void assertIteratorLength(Iterator 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 138 protected static boolean isAnonValue( Object 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 174 175 | Popular Tags |