1  /**************************************************************
2   *
3   * Licensed to the Apache Software Foundation (ASF) under one
4   * or more contributor license agreements.  See the NOTICE file
5   * distributed with this work for additional information
6   * regarding copyright ownership.  The ASF licenses this file
7   * to you under the Apache License, Version 2.0 (the
8   * "License"); you may not use this file except in compliance
9   * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   *
20   *************************************************************/
21  
22  package complexlib;
23  
24  import java.io.PrintWriter;
25  import java.lang.reflect.Method;
26  
27  /**
28   * Invoke a method of a class in an own thread. Provide a method to end
29   * the thread.
30   */
31  public class MethodThread extends Thread
32  {
33  
34      /** The method that should be executed **/
35      private Method mTestMethod = null;
36      /** The object that implements the method **/
37      private Object mInvokeClass = null;
38      /** A PrintWriter for debug Output **/
39      private PrintWriter mLog = null;
40      /** An Error String **/
41      private String mErrMessage = null;
42      /** Did an Exception happen? **/
43      private boolean mExceptionHappened = false;
44      private Object[] mParameter = null;
45  
46      /**
47       * Constructor.
48       * @param testMethod The method that will be invoked.
49       * @param invokeClass The class where the method is invoked.
50       * @param log The logging mechanism.
51       */
MethodThread(Method testMethod, Object invokeClass, PrintWriter log)52      public MethodThread(Method testMethod, Object invokeClass, PrintWriter log)
53      {
54          mTestMethod = testMethod;
55          mInvokeClass = invokeClass;
56          mLog = log;
57      }
58  
MethodThread(Method testMethod, Object invokeClass, Object[] parameter, PrintWriter log)59      public MethodThread(Method testMethod, Object invokeClass, Object[] parameter, PrintWriter log)
60      {
61          mTestMethod = testMethod;
62          mInvokeClass = invokeClass;
63          mParameter = parameter;
64          mLog = log;
65      }
66  
67      /**
68       * Invoke the method.
69       */
run()70      public void run()
71      {
72          try
73          {
74              mTestMethod.invoke(mInvokeClass, mParameter);
75          }
76          catch (IllegalAccessException e)
77          {
78              e.printStackTrace(mLog);
79              mErrMessage = e.getMessage();
80              mExceptionHappened = true;
81          }
82          catch (java.lang.reflect.InvocationTargetException e)
83          {
84              Throwable t = e.getTargetException();
85              if (!(t instanceof ComplexTestCase.AssureException))
86              {
87                  t.printStackTrace(mLog);
88                  mErrMessage = t.getMessage();
89                  if (mErrMessage == null)
90                  {
91                      mErrMessage = t.toString();
92                  }
93                  mExceptionHappened = true;
94              }
95  
96          }
97      }
98  
99      /**
100       * Get the error message
101       * @return The error message.
102       */
getErrorMessage()103      public String getErrorMessage()
104      {
105          return mErrMessage;
106      }
107  
108      /**
109       * Is there an error message?
110       * @return True, if an error did happen.
111       */
hasErrorMessage()112      public boolean hasErrorMessage()
113      {
114          return mExceptionHappened;
115      }
116  
117      /**
118       * Stop the running method.
119       */
destroy()120      public void destroy()
121      {
122          try
123          {
124              interrupt();
125          }
126          catch (SecurityException e)
127          {
128              e.printStackTrace(mLog);
129              mErrMessage = e.getMessage();
130              mExceptionHappened = true;
131          }
132      }
133  }
134