Skip to content

Commit

Permalink
JDK Platform Logging
Browse files Browse the repository at this point in the history
* Implements JDK Platform Logging
* Cleaner returns logger names to keeper, the keeper returns then to caller.
  • Loading branch information
vitalijr2 authored Nov 11, 2024
1 parent 6711610 commit 43fa632
Show file tree
Hide file tree
Showing 18 changed files with 583 additions and 12 deletions.
68 changes: 68 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Bug report
description: Create a report to help me improve
labels:
- bug
assignees: [ "vitalijr2" ]
body:
- type: markdown
attributes:
value: |
Thanks for taking the time to fill out this bug report!
**Before opening a new issue**, make sure to search for keywords in the issues
filtered by the "bug" label:
* https://github.com/vitalijr2/mock-loggers/issues?q=is%3Aissue+label%3Abug+
and verify the issue you're about to submit isn't a duplicate.
- type: input
id: summary
validations:
required: true
attributes:
label: Summary
description: A clear and concise description of what the bug is.
- type: textarea
id: what-happened
validations:
required: true
attributes:
label: What is the current bug behavior?
description: Describe what actually happens.
placeholder: Tell us what you do and what you see!
- type: textarea
id: steps
attributes:
label: Steps to reproduce
value: |
1. ...
2. ...
3. ...
...
- type: textarea
id: expected
validations:
required: true
attributes:
label: What is the expected correct behavior?
description: Describe what you should see instead.
placeholder: Tell us what you expected to get!
- type: textarea
id: misc
attributes:
label: Additional context
description: Add any other context about the problem here.
- type: textarea
id: fixes
attributes:
label: Possible fixes
description: If you can, link to the line of code that might be responsible for the problem.
- type: markdown
attributes:
value: |
Paste any relevant logs - please use code blocks (```)
to format console output, logs, and code as it's tough
to read otherwise.
**Do not attach ZIP files** of your code or compiled projects - instead,
please publish your code to a public GitHub repo & post a link to it.
44 changes: 44 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Feature request
description: Suggest an idea for this project
labels:
- enhancement
assignees: [ "vitalijr2" ]
body:
- type: markdown
attributes:
value: |
Thanks for taking the time to fill out this feature request!
**Before opening a new issue**, make sure to search for keywords in the issues
filtered by the "enhancement" and "suggestion" labels:
* https://github.com/vitalijr2/mock-loggers/issues?q=is%3Aissue+label%3Aenhancement+
* https://github.com/vitalijr2/mock-loggers/issues?q=is%3Aissue+label%3Asuggestion+
and verify the issue you're about to submit isn't a duplicate.
- type: input
id: summary
validations:
required: true
attributes:
label: Summary
description: A clear and concise description of what the bug is.
- type: textarea
id: expected
validations:
required: true
attributes:
label: What is the expected correct behavior?
description: Describe what you should see instead.
placeholder: Tell us what you expected to get!
- type: textarea
id: implementation
attributes:
label: Possible implementations
description: If you can, link to the line of code that might be responsible for the feature.
- type: markdown
attributes:
value: |
Paste any relevant logs - please use code blocks (```)
to format console output, logs, and code as it's tough
to read otherwise.
74 changes: 74 additions & 0 deletions .github/workflows/codacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: "Codacy"

on:
push:
branches: [ "**" ]
tags-ignore:
- v*
pull_request:
# The branches below must be a subset of the branches above
branches: [ "**" ]

permissions: read-all

jobs:
build:
name: Maven build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Java JDK
uses: actions/setup-java@v4
with:
distribution: 'corretto'
java-version: 11
- name: Build with Maven
run: ./mvnw --batch-mode
- name: Temporarily save target and test requests
uses: actions/upload-artifact@master
with:
name: targets
path: |
jdk-platform-logging/targets
junit-extension/target
keeper/target
retention-days: 1
codacy-security-scan:
name: Codacy Security Scan
needs: build
runs-on: ubuntu-latest
permissions:
security-events: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Run Codacy Analysis CLI
uses: codacy/codacy-analysis-cli-action@09916000460adeeedc96b9704f86deba53e2ad5d
with:
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
verbose: true
output: results.sarif
format: sarif
gh-code-scanning-compat: true
- name: Upload SARIF results file
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
codacy-coverage-reporter:
name: Codacy Coverage Reporter
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Retrieve saved test requests and target
uses: actions/download-artifact@master
with:
name: targets
path: .
- name: Run codacy-coverage-reporter
uses: codacy/codacy-coverage-reporter-action@a38818475bb21847788496e9f0fddaa4e84955ba
with:
coverage-reports: jdk-platform-logging/target/site/jacoco/jacoco.xml,junit-extension/target/site/jacoco/jacoco.xml,keeper/target/site/jacoco/jacoco.xml
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
85 changes: 85 additions & 0 deletions jdk-platform-logging/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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>mock-loggers-jdk-platform-logging</artifactId>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
<plugin>
<artifactId>jacoco-maven-plugin</artifactId>
<groupId>org.jacoco</groupId>
</plugin>
</plugins>
</build>
<dependencies>
<!-- main -->
<dependency>
<artifactId>annotations</artifactId>
<groupId>org.jetbrains</groupId>
<scope>provided</scope>
</dependency>
<dependency>
<artifactId>mock-loggers-keeper</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<artifactId>mockito-core</artifactId>
<groupId>org.mockito</groupId>
<scope>provided</scope>
</dependency>
<!-- test -->
<dependency>
<artifactId>hamcrest</artifactId>
<groupId>org.hamcrest</groupId>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>junit-jupiter-engine</artifactId>
<groupId>org.junit.jupiter</groupId>
<scope>provided</scope>
</dependency>
<dependency>
<artifactId>junit-jupiter-params</artifactId>
<groupId>org.junit.jupiter</groupId>
<scope>provided</scope>
</dependency>
<dependency>
<artifactId>mockito-junit-jupiter</artifactId>
<groupId>org.mockito</groupId>
<scope>provided</scope>
</dependency>
</dependencies>
<description>Mock loggers for JDK Platform Logging backed by Mockito.</description>
<name>Mock loggers for JDK Platform Logging</name>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>mock-loggers</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
<relativePath>../pom.xml</relativePath>
<version>1.0.0-SNAPSHOT</version>
</parent>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.vitalijr2.logging.platform;

import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;

import io.github.vitalijr2.logging.keeper.MockLoggerCleaner;
import java.lang.System.Logger;
import java.lang.System.LoggerFinder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.VisibleForTesting;

/**
* Uses {@link org.mockito.Mockito#mock(Class, String)} to get a mock that is adapted for {@link Logger}.
* <p>
* Example:
* <pre><code class="language-java">
* {@literal @}Test
* void helloWorld() {
* var helloService = new HelloService();
*
* assertDoesNotThrow(helloService::sayHelloWorld);
*
* verify(System.getLogger("HelloService")).log(Level.INFO, "Hello World!");
* }
* </code></pre>
*
* @since 1.0.0
*/
public class MockLoggerFinder extends LoggerFinder implements MockLoggerCleaner {

private final Map<String, Logger> loggers;

/**
* Create a map-based logger finder. The finder uses a concurrent map: a logger name is a key.
*/
public MockLoggerFinder() {
this(new ConcurrentHashMap<>());
}

@VisibleForTesting
MockLoggerFinder(Map<String, Logger> loggers) {
this.loggers = loggers;
subscribeToNotifications();
}

@Override
public List<String> cleanAndReset() {
var processedLoggers = new ArrayList<String>();

loggers.forEach((loggerName, logger) -> {
clearInvocations(logger);
reset(logger);
processedLoggers.add(loggerName);
});

return processedLoggers;
}

/**
* Returns an instance of Logger for the given name, module is ignored.
*
* @param name logging name
* @param module logging module
* @return mock logger
*/
@Override
public Logger getLogger(String name, Module module) {
return loggers.computeIfAbsent(name, key -> mock(Logger.class, "Mock for logger " + key));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Mock loggers for JDK Platform Logging backed by <a href="https://site.mockito.org/">Mockito</a>.
*
* @since 1.0.0
*/
package io.github.vitalijr2.logging.platform;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.vitalijr2.logging.platform.MockLoggerFinder
Loading

0 comments on commit 43fa632

Please sign in to comment.