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 8641 2012-09-27 06:45:17Z 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 | /** |
53 | * Gets the {@code Executor} implementation tests are performed with. |
54 | * |
55 | * @return the {@code Executor} implementation tests are performed with. |
56 | */ |
57 | public abstract Executor getExecutor(); |
58 | |
59 | //------------------------------------------------------------ExecutorTest-- |
60 | //--Tests------------------------------------------------------------------- |
61 | |
62 | /** Exception expected to be reported. */ |
63 | public static final class ExecutionException extends RuntimeException |
64 | { |
65 | } |
66 | |
67 | /** {@code Runnable} throwing {@code ExecutionException}. */ |
68 | public static final Runnable runnable = new Runnable() |
69 | { |
70 | |
71 | public void run() |
72 | { |
73 | throw new ExecutionException(); |
74 | } |
75 | |
76 | }; |
77 | |
78 | /** {@code ExceptionListener} providing the last reported event. */ |
79 | public static final class TestListener implements ExceptionListener |
80 | { |
81 | |
82 | /** The last reported event. */ |
83 | private ExceptionEvent event; |
84 | |
85 | /** |
86 | * Gets the last reported event. |
87 | * |
88 | * @return the last reported event. |
89 | */ |
90 | public ExceptionEvent getEvent() |
91 | { |
92 | return this.event; |
93 | } |
94 | |
95 | public void onException( final ExceptionEvent event ) |
96 | { |
97 | this.event = event; |
98 | } |
99 | |
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 | } |