|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractAsyncEnabledTestCase.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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 EDU.oswego.cs.dl.util.concurrent.Channel;
|
|
| 29 |
import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
|
|
| 30 |
import EDU.oswego.cs.dl.util.concurrent.ThreadedExecutor;
|
|
| 31 |
import org.jicarilla.framework.AbstractAsyncEnabled;
|
|
| 32 |
import org.jicarilla.framework.ExceptionListener;
|
|
| 33 |
import org.jicarilla.framework.Invocation;
|
|
| 34 |
import org.jicarilla.framework.NoopExceptionListener;
|
|
| 35 |
import junit.framework.TestCase;
|
|
| 36 |
|
|
| 37 |
import java.lang.reflect.Method;
|
|
| 38 |
|
|
| 39 |
/**
|
|
| 40 |
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
|
|
| 41 |
* AbstractAsyncEnabled.
|
|
| 42 |
*
|
|
| 43 |
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
|
|
| 44 |
* @version $Id: AbstractAsyncEnabledTestCase.java,v 1.1 2003/11/17 21:11:10
|
|
| 45 |
* lsimons Exp $
|
|
| 46 |
*/
|
|
| 47 |
public class AbstractAsyncEnabledTestCase extends TestCase |
|
| 48 |
{
|
|
| 49 |
public final static class Mock |
|
| 50 |
{
|
|
| 51 |
public boolean called = false; |
|
| 52 |
public Object result = new Object(); |
|
| 53 |
|
|
| 54 | 0 |
public Object call()
|
| 55 |
{
|
|
| 56 | 0 |
called = true;
|
| 57 | 0 |
privateCall(); |
| 58 | 0 |
return result;
|
| 59 |
} |
|
| 60 |
|
|
| 61 | 0 |
private void privateCall() {}; |
| 62 |
} |
|
| 63 |
|
|
| 64 |
public final static class MockEx |
|
| 65 |
{
|
|
| 66 |
public boolean called = false; |
|
| 67 |
public Exception ex = new Exception(); |
|
| 68 |
|
|
| 69 | 0 |
public void call() throws Exception |
| 70 |
{
|
|
| 71 | 0 |
called = true;
|
| 72 | 0 |
throw ex;
|
| 73 |
} |
|
| 74 |
} |
|
| 75 |
|
|
| 76 |
public final static class AsyncEnabledImpl |
|
| 77 |
extends AbstractAsyncEnabled
|
|
| 78 |
{
|
|
| 79 |
public Throwable m_initializeThrowable;
|
|
| 80 |
public RuntimeException m_disposeThrowable;
|
|
| 81 |
|
|
| 82 |
public boolean m_didInitialize = false; |
|
| 83 |
public boolean m_didDispose = false; |
|
| 84 |
|
|
| 85 | 0 |
public AsyncEnabledImpl(
|
| 86 |
Throwable initializeThrowable, |
|
| 87 |
RuntimeException disposeThrowable, |
|
| 88 |
ThreadedExecutor tx ) |
|
| 89 |
{
|
|
| 90 | 0 |
super( tx );
|
| 91 | 0 |
m_initializeThrowable = initializeThrowable; |
| 92 | 0 |
m_disposeThrowable = disposeThrowable; |
| 93 |
} |
|
| 94 |
|
|
| 95 | 0 |
public AsyncEnabledImpl(
|
| 96 |
Throwable initializeThrowable, |
|
| 97 |
RuntimeException disposeThrowable, |
|
| 98 |
ThreadedExecutor tx, |
|
| 99 |
ExceptionListener ex ) |
|
| 100 |
{
|
|
| 101 | 0 |
super( tx, ex );
|
| 102 | 0 |
m_initializeThrowable = initializeThrowable; |
| 103 | 0 |
m_disposeThrowable = disposeThrowable; |
| 104 |
} |
|
| 105 |
|
|
| 106 | 0 |
public boolean isRunning() |
| 107 |
{
|
|
| 108 | 0 |
return m_running;
|
| 109 |
} |
|
| 110 |
|
|
| 111 | 0 |
public boolean isStopped() |
| 112 |
{
|
|
| 113 | 0 |
return m_stopped;
|
| 114 |
} |
|
| 115 |
|
|
| 116 | 0 |
public boolean isDidInitialize() |
| 117 |
{
|
|
| 118 | 0 |
return m_didInitialize;
|
| 119 |
} |
|
| 120 |
|
|
| 121 | 0 |
public boolean isDidDispose() |
| 122 |
{
|
|
| 123 | 0 |
return m_didDispose;
|
| 124 |
} |
|
| 125 |
|
|
| 126 | 0 |
public void doInitialize() |
| 127 |
throws Throwable
|
|
| 128 |
{
|
|
| 129 | 0 |
super.doInitialize();
|
| 130 | 0 |
m_didInitialize = true;
|
| 131 |
|
|
| 132 | 0 |
if( m_initializeThrowable != null ) |
| 133 |
{
|
|
| 134 | 0 |
throw m_initializeThrowable;
|
| 135 |
} |
|
| 136 |
} |
|
| 137 |
|
|
| 138 | 0 |
public void doDispose() |
| 139 |
{
|
|
| 140 | 0 |
super.doDispose();
|
| 141 | 0 |
m_didDispose = true;
|
| 142 |
|
|
| 143 | 0 |
if( m_disposeThrowable != null ) |
| 144 |
{
|
|
| 145 | 0 |
throw m_disposeThrowable;
|
| 146 |
} |
|
| 147 |
} |
|
| 148 |
|
|
| 149 | 0 |
public Channel getIncoming()
|
| 150 |
{
|
|
| 151 | 0 |
return m_incoming;
|
| 152 |
} |
|
| 153 |
|
|
| 154 | 0 |
public Object getImmediateResult( Invocation invocation )
|
| 155 |
{
|
|
| 156 | 0 |
return super.getImmediateResult( invocation ); |
| 157 |
} |
|
| 158 |
|
|
| 159 | 0 |
public void doHandle( Invocation i ) |
| 160 |
{
|
|
| 161 | 0 |
super.doHandle( i );
|
| 162 |
} |
|
| 163 |
|
|
| 164 | 0 |
public void doHandleException( Invocation i ) |
| 165 |
{
|
|
| 166 | 0 |
super.doHandleException( i );
|
| 167 |
} |
|
| 168 |
|
|
| 169 | 0 |
public void work() throws Throwable |
| 170 |
{
|
|
| 171 | 0 |
super.work();
|
| 172 |
} |
|
| 173 |
} |
|
| 174 |
|
|
| 175 |
ThreadedExecutor tx = new ThreadedExecutor();
|
|
| 176 |
AsyncEnabledImpl aei = new AsyncEnabledImpl( null, null, tx ); |
|
| 177 |
|
|
| 178 | 0 |
public void testConstructor() |
| 179 |
{
|
|
| 180 | 0 |
assertNotNull( aei.getIncoming() ); |
| 181 | 0 |
assertTrue( aei.getIncoming() instanceof LinkedQueue );
|
| 182 |
|
|
| 183 | 0 |
aei = new AsyncEnabledImpl( null, null, tx, |
| 184 |
new NoopExceptionListener() );
|
|
| 185 | 0 |
assertTrue( aei.getIncoming() instanceof LinkedQueue );
|
| 186 |
} |
|
| 187 |
|
|
| 188 | 0 |
public void testInitializeDispose() throws Throwable |
| 189 |
{
|
|
| 190 | 0 |
aei.initialize(); |
| 191 | 0 |
aei.dispose(); |
| 192 |
} |
|
| 193 |
|
|
| 194 | 0 |
public void testGetImmediateResult() |
| 195 |
{
|
|
| 196 | 0 |
assertNull( aei.getImmediateResult( null ) );
|
| 197 | 0 |
assertNull( aei.getImmediateResult( new Invocation() ) );
|
| 198 |
} |
|
| 199 |
|
|
| 200 | 0 |
public void testHandle() throws Throwable |
| 201 |
{
|
|
| 202 | 0 |
Invocation i = new Invocation();
|
| 203 | 0 |
Object result = aei.handle( i ); |
| 204 | 0 |
assertNull( result ); |
| 205 | 0 |
assertEquals( i, aei.getIncoming().take() ); |
| 206 |
|
|
| 207 | 0 |
Throwable t = null;
|
| 208 | 0 |
try
|
| 209 |
{
|
|
| 210 | 0 |
aei.handle( null );
|
| 211 |
} |
|
| 212 |
catch( AssertionError ae )
|
|
| 213 |
{
|
|
| 214 | 0 |
t = ae; |
| 215 |
} |
|
| 216 | 0 |
assertNotNull( t ); |
| 217 |
} |
|
| 218 |
|
|
| 219 | 0 |
public void testDoHandle() throws Throwable |
| 220 |
{
|
|
| 221 | 0 |
Mock mock = new Mock();
|
| 222 | 0 |
Object[] args = new Object[0];
|
| 223 | 0 |
Method m = Mock.class.getMethod( "call", new Class[0] ); |
| 224 |
|
|
| 225 | 0 |
Invocation i = new Invocation( mock, null, m, args ); |
| 226 |
|
|
| 227 | 0 |
aei.doHandle( i ); |
| 228 | 0 |
assertTrue( mock.called ); |
| 229 | 0 |
assertEquals( mock.result, i.getResult() ); |
| 230 |
} |
|
| 231 |
|
|
| 232 | 0 |
public void testDoHandleInCaseOfException() throws Throwable |
| 233 |
{
|
|
| 234 | 0 |
MockEx mock = new MockEx();
|
| 235 | 0 |
Object[] args = new Object[0];
|
| 236 | 0 |
Method m = MockEx.class.getMethod( "call", new Class[0] ); |
| 237 |
|
|
| 238 | 0 |
Invocation i = new Invocation( mock, mock, m, args );
|
| 239 |
|
|
| 240 | 0 |
aei.doHandle( i ); |
| 241 | 0 |
assertTrue( mock.called ); |
| 242 | 0 |
assertEquals( mock.ex, i.getThrowable() ); |
| 243 |
} |
|
| 244 |
|
|
| 245 | 0 |
public void testDoHandleInCaseOfAccessException() throws Throwable |
| 246 |
{
|
|
| 247 | 0 |
Mock mock = new Mock();
|
| 248 | 0 |
Object[] args = new Object[0];
|
| 249 | 0 |
Method[] methods = Mock.class.getDeclaredMethods();
|
| 250 | 0 |
Method m = null;
|
| 251 | 0 |
for( int i = 0; i < methods.length; i++ ) |
| 252 |
{
|
|
| 253 | 0 |
if( methods[i].getName().equals( "privateCall" ) ) |
| 254 |
{
|
|
| 255 | 0 |
m = methods[i]; |
| 256 | 0 |
break;
|
| 257 |
} |
|
| 258 |
} |
|
| 259 |
|
|
| 260 | 0 |
Invocation i = new Invocation( mock, mock, m, args );
|
| 261 |
|
|
| 262 | 0 |
aei.doHandle( i ); |
| 263 | 0 |
assertTrue( i.getThrowable() instanceof IllegalAccessException );
|
| 264 |
} |
|
| 265 |
|
|
| 266 | 0 |
public void testDoHandleException() throws Throwable |
| 267 |
{
|
|
| 268 | 0 |
aei.doHandleException( new Invocation() );
|
| 269 |
} |
|
| 270 |
|
|
| 271 | 0 |
public void testWork() throws Throwable |
| 272 |
{
|
|
| 273 | 0 |
MockEx mock = new MockEx();
|
| 274 | 0 |
Object[] args = new Object[0];
|
| 275 | 0 |
Method m = MockEx.class.getMethod( "call", new Class[0] ); |
| 276 |
|
|
| 277 | 0 |
Invocation i = new Invocation( mock, null, m, args ); |
| 278 |
|
|
| 279 | 0 |
aei.getIncoming().put( i ); |
| 280 | 0 |
aei.work(); |
| 281 | 0 |
assertTrue( mock.called ); |
| 282 | 0 |
assertNull( aei.getIncoming().peek() ); |
| 283 |
} |
|
| 284 |
} |
|
| 285 |
|
|
||||||||||