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
;
10 import static org
.junit
.Assert
.fail
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
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
;
29 * Base class of asynchronous JniExecutor error tests.
31 public abstract class JniExecutorAsyncErrorTest
extends JniExecutorAsyncTest
{
34 * structure to hold all the needed data to run tests
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;
45 protected TestData
createTestData1() {
46 HostController hc1
= null;
48 hc1
= new HostController(null, TestConstants
.WORKINGDIR
, TestConstants
.EXECUTABLE
);
49 } catch (JniExecutorIllegalArgumentException e
) {
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
);
58 final JniExecutor je
= JniExecutor
.getInstance();
59 IJniExecutorObserver o1
= new Test1Observer(je
, module
, testcases
);
61 TestData td
= new TestData();
63 td
.mCfgFileName
= cfgFileName
;
65 td
.mTestcases
= testcases
;
70 // functions to get to different states
72 protected void gotoInactive( final JniExecutor je
, final TestData td
) {
74 // get to connected MC_INACTIVE
77 TestUtil
.assertState( McStateEnum
.MC_INACTIVE
);
78 } catch (JniExecutorWrongStateException
| JniExecutorJniLoadException e
) {
84 protected void gotoListening( final JniExecutor je
, final TestData td
) {
86 // get to MC_LISTENING
90 TestUtil
.assertState( McStateEnum
.MC_INACTIVE
);
92 je
.addHostController( td
.mHc
);
93 TestUtil
.assertState( McStateEnum
.MC_INACTIVE
);
95 je
.setConfigFileName( td
.mCfgFileName
);
96 TestUtil
.assertState( McStateEnum
.MC_INACTIVE
);
99 TestUtil
.assertState( McStateEnum
.MC_INACTIVE
);
102 TestUtil
.assertState( McStateEnum
.MC_LISTENING
);
103 } catch (JniExecutorIllegalArgumentException
| JniExecutorWrongStateException
| JniExecutorStartSessionException e
) {
109 protected void gotoListeningConfigured( final JniExecutor je
, final TestData td
) {
111 // get to MC_LISTENING_CONFIGURED
112 gotoListening(je
, td
);
115 TestUtil
.assertState( McStateEnum
.MC_LISTENING
);
116 final Object lock
= new Object();
117 synchronized (lock
) {
118 waitBefore(lock
, McStateEnum
.MC_LISTENING_CONFIGURED
);
122 TestUtil
.assertState( McStateEnum
.MC_LISTENING_CONFIGURED
);
123 } catch (JniExecutorWrongStateException e
) {
129 protected void gotoListeningWithoutAddHostController( final JniExecutor je
, final TestData td
) {
131 // get to MC_LISTENING
132 gotoInactive(je
, td
);
135 TestUtil
.assertState( McStateEnum
.MC_INACTIVE
);
137 je
.setConfigFileName( td
.mCfgFileName
);
138 TestUtil
.assertState( McStateEnum
.MC_INACTIVE
);
140 je
.setObserver(null);
141 TestUtil
.assertState( McStateEnum
.MC_INACTIVE
);
144 TestUtil
.assertState( McStateEnum
.MC_LISTENING
);
145 } catch (JniExecutorIllegalArgumentException
| JniExecutorWrongStateException
| JniExecutorStartSessionException e
) {
151 protected void gotoListeningConfiguredWithoutAddHostController( final JniExecutor je
, final TestData td
) {
153 // get to MC_LISTENING_CONFIGURED without addHostController()
154 gotoListeningWithoutAddHostController(je
, td
);
157 TestUtil
.assertState( McStateEnum
.MC_LISTENING
);
158 final Object lock
= new Object();
159 synchronized (lock
) {
160 waitBefore(lock
, McStateEnum
.MC_LISTENING_CONFIGURED
);
164 TestUtil
.assertState( McStateEnum
.MC_LISTENING_CONFIGURED
);
165 } catch (JniExecutorWrongStateException e
) {
171 protected void gotoHcConnected( final JniExecutor je
, final TestData td
) {
173 // get to MC_HC_CONNECTED
174 gotoListening(je
, td
);
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();
184 TestUtil
.assertState( McStateEnum
.MC_HC_CONNECTED
);
185 } catch (JniExecutorWrongStateException e
) {
191 protected void gotoActive( final JniExecutor je
, final TestData td
) {
194 gotoHcConnected(je
, td
);
197 TestUtil
.assertState( McStateEnum
.MC_HC_CONNECTED
);
198 final Object lock
= new Object();
199 synchronized (lock
) {
200 waitBefore(lock
, McStateEnum
.MC_ACTIVE
);
204 TestUtil
.assertState( McStateEnum
.MC_ACTIVE
);
205 } catch (JniExecutorWrongStateException e
) {
211 protected void gotoReady( final JniExecutor je
, final TestData td
) {
217 TestUtil
.assertState( McStateEnum
.MC_ACTIVE
);
218 final Object lock
= new Object();
219 synchronized (lock
) {
220 waitBefore(lock
, McStateEnum
.MC_READY
);
224 TestUtil
.assertState( McStateEnum
.MC_READY
);
225 } catch (JniExecutorWrongStateException e
) {
231 protected void gotoPaused( final JniExecutor je
, final TestData td
) {
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
);
245 TestUtil
.assertState( McStateEnum
.MC_PAUSED
);
246 } catch (JniExecutorWrongStateException
| JniExecutorIllegalArgumentException e
) {
253 * First part of waiting until asynchronous function gets to the expected state.
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.
260 * final Object lock = new Object();
261 * synchronized ( lock ) {
262 * waitBefore( McStateEnum.MC_READY );
267 * @param aLock lock for synchronization
268 * @param aExpectedState the expected state we are waiting for
269 * @see #waitAfter(Object)
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() {
276 public void statusChanged( final McStateEnum aNewState
) {
278 if ( aNewState
== aExpectedState
) {
279 synchronized (aLock
) {
287 public void error(int aSeverity
, String aMsg
) {
288 Log
.fi(aSeverity
, aMsg
);
289 synchronized (aLock
) {
296 public void notify(Timeval aTime
, String aSource
, int aSeverity
, String aMsg
) {
300 public void verdict(String aTestcase
, VerdictTypeEnum aVerdictType
) {
304 public void verdictStats(Map
<VerdictTypeEnum
, Integer
> aVerdictStats
) {
309 } catch (JniExecutorWrongStateException e
) {
311 //it happens only in disconnected state, but it cannot happen,
312 //we get here only if we are connected
319 * Pair of {@link #waitBefore(Object, McStateEnum)}
320 * @param aLock lock for synchronization
321 * @see #waitBefore(Object, McStateEnum)
323 protected static void waitAfter( final Object aLock
) {
326 Log
.f("aLock.wait() BEFORE");
328 Log
.f("aLock.wait() AFTER");
329 } catch (InterruptedException e
) {