1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
;
15 import static org
.junit
.Assert
.fail
;
17 import java
.lang
.reflect
.InvocationTargetException
;
18 import java
.lang
.reflect
.Method
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.IAnalysisModule
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
26 * This class contains code for some common use cases of things that would be
27 * illegal to do in normal code, but are useful if not necessary in unit tests,
28 * like executing protected methods, using reflection.
30 * It may also serve as example for developers who want to do similar things in
31 * less common use cases.
33 * @author Geneviève Bastien
35 public class TmfTestHelper
{
38 * Calls the {@link TmfAbstractAnalysisModule#executeAnalysis} method of an
39 * analysis module. This method does not return until the analysis is
40 * completed and it returns the result of the method. It allows to execute
41 * the analysis without requiring an Eclipse job and waiting for completion.
44 * The analysis module to execute
45 * @return The return value of the
46 * {@link TmfAbstractAnalysisModule#executeAnalysis} method
48 public static boolean executeAnalysis(IAnalysisModule module
) {
49 if (module
instanceof TmfAbstractAnalysisModule
) {
51 Class
<?
>[] argTypes
= new Class
[] { IProgressMonitor
.class };
52 Method method
= TmfAbstractAnalysisModule
.class.getDeclaredMethod("executeAnalysis", argTypes
);
53 method
.setAccessible(true);
54 Object obj
= method
.invoke(module
, new NullProgressMonitor());
56 } catch (IllegalAccessException
| IllegalArgumentException
| InvocationTargetException
| NoSuchMethodException
| SecurityException e
) {
60 throw new RuntimeException("This analysis module does not have a protected method to execute. Maybe it can be executed differently? Or it is not supported yet in this method?");