Skip to content

Commit

Permalink
#57: replaced simple map with ReportingEntry object
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Merdes authored and Matthias Merdes committed Jan 20, 2016
1 parent f54b2c2 commit 59ca2c8
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 39 deletions.
@@ -0,0 +1,29 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.gen5.commons.reporting;

import java.util.Map;

public class ReportingEntry {

private final Map<String, String> values;

/**
* @param values the values to be published
*/
public ReportingEntry(Map<String, String> values) {
this.values = values;
}

public Map<String, String> getValues() {
return values;
}
}
Expand Up @@ -10,8 +10,7 @@


package org.junit.gen5.engine; package org.junit.gen5.engine;


import java.util.*; import org.junit.gen5.commons.reporting.ReportingEntry;

import org.junit.gen5.engine.TestExecutionResult.Status; import org.junit.gen5.engine.TestExecutionResult.Status;


/** /**
Expand All @@ -35,13 +34,12 @@ public interface EngineExecutionListener {
* <li>Information about test context or test data</li> * <li>Information about test context or test data</li>
* </ul> * </ul>
* *
* <p>The current lifecycle state of {@code testDescriptor} is not relevant; that means that reporting events * <p>The current lifecycle state of {@code testDescriptor} is not relevant;
* can occur at all times. * that means that reporting events can occur at all times.
*
* @param testDescriptor the descriptor of the test or container to which the entry belongs * @param testDescriptor the descriptor of the test or container to which the entry belongs
* @param entry a collection of key value pairs to be reported * @param entry a {@code ReportingEntry} instance to be published
*/ */
void reportingEntryPublished(TestDescriptor testDescriptor, Map<String, String> entry); void reportingEntryPublished(TestDescriptor testDescriptor, ReportingEntry entry);


/** /**
* Must be called when a new, dynamic {@link TestDescriptor} has been * Must be called when a new, dynamic {@link TestDescriptor} has been
Expand Down
Expand Up @@ -10,12 +10,9 @@


package org.junit.gen5.launcher; package org.junit.gen5.launcher;


import java.util.Map; import org.junit.gen5.commons.reporting.ReportingEntry;

import org.junit.gen5.engine.TestExecutionResult; import org.junit.gen5.engine.TestExecutionResult;
import org.junit.gen5.engine.TestExecutionResult.Status; import org.junit.gen5.engine.TestExecutionResult.Status;
import org.junit.gen5.launcher.TestIdentifier;
import org.junit.gen5.launcher.TestPlan;
import org.junit.gen5.launcher.main.Launcher; import org.junit.gen5.launcher.main.Launcher;


/** /**
Expand Down Expand Up @@ -50,9 +47,9 @@ public interface TestExecutionListener {
* the supplied {@link TestIdentifier}. Can be called at all times. * the supplied {@link TestIdentifier}. Can be called at all times.
* *
* @param testIdentifier describes the test or container to which the entry pertains * @param testIdentifier describes the test or container to which the entry pertains
* @param entry a collection of key-value pairs to be reported * @param entry a {@code ReportingEntry} instance to be published
*/ */
default void reportingEntryPublished(TestIdentifier testIdentifier, Map<String, String> entry) { default void reportingEntryPublished(TestIdentifier testIdentifier, ReportingEntry entry) {
} }


/** /**
Expand Down
Expand Up @@ -10,8 +10,7 @@


package org.junit.gen5.launcher.main; package org.junit.gen5.launcher.main;


import java.util.Map; import org.junit.gen5.commons.reporting.ReportingEntry;

import org.junit.gen5.engine.EngineExecutionListener; import org.junit.gen5.engine.EngineExecutionListener;
import org.junit.gen5.engine.TestDescriptor; import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestExecutionResult; import org.junit.gen5.engine.TestExecutionResult;
Expand All @@ -31,7 +30,7 @@ public ExecutionListenerAdapter(TestPlan testPlan, TestExecutionListener testExe
} }


@Override @Override
public void reportingEntryPublished(TestDescriptor testDescriptor, Map<String, String> entry) { public void reportingEntryPublished(TestDescriptor testDescriptor, ReportingEntry entry) {
testExecutionListener.reportingEntryPublished(getTestIdentifier(testDescriptor), entry); testExecutionListener.reportingEntryPublished(getTestIdentifier(testDescriptor), entry);
} }


Expand Down
Expand Up @@ -17,6 +17,7 @@
import java.util.*; import java.util.*;
import java.util.function.Predicate; import java.util.function.Predicate;


import org.junit.gen5.commons.reporting.ReportingEntry;
import org.junit.gen5.commons.util.ToStringBuilder; import org.junit.gen5.commons.util.ToStringBuilder;


/** /**
Expand All @@ -30,7 +31,7 @@ public enum Type {
DYNAMIC_TEST_REGISTERED, SKIPPED, STARTED, FINISHED, REPORTING_ENTRY_PUBLISHED DYNAMIC_TEST_REGISTERED, SKIPPED, STARTED, FINISHED, REPORTING_ENTRY_PUBLISHED
} }


public static ExecutionEvent reportingEntryPublished(TestDescriptor testDescriptor, Map<String, String> entry) { public static ExecutionEvent reportingEntryPublished(TestDescriptor testDescriptor, ReportingEntry entry) {
return new ExecutionEvent(REPORTING_ENTRY_PUBLISHED, testDescriptor, entry); return new ExecutionEvent(REPORTING_ENTRY_PUBLISHED, testDescriptor, entry);
} }


Expand Down
Expand Up @@ -22,11 +22,11 @@
import static org.junit.gen5.engine.ExecutionEvent.byType; import static org.junit.gen5.engine.ExecutionEvent.byType;


import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream; import java.util.stream.Stream;


import org.junit.gen5.commons.reporting.ReportingEntry;
import org.junit.gen5.engine.ExecutionEvent.Type; import org.junit.gen5.engine.ExecutionEvent.Type;
import org.junit.gen5.engine.TestExecutionResult.Status; import org.junit.gen5.engine.TestExecutionResult.Status;


Expand All @@ -47,7 +47,7 @@ public static List<ExecutionEvent> execute(TestEngine testEngine, EngineDiscover
public final List<ExecutionEvent> executionEvents = new CopyOnWriteArrayList<>(); public final List<ExecutionEvent> executionEvents = new CopyOnWriteArrayList<>();


@Override @Override
public void reportingEntryPublished(TestDescriptor testDescriptor, Map<String, String> entry) { public void reportingEntryPublished(TestDescriptor testDescriptor, ReportingEntry entry) {
addEvent(ExecutionEvent.reportingEntryPublished(testDescriptor, entry)); addEvent(ExecutionEvent.reportingEntryPublished(testDescriptor, entry));
} }


Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.junit.gen5.api.BeforeEach; import org.junit.gen5.api.BeforeEach;
import org.junit.gen5.api.Test; import org.junit.gen5.api.Test;
import org.junit.gen5.api.TestReporter; import org.junit.gen5.api.TestReporter;
import org.junit.gen5.commons.reporting.ReportingEntry;
import org.junit.gen5.engine.ExecutionEventRecorder; import org.junit.gen5.engine.ExecutionEventRecorder;
import org.junit.gen5.launcher.TestDiscoveryRequest; import org.junit.gen5.launcher.TestDiscoveryRequest;


Expand All @@ -44,17 +45,17 @@ class MyReportingTestCase {


@BeforeEach @BeforeEach
void before(TestReporter reporter) { void before(TestReporter reporter) {
reporter.publishEntry(new HashMap<>()); reporter.publishEntry(new ReportingEntry(new HashMap<>()));
} }


@AfterEach @AfterEach
void after(TestReporter reporter) { void after(TestReporter reporter) {
reporter.publishEntry(new HashMap<>()); reporter.publishEntry(new ReportingEntry(new HashMap<>()));
} }


@Test @Test
void succeedingTest(TestReporter reporter) { void succeedingTest(TestReporter reporter) {
reporter.publishEntry(new HashMap<>()); reporter.publishEntry(new ReportingEntry(new HashMap<>()));
} }


} }
Expand Up @@ -13,12 +13,12 @@
import static org.junit.gen5.api.Assertions.*; import static org.junit.gen5.api.Assertions.*;


import java.util.Collections; import java.util.Collections;
import java.util.Map;
import java.util.Optional; import java.util.Optional;


import org.junit.gen5.api.Assertions; import org.junit.gen5.api.Assertions;
import org.junit.gen5.api.Test; import org.junit.gen5.api.Test;
import org.junit.gen5.api.extension.ExtensionContext; import org.junit.gen5.api.extension.ExtensionContext;
import org.junit.gen5.commons.reporting.ReportingEntry;
import org.junit.gen5.engine.EngineExecutionListener; import org.junit.gen5.engine.EngineExecutionListener;
import org.junit.gen5.engine.TestDescriptor; import org.junit.gen5.engine.TestDescriptor;
import org.mockito.Mockito; import org.mockito.Mockito;
Expand Down Expand Up @@ -73,16 +73,14 @@ public void reportEntriesArePublishedToExecutionContext() {
ExtensionContext extensionContext = new ClassBasedContainerExtensionContext(null, engineExecutionListener, ExtensionContext extensionContext = new ClassBasedContainerExtensionContext(null, engineExecutionListener,
classTestDescriptor); classTestDescriptor);


Map<String, String> reportEntry1 = Collections.emptyMap(); ReportingEntry entry1 = new ReportingEntry(Collections.emptyMap());
Map<String, String> reportEntry2 = Collections.singletonMap("key", "value"); ReportingEntry entry2 = new ReportingEntry(Collections.singletonMap("key", "value"));


extensionContext.publishReportEntry(reportEntry1); extensionContext.publishReportEntry(entry1);
extensionContext.publishReportEntry(reportEntry2); extensionContext.publishReportEntry(entry2);


Mockito.verify(engineExecutionListener, Mockito.times(1)).reportingEntryPublished(classTestDescriptor, Mockito.verify(engineExecutionListener, Mockito.times(1)).reportingEntryPublished(classTestDescriptor, entry1);
reportEntry1); Mockito.verify(engineExecutionListener, Mockito.times(1)).reportingEntryPublished(classTestDescriptor, entry2);
Mockito.verify(engineExecutionListener, Mockito.times(1)).reportingEntryPublished(classTestDescriptor,
reportEntry2);
} }


@Test @Test
Expand Down
11 changes: 9 additions & 2 deletions junit5-api/src/main/java/org/junit/gen5/api/TestReporter.java
Expand Up @@ -10,14 +10,21 @@


package org.junit.gen5.api; package org.junit.gen5.api;


import java.util.Map; import org.junit.gen5.commons.reporting.ReportingEntry;


/** /**
* Variables of type {@code TestReporter} can be injected into
* methods of test classes annotated with {@link BeforeEach},
* {@link AfterEach}, and {@link Test} annotations, respectively.
*
* <p>Within these methods these references can then be used
* to publish {@link ReportingEntry} instances.
*
* @since 5.0 * @since 5.0
*/ */
@FunctionalInterface @FunctionalInterface
public interface TestReporter { public interface TestReporter {


void publishEntry(Map<String, String> entry); void publishEntry(ReportingEntry entry);


} }
Expand Up @@ -13,11 +13,11 @@
import java.lang.reflect.AnnotatedElement; import java.lang.reflect.AnnotatedElement;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;


import org.junit.gen5.commons.reporting.ReportingEntry;
import org.junit.gen5.commons.util.Preconditions; import org.junit.gen5.commons.util.Preconditions;


/** /**
Expand All @@ -32,12 +32,12 @@
public interface ExtensionContext { public interface ExtensionContext {


/** /**
* Publish a report {@code entry} to be consumed by an * Publish a {@code ReportingEntry} to be consumed by an
* {@code org.junit.gen5.engine.EngineExecutionListener}. * {@code org.junit.gen5.engine.EngineExecutionListener}.
* *
* @param entry the entry to be published * @param entry the entry to be reported
*/ */
void publishReportEntry(Map<String, String> entry); void publishReportEntry(ReportingEntry entry);


/** /**
* Get the parent extension context if there is one. * Get the parent extension context if there is one.
Expand Down
Expand Up @@ -10,10 +10,10 @@


package org.junit.gen5.engine.junit5.descriptor; package org.junit.gen5.engine.junit5.descriptor;


import java.util.Map;
import java.util.Optional; import java.util.Optional;


import org.junit.gen5.api.extension.ExtensionContext; import org.junit.gen5.api.extension.ExtensionContext;
import org.junit.gen5.commons.reporting.ReportingEntry;
import org.junit.gen5.engine.EngineExecutionListener; import org.junit.gen5.engine.EngineExecutionListener;
import org.junit.gen5.engine.TestDescriptor; import org.junit.gen5.engine.TestDescriptor;


Expand Down Expand Up @@ -44,7 +44,7 @@ private ExtensionValuesStore createStore(ExtensionContext parent) {
} }


@Override @Override
public void publishReportEntry(Map<String, String> entry) { public void publishReportEntry(ReportingEntry entry) {
engineExecutionListener.reportingEntryPublished(this.testDescriptor, entry); engineExecutionListener.reportingEntryPublished(this.testDescriptor, entry);
} }


Expand Down

0 comments on commit 59ca2c8

Please sign in to comment.