001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestFilters.java,v 1.8 2008/01/02 12:08:45 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.util.iterator.test;
008:
009: import java.util.*;
010:
011: import junit.framework.TestSuite;
012:
013: import com.hp.hpl.jena.rdf.model.test.ModelTestBase;
014: import com.hp.hpl.jena.shared.JenaException;
015: import com.hp.hpl.jena.util.iterator.*;
016:
017: public class TestFilters extends ModelTestBase {
018: public TestFilters(String name) {
019: super (name);
020: }
021:
022: public static TestSuite suite() {
023: return new TestSuite(TestFilters.class);
024: }
025:
026: public void testFilterAnyExists() {
027: assertInstanceOf(Filter.class, Filter.any);
028: }
029:
030: public void testFilterAnyAcceptsThings() {
031: assertTrue(Filter.any.accept("hello"));
032: assertTrue(Filter.any.accept(new Integer(17)));
033: assertTrue(Filter.any.accept(node("frodo")));
034: assertTrue(Filter.any.accept(node("_cheshire")));
035: assertTrue(Filter.any.accept(node("17")));
036: assertTrue(Filter.any.accept(triple("s p o")));
037: assertTrue(Filter.any.accept(Filter.any));
038: assertTrue(Filter.any.accept(this ));
039: }
040:
041: public void testFilterFilterMethod() {
042: ExtendedIterator it = Filter.any
043: .filterKeep(NullIterator.instance);
044: assertFalse(it.hasNext());
045: }
046:
047: public void testFilteringThings() {
048: ExtendedIterator it = iteratorOfStrings("gab geb bag big lava hall end");
049: Filter f = new Filter() {
050: public boolean accept(Object o) {
051: return ((String) o).charAt(1) == 'a';
052: }
053: };
054: assertEquals(listOfStrings("gab bag lava hall"),
055: iteratorToList(f.filterKeep(it)));
056: }
057:
058: public void testAnyFilterSimple() {
059: ExtendedIterator it = iteratorOfStrings("");
060: assertSame(it, Filter.any.filterKeep(it));
061: }
062:
063: protected Filter containsA = new Filter() {
064: public boolean accept(Object o) {
065: return contains(o, 'a');
066: }
067: };
068:
069: public void testFilterAnd() {
070: Filter containsB = new Filter() {
071: public boolean accept(Object o) {
072: return contains(o, 'b');
073: }
074: };
075: Filter f12 = containsA.and(containsB);
076: assertFalse(f12.accept("a"));
077: assertFalse(f12.accept("b"));
078: assertTrue(f12.accept("ab"));
079: assertTrue(f12.accept("xyzapqrbijk"));
080: assertTrue(f12.accept("ba"));
081: }
082:
083: public void testFilterShortcircuit() {
084: Filter oops = new Filter() {
085: public boolean accept(Object o) {
086: throw new JenaException("oops");
087: }
088: };
089: Filter f12 = containsA.and(oops);
090: assertFalse(f12.accept("z"));
091: try {
092: f12.accept("a");
093: fail("oops");
094: } catch (JenaException e) {
095: assertEquals("oops", e.getMessage());
096: }
097: }
098:
099: public void testAnyAndTrivial() {
100: assertSame(containsA, Filter.any.and(containsA));
101: }
102:
103: public void testSomethingAndAny() {
104: assertSame(containsA, containsA.and(Filter.any));
105: }
106:
107: public void testFilterDropIterator() {
108: Iterator i = iteratorOfStrings("there's an a in some animals");
109: Iterator it = new FilterDropIterator(containsA, i);
110: assertEquals(listOfStrings("there's in some"),
111: iteratorToList(it));
112: }
113:
114: public void testFilterKeepIterator() {
115: Iterator i = iteratorOfStrings("there's an a in some animals");
116: Iterator it = new FilterKeepIterator(containsA, i);
117: assertEquals(listOfStrings("an a animals"), iteratorToList(it));
118: }
119:
120: protected boolean contains(Object o, char ch) {
121: return o.toString().indexOf(ch) > -1;
122: }
123: }
124:
125: /*
126: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
127: * All rights reserved.
128: *
129: * Redistribution and use in source and binary forms, with or without
130: * modification, are permitted provided that the following conditions
131: * are met:
132: * 1. Redistributions of source code must retain the above copyright
133: * notice, this list of conditions and the following disclaimer.
134: * 2. Redistributions in binary form must reproduce the above copyright
135: * notice, this list of conditions and the following disclaimer in the
136: * documentation and/or other materials provided with the distribution.
137: * 3. The name of the author may not be used to endorse or promote products
138: * derived from this software without specific prior written permission.
139: *
140: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
141: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
142: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
143: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
144: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
145: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
146: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
147: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
148: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
149: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
150: */
|