org.itsallcode.junit.sysextensions.security.ExitGuardSecurityManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit5-system-extensions Show documentation
Show all versions of junit5-system-extensions Show documentation
These extensions help testing behavior that is related to `System.x` calls like `exit(int).
package org.itsallcode.junit.sysextensions.security;
/*-
* #%L
* JUnit5 System Extensions
* %%
* Copyright (C) 2018 itsallcode.org
* %%
* All rights reserved. This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v2.0 which accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
* #L%
*/
import java.security.Permission;
public class ExitGuardSecurityManager extends SecurityManager
{
private boolean trapExit = false;
@Override
public synchronized void checkExit(final int status)
{
if (this.trapExit)
{
this.trapExit = false;
throw new ExitTrapException(this.getClass().getSimpleName() + " intercepted a System.exit(" + status + ").",
status);
}
}
@Override
public void checkPermission(final Permission perm)
{
// intentionally empty
}
/**
* Switch trapping {@link System#exit(int)} calls on or off.
*
* @param trapExit set to true
if the
* {@link ExitGuardSecurityManager} should trap exit calls
*/
public void trapExit(final boolean trapExit)
{
this.trapExit = trapExit;
}
}