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.lang.test;
027:
028: import EDU.oswego.cs.dl.util.concurrent.Executor;
029: import EDU.oswego.cs.dl.util.concurrent.ThreadedExecutor;
030: import org.jicarilla.lang.AbstractExecutable;
031: import org.jicarilla.lang.ExceptionListener;
032: import org.jicarilla.lang.NoopExceptionListener;
033: import junit.framework.TestCase;
034:
035: /**
036: * <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
037: * AbstractExecutable.
038: *
039: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
040: * @version $Id: AbstracExecutableTestCase.java,v 1.2 2003/11/18 16:09:16
041: * lsimons Exp $
042: */
043: public class AbstracExecutableTestCase extends TestCase {
044: public static class StoringExceptionListener implements
045: ExceptionListener {
046: public Throwable t = null;
047:
048: public void exceptionOccurred(Throwable th) {
049: t = th;
050: }
051: }
052:
053: public static class ExecutableImpl extends AbstractExecutable {
054: public int m_workDone = 0;
055: public Throwable m_workException = null;
056:
057: public ExecutableImpl(Throwable workException,
058: Executor executor, ExceptionListener listener) {
059: this (executor, listener);
060: m_workException = workException;
061: }
062:
063: public ExecutableImpl() {
064: super ();
065: }
066:
067: public ExecutableImpl(Executor executor) {
068: super (executor);
069: }
070:
071: public ExecutableImpl(Executor executor,
072: ExceptionListener listener) {
073: super (executor, listener);
074: }
075:
076: public Executor getExecutor() {
077: return m_executor;
078: }
079:
080: public AbstractExecutable.Worker getWorker() {
081: return m_worker;
082: }
083:
084: public ExceptionListener getListener() {
085: return m_listener;
086: }
087:
088: public void work() throws Throwable {
089: super .work();
090: m_workDone++;
091: if (m_workException != null) {
092: throw m_workException;
093: }
094: }
095: }
096:
097: public void testConstructors() {
098: ExecutableImpl ex = new ExecutableImpl();
099: assertTrue(ex.getExecutor() instanceof ThreadedExecutor);
100: //assertNotNull( ex.getListener() );
101: //assertTrue( ex.getListener() instanceof NoopExceptionListener );
102: assertNull(ex.getListener());
103: assertNull(ex.getWorker());
104:
105: ThreadedExecutor te = new ThreadedExecutor();
106: ex = new ExecutableImpl(te);
107: assertEquals(te, ex.getExecutor());
108: //assertNotNull( ex.getListener() );
109: //assertTrue( ex.getListener() instanceof NoopExceptionListener );
110: assertNull(ex.getListener());
111: assertNull(ex.getWorker());
112:
113: NoopExceptionListener nel = new NoopExceptionListener();
114: ex = new ExecutableImpl(te, nel);
115: assertEquals(te, ex.getExecutor());
116: assertEquals(nel, ex.getListener());
117: assertNull(ex.getWorker());
118:
119: Throwable t = null;
120: try {
121: ex = new ExecutableImpl(null, nel);
122: } catch (AssertionError ae) {
123: t = ae;
124: }
125: assertNotNull(t);
126:
127: ex = new ExecutableImpl(te, null);
128: }
129:
130: public void testInitialize() throws Throwable {
131: ThreadedExecutor te = new ThreadedExecutor();
132: NoopExceptionListener nel = new NoopExceptionListener();
133:
134: ExecutableImpl ex = new ExecutableImpl(te, nel);
135: assertNull(ex.getWorker());
136: ex.initialize();
137: assertNotNull(ex.getWorker());
138:
139: Thread.sleep(1000);
140:
141: assertTrue(ex.m_workDone > 0);
142: }
143:
144: public void testListenToWorkException() throws Throwable {
145: ThreadedExecutor te = new ThreadedExecutor();
146: StoringExceptionListener sel = new StoringExceptionListener();
147:
148: Exception e = new Exception();
149:
150: ExecutableImpl ex = new ExecutableImpl(e, te, sel);
151: ex.initialize();
152:
153: Thread.sleep(500);
154:
155: assertEquals(e, sel.t);
156: }
157:
158: public void testNoListener() throws Throwable {
159: ThreadedExecutor te = new ThreadedExecutor();
160:
161: Exception e = new Exception();
162:
163: ExecutableImpl ex = new ExecutableImpl(e, te, null);
164: ex.initialize();
165: Thread.sleep(500);
166: }
167:
168: public void testDispose() throws Throwable {
169: ExecutableImpl ex = new ExecutableImpl();
170:
171: ex.initialize();
172: Thread.sleep(500);
173: ex.dispose();
174: Thread.sleep(200);
175:
176: assertNull(ex.getWorker());
177: assertNull(ex.getListener());
178: assertNull(ex.getExecutor());
179: }
180: }
|