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 8743 2012-10-07 03:06:20Z 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 /** Creates a new {@code ExecutorTest} instance. */ 053 public ExecutorTest() 054 { 055 super(); 056 } 057 058 /** 059 * Gets the {@code Executor} implementation tests are performed with. 060 * 061 * @return the {@code Executor} implementation tests are performed with. 062 */ 063 public abstract Executor getExecutor(); 064 065 //------------------------------------------------------------ExecutorTest-- 066 //--Tests------------------------------------------------------------------- 067 068 /** Exception expected to be reported. */ 069 public static final class ExecutionException extends RuntimeException 070 { 071 072 public ExecutionException() 073 { 074 super(); 075 } 076 077 } 078 079 /** {@code Runnable} throwing {@code ExecutionException}. */ 080 public static final Runnable runnable = new Runnable() 081 { 082 083 public void run() 084 { 085 throw new ExecutionException(); 086 } 087 088 }; 089 090 /** {@code ExceptionListener} providing the last reported event. */ 091 public static final class TestListener implements ExceptionListener 092 { 093 094 /** The last reported event. */ 095 private ExceptionEvent event; 096 097 /** Creates a new {@code TestListener} instance. */ 098 public TestListener() 099 { 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}