From b263243f7b3827e37ecb4d010b7dc08ad7e2377f Mon Sep 17 00:00:00 2001 From: Vitalij Berdinskih Date: Wed, 17 Jul 2024 13:06:07 +0300 Subject: [PATCH] Add integrations tests and demo. * Bump org.sonatype.central:central-publishing-maven-plugin Bumps [org.sonatype.central:central-publishing-maven-plugin](https://github.com/sonatype/central-publishing-maven-plugin) from 0.4.0 to 0.5.0. - [Commits](https://github.com/sonatype/central-publishing-maven-plugin/commits) --- updated-dependencies: - dependency-name: org.sonatype.central:central-publishing-maven-plugin dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] * Prepare integration tests * Add a demo integration test * Add a demo integration test * Codacy: ignore the demo * CodeNarc: ignore the demo * Update README * Update README * Add badges for Maven Central and Javadoc.io --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .codacy.yml | 5 + pom.xml | 2 +- readme.md | 26 +++-- src/it/hello-world/pom.xml | 99 +++++++++++++++++++ .../main/java/example/hello/HelloService.java | 27 +++++ .../example/hello/HelloServiceBasicTest.java | 21 ++++ .../example/hello/HelloServiceFullTest.java | 63 ++++++++++++ src/it/hello-world/verify.groovy | 1 + src/it/settings.xml | 35 +++++++ 9 files changed, 271 insertions(+), 8 deletions(-) create mode 100644 src/it/hello-world/pom.xml create mode 100644 src/it/hello-world/src/main/java/example/hello/HelloService.java create mode 100644 src/it/hello-world/src/test/java/example/hello/HelloServiceBasicTest.java create mode 100644 src/it/hello-world/src/test/java/example/hello/HelloServiceFullTest.java create mode 100644 src/it/hello-world/verify.groovy create mode 100644 src/it/settings.xml diff --git a/.codacy.yml b/.codacy.yml index 11a8c46..50820f8 100644 --- a/.codacy.yml +++ b/.codacy.yml @@ -1,5 +1,10 @@ --- +engines: + codenarc: + exclude_paths: + - "src/it/**" exclude_paths: + - "src/it/**" - "**/mock-jdk-platform-logging.css" - "**/prism.css" - "**/prism.js" diff --git a/pom.xml b/pom.xml index 38a23b4..216a2d2 100644 --- a/pom.xml +++ b/pom.xml @@ -77,7 +77,7 @@ central-publishing-maven-plugin org.sonatype.central - 0.4.0 + 0.5.0 diff --git a/readme.md b/readme.md index 9b4488a..7325914 100644 --- a/readme.md +++ b/readme.md @@ -5,26 +5,37 @@ JDK Platform Logging Service with mocked loggers backed by [Mockito][]. [![Codacy Badge][codacy-badge]][codacy-badge-link] [![Codacy Coverage][codacy-coverage]][codacy-coverage-link] [![Libraries.io dependency status for GitHub repo][dependency-status]][dependencies] -[![Java Version][java-version]][jdk-download] +[![Java Version][java-version]][jdk-download] +[![Maven Central](https://img.shields.io/maven-central/v/io.github.vitalijr2.logging/mock-jdk-platform-logging)](https://search.maven.org/artifact/io.github.vitalijr2.logging/mock-jdk-platform-logging) +[![Javadoc](https://javadoc.io/badge2/io.github.vitalijr2.logging/mock-jdk-platform-logging/javadoc.svg)](https://javadoc.io/doc/io.github.vitalijr2.logging/mock-jdk-platform-logging) ## How to use Just put to your POM: ```xml - - ... mock-jdk-platform-logging io.github.vitalijr2.logging test 1.0.0 - ... - +``` + +The most basic usage example looks like this: +```java + @Test + void helloWorld() { + var helloService = new HelloService(); + assertDoesNotThrow(helloService::sayHelloWorld); + + verify(System.getLogger("HelloService")).log(System.Logger.Level.INFO, "Hello World!"); + } ``` +See more details at [HelloServiceBasicTest.java](src/it/hello-world/src/test/java/example/hello/HelloServiceBasicTest.java) -Example: +It should be taken into account that all loggers are initialized only once during the run of tests. +Therefore, a more complex example cleans the loggers before (or after) each test: ```java // the static logger instance private static Logger logger; @@ -56,6 +67,7 @@ Example: verify(logger).log(level, "test message"); } ``` +See more details at [HelloServiceFullTest.java](src/it/hello-world/src/test/java/example/hello/HelloServiceFullTest.java) ## Credits @@ -74,7 +86,7 @@ See [Changelog](changelog.md) ## License -Copyright 2023-2024 Vitalij Berdinskih +Copyright 2024 Vitalij Berdinskih Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/it/hello-world/pom.xml b/src/it/hello-world/pom.xml new file mode 100644 index 0000000..c3a807a --- /dev/null +++ b/src/it/hello-world/pom.xml @@ -0,0 +1,99 @@ + + + basic + + + + maven-compiler-plugin + + ${java.version} + + org.apache.maven.plugins + 3.13.0 + + + maven-surefire-plugin + + true + + org.apache.maven.plugins + 3.3.1 + + + maven-failsafe-plugin + + + **/*Test.java + + + + + + integration-test + verify + + + + org.apache.maven.plugins + 3.3.1 + + + + + + + junit-jupiter-api + org.junit.jupiter + test + ${junit-jupiter.version} + + + junit-jupiter-engine + org.junit.jupiter + test + ${junit-jupiter.version} + + + junit-jupiter-params + org.junit.jupiter + test + ${junit-jupiter.version} + + + mockito-core + org.mockito + test + ${mockito.version} + + + mockito-junit-jupiter + org.mockito + test + ${mockito.version} + + + @project.artifactId@ + @project.groupId@ + test + @project.version@ + + + hamcrest + org.hamcrest + test + 2.2 + + + Basic example + example.hello + 4.0.0 + + 11 + 5.10.3 + 5.12.0 + UTF-8 + + 1.0.0 + \ No newline at end of file diff --git a/src/it/hello-world/src/main/java/example/hello/HelloService.java b/src/it/hello-world/src/main/java/example/hello/HelloService.java new file mode 100644 index 0000000..5ddc765 --- /dev/null +++ b/src/it/hello-world/src/main/java/example/hello/HelloService.java @@ -0,0 +1,27 @@ +package example.hello; + +import static java.util.Objects.requireNonNull; + +import java.lang.System.Logger; + +public class HelloService { + + private final Logger logger = System.getLogger("HelloService"); + + public String sayHelloWorld() { + return sayHello("World"); + } + + public String sayHello(String name) { + if (requireNonNull(name, "Name is missed").isBlank()) { + throw new IllegalArgumentException("Name is empty"); + } + + var greeting = "Hello " + name + "!"; + + logger.log(System.Logger.Level.INFO, greeting); + + return greeting; + } + +} diff --git a/src/it/hello-world/src/test/java/example/hello/HelloServiceBasicTest.java b/src/it/hello-world/src/test/java/example/hello/HelloServiceBasicTest.java new file mode 100644 index 0000000..13145d0 --- /dev/null +++ b/src/it/hello-world/src/test/java/example/hello/HelloServiceBasicTest.java @@ -0,0 +1,21 @@ +package example.hello; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class HelloServiceBasicTest { + + @DisplayName("Hello world") + @Test + void helloWorld() { + var helloService = new HelloService(); + + assertDoesNotThrow(helloService::sayHelloWorld); + + verify(System.getLogger("HelloService")).log(System.Logger.Level.INFO, "Hello World!"); + } + +} \ No newline at end of file diff --git a/src/it/hello-world/src/test/java/example/hello/HelloServiceFullTest.java b/src/it/hello-world/src/test/java/example/hello/HelloServiceFullTest.java new file mode 100644 index 0000000..8e5a610 --- /dev/null +++ b/src/it/hello-world/src/test/java/example/hello/HelloServiceFullTest.java @@ -0,0 +1,63 @@ +package example.hello; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.StringStartsWith.startsWith; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.clearInvocations; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +import java.lang.System.Logger; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +class HelloServiceFullTest { + + private static Logger logger; + + @BeforeAll + static void setUpClass() throws Exception { + logger = System.getLogger("HelloService"); + } + + @BeforeEach + void setUp() throws Exception { + clearInvocations(logger); + } + + @DisplayName("Names") + @ParameterizedTest(name = "<{0}>") + @ValueSource(strings = {"John", "Jane"}) + void names(String name) { + var helloService = new HelloService(); + + assertDoesNotThrow(() -> helloService.sayHello(name)); + + var logger = System.getLogger("HelloService"); + + verify(logger).log(System.Logger.Level.INFO, "Hello " + name + "!"); + verifyNoMoreInteractions(logger); + } + + @DisplayName("Null or empty name") + @ParameterizedTest(name = "<{0}>") + @NullAndEmptySource + @ValueSource(strings = " ") + void nullOrEmptyName(String name) { + var helloService = new HelloService(); + + var exception = assertThrows(RuntimeException.class, () -> helloService.sayHello(name)); + + verifyNoInteractions(System.getLogger("HelloService")); + + assertThat(exception.getMessage(), startsWith("Name is")); + } + + +} \ No newline at end of file diff --git a/src/it/hello-world/verify.groovy b/src/it/hello-world/verify.groovy new file mode 100644 index 0000000..a5d5085 --- /dev/null +++ b/src/it/hello-world/verify.groovy @@ -0,0 +1 @@ +println "Hello World!" \ No newline at end of file diff --git a/src/it/settings.xml b/src/it/settings.xml new file mode 100644 index 0000000..12780df --- /dev/null +++ b/src/it/settings.xml @@ -0,0 +1,35 @@ + + + + + + true + + it-repo + + + local.central + + true + + + true + + @localRepositoryUrl@ + + + + + local.central + + true + + + true + + @localRepositoryUrl@ + + + + +