-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
How to use junit.jupiter Extensions in OSGi and plain java
logs start, stop, duration, error for all tests executed
- Loading branch information
1 parent
b2d6bdd
commit dea1467
Showing
6 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
.../bundles/org.eclipse.test/src/META-INF/services/org.junit.jupiter.api.extension.Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.eclipse.test.services.LoggingTestExtension |
1 change: 1 addition & 0 deletions
1
eclipse.platform.releng/bundles/org.eclipse.test/src/junit-platform.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
junit.jupiter.extensions.autodetection.enabled=true |
56 changes: 56 additions & 0 deletions
56
...m.releng/bundles/org.eclipse.test/src/org/eclipse/test/services/LoggingTestExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Joerg Kubitz and others. | ||
* | ||
* 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 | ||
* | ||
* Contributors: | ||
* Joerg Kubitz - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.test.services; | ||
|
||
import org.junit.jupiter.api.extension.AfterTestExecutionCallback; | ||
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; | ||
import org.junit.jupiter.api.extension.ExtensionContext.Store; | ||
|
||
/** logs start, stop, duration, error for all tests executed **/ | ||
public class LoggingTestExtension implements AfterTestExecutionCallback, BeforeTestExecutionCallback { | ||
public static String BEFORE_TEST_START = "Test Before: "; | ||
public static String AFTER_TEST_PASSED = "Test Passed: "; | ||
public static String AFTER_TEST_FAILED = "Test Failed: "; | ||
|
||
public LoggingTestExtension() { | ||
System.out.println("LoggingTestService"); | ||
} | ||
@Override | ||
public void beforeTestExecution(ExtensionContext context) throws Exception { | ||
long n0 = System.nanoTime(); | ||
getStore(context).put("TIME", n0); | ||
System.out.println(BEFORE_TEST_START + context.getDisplayName()); | ||
} | ||
|
||
@Override | ||
public void afterTestExecution(ExtensionContext context) throws Exception { | ||
long t1 = System.nanoTime(); | ||
long t0 = getStore(context).remove("TIME", long.class); | ||
String took = " after: " + (t1 - t0) / 1_000_000 + "ms"; | ||
Throwable t = context.getExecutionException().orElse(null); | ||
if (t == null) { | ||
System.out.println(AFTER_TEST_PASSED + context.getDisplayName() + took); | ||
} else { | ||
System.out.println(AFTER_TEST_FAILED + context.getDisplayName() + took); | ||
t.printStackTrace(System.out); | ||
} | ||
} | ||
|
||
private Store getStore(ExtensionContext context) { | ||
return context.getStore(Namespace.create(getClass(), context.getRequiredTestMethod())); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...leng/bundles/org.eclipse.test/src/org/eclipse/test/services/LoggingTestExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Joerg Kubitz and others. | ||
* | ||
* 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 | ||
* | ||
* Contributors: | ||
* Joerg Kubitz - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.test.services; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** verifies the Extension is used **/ | ||
public class LoggingTestExtensionTest { | ||
private static final String SOMETHING = "something"; | ||
private static final ByteArrayOutputStream OUT = new ByteArrayOutputStream(); | ||
private static PrintStream oldStdOut; | ||
|
||
@BeforeAll | ||
public static void beforeAll() { | ||
oldStdOut = System.out; | ||
System.setOut(new PrintStream(OUT)); | ||
} | ||
@Test | ||
public void testWritten() { | ||
System.out.println(SOMETHING); | ||
} | ||
|
||
@AfterAll | ||
public static void afterAll() { | ||
String output = new String(OUT.toByteArray()); | ||
System.setOut(oldStdOut); | ||
assertTrue(output, output.contains("testWritten")); | ||
assertTrue(output, output.contains(SOMETHING)); | ||
assertTrue(output, output.contains(org.eclipse.test.services.LoggingTestExtension.BEFORE_TEST_START)); | ||
assertTrue(output, output.contains(org.eclipse.test.services.LoggingTestExtension.AFTER_TEST_PASSED)); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...eng/bundles/org.eclipse.test/src/org/eclipse/test/services/LoggingTestExtensionTest2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Joerg Kubitz and others. | ||
* | ||
* 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 | ||
* | ||
* Contributors: | ||
* Joerg Kubitz - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.test.services; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
// Manual demonstration: | ||
public class LoggingTestExtensionTest2 { | ||
|
||
@Test | ||
public void testWritten() { | ||
System.out.println("real"); | ||
} | ||
|
||
@Test | ||
public void testWritten2() { | ||
throw new RuntimeException("intended"); | ||
} | ||
|
||
} |