Titan Core Initial Contribution
[deliverable/titan.core.git] / titan_executor_api / TITAN_Executor_API_test / src / org / eclipse / titan / executorapi / test / TestObserverBase.java
1 /******************************************************************************
2 * Copyright (c) 2000-2014 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 java.util.List;
11 import java.util.Map;
12
13 import org.eclipse.titan.executor.jni.McStateEnum;
14 import org.eclipse.titan.executor.jni.Timeval;
15 import org.eclipse.titan.executor.jni.VerdictTypeEnum;
16 import org.eclipse.titan.executorapi.IJniExecutorObserver;
17 import org.eclipse.titan.executorapi.JniExecutor;
18 import org.eclipse.titan.executorapi.exception.JniExecutorException;
19 import org.eclipse.titan.executorapi.test.JniExecutorAsyncHappyTest.ExecutionMethod;
20 import org.eclipse.titan.executorapi.test.JniExecutorAsyncHappyTest.McRoute;
21 import org.eclipse.titan.executorapi.util.Log;
22
23 abstract class TestObserverBase implements IJniExecutorObserver {
24
25 /** Executor instance, NOT OWNED */
26 protected final JniExecutor mJe;
27
28 /** TTCN-3 module name of the test control or test case(s), it can be null (it means execution list from cfg file is used) */
29 protected String mModule = null;
30
31 /** List of test cases, it can be null (it means test control is executed) */
32 protected List<String> mTestcases = null;
33
34 protected final McRoute mRoute;
35 protected final ExecutionMethod mExecMethod;
36
37 /**
38 * Lock object for waiting completion. NOT OWNED
39 */
40 private Object mLockCompletion = null;
41
42 /**
43 * OWNED
44 * verdict in the observer
45 * true: PASS -> it can still be FAIL in the testcase!
46 * false: FAIL
47 */
48 private volatile boolean mVerdict = true;
49
50 public void setLock( final Object aLockObject ) {
51 mLockCompletion = aLockObject;
52 }
53
54 public boolean getVerdict() {
55 return mVerdict;
56 }
57
58 public void setModule( final String aModule ) {
59 mModule = aModule;
60 }
61
62 public void setTestcases( final List<String> aTestcases ) {
63 mTestcases = aTestcases;
64 }
65
66 protected TestObserverBase( final JniExecutor aJe, final McRoute aRoute, final ExecutionMethod aExecMethod ) {
67 mJe = aJe;
68 mRoute = aRoute;
69 mExecMethod = aExecMethod;
70 }
71
72 @Override
73 public void statusChanged(McStateEnum aNewState) {
74 Log.fi(aNewState);
75 if ( aNewState == McStateEnum.MC_INACTIVE ) {
76 // session is shut down
77 // (It cannot be the initial MC_INACTIVE state, because we are at least
78 // in MC_LISTENING state when the 1st asynchronous request is sent.)
79 releaseLock();
80 }
81 try {
82 statusChanged2( aNewState );
83 } catch (JniExecutorException e) {
84 Log.f( e.toString() );
85 fail();
86 }
87 Log.fo();
88 }
89
90 @Override
91 public void error(int aSeverity, String aMsg) {
92 Log.fi( aSeverity, aMsg );
93 fail();
94 Log.fo();
95 }
96
97 @Override
98 public void notify(Timeval aTime, String aSource, int aSeverity, String aMsg) {
99 Log.fi( aTime, aSource, aSeverity, aMsg );
100 Log.fo();
101 }
102
103 @Override
104 public void verdict(String aTestcase, VerdictTypeEnum aVerdictType) {
105 Log.fi( aTestcase, aVerdictType );
106 Log.fo();
107 }
108
109 @Override
110 public void verdictStats(Map<VerdictTypeEnum, Integer> aVerdictStats) {
111 Log.fi( aVerdictStats );
112 Log.fo();
113 }
114
115 /**
116 * Exception proof function instead of statusChanged(), exception is handled in statusChanged()
117 * @param aNewState
118 * @throws JniExecutorException
119 * @see #statusChanged(McStateEnum)
120 */
121 abstract protected void statusChanged2( final McStateEnum aNewState ) throws JniExecutorException;
122
123 /**
124 * releases the lock of the test client
125 */
126 private void releaseLock() {
127 if (mLockCompletion != null) {
128 synchronized (mLockCompletion) {
129 if (mLockCompletion != null) {
130 mLockCompletion.notifyAll();
131 }
132 }
133 }
134 }
135
136 /**
137 * Set verdict to FAIL on the observer, so to can be read by the test method
138 */
139 private void fail() {
140 mVerdict = false;
141 releaseLock();
142 }
143
144 /**
145 * sets lock, waits until lock is released
146 */
147 public void waitForCompletion() {
148 if (mLockCompletion != null) {
149 synchronized (mLockCompletion) {
150 if (mLockCompletion != null) {
151 try {
152 mLockCompletion.wait();
153 } catch (InterruptedException e) {
154 }
155 }
156 }
157 }
158 }
159 }
This page took 0.036662 seconds and 5 git commands to generate.