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.Channel;
029: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
030: import EDU.oswego.cs.dl.util.concurrent.ThreadedExecutor;
031: import org.jicarilla.lang.AbstractAsyncEnabled;
032: import org.jicarilla.lang.ExceptionListener;
033: import org.jicarilla.lang.Invocation;
034: import org.jicarilla.lang.NoopExceptionListener;
035: import junit.framework.TestCase;
036:
037: import java.lang.reflect.Method;
038:
039: /**
040: * <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
041: * AbstractAsyncEnabled.
042: *
043: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
044: * @version $Id: AbstracAsyncEnabledTestCase.java,v 1.1 2003/11/17 21:11:10
045: * lsimons Exp $
046: */
047: public class AbstracAsyncEnabledTestCase extends TestCase {
048: public final static class Mock {
049: public boolean called = false;
050: public Object result = new Object();
051:
052: public Object call() {
053: called = true;
054: privateCall();
055: return result;
056: }
057:
058: private void privateCall() {
059: };
060: }
061:
062: public final static class MockEx {
063: public boolean called = false;
064: public Exception ex = new Exception();
065:
066: public void call() throws Exception {
067: called = true;
068: throw ex;
069: }
070: }
071:
072: public final static class AsyncEnabledImpl extends
073: AbstractAsyncEnabled {
074: public Throwable m_initializeThrowable;
075: public RuntimeException m_disposeThrowable;
076:
077: public boolean m_didInitialize = false;
078: public boolean m_didDispose = false;
079:
080: public AsyncEnabledImpl(Throwable initializeThrowable,
081: RuntimeException disposeThrowable, ThreadedExecutor tx) {
082: super (tx);
083: m_initializeThrowable = initializeThrowable;
084: m_disposeThrowable = disposeThrowable;
085: }
086:
087: public AsyncEnabledImpl(Throwable initializeThrowable,
088: RuntimeException disposeThrowable, ThreadedExecutor tx,
089: ExceptionListener ex) {
090: super (tx, ex);
091: m_initializeThrowable = initializeThrowable;
092: m_disposeThrowable = disposeThrowable;
093: }
094:
095: public boolean isRunning() {
096: return m_running;
097: }
098:
099: public boolean isStopped() {
100: return m_stopped;
101: }
102:
103: public boolean isDidInitialize() {
104: return m_didInitialize;
105: }
106:
107: public boolean isDidDispose() {
108: return m_didDispose;
109: }
110:
111: public void doInitialize() throws Throwable {
112: super .doInitialize();
113: m_didInitialize = true;
114:
115: if (m_initializeThrowable != null) {
116: throw m_initializeThrowable;
117: }
118: }
119:
120: public void doDispose() {
121: super .doDispose();
122: m_didDispose = true;
123:
124: if (m_disposeThrowable != null) {
125: throw m_disposeThrowable;
126: }
127: }
128:
129: public Channel getIncoming() {
130: return m_incoming;
131: }
132:
133: public Object getImmediateResult(Invocation invocation) {
134: return super .getImmediateResult(invocation);
135: }
136:
137: public void doHandle(Invocation i) {
138: super .doHandle(i);
139: }
140:
141: public void doHandleException(Invocation i) {
142: super .doHandleException(i);
143: }
144:
145: public void work() throws Throwable {
146: super .work();
147: }
148: }
149:
150: ThreadedExecutor tx = new ThreadedExecutor();
151: AsyncEnabledImpl aei = new AsyncEnabledImpl(null, null, tx);
152:
153: public void testConstructor() {
154: assertNotNull(aei.getIncoming());
155: assertTrue(aei.getIncoming() instanceof LinkedQueue);
156:
157: aei = new AsyncEnabledImpl(null, null, tx,
158: new NoopExceptionListener());
159: assertTrue(aei.getIncoming() instanceof LinkedQueue);
160: }
161:
162: public void testInitializeDispose() throws Throwable {
163: aei.initialize();
164: aei.dispose();
165: }
166:
167: public void testGetImmediateResult() {
168: assertNull(aei.getImmediateResult(null));
169: assertNull(aei.getImmediateResult(new Invocation()));
170: }
171:
172: public void testHandle() throws Throwable {
173: Invocation i = new Invocation();
174: Object result = aei.handle(i);
175: assertNull(result);
176: assertEquals(i, aei.getIncoming().take());
177:
178: Throwable t = null;
179: try {
180: aei.handle(null);
181: } catch (AssertionError ae) {
182: t = ae;
183: }
184: assertNotNull(t);
185: }
186:
187: public void testDoHandle() throws Throwable {
188: Mock mock = new Mock();
189: Object[] args = new Object[0];
190: Method m = Mock.class.getMethod("call", new Class[0]);
191:
192: Invocation i = new Invocation(mock, null, m, args);
193:
194: aei.doHandle(i);
195: assertTrue(mock.called);
196: assertEquals(mock.result, i.getResult());
197: }
198:
199: public void testDoHandleInCaseOfException() throws Throwable {
200: MockEx mock = new MockEx();
201: Object[] args = new Object[0];
202: Method m = MockEx.class.getMethod("call", new Class[0]);
203:
204: Invocation i = new Invocation(mock, mock, m, args);
205:
206: aei.doHandle(i);
207: assertTrue(mock.called);
208: assertEquals(mock.ex, i.getThrowable());
209: }
210:
211: public void testDoHandleInCaseOfAccessException() throws Throwable {
212: Mock mock = new Mock();
213: Object[] args = new Object[0];
214: Method[] methods = Mock.class.getDeclaredMethods();
215: Method m = null;
216: for (int i = 0; i < methods.length; i++) {
217: if (methods[i].getName().equals("privateCall")) {
218: m = methods[i];
219: break;
220: }
221: }
222:
223: Invocation i = new Invocation(mock, mock, m, args);
224:
225: aei.doHandle(i);
226: assertTrue(i.getThrowable() instanceof IllegalAccessException);
227: }
228:
229: public void testDoHandleException() throws Throwable {
230: aei.doHandleException(new Invocation());
231: }
232:
233: public void testWork() throws Throwable {
234: MockEx mock = new MockEx();
235: Object[] args = new Object[0];
236: Method m = MockEx.class.getMethod("call", new Class[0]);
237:
238: Invocation i = new Invocation(mock, null, m, args);
239:
240: aei.getIncoming().put(i);
241: aei.work();
242: assertTrue(mock.called);
243: assertNull(aei.getIncoming().peek());
244: }
245: }
|