Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
Add integrations tests and demo.
Browse files Browse the repository at this point in the history
* 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] <support@github.com>

* 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] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
vitalijr2 and dependabot[bot] authored Jul 17, 2024
1 parent 22e7cde commit b263243
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
---
engines:
codenarc:
exclude_paths:
- "src/it/**"
exclude_paths:
- "src/it/**"
- "**/mock-jdk-platform-logging.css"
- "**/prism.css"
- "**/prism.js"
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<plugin>
<artifactId>central-publishing-maven-plugin</artifactId>
<groupId>org.sonatype.central</groupId>
<version>0.4.0</version>
<version>0.5.0</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down
26 changes: 19 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<dependencies>
...
<dependency>
<artifactId>mock-jdk-platform-logging</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
<scope>test</scope>
<version>1.0.0</version>
</dependency>
...
</dependencies>
```

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;
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down
99 changes: 99 additions & 0 deletions src/it/hello-world/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>basic</artifactId>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>${java.version}</release>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<groupId>org.apache.maven.plugins</groupId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<!-- test -->
<dependency>
<artifactId>junit-jupiter-api</artifactId>
<groupId>org.junit.jupiter</groupId>
<scope>test</scope>
<version>${junit-jupiter.version}</version>
</dependency>
<dependency>
<artifactId>junit-jupiter-engine</artifactId>
<groupId>org.junit.jupiter</groupId>
<scope>test</scope>
<version>${junit-jupiter.version}</version>
</dependency>
<dependency>
<artifactId>junit-jupiter-params</artifactId>
<groupId>org.junit.jupiter</groupId>
<scope>test</scope>
<version>${junit-jupiter.version}</version>
</dependency>
<dependency>
<artifactId>mockito-core</artifactId>
<groupId>org.mockito</groupId>
<scope>test</scope>
<version>${mockito.version}</version>
</dependency>
<dependency>
<artifactId>mockito-junit-jupiter</artifactId>
<groupId>org.mockito</groupId>
<scope>test</scope>
<version>${mockito.version}</version>
</dependency>
<dependency>
<artifactId>@project.artifactId@</artifactId>
<groupId>@project.groupId@</groupId>
<scope>test</scope>
<version>@project.version@</version>
</dependency>
<dependency>
<artifactId>hamcrest</artifactId>
<groupId>org.hamcrest</groupId>
<scope>test</scope>
<version>2.2</version>
</dependency>
</dependencies>
<description>Basic example</description>
<groupId>example.hello</groupId>
<modelVersion>4.0.0</modelVersion>
<properties>
<java.version>11</java.version>
<junit-jupiter.version>5.10.3</junit-jupiter.version>
<mockito.version>5.12.0</mockito.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<version>1.0.0</version>
</project>
27 changes: 27 additions & 0 deletions src/it/hello-world/src/main/java/example/hello/HelloService.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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!");
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}


}
1 change: 1 addition & 0 deletions src/it/hello-world/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
println "Hello World!"
35 changes: 35 additions & 0 deletions src/it/settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<id>it-repo</id>
<pluginRepositories>
<pluginRepository>
<id>local.central</id>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<url>@localRepositoryUrl@</url>
</pluginRepository>
</pluginRepositories>
<repositories>
<repository>
<id>local.central</id>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<url>@localRepositoryUrl@</url>
</repository>
</repositories>
</profile>
</profiles>
</settings>

0 comments on commit b263243

Please sign in to comment.