Commit | Line | Data |
---|---|---|
0e3d9d4a GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 École Polytechnique de Montréal | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.tests.shared; | |
14 | ||
15 | import static org.junit.Assert.fail; | |
16 | ||
17 | import java.lang.reflect.InvocationTargetException; | |
18 | import java.lang.reflect.Method; | |
19 | ||
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; | |
24 | ||
25 | /** | |
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. | |
29 | * | |
30 | * It may also serve as example for developers who want to do similar things in | |
31 | * less common use cases. | |
32 | * | |
33 | * @author Geneviève Bastien | |
34 | */ | |
35 | public class TmfTestHelper { | |
36 | ||
37 | /** | |
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. | |
42 | * | |
43 | * @param module | |
44 | * The analysis module to execute | |
45 | * @return The return value of the | |
46 | * {@link TmfAbstractAnalysisModule#executeAnalysis} method | |
47 | */ | |
48 | public static boolean executeAnalysis(IAnalysisModule module) { | |
49 | if (module instanceof TmfAbstractAnalysisModule) { | |
50 | try { | |
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()); | |
55 | return (Boolean) obj; | |
56 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { | |
57 | fail(e.toString()); | |
58 | } | |
59 | } | |
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?"); | |
61 | } | |
62 | ||
63 | } |