| 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.shared; |
| 14 | |
| 15 | import java.io.File; |
| 16 | import java.io.IOException; |
| 17 | import java.net.URISyntaxException; |
| 18 | import java.net.URL; |
| 19 | |
| 20 | import org.eclipse.core.runtime.FileLocator; |
| 21 | import org.eclipse.core.runtime.Path; |
| 22 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
| 23 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; |
| 24 | import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; |
| 25 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 26 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; |
| 27 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub2; |
| 28 | |
| 29 | /** |
| 30 | * Generic TMF test traces |
| 31 | * |
| 32 | * @author Geneviève Bastien |
| 33 | */ |
| 34 | public enum TmfTestTrace { |
| 35 | /** A test */ |
| 36 | A_TEST_10K("A-Test-10K"), |
| 37 | /** A second trace */ |
| 38 | A_TEST_10K2("A-Test-10K-2"), |
| 39 | /** A third trace */ |
| 40 | E_TEST_10K("E-Test-10K"), |
| 41 | /** A fourth trace */ |
| 42 | O_TEST_10K("O-Test-10K"), |
| 43 | /** And oh! a fifth trace */ |
| 44 | R_TEST_10K("R-Test-10K"); |
| 45 | |
| 46 | private final String fPath; |
| 47 | private final String fDirectory = "../org.eclipse.linuxtools.tmf.core.tests/testfiles"; |
| 48 | private ITmfTrace fTrace = null; |
| 49 | |
| 50 | private TmfTestTrace(String file) { |
| 51 | fPath = file; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the path of the trace |
| 56 | * |
| 57 | * @return The path of this trace |
| 58 | */ |
| 59 | public String getPath() { |
| 60 | return fPath; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the full path of the trace |
| 65 | * |
| 66 | * @return The full path of the trace |
| 67 | */ |
| 68 | public String getFullPath() { |
| 69 | return fDirectory + File.separator + fPath; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Return a ITmfTrace object of this test trace. It will be already |
| 74 | * initTrace()'ed. This method will always return a new trace and dispose of |
| 75 | * the old one. |
| 76 | * |
| 77 | * After being used by unit tests, traces must be properly disposed of by |
| 78 | * calling the {@link TmfTestTrace#dispose()} method. |
| 79 | * |
| 80 | * @return A {@link ITmfTrace} reference to this trace |
| 81 | */ |
| 82 | public ITmfTrace getTrace() { |
| 83 | if (fTrace != null) { |
| 84 | fTrace.dispose(); |
| 85 | } |
| 86 | final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(fDirectory + File.separator + fPath), null); |
| 87 | try { |
| 88 | File test = new File(FileLocator.toFileURL(location).toURI()); |
| 89 | fTrace = new TmfTraceStub(test.toURI().getPath(), ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false, null); |
| 90 | |
| 91 | } catch (URISyntaxException | IOException | TmfTraceException e) { |
| 92 | throw new IllegalStateException(e); |
| 93 | } |
| 94 | return fTrace; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Return a ITmfTrace object that is of type {@link TmfTraceStub2}. It |
| 99 | * will be already initTrace()'ed. But the trace will be deregistered from |
| 100 | * signal managers and will need to be manually disposed of by the caller. |
| 101 | * |
| 102 | * @return a {@link ITmfTrace} reference to this trace |
| 103 | */ |
| 104 | public ITmfTrace getTraceAsStub2() { |
| 105 | ITmfTrace trace = null; |
| 106 | final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(fDirectory + File.separator + fPath), null); |
| 107 | try { |
| 108 | File test = new File(FileLocator.toFileURL(location).toURI()); |
| 109 | trace = new TmfTraceStub2(test.toURI().getPath(), ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false, null); |
| 110 | TmfSignalManager.deregister(trace); |
| 111 | |
| 112 | } catch (URISyntaxException | IOException | TmfTraceException e) { |
| 113 | throw new IllegalStateException(e); |
| 114 | } |
| 115 | return trace; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Dispose of the trace |
| 120 | */ |
| 121 | public void dispose() { |
| 122 | if (fTrace != null) { |
| 123 | fTrace.dispose(); |
| 124 | fTrace = null; |
| 125 | } |
| 126 | } |
| 127 | } |