001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.plumbing.test;
027:
028: import org.jicarilla.plumbing.NoopSink;
029: import org.jicarilla.lang.Selector;
030: import org.jicarilla.plumbing.SimpleScreener;
031: import org.jicarilla.plumbing.Sink;
032: import org.jicarilla.plumbing.Sink;
033: import org.jicarilla.plumbing.SimpleScreener;
034: import junit.framework.TestCase;
035:
036: import java.util.ArrayList;
037: import java.util.List;
038:
039: /**
040: * <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
041: * SimpleScreener.
042: *
043: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
044: * @version $Id: SimpleScreenerTestCase.java,v 1.1 2003/11/18 16:09:16 lsimons
045: * Exp $
046: */
047: public class SimpleScreenerTestCase extends TestCase {
048: public static class CountingSink implements Sink {
049: public int put = 0;
050: public int offered = 0;
051: public static int sput = 0;
052: public static int soffered = 0;
053:
054: public void put(Object o) throws InterruptedException {
055: put++;
056: sput++;
057: }
058:
059: public boolean offer(Object o, long l)
060: throws InterruptedException {
061: offered++;
062: soffered++;
063: return true;
064: }
065: }
066:
067: public static class YesSelector implements Selector {
068: public boolean select(Object object) {
069: return true;
070: }
071: }
072:
073: public static class NoSelector implements Selector {
074: public boolean select(Object object) {
075: return false;
076: }
077: }
078:
079: public void testConstructor() {
080: new SimpleScreener();
081:
082: assertEquals(0, new Screener().getSinks().size());
083: assertEquals(0, new Screener().getSize());
084: }
085:
086: public void testAdd() {
087: Screener sc = new Screener();
088:
089: NoopSink s = new NoopSink();
090: sc.addSink(new NoSelector(), s);
091:
092: assertEquals(1, sc.getSinks().size());
093: assertEquals(sc.getSinks().size(), sc.getSize());
094:
095: NoopSink s2 = new NoopSink();
096: sc.addSink(new NoSelector(), s2);
097: assertEquals(2, sc.getSinks().size());
098:
099: Throwable t = null;
100: try {
101: sc.addSink(null, s2);
102: } catch (AssertionError ae) {
103: t = ae;
104: }
105: assertNotNull(t);
106:
107: t = null;
108: try {
109: sc.addSink(new NoSelector(), null);
110: } catch (AssertionError ae) {
111: t = ae;
112: }
113: assertNotNull(t);
114: }
115:
116: public void testPut() throws InterruptedException {
117: Screener sc = new Screener();
118:
119: CountingSink s = new CountingSink();
120: sc.addSink(new NoSelector(), s);
121: CountingSink s2 = new CountingSink();
122: sc.addSink(new YesSelector(), s2);
123:
124: sc.put(new Object());
125: sc.put(new Object());
126: sc.put(new Object());
127: sc.put(new Object());
128:
129: assertEquals(4, s2.put);
130: assertEquals(4, CountingSink.sput);
131: }
132:
133: public void testExhaustedPut() throws Exception {
134: Screener sc = new Screener();
135:
136: CountingSink s = new CountingSink();
137: sc.addSink(new NoSelector(), s);
138: CountingSink s2 = new CountingSink();
139: sc.addSink(new NoSelector(), s2);
140: CountingSink s3 = new CountingSink();
141: sc.addSink(new NoSelector(), s3);
142: CountingSink s4 = new CountingSink();
143: sc.addSink(new NoSelector(), s4);
144:
145: CountingSink.sput = 0;
146: sc.put(new Object());
147: sc.put(new Object());
148: sc.put(new Object());
149: sc.put(new Object());
150:
151: assertEquals(4, s4.put);
152: assertEquals(4, CountingSink.sput);
153: }
154:
155: public void testOffer() throws InterruptedException {
156: Screener sc = new Screener();
157:
158: CountingSink s = new CountingSink();
159: sc.addSink(new NoSelector(), s);
160: CountingSink s2 = new CountingSink();
161: sc.addSink(new YesSelector(), s2);
162:
163: CountingSink.soffered = 0;
164:
165: sc.offer(new Object(), 10);
166: sc.offer(new Object(), 10);
167: sc.offer(new Object(), 10);
168: sc.offer(new Object(), 10);
169:
170: assertEquals(4, s2.offered);
171: assertEquals(4, CountingSink.soffered);
172: }
173:
174: public void testOffer2() throws InterruptedException {
175: Screener sc = new Screener();
176:
177: CountingSink s = new CountingSink();
178: sc.addSink(new NoSelector(), s);
179: CountingSink s2 = new CountingSink();
180: sc.addSink(new NoSelector(), s2);
181: CountingSink s3 = new CountingSink();
182: sc.addSink(new NoSelector(), s3);
183: CountingSink s4 = new CountingSink();
184: sc.addSink(new YesSelector(), s4);
185: CountingSink s5 = new CountingSink();
186: sc.addSink(new NoSelector(), s5);
187:
188: CountingSink.soffered = 0;
189:
190: sc.offer(new Object(), 10);
191: sc.offer(new Object(), 10);
192: sc.offer(new Object(), 10);
193: sc.offer(new Object(), 10);
194:
195: assertEquals(0, s2.offered);
196: assertEquals(4, s4.offered);
197: assertEquals(4, CountingSink.soffered);
198: }
199:
200: public static class Screener extends SimpleScreener {
201: public List getSinks() {
202: List sinks = new ArrayList();
203: sinks.addAll(super .m_switch.values());
204: return sinks;
205: }
206:
207: public int getSize() {
208: return super.m_switch.size();
209: }
210: }
211: }
|