Skip to content

Commit

Permalink
tmf: Unit-test TmfTraceType.selectExperimentType
Browse files Browse the repository at this point in the history
Add possible test coverage for the recently added selectExperimentType
in TmfTraceType. Briefly comment where coverage isn't done.

Augment the existing use of TmfExperimentStub for this added purpose.
Base this added validateWithTraces method on the recently introduced
TmfExperiment one. Introduce TmfExperimentSyslog as a supplemental yet
sibling for this same unit testing purpose, just to cover some more.

Fix a minor IDE warning (javadoc) and SonarLint issue (return) locally
in TmfTraceType while there. Fix such minor warnings also in the hereby
touched TraceAndExperimentTypeTest.

Change-Id: Ib7d792ee692e785f3b60158758ff1fc38ce7efcd
Signed-off-by: Marco Miller <marco.miller@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass/org.eclipse.tracecompass/+/203282
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
  • Loading branch information
marco-miller committed Jul 24, 2023
1 parent 01d20eb commit 935bc75
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.tracecompass.internal.tmf.core.Activator;
import org.eclipse.tracecompass.internal.tmf.core.request.TmfCoalescedEventRequest;
import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
import org.eclipse.tracecompass.tmf.core.component.TmfEventProvider;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;

Expand Down Expand Up @@ -120,4 +124,24 @@ public void setTimerEnabledFlag(boolean enabled) throws Exception {
public void addAnalysisModule(IAnalysisModule module) {
fAdditionalModules.add(module);
}

/**
* Make this specific stub meant to support traces with at least one
* prefixed with "A-".
*/
@Override
public IStatus validateWithTraces(List<ITmfTrace> traces) {
if (getClass() == TmfExperimentStub.class) {
int confidence = 0;
for (ITmfTrace trace : traces) {
if (trace.getName().startsWith("A-")) {
confidence = DEFAULT_GENERIC_EXPERIMENT_CONFIDENCE;
} else if (trace.getName().startsWith("E-")) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "stubbed error case"); //$NON-NLS-1$
}
}
return new TraceValidationStatus(confidence, Activator.PLUGIN_ID);
}
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "class extends TmfExperimentStub"); //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2023 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

package org.eclipse.tracecompass.tmf.tests.stubs.trace;

import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.tracecompass.internal.tmf.core.Activator;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;

/**
* @see TmfExperimentStub
*/
public class TmfExperimentSyslog extends TmfExperiment {

/**
* Default constructor. Should not be called directly by the code, but
* needed for the extension point.
*
* Do not call this directly (but do not remove it either!)
*/
public TmfExperimentSyslog() {
super();
}

@Override
protected ITmfTraceIndexer createIndexer(int interval) {
return new TmfIndexerStub(this, interval);
}

@Override
public TmfIndexerStub getIndexer() {
return (TmfIndexerStub) super.getIndexer();
}

/**
* Make this specific stub meant to support traces with at least one
* prefixed with "syslog".
*/
@Override
public IStatus validateWithTraces(List<ITmfTrace> traces) {
if (getClass() == TmfExperimentSyslog.class) {
int confidence = 0;
for (ITmfTrace trace : traces) {
if (trace.getName().startsWith("syslog")) {
confidence = DEFAULT_GENERIC_EXPERIMENT_CONFIDENCE;
} else if (trace.getName().startsWith("E-")) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "stubbed error case"); //$NON-NLS-1$
}
}
return new TraceValidationStatus(confidence, Activator.PLUGIN_ID);
}
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "class extends TmfExperimentSyslog"); //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,10 @@ public static List<TraceTypeHelper> selectExperimentType(List<ITmfTrace> traces,
}
}
List<TraceTypeHelper> returned = new ArrayList<>();
// If no valid candidates are found, then add generic experiment type

// If no valid candidates are found, then add generic experiment type.
// This case is not unit-testable, as TmfExperiment is always deemed as
// the valid minimum. This remains as a real-life fallback just in case.
if (validCandidates.isEmpty()) {
Activator.logInfo("No valid candidates were found, selecting generic TMF experiment type"); //$NON-NLS-1$
returned.add(getTraceType(DEFAULT_EXPERIMENT_TYPE));
Expand All @@ -668,11 +671,13 @@ public static List<TraceTypeHelper> selectExperimentType(List<ITmfTrace> traces,
if (validCandidates.size() != 1) {
List<Pair<Integer, TraceTypeHelper>> reducedCandidates = reduce(validCandidates);
if (reducedCandidates.isEmpty()) {
// This is barely unit-testable, only a real-life fallback.
Activator.logInfo("Error reducing experiment type candidates, selecting generic TMF experiment type"); //$NON-NLS-1$
returned.add(getTraceType(DEFAULT_EXPERIMENT_TYPE));
return returned;
} else if (reducedCandidates.size() == 1) {
// Don't select the exp type if it has the lowest confidence
// Another barely unit-testable flow; real-life fallback.
// Don't select the exp type if it has the lowest confidence.
if (reducedCandidates.get(0).getFirst() > 0) {
returned.add(reducedCandidates.get(0).getSecond());
}
Expand Down Expand Up @@ -718,6 +723,8 @@ public static List<TraceTypeHelper> selectExperimentType(List<ITmfTrace> traces,
* @param traceTypeId
* the trace type Id
* @return an instance of {@link ITmfEvent} or null
* @throws CoreException
* upon such a failing createExecutableExtension call
*
* @since 9.1
*/
Expand All @@ -742,7 +749,6 @@ public static List<TraceTypeHelper> selectExperimentType(List<ITmfTrace> traces,
if (ce == null) {
return null;
}
ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
return event;
return (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ public class TmfExperiment extends TmfTrace implements ITmfPersistentlyIndexable

/**
* The default confidence for the generic Tmf experiment
*
* @since 9.1
*/
private static final int DEFAULT_GENERIC_EXPERIMENT_CONFIDENCE = 1;
protected static final int DEFAULT_GENERIC_EXPERIMENT_CONFIDENCE = 1;

// ------------------------------------------------------------------------
// Attributes
Expand Down
9 changes: 9 additions & 0 deletions tmf/org.eclipse.tracecompass.tmf.ui.tests/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@
</analysisModuleClass>
</output>
</extension>
<extension
point="org.eclipse.linuxtools.tmf.core.tracetype">
<experiment
category="org.eclipse.linuxtools.tmf.core.tests.category"
id="org.eclipse.linuxtools.tmf.core.tests.experimenttype.syslog"
name="Test experiment syslog"
experiment_type="org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfExperimentSyslog">
</experiment>
</extension>
<extension
point="org.eclipse.ui.editors">
<editor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfExperimentStub;
Expand All @@ -50,11 +55,12 @@
public class TraceAndExperimentTypeTest {

/** Test experiment type id */
public final static String TEST_EXPERIMENT_TYPE = "org.eclipse.linuxtools.tmf.core.tests.experimenttype";
public static final String TEST_EXPERIMENT_TYPE = "org.eclipse.linuxtools.tmf.core.tests.experimenttype";

private List<ITmfTrace> fTraces;
private TmfProjectElement fixture;
private TmfExperimentElement fExperiment;
private final String EXPERIMENT_NAME = "exp_test";
private static final String EXPERIMENT_NAME = "exp_test";

/**
* Perform pre-test initialization.
Expand All @@ -65,6 +71,7 @@ public void setUp() {
fixture = ProjectModelTestData.getFilledProject();
fExperiment = ProjectModelTestData.addExperiment(fixture, EXPERIMENT_NAME);
assertNotNull(fExperiment);
fTraces = new ArrayList<>();
} catch (CoreException e) {
fail(e.getMessage());
}
Expand All @@ -76,6 +83,9 @@ public void setUp() {
@After
public void cleanUp() {
ProjectModelTestData.deleteProject(fixture);
for (ITmfTrace trace : fTraces) {
trace.dispose();
}
}

/**
Expand All @@ -92,6 +102,83 @@ public void testDefaultExperimentType() {
experiment.dispose();
}

/**
* Test selecting experiment type based on valid hint (default)
*/
@Test
public void testSelectExperimentTypeWithHint() {
fTraces.add(TmfTestTrace.A_TEST_10K.getTrace());
fTraces.add(TmfTestTrace.A_TEST_10K2.getTrace());

String expected = TmfTraceType.DEFAULT_EXPERIMENT_TYPE;
List<TraceTypeHelper> types = TmfTraceType.selectExperimentType(fTraces, expected);
assertEquals(1, types.size());
assertEquals(expected, types.get(0).getTraceTypeId());
}

/**
* Test selecting experiment type based on no hint (null) => main stub
*/
@Test
public void testSelectExperimentTypeWithoutHint() {
fTraces.add(TmfTestTrace.A_TEST_10K.getTrace());
fTraces.add(TmfTestTrace.A_TEST_10K2.getTrace());

List<TraceTypeHelper> types = TmfTraceType.selectExperimentType(fTraces, null);
assertEquals(1, types.size());
assertEquals(TEST_EXPERIMENT_TYPE, types.get(0).getTraceTypeId());
}

/**
* Test selecting experiment type based on no hint, SysLog traces
*/
@Test
public void testSelectExperimentTypeSyslog() {
fTraces.add(TmfTestTrace.SYSLOG_1.getTrace());
fTraces.add(TmfTestTrace.SYSLOG_2.getTrace());

List<TraceTypeHelper> types = TmfTraceType.selectExperimentType(fTraces, null);
assertEquals(1, types.size());
assertEquals(TEST_EXPERIMENT_TYPE + ".syslog", types.get(0).getTraceTypeId());
}

/**
* Test selecting experiment type based on no hint, mixed traces
*/
@Test
public void testSelectExperimentTypeMixed() {
fTraces.add(TmfTestTrace.A_TEST_10K.getTrace());
fTraces.add(TmfTestTrace.SYSLOG_1.getTrace());

List<TraceTypeHelper> types = TmfTraceType.selectExperimentType(fTraces, null);
assertEquals(2, types.size());
}

/**
* Test selecting experiment type based on no hint, unknown traces
*/
@Test
public void testSelectExperimentTypeUnknown() {
fTraces.add(TmfTestTrace.O_TEST_10K.getTrace());
fTraces.add(TmfTestTrace.R_TEST_10K.getTrace());

List<TraceTypeHelper> types = TmfTraceType.selectExperimentType(fTraces, null);
assertTrue(types.isEmpty());
}

/**
* Test selecting experiment type based on no hint, error trace => default
*/
@Test
public void testSelectExperimentTypeError() {
fTraces.add(TmfTestTrace.A_TEST_10K.getTrace());
fTraces.add(TmfTestTrace.E_TEST_10K.getTrace()); // stubbed error case

List<TraceTypeHelper> types = TmfTraceType.selectExperimentType(fTraces, null);
assertEquals(1, types.size());
assertEquals(TmfTraceType.DEFAULT_EXPERIMENT_TYPE, types.get(0).getTraceTypeId());
}

/**
* Test that the experiment opened is of the right class
*/
Expand Down Expand Up @@ -129,7 +216,9 @@ public void testNoExperimentTypeChildren() {
final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
IEditorPart editor = activePage.getActiveEditor();

/* Test the editor class. Cannot test table class since it is unexposed */
/*
* Test the editor class. Cannot test table class since it is unexposed
*/
assertNotNull(editor);
assertTrue(editor.getClass().equals(TmfEventsEditor.class));

Expand Down Expand Up @@ -175,7 +264,8 @@ public void testExperimentTypeChildren() {
}

/**
* Test that the analysis get populated under an experiment of the proper type
* Test that the analysis get populated under an experiment of the proper
* type
*/
@Test
public void testExperimentTypeAnalysis() {
Expand Down

0 comments on commit 935bc75

Please sign in to comment.