Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#21: Delegate Security Manager #24

Merged
merged 18 commits into from Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -46,7 +46,9 @@ class TestSystemExit
}
```

Note that in order to be able to trap system exit, the `ExitGuard` temporarily replaces the existing security manager (if any).
The `ExitGuard` temporarily replaces the existing security manager.

From version 1.2.0 on if a security guard existed before, it serves as a delegate for all security checks with the exception of the `checkExit`.

## Asserting Data Sent to `System.out`

Expand Down Expand Up @@ -91,6 +93,10 @@ Capturing data sent to `System.err` works in the exact same way as in the [`Syst

Please check our [contribution guide](.github/CONTRIBUTING.md) to learn how you can help with the project, report errors or request features.

## Changelog

[Changelog](doc/changes/changelog.md)

## Development

### Build Time Dependencies
Expand Down Expand Up @@ -177,4 +183,4 @@ mvn versions:display-plugin-updates

1. Merge `develop` to `master` branch
1. Create a [release](https://github.com/itsallcode/junit5-system-extensions/releases) of the `master` branch on GitHub.
1. After some time the release will be available at [Maven Central](https://repo1.maven.org/maven2/org/itsallcode/junit5-system-extensions/).
1. After some time the release will be available at [Maven Central](https://repo1.maven.org/maven2/org/itsallcode/junit5-system-extensions/).
4 changes: 4 additions & 0 deletions doc/changes/changelog.md
@@ -0,0 +1,4 @@
# Changes

* [1.2.0](changes_1.2.0.md)
* [1.1.0](changes_1.1.0.md)
11 changes: 11 additions & 0 deletions doc/changes/changes_1.1.0.md
@@ -0,0 +1,11 @@
# JUnit5 System Extensions 1.1.0, released 2019-12-28

Code name: Mute output

## Summary

To mute the output (i.e. don't forward output to System.out / System.err) call stream.captureMuted() instead of stream.capture(). This can be useful to speed up unit tests.
redcatbear marked this conversation as resolved.
Show resolved Hide resolved

## Features

* #16: Mute output of captured streams
redcatbear marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions doc/changes/changes_1.2.0.md
@@ -0,0 +1,17 @@
# JUnit5 System Extensions 1.2.0, released 2019-04-22

Code name: Mute output
redcatbear marked this conversation as resolved.
Show resolved Hide resolved

## Summary

When trapping `System.exit` calls, the `ExitGuardSecurityManager` now doesn't simply replace an existing security manager anymore. Instead it uses the existing one as a delegate for all checks except the exit check.

This way applications that require a security manager don't change their behavior.

## Features

* #23: Delegate security manager calls to the original security manager
redcatbear marked this conversation as resolved.
Show resolved Hide resolved

## Refactoring

* #21: Migrate to Maven Central
redcatbear marked this conversation as resolved.
Show resolved Hide resolved
48 changes: 24 additions & 24 deletions launch/j5se - run all tests.launch
@@ -1,27 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/junit5-system-extensions"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
<listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
</listAttribute>
<listAttribute key="org.eclipse.eclemma.core.SCOPE_IDS">
<listEntry value="=junit5-system-extensions/src\/main\/java"/>
<listEntry value="=junit5-system-extensions/src\/main\/resources"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value="=junit5-system-extensions"/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit5"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value=""/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="junit5-system-extensions"/>
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-ea"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/junit5-system-extensions"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
<listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
</listAttribute>
<listAttribute key="org.eclipse.eclemma.core.SCOPE_IDS">
<listEntry value="=junit5-system-extensions/src\/main\/java"/>
<listEntry value="=junit5-system-extensions/src\/main\/resources"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value="=junit5-system-extensions"/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit5"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value=""/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="junit5-system-extensions"/>
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-ea"/>
</launchConfiguration>
18 changes: 12 additions & 6 deletions src/main/java/org/itsallcode/junit/sysextensions/ExitGuard.java
Expand Up @@ -9,13 +9,13 @@
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License, v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is
* available at https://www.gnu.org/software/classpath/license.html.
*
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
* #L%
*/
Expand Down Expand Up @@ -48,7 +48,8 @@ private void saveCurrentSecurityManager(final ExtensionContext context)

private void installExitGuardSecurityManager(final ExtensionContext context)
{
final SecurityManager exitGuardSecurityManager = new ExitGuardSecurityManager();
final SecurityManager previousSecurityManager = getPreviousSecurityManager(context);
final SecurityManager exitGuardSecurityManager = new ExitGuardSecurityManager(previousSecurityManager);
System.setSecurityManager(exitGuardSecurityManager);
context.getStore(getNamespace()).put(EXIT_GUARD_SECURITY_MANAGER_KEY, exitGuardSecurityManager);
}
Expand Down Expand Up @@ -78,8 +79,13 @@ public void afterTestExecution(final ExtensionContext context) throws Exception
@Override
public void afterAll(final ExtensionContext context) throws Exception
{
final SecurityManager previousManager = (SecurityManager) context.getStore(getNamespace())
.get(PREVIOUS_SECURITY_MANAGER_KEY);
final SecurityManager previousManager = getPreviousSecurityManager(context);
System.setSecurityManager(previousManager);
}
}

private SecurityManager getPreviousSecurityManager(final ExtensionContext context)
{
return (SecurityManager) context.getStore(getNamespace())
.get(PREVIOUS_SECURITY_MANAGER_KEY);
}
}