|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractActiveTestCase.java | 50% | 7.7% | 23.8% | 11.6% |
|
||||||||||||||
| 1 |
/* ====================================================================
|
|
| 2 |
The Jicarilla Software License
|
|
| 3 |
|
|
| 4 |
Copyright (c) 2003 Leo Simons.
|
|
| 5 |
All rights reserved.
|
|
| 6 |
|
|
| 7 |
Permission is hereby granted, free of charge, to any person obtaining
|
|
| 8 |
a copy of this software and associated documentation files (the
|
|
| 9 |
"Software"), to deal in the Software without restriction, including
|
|
| 10 |
without limitation the rights to use, copy, modify, merge, publish,
|
|
| 11 |
distribute, sublicense, and/or sell copies of the Software, and to
|
|
| 12 |
permit persons to whom the Software is furnished to do so, subject to
|
|
| 13 |
the following conditions:
|
|
| 14 |
|
|
| 15 |
The above copyright notice and this permission notice shall be
|
|
| 16 |
included in all copies or substantial portions of the Software.
|
|
| 17 |
|
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
| 19 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
| 20 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
| 21 |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
| 22 |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
| 23 |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
| 24 |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
| 25 |
==================================================================== */
|
|
| 26 |
package org.jicarilla.framework.test;
|
|
| 27 |
|
|
| 28 |
import org.jicarilla.framework.AbstractActive;
|
|
| 29 |
import org.jicarilla.framework.Recyclable;
|
|
| 30 |
import junit.framework.TestCase;
|
|
| 31 |
|
|
| 32 |
/**
|
|
| 33 |
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
|
|
| 34 |
* AbstractActiveTestCase.
|
|
| 35 |
*
|
|
| 36 |
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
|
|
| 37 |
* @version $Id: AbstractActiveTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons
|
|
| 38 |
* Exp $
|
|
| 39 |
*/
|
|
| 40 |
public class AbstractActiveTestCase extends TestCase |
|
| 41 |
{
|
|
| 42 |
public static class ActiveImpl extends AbstractActive |
|
| 43 |
{
|
|
| 44 |
public Throwable m_initializeThrowable;
|
|
| 45 |
public RuntimeException m_disposeThrowable;
|
|
| 46 |
|
|
| 47 | 2 |
public ActiveImpl( Throwable initializeThrowable,
|
| 48 |
RuntimeException disposeThrowable ) |
|
| 49 |
{
|
|
| 50 | 2 |
m_initializeThrowable = initializeThrowable; |
| 51 | 2 |
m_disposeThrowable = disposeThrowable; |
| 52 |
} |
|
| 53 |
|
|
| 54 | 0 |
public boolean isRunning() |
| 55 |
{
|
|
| 56 | 0 |
return super.isActive(); |
| 57 |
} |
|
| 58 |
|
|
| 59 | 0 |
public boolean isStopped() |
| 60 |
{
|
|
| 61 | 0 |
return super.isDisposed(); |
| 62 |
} |
|
| 63 |
|
|
| 64 | 1 |
public boolean isDidInitialize() |
| 65 |
{
|
|
| 66 | 1 |
return super.isInitialized(); |
| 67 |
} |
|
| 68 |
|
|
| 69 | 1 |
public boolean isDidDispose() |
| 70 |
{
|
|
| 71 | 1 |
return super.isDisposed(); |
| 72 |
} |
|
| 73 |
|
|
| 74 | 2 |
public void doInitialize() |
| 75 |
throws Throwable
|
|
| 76 |
{
|
|
| 77 | 2 |
super.doInitialize();
|
| 78 |
|
|
| 79 | 2 |
if( m_initializeThrowable != null ) |
| 80 |
{
|
|
| 81 | 0 |
throw m_initializeThrowable;
|
| 82 |
} |
|
| 83 |
} |
|
| 84 |
|
|
| 85 | 1 |
public void doDispose() throws Throwable |
| 86 |
{
|
|
| 87 | 1 |
super.doDispose();
|
| 88 |
|
|
| 89 | 1 |
if( m_disposeThrowable != null ) |
| 90 |
{
|
|
| 91 | 0 |
throw m_disposeThrowable;
|
| 92 |
} |
|
| 93 |
} |
|
| 94 |
|
|
| 95 | 0 |
public void doStuff() |
| 96 |
{
|
|
| 97 | 0 |
super.checkActive();
|
| 98 |
|
|
| 99 |
// do stuff
|
|
| 100 |
} |
|
| 101 |
|
|
| 102 | 0 |
public void doStuffLazily() throws Throwable |
| 103 |
{
|
|
| 104 | 0 |
super.lazyInitialization();
|
| 105 |
} |
|
| 106 |
} |
|
| 107 |
|
|
| 108 |
public final static class ActiveRecyclableImpl extends ActiveImpl |
|
| 109 |
implements Recyclable
|
|
| 110 |
{
|
|
| 111 | 0 |
public ActiveRecyclableImpl( Throwable initializeThrowable,
|
| 112 |
RuntimeException disposeThrowable ) |
|
| 113 |
{
|
|
| 114 | 0 |
super( initializeThrowable, disposeThrowable );
|
| 115 |
} |
|
| 116 |
|
|
| 117 | 0 |
public void recycle() |
| 118 |
{
|
|
| 119 |
} |
|
| 120 |
} |
|
| 121 |
|
|
| 122 |
ActiveImpl act = new ActiveImpl( null, null ); |
|
| 123 |
|
|
| 124 | 0 |
public void testRunningFlag() throws Throwable |
| 125 |
{
|
|
| 126 | 0 |
assertFalse( act.isRunning() ); |
| 127 | 0 |
act.initialize(); |
| 128 | 0 |
assertTrue( act.isRunning() ); |
| 129 | 0 |
act.dispose(); |
| 130 | 0 |
assertFalse( act.isRunning() ); |
| 131 |
} |
|
| 132 |
|
|
| 133 | 0 |
public void testStoppedFlag() throws Throwable |
| 134 |
{
|
|
| 135 | 0 |
assertFalse( act.isStopped() ); |
| 136 | 0 |
act.initialize(); |
| 137 | 0 |
assertFalse( act.isStopped() ); |
| 138 | 0 |
act.dispose(); |
| 139 | 0 |
assertTrue( act.isStopped() ); |
| 140 |
} |
|
| 141 |
|
|
| 142 | 0 |
public void testDoInitializeIsCalled() throws Throwable |
| 143 |
{
|
|
| 144 | 0 |
assertFalse( act.isDidInitialize() ); |
| 145 | 0 |
act.initialize(); |
| 146 | 0 |
assertTrue( act.isDidInitialize() ); |
| 147 |
} |
|
| 148 |
|
|
| 149 | 0 |
public void testDoDisposeIsCalled() throws Throwable |
| 150 |
{
|
|
| 151 | 0 |
assertFalse( act.isDidDispose() ); |
| 152 | 0 |
act.initialize(); |
| 153 | 0 |
act.dispose(); |
| 154 | 0 |
assertTrue( act.isDidDispose() ); |
| 155 |
} |
|
| 156 |
|
|
| 157 | 0 |
public void testInitializeMultipleIsOkay() throws Throwable |
| 158 |
{
|
|
| 159 | 0 |
act.initialize(); |
| 160 | 0 |
act.initialize(); |
| 161 | 0 |
act.initialize(); |
| 162 | 0 |
act.initialize(); |
| 163 | 0 |
act.initialize(); |
| 164 |
} |
|
| 165 |
|
|
| 166 | 0 |
public void testDisposeMultipleIsOkay() throws Throwable |
| 167 |
{
|
|
| 168 | 0 |
act.initialize(); |
| 169 | 0 |
act.dispose(); |
| 170 | 0 |
act.dispose(); |
| 171 | 0 |
act.dispose(); |
| 172 | 0 |
act.dispose(); |
| 173 |
} |
|
| 174 |
|
|
| 175 | 0 |
public void testInitializePropagatesException() |
| 176 |
{
|
|
| 177 | 0 |
Exception ex = new Exception();
|
| 178 | 0 |
act = new ActiveImpl( ex, null ); |
| 179 |
|
|
| 180 | 0 |
Throwable t = null;
|
| 181 | 0 |
try
|
| 182 |
{
|
|
| 183 | 0 |
act.initialize(); |
| 184 |
} |
|
| 185 |
catch( Throwable th )
|
|
| 186 |
{
|
|
| 187 | 0 |
t = th; |
| 188 |
} |
|
| 189 | 0 |
assertNotNull( t ); |
| 190 | 0 |
assertEquals( ex, t ); |
| 191 |
} |
|
| 192 |
|
|
| 193 | 0 |
public void testDisposePropagatesException() throws Throwable |
| 194 |
{
|
|
| 195 | 0 |
RuntimeException ex = new RuntimeException();
|
| 196 | 0 |
act = new ActiveImpl( null, ex ); |
| 197 | 0 |
act.initialize(); |
| 198 |
|
|
| 199 | 0 |
Throwable t = null;
|
| 200 | 0 |
try
|
| 201 |
{
|
|
| 202 | 0 |
act.dispose(); |
| 203 |
} |
|
| 204 |
catch( RuntimeException re )
|
|
| 205 |
{
|
|
| 206 | 0 |
t = re; |
| 207 |
} |
|
| 208 | 0 |
assertNotNull( t ); |
| 209 | 0 |
assertEquals( ex, t ); |
| 210 |
} |
|
| 211 |
|
|
| 212 | 0 |
public void testCheckActive() throws Throwable |
| 213 |
{
|
|
| 214 | 0 |
Throwable t = null;
|
| 215 | 0 |
try
|
| 216 |
{
|
|
| 217 | 0 |
act.doStuff(); |
| 218 |
} |
|
| 219 |
catch( AssertionError th )
|
|
| 220 |
{
|
|
| 221 | 0 |
t = th; |
| 222 |
} |
|
| 223 | 0 |
assertNotNull( t ); |
| 224 |
|
|
| 225 | 0 |
act.initialize(); |
| 226 | 0 |
act.doStuff(); |
| 227 | 0 |
act.doStuff(); |
| 228 | 0 |
act.doStuff(); |
| 229 | 0 |
act.dispose(); |
| 230 |
|
|
| 231 | 0 |
t = null;
|
| 232 | 0 |
try
|
| 233 |
{
|
|
| 234 | 0 |
act.doStuff(); |
| 235 |
} |
|
| 236 |
catch( AssertionError th )
|
|
| 237 |
{
|
|
| 238 | 0 |
t = th; |
| 239 |
} |
|
| 240 | 0 |
assertNotNull( t ); |
| 241 |
} |
|
| 242 |
|
|
| 243 | 0 |
public void testLazyInitialization() throws Throwable |
| 244 |
{
|
|
| 245 | 0 |
act.doStuffLazily(); |
| 246 | 0 |
assertTrue( act.isDidInitialize() ); |
| 247 | 0 |
assertTrue( act.isRunning() ); |
| 248 |
|
|
| 249 | 0 |
act.doStuffLazily(); |
| 250 | 0 |
assertTrue( act.isDidInitialize() ); |
| 251 | 0 |
assertTrue( act.isRunning() ); |
| 252 |
|
|
| 253 | 0 |
act.dispose(); |
| 254 | 0 |
assertFalse( act.isRunning() ); |
| 255 | 0 |
assertTrue( act.isDidDispose() ); |
| 256 |
|
|
| 257 | 0 |
Throwable t = null;
|
| 258 | 0 |
try
|
| 259 |
{
|
|
| 260 | 0 |
act.doStuffLazily(); |
| 261 |
} |
|
| 262 |
catch( AssertionError th )
|
|
| 263 |
{
|
|
| 264 | 0 |
t = th; |
| 265 |
} |
|
| 266 | 0 |
assertNotNull( t ); |
| 267 | 0 |
assertFalse( act.isRunning() ); |
| 268 |
|
|
| 269 | 0 |
ActiveRecyclableImpl act2 = new ActiveRecyclableImpl( null, null ); |
| 270 | 0 |
act2.doStuffLazily(); |
| 271 | 0 |
assertTrue( act2.isDidInitialize() ); |
| 272 | 0 |
assertTrue( act2.isRunning() ); |
| 273 |
|
|
| 274 | 0 |
act2.doStuffLazily(); |
| 275 | 0 |
assertTrue( act2.isDidInitialize() ); |
| 276 | 0 |
assertTrue( act2.isRunning() ); |
| 277 |
|
|
| 278 | 0 |
act2.dispose(); |
| 279 | 0 |
assertFalse( act2.isRunning() ); |
| 280 | 0 |
assertTrue( act2.isDidDispose() ); |
| 281 |
|
|
| 282 |
// now this is okay
|
|
| 283 | 0 |
act2.doStuffLazily(); |
| 284 | 0 |
assertTrue( act2.isDidInitialize() ); |
| 285 | 0 |
assertTrue( act2.isRunning() ); |
| 286 | 0 |
assertFalse( act2.isDidDispose() ); |
| 287 | 0 |
act2.doStuffLazily(); |
| 288 |
} |
|
| 289 |
} |
|
| 290 |
|
|
||||||||||