Coder Social home page Coder Social logo

itsallcode / junit5-system-extensions Goto Github PK

View Code? Open in Web Editor NEW
8.0 8.0 1.0 168 KB

JUnit5 extensions to test Java System.x functions

License: Eclipse Public License 2.0

Java 100.00%
extension java junit junit5 junit5-extension unit-testing

junit5-system-extensions's People

Contributors

dependabot[bot] avatar jakobbraun avatar kaklakariada avatar redcatbear avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

jakobbraun

junit5-system-extensions's Issues

Delegate Security Manager for Exit Guard

Situation

Currently the implementation of the SystemExitGuard extension replaces the existing security guard with a specialized one that intercepts the System.exit. This one has now special permissions and it also has no relationship to a pre-existing one. Other than saving it and restoring it later that is.

What would be less invasive is if the replacement took the existing one as delegate and delegates all checks but the exit check to the original one.

Acceptance Criteria

  • ExitGuardSecurityManager delegates all checks to the preexisting one (with exception of the intercepted system exit.
  • If no security manager was set, then all checks are granted.

Document how to use ExitGuard with Java > 18

SecurityManager which is used by ExitGuard is deprecated in Java 17. Starting with Java 18 it requires argument -Djava.security.manager=allow when starting the JVM, see

If the option is missing, this exception will be thrown:

UnsupportedOperation The Security Manager is deprecated and will be removed in a future release

We need to document this and provide a workaround similar to this one.

Capture internal stream on `close()`

Description

When the Capturable is closed, getCapturedData() fails with a NullPointerException. Reason is that capturing is missing right before the internal stream is closed.

Steps to Reproduce

  1. Extend test class with SystemOutGuard
  2. Write unit test that captures @SysOut
  3. Use capture() on the Capturable
  4. Close Capturable
  5. Use getCapturedData()
  6. NullPointerException

Expected behavior

getCapturedData() returns data captured right before the stream was closed.

Environment

  • JUnit5 System Extensions: 1.01

Logs truncated when using `SystemErrGuard `

Description

In test classes annotated with @ExtendWith(SystemErrGuard.class) and with multiple test cases that sniff the System.Err, the logs are truncated after the execution of the first test case.

An instance of java.util.logging.Logger uses by default ConsoleHandler that is bounded to a reference of the System.Err.
This binding happens upon creation of the Consolehandler, which is lazily instantiated, for example when logging a message with the logger for the first time (ie, LOGGER.info("message")).

If the System.Err is changed (ie, System.setErr(newSystemStream)), all the already existent instances of ConsoleHandler are not updated, so they don't log to the new System.Err.

Steps to Reproduce

  1. Implement the following classes
import java.util.logging.Logger;
public final class TesteableClass {
    public static final Logger LOGGER = Logger.getLogger(TesteableClass.class.getName());

    public static String testeableMethod() {
            LOGGER.info("TESTEABLE LOG MESSAGE");
    }
}
@ExtendWith(SystemErrGuard.class)
class TestClass {
    @Test
    void firstTestCase(final Capturable stream) {
          stream.capture();
          TesteableClass.testeableMethod();
          assertThat(stream.getCapturedData(), containsString("TESTEABLE LOG MESSAGE"));
    }
    @Test
    void secondTestCase(final Capturable stream) {
          stream.capture();
          TesteableClass.testeableMethod();
          assertThat(stream.getCapturedData(), containsString("TESTEABLE LOG MESSAGE"));
    }
}
  1. Execute the tests.
  2. Verify the console does not show all the logs (you can see in the console only one "TESTEABLE LOG MESSAGE" message instead of two)
  3. As a work around, you can reset the ConsoleHandler before executing each tests and disabling the use of the parent ConsoleHandler (cause if not it would log twice, once for each handler, during the execution of the first test), for example:
    @BeforeEach
    void beforeEach() {
        TesteableClass.LOGGER.setUseParentHandlers(false);
        final Handler[] handlers = TesteableClass.LOGGER.getHandlers();
        for (final Handler handler : handlers) {
            TesteableClass.LOGGER.removeHandler(handler);
        }
        TesteableClass.LOGGER.addHandler(new ConsoleHandler());
    }

Expected behavior

Tests should pass and "TESTEABLE LOG MESSAGE" should be visible twice in the console.

Environment

  • JUnit5 System Extensions: 1.1.0
  • OS: Ubuntu 20.04.1
  • Java Version: 11
  • Maven Version (in case of build problems only): 3.6.3

Fix code smells in 1.2.0

Situation

The project collected a number of code smells that need to be addressed in the version up to 1.2.0

Acceptance Criteria

  1. All code smells fixed.

Add CI build

As a developer
I want to get a report from a Continuous Integration Build
in order to only complete pull requests where all tests are green.

Deployment to Maven Central fails

See https://github.com/itsallcode/junit5-system-extensions/actions/runs/9853004485/job/27202629886

[INFO]  * Upload of locally staged artifacts finished.
[INFO]  * Closing staging repository with ID "orgitsallcode-1111".
Waiting for operation to complete...
..............................
Error:  Rule failure while trying to close staging repository with ID "orgitsallcode-1111".
Error:  
Error:  Nexus Staging Rules Failure Report
Error:  ==================================
Error:  
Error:  Repository "orgitsallcode-1111" failures
Error:    Rule "signature-staging" failures
Error:      * Missing Signature: '/org/itsallcode/junit5-system-extensions/1.2.1/junit5-system-extensions-1.2.1.pom.asc' does not exist for 'junit5-system-extensions-1.2.1.pom'.
Error:      * Missing Signature: '/org/itsallcode/junit5-system-extensions/1.2.1/junit5-system-extensions-1.2.1-sources.jar.asc' does not exist for 'junit5-system-extensions-1.2.1-sources.jar'.
Error:      * Missing Signature: '/org/itsallcode/junit5-system-extensions/1.2.1/junit5-system-extensions-1.2.1-javadoc.jar.asc' does not exist for 'junit5-system-extensions-1.2.1-javadoc.jar'.
Error:      * Missing Signature: '/org/itsallcode/junit5-system-extensions/1.2.1/junit5-system-extensions-1.2.1.jar.asc' does not exist for 'junit5-system-extensions-1.2.1.jar'.
Error:  
Error:  
Error:  Cleaning up local stage directory after a Rule failure during close of staging repositories: [orgitsallcode-1111]
Error:   * Deleting context 546ea6ce74787e.properties
Error:  Cleaning up remote stage repositories after a Rule failure during close of staging repositories: [orgitsallcode-1111]
Error:   * Dropping failed staging repository with ID "orgitsallcode-1111" (Rule failure during close of staging repositories: [orgitsallcode-1111]).
Waiting for operation to complete...
....
Error:  Remote staging finished with a failure: Staging rules failure!

Assert `System.out`

As a developer
I want to test output sent to System.out.x()
in order to make sure that the output is correct.

No more Java8 support?

We have problems using the plugin in a Java8 project:

[ERROR] /home/travis/build/checkstyle/checkstyle/src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java:[27,49] cannot access org.itsallcode.junit.sysextensions.AssertExit
bad class file: /home/travis/.m2/repository/org/itsallcode/junit5-system-extensions/1.2.0/junit5-system-extensions-1.2.0.jar(org/itsallcode/junit/sysextensions/AssertExit.class)
class file has wrong version 55.0, should be 52.0

Please indicate your position on the minimum version of the JDK.

Build with Java 17 fails due to missing Javadoc comments

Description

Building the project with Java 17 fails due to missing Javadoc comments

Steps to Reproduce

  1. Run JAVA_HOME=$JAVA17_HOME mvn javadoc:aggregate

Expected behavior

  • The build runs without error
  • The build-next-java.yml workflow is activated again

Environment

  • Java Version: 17

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.