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