| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013 É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.analysis; |
| 14 | |
| 15 | import static org.junit.Assert.assertEquals; |
| 16 | import static org.junit.Assert.assertFalse; |
| 17 | import static org.junit.Assert.assertNotNull; |
| 18 | import static org.junit.Assert.assertNull; |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | import static org.junit.Assert.fail; |
| 21 | |
| 22 | import org.eclipse.core.runtime.IStatus; |
| 23 | import org.eclipse.core.runtime.Status; |
| 24 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule; |
| 25 | import org.eclipse.linuxtools.tmf.core.analysis.Messages; |
| 26 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAbstractAnalysisModule; |
| 27 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException; |
| 28 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace; |
| 29 | import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestTrace; |
| 30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 31 | import org.eclipse.linuxtools.tmf.tests.stubs.analysis.TestAnalysis; |
| 32 | import org.eclipse.linuxtools.tmf.tests.stubs.analysis.TestCtfAnalysis; |
| 33 | import org.eclipse.osgi.util.NLS; |
| 34 | import org.junit.After; |
| 35 | import org.junit.Test; |
| 36 | |
| 37 | /** |
| 38 | * Test suite for the {@link TmfAbstractAnalysisModule} class |
| 39 | */ |
| 40 | public class AnalysisModuleTest { |
| 41 | |
| 42 | private static String MODULE_GENERIC_ID = "test.id"; |
| 43 | private static String MODULE_GENERIC_NAME = "Test analysis"; |
| 44 | |
| 45 | /** |
| 46 | * Some tests use traces, let's clean them here |
| 47 | */ |
| 48 | @After |
| 49 | public void cleanupTraces() { |
| 50 | TmfTestTrace.A_TEST_10K.dispose(); |
| 51 | CtfTmfTestTrace.KERNEL.dispose(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Test suite for analysis module getters and setters |
| 56 | */ |
| 57 | @Test |
| 58 | public void testGettersSetters() { |
| 59 | IAnalysisModule module = new TestAnalysis(); |
| 60 | |
| 61 | module.setName(MODULE_GENERIC_NAME); |
| 62 | module.setId(MODULE_GENERIC_ID); |
| 63 | assertEquals(MODULE_GENERIC_ID, module.getId()); |
| 64 | assertEquals(MODULE_GENERIC_NAME, module.getName()); |
| 65 | |
| 66 | module.setAutomatic(false); |
| 67 | assertFalse(module.isAutomatic()); |
| 68 | module.setAutomatic(true); |
| 69 | assertTrue(module.isAutomatic()); |
| 70 | module.addParameter(TestAnalysis.PARAM_TEST); |
| 71 | assertNull(module.getParameter(TestAnalysis.PARAM_TEST)); |
| 72 | module.setParameter(TestAnalysis.PARAM_TEST, 1); |
| 73 | assertEquals(1, module.getParameter(TestAnalysis.PARAM_TEST)); |
| 74 | |
| 75 | /* Try to set and get wrong parameter */ |
| 76 | String wrongParam = "abc"; |
| 77 | Exception exception = null; |
| 78 | try { |
| 79 | module.setParameter(wrongParam, 1); |
| 80 | } catch (RuntimeException e) { |
| 81 | exception = e; |
| 82 | assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_InvalidParameter, wrongParam, module.getName()), e.getMessage()); |
| 83 | } |
| 84 | assertNotNull(exception); |
| 85 | assertNull(module.getParameter(wrongParam)); |
| 86 | } |
| 87 | |
| 88 | private static TestAnalysis setUpAnalysis() { |
| 89 | TestAnalysis module = new TestAnalysis(); |
| 90 | |
| 91 | module.setName(MODULE_GENERIC_NAME); |
| 92 | module.setId(MODULE_GENERIC_ID); |
| 93 | module.addParameter(TestAnalysis.PARAM_TEST); |
| 94 | |
| 95 | return module; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Test suite for analysis module |
| 101 | * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful |
| 102 | * execution |
| 103 | */ |
| 104 | @Test |
| 105 | public void testWaitForCompletionSuccess() { |
| 106 | TestAnalysis module = setUpAnalysis(); |
| 107 | |
| 108 | IStatus status = module.schedule(); |
| 109 | assertEquals(IStatus.ERROR, status.getSeverity()); |
| 110 | |
| 111 | /* Set a stub trace for analysis */ |
| 112 | try { |
| 113 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); |
| 114 | } catch (TmfAnalysisException e) { |
| 115 | fail(e.getMessage()); |
| 116 | } |
| 117 | |
| 118 | /* Default execution, with output 1 */ |
| 119 | module.setParameter(TestAnalysis.PARAM_TEST, 1); |
| 120 | status = module.schedule(); |
| 121 | assertEquals(Status.OK_STATUS, status); |
| 122 | boolean completed = module.waitForCompletion(); |
| 123 | |
| 124 | assertTrue(completed); |
| 125 | assertEquals(1, module.getAnalysisOutput()); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with cancellation |
| 130 | */ |
| 131 | @Test |
| 132 | public void testWaitForCompletionCancelled() { |
| 133 | TestAnalysis module = setUpAnalysis(); |
| 134 | |
| 135 | /* Set a stub trace for analysis */ |
| 136 | ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace(); |
| 137 | try { |
| 138 | module.setTrace(trace); |
| 139 | } catch (TmfAnalysisException e) { |
| 140 | fail(e.getMessage()); |
| 141 | } |
| 142 | |
| 143 | module.setParameter(TestAnalysis.PARAM_TEST, 0); |
| 144 | IStatus status = module.schedule(); |
| 145 | assertEquals(Status.OK_STATUS, status); |
| 146 | boolean completed = module.waitForCompletion(); |
| 147 | |
| 148 | assertFalse(completed); |
| 149 | assertEquals(0, module.getAnalysisOutput()); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method with wrong trace |
| 154 | */ |
| 155 | @Test |
| 156 | public void testSetWrongTrace() { |
| 157 | IAnalysisModule module = new TestCtfAnalysis(); |
| 158 | |
| 159 | module.setName(MODULE_GENERIC_NAME); |
| 160 | module.setId(MODULE_GENERIC_ID); |
| 161 | assertEquals(MODULE_GENERIC_ID, module.getId()); |
| 162 | assertEquals(MODULE_GENERIC_NAME, module.getName()); |
| 163 | |
| 164 | Exception exception = null; |
| 165 | try { |
| 166 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); |
| 167 | } catch (TmfAnalysisException e) { |
| 168 | exception = e; |
| 169 | } |
| 170 | assertNotNull(exception); |
| 171 | assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute, module.getName()), exception.getMessage()); |
| 172 | |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method |
| 177 | */ |
| 178 | @Test |
| 179 | public void testCancel() { |
| 180 | TestAnalysis module = setUpAnalysis(); |
| 181 | |
| 182 | module.setParameter(TestAnalysis.PARAM_TEST, 999); |
| 183 | try { |
| 184 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); |
| 185 | } catch (TmfAnalysisException e) { |
| 186 | fail(e.getMessage()); |
| 187 | } |
| 188 | |
| 189 | assertEquals(Status.OK_STATUS, module.schedule()); |
| 190 | |
| 191 | /* Give the job a chance to start */ |
| 192 | try { |
| 193 | Thread.sleep(1000); |
| 194 | } catch (InterruptedException e) { |
| 195 | fail(e.getMessage()); |
| 196 | } |
| 197 | |
| 198 | module.cancel(); |
| 199 | assertFalse(module.waitForCompletion()); |
| 200 | assertEquals(-1, module.getAnalysisOutput()); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)} |
| 205 | * method |
| 206 | */ |
| 207 | @Test |
| 208 | public void testParameterChanged() { |
| 209 | TestAnalysis module = setUpAnalysis(); |
| 210 | |
| 211 | try { |
| 212 | module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()); |
| 213 | } catch (TmfAnalysisException e) { |
| 214 | fail(e.getMessage()); |
| 215 | } |
| 216 | |
| 217 | /* Check exception if no wrong parameter name */ |
| 218 | Exception exception = null; |
| 219 | try { |
| 220 | module.notifyParameterChanged("aaa"); |
| 221 | } catch (RuntimeException e) { |
| 222 | exception = e; |
| 223 | } |
| 224 | assertNotNull(exception); |
| 225 | |
| 226 | /* |
| 227 | * Cannot test anymore of this method, need a parameter provider to do |
| 228 | * this |
| 229 | */ |
| 230 | } |
| 231 | } |