e3ca90c1c210bbbe297c0bfa9bb469a21b17d497
[deliverable/titan.core.git] / titan_executor_api / TITAN_Executor_API_test / src / org / eclipse / titan / executorapi / test / JniExecutorAsyncErrorTest.java
1 /******************************************************************************
2 * Copyright (c) 2000-2015 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 ******************************************************************************/
8 package org.eclipse.titan.executorapi.test;
9
10 import static org.junit.Assert.fail;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15
16 import org.eclipse.titan.executor.jni.McStateEnum;
17 import org.eclipse.titan.executor.jni.Timeval;
18 import org.eclipse.titan.executor.jni.VerdictTypeEnum;
19 import org.eclipse.titan.executorapi.HostController;
20 import org.eclipse.titan.executorapi.IJniExecutorObserver;
21 import org.eclipse.titan.executorapi.JniExecutor;
22 import org.eclipse.titan.executorapi.exception.JniExecutorIllegalArgumentException;
23 import org.eclipse.titan.executorapi.exception.JniExecutorJniLoadException;
24 import org.eclipse.titan.executorapi.exception.JniExecutorStartSessionException;
25 import org.eclipse.titan.executorapi.exception.JniExecutorWrongStateException;
26 import org.eclipse.titan.executorapi.util.Log;
27
28 /**
29 * Base class of asynchronous JniExecutor error tests.
30 */
31 public abstract class JniExecutorAsyncErrorTest extends JniExecutorAsyncTest {
32
33 /**
34 * structure to hold all the needed data to run tests
35 *
36 */
37 protected class TestData {
38 public HostController mHc = null;
39 public String mCfgFileName = null;
40 public String mModule = null;
41 public List<String> mTestcases = null;
42 public IJniExecutorObserver mObserver = null;
43 }
44
45 protected TestData createTestData1() {
46 HostController hc1 = null;
47 try {
48 hc1 = new HostController(null, TestConstants.WORKINGDIR, TestConstants.EXECUTABLE);
49 } catch (JniExecutorIllegalArgumentException e) {
50 fail();
51 }
52 final String cfgFileName = TestConstants.CFG_FILE;
53 final String module = TestConstants.MODULE;
54 final List<String> testcases = new ArrayList<String>();
55 testcases.add(TestConstants.TESTCASE1);
56 testcases.add(TestConstants.TESTCASE2);
57
58 final JniExecutor je = JniExecutor.getInstance();
59 IJniExecutorObserver o1 = new Test1Observer(je, module, testcases);
60
61 TestData td = new TestData();
62 td.mHc = hc1;
63 td.mCfgFileName = cfgFileName;
64 td.mModule = module;
65 td.mTestcases = testcases;
66 td.mObserver = o1;
67 return td;
68 }
69
70 // functions to get to different states
71
72 protected void gotoInactive( final JniExecutor je, final TestData td ) {
73 Log.fi();
74 // get to connected MC_INACTIVE
75 try {
76 je.init();
77 TestUtil.assertState( McStateEnum.MC_INACTIVE );
78 } catch (JniExecutorWrongStateException | JniExecutorJniLoadException e) {
79 fail();
80 }
81 Log.fo();
82 }
83
84 protected void gotoListening( final JniExecutor je, final TestData td ) {
85 Log.fi();
86 // get to MC_LISTENING
87 gotoInactive(je, td);
88
89 try {
90 TestUtil.assertState( McStateEnum.MC_INACTIVE );
91
92 je.addHostController( td.mHc );
93 TestUtil.assertState( McStateEnum.MC_INACTIVE );
94
95 je.setConfigFileName( td.mCfgFileName );
96 TestUtil.assertState( McStateEnum.MC_INACTIVE );
97
98 je.setObserver(null);
99 TestUtil.assertState( McStateEnum.MC_INACTIVE );
100
101 je.startSession();
102 TestUtil.assertState( McStateEnum.MC_LISTENING );
103 } catch (JniExecutorIllegalArgumentException | JniExecutorWrongStateException | JniExecutorStartSessionException e) {
104 fail();
105 }
106 Log.fo();
107 }
108
109 protected void gotoListeningConfigured( final JniExecutor je, final TestData td ) {
110 Log.fi();
111 // get to MC_LISTENING_CONFIGURED
112 gotoListening(je, td);
113
114 try {
115 TestUtil.assertState( McStateEnum.MC_LISTENING );
116 final Object lock = new Object();
117 synchronized (lock) {
118 waitBefore(lock, McStateEnum.MC_LISTENING_CONFIGURED );
119 je.configure();
120 waitAfter(lock);
121 }
122 TestUtil.assertState( McStateEnum.MC_LISTENING_CONFIGURED );
123 } catch (JniExecutorWrongStateException e) {
124 fail();
125 }
126 Log.fo();
127 }
128
129 protected void gotoListeningWithoutAddHostController( final JniExecutor je, final TestData td ) {
130 Log.fi();
131 // get to MC_LISTENING
132 gotoInactive(je, td);
133
134 try {
135 TestUtil.assertState( McStateEnum.MC_INACTIVE );
136
137 je.setConfigFileName( td.mCfgFileName );
138 TestUtil.assertState( McStateEnum.MC_INACTIVE );
139
140 je.setObserver(null);
141 TestUtil.assertState( McStateEnum.MC_INACTIVE );
142
143 je.startSession();
144 TestUtil.assertState( McStateEnum.MC_LISTENING );
145 } catch (JniExecutorIllegalArgumentException | JniExecutorWrongStateException | JniExecutorStartSessionException e) {
146 fail();
147 }
148 Log.fo();
149 }
150
151 protected void gotoListeningConfiguredWithoutAddHostController( final JniExecutor je, final TestData td ) {
152 Log.fi();
153 // get to MC_LISTENING_CONFIGURED without addHostController()
154 gotoListeningWithoutAddHostController(je, td);
155
156 try {
157 TestUtil.assertState( McStateEnum.MC_LISTENING );
158 final Object lock = new Object();
159 synchronized (lock) {
160 waitBefore(lock, McStateEnum.MC_LISTENING_CONFIGURED );
161 je.configure();
162 waitAfter(lock);
163 }
164 TestUtil.assertState( McStateEnum.MC_LISTENING_CONFIGURED );
165 } catch (JniExecutorWrongStateException e) {
166 fail();
167 }
168 Log.fo();
169 }
170
171 protected void gotoHcConnected( final JniExecutor je, final TestData td ) {
172 Log.fi();
173 // get to MC_HC_CONNECTED
174 gotoListening(je, td);
175
176 try {
177 TestUtil.assertState( McStateEnum.MC_LISTENING );
178 final Object lock = new Object();
179 synchronized (lock) {
180 waitBefore(lock, McStateEnum.MC_HC_CONNECTED );
181 je.startHostControllers();
182 waitAfter(lock);
183 }
184 TestUtil.assertState( McStateEnum.MC_HC_CONNECTED );
185 } catch (JniExecutorWrongStateException e) {
186 fail();
187 }
188 Log.fo();
189 }
190
191 protected void gotoActive( final JniExecutor je, final TestData td ) {
192 Log.fi();
193 // get to MC_ACTIVE
194 gotoHcConnected(je, td);
195
196 try {
197 TestUtil.assertState( McStateEnum.MC_HC_CONNECTED );
198 final Object lock = new Object();
199 synchronized (lock) {
200 waitBefore(lock, McStateEnum.MC_ACTIVE );
201 je.configure();
202 waitAfter(lock);
203 }
204 TestUtil.assertState( McStateEnum.MC_ACTIVE );
205 } catch (JniExecutorWrongStateException e) {
206 fail();
207 }
208 Log.fo();
209 }
210
211 protected void gotoReady( final JniExecutor je, final TestData td ) {
212 Log.fi();
213 // get to MC_READY
214 gotoActive(je, td);
215
216 try {
217 TestUtil.assertState( McStateEnum.MC_ACTIVE );
218 final Object lock = new Object();
219 synchronized (lock) {
220 waitBefore(lock, McStateEnum.MC_READY );
221 je.createMTC();
222 waitAfter(lock);
223 }
224 TestUtil.assertState( McStateEnum.MC_READY );
225 } catch (JniExecutorWrongStateException e) {
226 fail();
227 }
228 Log.fo();
229 }
230
231 protected void gotoPaused( final JniExecutor je, final TestData td ) {
232 Log.fi();
233 // get to MC_PAUSED
234 gotoReady(je, td);
235
236 try {
237 TestUtil.assertState( McStateEnum.MC_READY );
238 je.pauseExecution(true);
239 final Object lock = new Object();
240 synchronized (lock) {
241 waitBefore(lock, McStateEnum.MC_PAUSED );
242 je.executeControl(td.mModule);
243 waitAfter(lock);
244 }
245 TestUtil.assertState( McStateEnum.MC_PAUSED );
246 } catch (JniExecutorWrongStateException | JniExecutorIllegalArgumentException e) {
247 fail();
248 }
249 Log.fo();
250 }
251
252 /**
253 * First part of waiting until asynchronous function gets to the expected state.
254 * <p>
255 * This must be called BEFORE calling the asynchronous function to make sure that observer
256 * does the right action, if statusChange() is called back before asynchronous function returns.
257 * <br>
258 * Example usage:
259 * <pre>
260 * final Object lock = new Object();
261 * synchronized ( lock ) {
262 * waitBefore( McStateEnum.MC_READY );
263 * je.createMTC();
264 * waitAfter( lock );
265 * }
266 * </pre>
267 * @param aLock lock for synchronization
268 * @param aExpectedState the expected state we are waiting for
269 * @see #waitAfter(Object)
270 */
271 protected static void waitBefore( final Object aLock, final McStateEnum aExpectedState ) {
272 Log.fi( aExpectedState );
273 final JniExecutor je = JniExecutor.getInstance();
274 IJniExecutorObserver o = new IJniExecutorObserver() {
275 @Override
276 public void statusChanged( final McStateEnum aNewState ) {
277 Log.fi(aNewState);
278 if ( aNewState == aExpectedState ) {
279 synchronized (aLock) {
280 aLock.notifyAll();
281 }
282 }
283 Log.fo();
284 }
285
286 @Override
287 public void error(int aSeverity, String aMsg) {
288 Log.fi(aSeverity, aMsg);
289 synchronized (aLock) {
290 aLock.notifyAll();
291 }
292 Log.fo();
293 }
294
295 @Override
296 public void notify(Timeval aTime, String aSource, int aSeverity, String aMsg) {
297 }
298
299 @Override
300 public void verdict(String aTestcase, VerdictTypeEnum aVerdictType) {
301 }
302
303 @Override
304 public void verdictStats(Map<VerdictTypeEnum, Integer> aVerdictStats) {
305 }
306 };
307 try {
308 je.setObserver(o);
309 } catch (JniExecutorWrongStateException e) {
310 Log.f(e.toString());
311 //it happens only in disconnected state, but it cannot happen,
312 //we get here only if we are connected
313 fail();
314 }
315 Log.fo();
316 }
317
318 /**
319 * Pair of {@link #waitBefore(Object, McStateEnum)}
320 * @param aLock lock for synchronization
321 * @see #waitBefore(Object, McStateEnum)
322 */
323 protected static void waitAfter( final Object aLock ) {
324 Log.fi();
325 try {
326 Log.f("aLock.wait() BEFORE");
327 aLock.wait();
328 Log.f("aLock.wait() AFTER");
329 } catch (InterruptedException e) {
330 Log.f(e.toString());
331 fail();
332 }
333 Log.fo();
334 }
335 }
This page took 0.038515 seconds and 4 git commands to generate.