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 org.jicarilla.lang.AbstractActive;
029: import org.jicarilla.lang.Recyclable;
030: import junit.framework.TestCase;
031:
032: /**
033: * <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
034: * AbstracActiveTestCase.
035: *
036: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
037: * @version $Id: AbstracActiveTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons
038: * Exp $
039: */
040: public class AbstracActiveTestCase extends TestCase {
041: public static class ActiveImpl extends AbstractActive {
042: public Throwable m_initializeThrowable;
043: public RuntimeException m_disposeThrowable;
044:
045: public ActiveImpl(Throwable initializeThrowable,
046: RuntimeException disposeThrowable) {
047: m_initializeThrowable = initializeThrowable;
048: m_disposeThrowable = disposeThrowable;
049: }
050:
051: public boolean isRunning() {
052: return super .isActive();
053: }
054:
055: public boolean isStopped() {
056: return super .isDisposed();
057: }
058:
059: public boolean isDidInitialize() {
060: return super .isInitialized();
061: }
062:
063: public boolean isDidDispose() {
064: return super .isDisposed();
065: }
066:
067: public void doInitialize() throws Throwable {
068: super .doInitialize();
069:
070: if (m_initializeThrowable != null) {
071: throw m_initializeThrowable;
072: }
073: }
074:
075: public void doDispose() throws Throwable {
076: super .doDispose();
077:
078: if (m_disposeThrowable != null) {
079: throw m_disposeThrowable;
080: }
081: }
082:
083: public void doStuff() {
084: super .checkActive();
085:
086: // do stuff
087: }
088:
089: public void doStuffLazily() throws Throwable {
090: super .lazyInitialization();
091: }
092: }
093:
094: public final static class ActiveRecyclableImpl extends ActiveImpl
095: implements Recyclable {
096: public ActiveRecyclableImpl(Throwable initializeThrowable,
097: RuntimeException disposeThrowable) {
098: super (initializeThrowable, disposeThrowable);
099: }
100:
101: public void recycle() {
102: }
103: }
104:
105: ActiveImpl act = new ActiveImpl(null, null);
106:
107: public void testRunningFlag() throws Throwable {
108: assertFalse(act.isRunning());
109: act.initialize();
110: assertTrue(act.isRunning());
111: act.dispose();
112: assertFalse(act.isRunning());
113: }
114:
115: public void testStoppedFlag() throws Throwable {
116: assertFalse(act.isStopped());
117: act.initialize();
118: assertFalse(act.isStopped());
119: act.dispose();
120: assertTrue(act.isStopped());
121: }
122:
123: public void testDoInitializeIsCalled() throws Throwable {
124: assertFalse(act.isDidInitialize());
125: act.initialize();
126: assertTrue(act.isDidInitialize());
127: }
128:
129: public void testDoDisposeIsCalled() throws Throwable {
130: assertFalse(act.isDidDispose());
131: act.initialize();
132: act.dispose();
133: assertTrue(act.isDidDispose());
134: }
135:
136: public void testInitializeMultipleIsOkay() throws Throwable {
137: act.initialize();
138: act.initialize();
139: act.initialize();
140: act.initialize();
141: act.initialize();
142: }
143:
144: public void testDisposeMultipleIsOkay() throws Throwable {
145: act.initialize();
146: act.dispose();
147: act.dispose();
148: act.dispose();
149: act.dispose();
150: }
151:
152: public void testInitializePropagatesException() {
153: Exception ex = new Exception();
154: act = new ActiveImpl(ex, null);
155:
156: Throwable t = null;
157: try {
158: act.initialize();
159: } catch (Throwable th) {
160: t = th;
161: }
162: assertNotNull(t);
163: assertEquals(ex, t);
164: }
165:
166: public void testDisposePropagatesException() throws Throwable {
167: RuntimeException ex = new RuntimeException();
168: act = new ActiveImpl(null, ex);
169: act.initialize();
170:
171: Throwable t = null;
172: try {
173: act.dispose();
174: } catch (RuntimeException re) {
175: t = re;
176: }
177: assertNotNull(t);
178: assertEquals(ex, t);
179: }
180:
181: public void testCheckActive() throws Throwable {
182: Throwable t = null;
183: try {
184: act.doStuff();
185: } catch (AssertionError th) {
186: t = th;
187: }
188: assertNotNull(t);
189:
190: act.initialize();
191: act.doStuff();
192: act.doStuff();
193: act.doStuff();
194: act.dispose();
195:
196: t = null;
197: try {
198: act.doStuff();
199: } catch (AssertionError th) {
200: t = th;
201: }
202: assertNotNull(t);
203: }
204:
205: public void testLazyInitialization() throws Throwable {
206: act.doStuffLazily();
207: assertTrue(act.isDidInitialize());
208: assertTrue(act.isRunning());
209:
210: act.doStuffLazily();
211: assertTrue(act.isDidInitialize());
212: assertTrue(act.isRunning());
213:
214: act.dispose();
215: assertFalse(act.isRunning());
216: assertTrue(act.isDidDispose());
217:
218: Throwable t = null;
219: try {
220: act.doStuffLazily();
221: } catch (AssertionError th) {
222: t = th;
223: }
224: assertNotNull(t);
225: assertFalse(act.isRunning());
226:
227: ActiveRecyclableImpl act2 = new ActiveRecyclableImpl(null, null);
228: act2.doStuffLazily();
229: assertTrue(act2.isDidInitialize());
230: assertTrue(act2.isRunning());
231:
232: act2.doStuffLazily();
233: assertTrue(act2.isDidInitialize());
234: assertTrue(act2.isRunning());
235:
236: act2.dispose();
237: assertFalse(act2.isRunning());
238: assertTrue(act2.isDidDispose());
239:
240: // now this is okay
241: act2.doStuffLazily();
242: assertTrue(act2.isDidInitialize());
243: assertTrue(act2.isRunning());
244: assertFalse(act2.isDidDispose());
245: act2.doStuffLazily();
246: }
247: }
|