KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > beanflow > util > ParallelTestCase


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.beanflow.util;
18
19 import org.apache.servicemix.beanflow.JoinAll;
20 import org.apache.servicemix.beanflow.JoinSupport;
21 import org.apache.servicemix.beanflow.ParallelActivity;
22 import org.apache.servicemix.beanflow.annotations.Parallel;
23
24 import java.util.concurrent.Executor JavaDoc;
25 import java.util.concurrent.Executors JavaDoc;
26
27 /**
28  * A JUnit test case which supports the {@link Parallel} annotations for running
29  * concurrent threads in a test case along with the methods {@link #sync()} or
30  * the {@link #sync(long)} which provide a simple cross-thread synchronisation
31  * mechanism
32  *
33  * @version $Revision: $
34  */

35 public abstract class ParallelTestCase extends ActivityTestSupport {
36     private ParallelActivity activity;
37     private Executor JavaDoc executor;
38     private JoinSupport join;
39     private int maxThreadPoolSize = 20;
40     private long testTimeout = 10000;
41
42     @SuppressWarnings JavaDoc("unchecked")
43     public void testParallelMethods() throws Exception JavaDoc {
44         startActivity(getActivity(), testTimeout);
45         getActivity().join();
46         assertStopped(getActivity());
47     }
48
49     /**
50      * Blocks the parallel thread until all the other parallel threads have
51      * reached the same synchronisation point before continuing.
52      */

53     @SuppressWarnings JavaDoc("unchecked")
54     public void sync() {
55         getActivity().sync();
56     }
57
58     /**
59      * Blocks up to the give timeout value in the parallel thread until all the
60      * other parallel threads have reached the same synchronisation point before
61      * continuing.
62      *
63      * @return true if the sync completed otherwise false indicating a timeout
64      */

65     public boolean sync(long millis) {
66         return getActivity().sync(millis);
67     }
68
69     // Properties
70
// -------------------------------------------------------------------------
71

72     public ParallelActivity getActivity() {
73         if (activity == null) {
74             activity = createActivity();
75         }
76         return activity;
77     }
78
79     public Executor JavaDoc getExecutor() {
80         if (executor == null) {
81             executor = createExecutor();
82         }
83         return executor;
84     }
85
86     public void setExecutor(Executor JavaDoc executor) {
87         this.executor = executor;
88     }
89
90     public JoinSupport getJoin() {
91         if (join == null) {
92             join = createJoin();
93         }
94         return join;
95     }
96
97     public void setJoin(JoinSupport join) {
98         this.join = join;
99     }
100
101     public int getMaxThreadPoolSize() {
102         return maxThreadPoolSize;
103     }
104
105     public void setMaxThreadPoolSize(int maxThreadPoolSize) {
106         this.maxThreadPoolSize = maxThreadPoolSize;
107     }
108
109     // Factory methods
110
// -------------------------------------------------------------------------
111
protected ParallelActivity createActivity() {
112         return ParallelActivity.newParallelMethodActivity(getJoin(), getExecutor(), this);
113     }
114
115     protected Executor JavaDoc createExecutor() {
116         return Executors.newFixedThreadPool(maxThreadPoolSize);
117     }
118
119     protected JoinSupport createJoin() {
120         return new JoinAll();
121     }
122
123     protected void sleep(int timeout) {
124         try {
125             Thread.sleep(3000);
126         }
127         catch (InterruptedException JavaDoc e) {
128             Thread.currentThread().interrupt();
129         }
130     }
131 }
132
Popular Tags