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 EDU.oswego.cs.dl.util.concurrent.Executor;
029: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
030: import EDU.oswego.cs.dl.util.concurrent.ThreadedExecutor;
031: import org.jicarilla.plumbing.Connector;
032: import org.jicarilla.plumbing.Sink;
033: import org.jicarilla.plumbing.Source;
034: import org.jicarilla.plumbing.Sink;
035: import org.jicarilla.plumbing.Source;
036: import junit.framework.TestCase;
037:
038: /**
039: * <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
040: * Connector.
041: *
042: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
043: * @version $Id: ConnectorTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons Exp
044: * $
045: */
046: public class ConnectorTestCase extends TestCase {
047: public static class AccessibleConnector extends Connector {
048: public AccessibleConnector(Executor ex, Source source, Sink sink) {
049: super (source, sink, ex);
050: }
051:
052: public Source getSource() {
053: return m_source;
054: }
055:
056: public Sink getSink() {
057: return m_sink;
058: }
059:
060: public Executor getExecutor() {
061: return m_executor;
062: }
063: }
064:
065: ThreadedExecutor tx = new ThreadedExecutor();
066: LinkedQueue q = new LinkedQueue();
067:
068: public static class MockSource implements Source {
069: Object o = new Object();
070: int takes = 0;
071:
072: public Object take() throws InterruptedException {
073: takes++;
074: return o;
075: }
076:
077: public Object poll(long l) throws InterruptedException {
078: takes++;
079: return o;
080: }
081:
082: };
083:
084: public static class MockSink implements Sink {
085: Object o = null;
086: int puts = 0;
087:
088: public void put(Object obj) throws InterruptedException {
089: puts++;
090: o = obj;
091: }
092:
093: public boolean offer(Object obj, long l)
094: throws InterruptedException {
095: puts++;
096: o = obj;
097: return true;
098: }
099: }
100:
101: MockSource source = new MockSource();
102: MockSink sink = new MockSink();
103: public AccessibleConnector c = new AccessibleConnector(tx, source,
104: sink);
105:
106: public void testConstructor() {
107: assertEquals(sink, c.getSink());
108: assertEquals(source, c.getSource());
109: assertEquals(tx, c.getExecutor());
110:
111: Throwable t = null;
112: try {
113: new AccessibleConnector(null, source, sink);
114: } catch (AssertionError ae) {
115: t = ae;
116: }
117: assertNotNull(t);
118:
119: t = null;
120: try {
121: new AccessibleConnector(tx, null, sink);
122: } catch (AssertionError ae) {
123: t = ae;
124: }
125: assertNotNull(t);
126:
127: t = null;
128: try {
129: new AccessibleConnector(tx, source, null);
130: } catch (AssertionError ae) {
131: t = ae;
132: }
133: assertNotNull(t);
134: }
135:
136: public void testWork() throws Throwable {
137: c.initialize();
138: Thread.sleep(400);
139: c.dispose();
140: Thread.sleep(400);
141:
142: assertEquals(source.o, sink.o);
143: assertTrue(source.takes > 2);
144: assertTrue(sink.puts > 2);
145: }
146: }
|