1 /* 2 * jDTAUS Core Test Suite 3 * Copyright (C) 2005 Christian Schulte 4 * <cs@schulte.it> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 * 20 */ 21 package org.jdtaus.core.lang.spi.it; 22 23 import junit.framework.Assert; 24 import org.jdtaus.core.lang.ExceptionEvent; 25 import org.jdtaus.core.lang.ExceptionEventSource; 26 import org.jdtaus.core.lang.ExceptionListener; 27 import org.jdtaus.core.lang.it.ExceptionEventSourceTest; 28 import org.jdtaus.core.lang.spi.Executor; 29 30 /** 31 * Testcase for {@code Executor} implementations. 32 * 33 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 34 * @version $JDTAUS: ExecutorTest.java 8743 2012-10-07 03:06:20Z schulte $ 35 */ 36 public abstract class ExecutorTest extends ExceptionEventSourceTest 37 { 38 //--ExceptionEventSourceTest------------------------------------------------ 39 40 /** 41 * {@inheritDoc} 42 * @see #getExecutor() 43 */ 44 public final ExceptionEventSource getExceptionEventSource() 45 { 46 return this.getExecutor(); 47 } 48 49 //------------------------------------------------ExceptionEventSourceTest-- 50 //--ExecutorTest------------------------------------------------------------ 51 52 /** Creates a new {@code ExecutorTest} instance. */ 53 public ExecutorTest() 54 { 55 super(); 56 } 57 58 /** 59 * Gets the {@code Executor} implementation tests are performed with. 60 * 61 * @return the {@code Executor} implementation tests are performed with. 62 */ 63 public abstract Executor getExecutor(); 64 65 //------------------------------------------------------------ExecutorTest-- 66 //--Tests------------------------------------------------------------------- 67 68 /** Exception expected to be reported. */ 69 public static final class ExecutionException extends RuntimeException 70 { 71 72 public ExecutionException() 73 { 74 super(); 75 } 76 77 } 78 79 /** {@code Runnable} throwing {@code ExecutionException}. */ 80 public static final Runnable runnable = new Runnable() 81 { 82 83 public void run() 84 { 85 throw new ExecutionException(); 86 } 87 88 }; 89 90 /** {@code ExceptionListener} providing the last reported event. */ 91 public static final class TestListener implements ExceptionListener 92 { 93 94 /** The last reported event. */ 95 private ExceptionEvent event; 96 97 /** Creates a new {@code TestListener} instance. */ 98 public TestListener() 99 { 100 super(); 101 } 102 103 /** 104 * Gets the last reported event. 105 * 106 * @return the last reported event. 107 */ 108 public ExceptionEvent getEvent() 109 { 110 return this.event; 111 } 112 113 public void onException( final ExceptionEvent event ) 114 { 115 this.event = event; 116 } 117 118 } 119 120 /** 121 * Tests the {@link Executor#executeAsynchronously(Runnable)} method to 122 * handle {@code null} arguments correctly by throwing a corresponding 123 * {@code NullPointerException}. 124 */ 125 public void testNullArguments() throws Exception 126 { 127 super.testNullArguments(); 128 129 assert this.getExecutor() != null; 130 131 try 132 { 133 this.getExecutor().executeAsynchronously( null ); 134 throw new AssertionError(); 135 } 136 catch ( final NullPointerException e ) 137 { 138 Assert.assertNotNull( e.getMessage() ); 139 System.out.println( e.toString() ); 140 } 141 142 } 143 144 /** 145 * Tests the {@link Executor#executeAsynchronously(Runnable)} method to 146 * report any exceptions thrown during execution. 147 */ 148 public void testOnException() throws Exception 149 { 150 assert this.getExecutor() != null; 151 152 final TestListener listener = new TestListener(); 153 this.getExecutor().addExceptionListener( listener ); 154 this.getExecutor().executeAsynchronously( runnable ); 155 156 // Wait 5 seconds and test the listener. 157 Thread.sleep( 5000L ); 158 159 Assert.assertTrue( listener.getEvent(). 160 getException() instanceof ExecutionException ); 161 162 } 163 164 //-------------------------------------------------------------------Tests-- 165 }