1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
package org.biojava.utils.walker;
import junit.framework.TestCase;
import org.biojava.bio.BioException;
import org.biojava.bio.seq.ComponentFeature;
import org.biojava.bio.seq.FeatureFilter;
import org.biojava.bio.seq.StrandedFeature;
import org.biojava.bio.symbol.RangeLocation;
/**
* Test some walkers for some viewers, ensuring that they get the events we
* would expect.
*
* @author Matthew Pocock
*/
public class WalkerTest
extends TestCase {
private FeatureFilter booring1;
private FeatureFilter booring2;
private FeatureFilter booring4;
private FeatureFilter and;
private FeatureFilter andOr;
protected void setUp() {
booring1 = new FeatureFilter.OverlapsLocation(new RangeLocation(20, 50));
booring2 = new FeatureFilter.ByClass(StrandedFeature.class);
booring4 = new FeatureFilter.ByClass(ComponentFeature.class);
and = new FeatureFilter.And(booring1, booring2);
andOr = new FeatureFilter.And(booring1,
new FeatureFilter.Or(booring2, booring4));
}
public void testCountAll() {
try {
CountAll ca = new CountAll();
Walker walker = WalkerFactory.getInstance().getWalker(ca);
walker.walk(booring1, ca);
assertEquals("One filter: " + booring1, 1, ca.count);
ca.count = 0;
walker.walk(and, ca);
assertEquals("Three filters: " + and, 3, ca.count);
ca.count = 0;
walker.walk(andOr, ca);
assertEquals("Five filters: " + andOr, 5, ca.count);
} catch (BioException be) {
throw (AssertionError) new AssertionError(
"Could not instantiate visitor").initCause(be);
}
}
public void testCountSome() {
try {
CountByClass cbc = new CountByClass();
Walker walker = WalkerFactory.getInstance().getWalker(cbc);
walker.walk(booring1, cbc);
assertEquals("One filter, none interesting", 0, cbc.count);
cbc.count = 0;
walker.walk(booring2, cbc);
assertEquals("One filter, one interesting", 1, cbc.count);
cbc.count = 0;
walker.walk(andOr, cbc);
assertEquals("Five filter, two interesting", 2, cbc.count);
} catch (BioException be) {
throw (AssertionError) new AssertionError(
"Could not instantiate visitor").initCause(be);
}
}
public void testCountWithFallback() {
try {
CountWithFallback cwf = new CountWithFallback();
Walker walker = WalkerFactory.getInstance().getWalker(cwf);
walker.walk(booring1, cwf);
assertEquals("One filter, one booring", 0, cwf.others);
assertEquals("One filter, zero interesting", 1, cwf.byLoc);
cwf.others = cwf.byLoc = 0;
walker.walk(booring2, cwf);
assertEquals("One filter, one booring", 1, cwf.others);
assertEquals("One filter, zero interesting", 0, cwf.byLoc);
cwf.others = cwf.byLoc = 0;
walker.walk(andOr, cwf);
assertEquals("One filter, four booring", 4, cwf.others);
assertEquals("One filter, one interesting", 1, cwf.byLoc);
} catch (BioException be) {
throw (AssertionError) new AssertionError(
"Could not instantiate visitor").initCause(be);
}
}
public void testReturnAll() {
try {
ReturnOne ra = new ReturnOne();
Walker walker = WalkerFactory.getInstance().getWalker(ra);
walker.walk(booring1, ra);
assertEquals("One filter", new Integer(1), walker.getValue());
walker.walk(andOr, ra);
assertEquals("Five filters", new Integer(1), walker.getValue());
} catch (BioException be) {
throw (AssertionError) new AssertionError(
"Could not instantiate visitor").initCause(be);
}
}
public class CountAll implements Visitor {
int count = 0;
public void featureFilter(FeatureFilter filter) {
System.err.println("Increasing counter: " + filter);
count++;
}
}
public class CountByClass
implements Visitor {
int count = 0;
public void byClass(FeatureFilter.ByClass byClass) {
count++;
}
}
public class CountWithFallback
implements Visitor {
int byLoc = 0;
int others = 0;
public void featureFilter(FeatureFilter filter) {
System.err.println("Feature: " + filter);
others++;
}
public void overlapsLocation(FeatureFilter.OverlapsLocation overlaps) {
System.err.println("OverlapsLocation: " + overlaps);
byLoc++;
}
}
public class ReturnOne
implements Visitor {
public Integer featureFilter(FeatureFilter filter) {
return new Integer(1);
}
}
}
|