001/* 002 * jDTAUS Core Test Suite 003 * Copyright (C) 2005 Christian Schulte 004 * <cs@schulte.it> 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License, or any later version. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 019 * 020 */ 021package org.jdtaus.core.lang.spi.it; 022 023import junit.framework.Assert; 024import org.jdtaus.core.lang.ExceptionEvent; 025import org.jdtaus.core.lang.ExceptionEventSource; 026import org.jdtaus.core.lang.ExceptionListener; 027import org.jdtaus.core.lang.it.ExceptionEventSourceTest; 028import org.jdtaus.core.lang.spi.Executor; 029 030/** 031 * Testcase for {@code Executor} implementations. 032 * 033 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 034 * @version $JDTAUS: ExecutorTest.java 8641 2012-09-27 06:45:17Z schulte $ 035 */ 036public abstract class ExecutorTest extends ExceptionEventSourceTest 037{ 038 //--ExceptionEventSourceTest------------------------------------------------ 039 040 /** 041 * {@inheritDoc} 042 * @see #getExecutor() 043 */ 044 public final ExceptionEventSource getExceptionEventSource() 045 { 046 return this.getExecutor(); 047 } 048 049 //------------------------------------------------ExceptionEventSourceTest-- 050 //--ExecutorTest------------------------------------------------------------ 051 052 /** 053 * Gets the {@code Executor} implementation tests are performed with. 054 * 055 * @return the {@code Executor} implementation tests are performed with. 056 */ 057 public abstract Executor getExecutor(); 058 059 //------------------------------------------------------------ExecutorTest-- 060 //--Tests------------------------------------------------------------------- 061 062 /** Exception expected to be reported. */ 063 public static final class ExecutionException extends RuntimeException 064 { 065 } 066 067 /** {@code Runnable} throwing {@code ExecutionException}. */ 068 public static final Runnable runnable = new Runnable() 069 { 070 071 public void run() 072 { 073 throw new ExecutionException(); 074 } 075 076 }; 077 078 /** {@code ExceptionListener} providing the last reported event. */ 079 public static final class TestListener implements ExceptionListener 080 { 081 082 /** The last reported event. */ 083 private ExceptionEvent event; 084 085 /** 086 * Gets the last reported event. 087 * 088 * @return the last reported event. 089 */ 090 public ExceptionEvent getEvent() 091 { 092 return this.event; 093 } 094 095 public void onException( final ExceptionEvent event ) 096 { 097 this.event = event; 098 } 099 100 } 101 102 /** 103 * Tests the {@link Executor#executeAsynchronously(Runnable)} method to 104 * handle {@code null} arguments correctly by throwing a corresponding 105 * {@code NullPointerException}. 106 */ 107 public void testNullArguments() throws Exception 108 { 109 super.testNullArguments(); 110 111 assert this.getExecutor() != null; 112 113 try 114 { 115 this.getExecutor().executeAsynchronously( null ); 116 throw new AssertionError(); 117 } 118 catch ( NullPointerException e ) 119 { 120 Assert.assertNotNull( e.getMessage() ); 121 System.out.println( e.toString() ); 122 } 123 124 } 125 126 /** 127 * Tests the {@link Executor#executeAsynchronously(Runnable)} method to 128 * report any exceptions thrown during execution. 129 */ 130 public void testOnException() throws Exception 131 { 132 assert this.getExecutor() != null; 133 134 final TestListener listener = new TestListener(); 135 this.getExecutor().addExceptionListener( listener ); 136 this.getExecutor().executeAsynchronously( runnable ); 137 138 // Wait 5 seconds and test the listener. 139 Thread.sleep( 5000L ); 140 141 Assert.assertTrue( listener.getEvent(). 142 getException() instanceof ExecutionException ); 143 144 } 145 146 //-------------------------------------------------------------------Tests-- 147}