Skip to content

Commit

Permalink
Merge pull request #1 from jspetrak/upgrade_spring6_boot3
Browse files Browse the repository at this point in the history
Upgrade to Spring Framework 6 and Spring Boot 3
  • Loading branch information
jspetrak authored Feb 3, 2023
2 parents b902c20 + 0a928be commit b6e5dee
Show file tree
Hide file tree
Showing 21 changed files with 269 additions and 176 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Java CI with Maven

on:
push

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
6 changes: 3 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ If you want to use the plain aspect and do all what it takes to make it work, th
<dependency>
<groupId>io.github.georgwittberger</groupId>
<artifactId>extend-mdc-aspect</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</dependency>
----

Expand All @@ -58,7 +58,7 @@ If you are using Spring you can let the framework set up the aspect for you (not
<dependency>
<groupId>io.github.georgwittberger</groupId>
<artifactId>extend-mdc-aspect</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand Down Expand Up @@ -101,7 +101,7 @@ If you are using Spring Boot you can leverage its auto-configuration mechanism t
<dependency>
<groupId>io.github.georgwittberger</groupId>
<artifactId>extend-mdc-spring-boot-starter</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</dependency>
----

Expand Down
38 changes: 9 additions & 29 deletions aspect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.georgwittberger</groupId>
<artifactId>extend-mdc</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>extend-mdc-aspect</artifactId>
Expand Down Expand Up @@ -69,8 +69,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand All @@ -80,36 +81,15 @@
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import io.github.georgwittberger.extendmdc.annotation.ExtendMDC;
import io.github.georgwittberger.extendmdc.annotation.MDCValue;

@Aspect
public class ExtendMDCAspect {
private static final Logger LOG = LoggerFactory.getLogger(ExtendMDCAspect.class);

@Around("@annotation(io.github.georgwittberger.extendmdc.annotation.ExtendMDC)")
public Object extendMDC(ProceedingJoinPoint pjp) throws Throwable {
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
Expand All @@ -30,13 +34,15 @@ public Object extendMDC(ProceedingJoinPoint pjp) throws Throwable {

private void addMethodDefinedValues(ExtendMDC methodAnnotation) {
for (MDCValue value : methodAnnotation.value()) {
LOG.debug(String.format("Putting %s=%s into MDC", value.value(), value.content()));
// TODO: Backup value from MDC if it already contains a value for the given key
MDC.put(value.value(), value.content());
}
}

private void removeMethodDefinedValues(ExtendMDC methodAnnotation) {
for (MDCValue value : methodAnnotation.value()) {
LOG.debug(String.format("Removing %s=%s from MDC", value.value(), value.content()));
MDC.remove(value.value());
// TODO: Restore value to MDC if it already contained a value for the given key
}
Expand All @@ -47,6 +53,7 @@ private void addParameterDefinedValues(Parameter[] parameters, Object[] args) {
Parameter parameter = parameters[i];
MDCValue value = parameter.getAnnotation(MDCValue.class);
if (value != null) {
LOG.debug(String.format("Putting %s=%s into MDC", value.value(), value.content()));
// TODO: Implement formatted output of certain types (e.g. date/time)
// @MDCValue should provide an optional element "pattern" for that.
// TODO: Backup value from MDC if it already contains a value for the given key
Expand All @@ -60,6 +67,7 @@ private void removeParameterDefinedValues(Parameter[] parameters) {
Parameter parameter = parameters[i];
MDCValue value = parameter.getAnnotation(MDCValue.class);
if (value != null) {
LOG.debug(String.format("Removing %s=%s from MDC", value.value(), value.content()));
MDC.remove(value.value());
// TODO: Restore value to MDC if it already contained a value for the given key
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;

import static org.hamcrest.MatcherAssert.assertThat;

import org.slf4j.MDC;

import io.github.georgwittberger.extendmdc.annotation.ExtendMDC;
import io.github.georgwittberger.extendmdc.annotation.MDCValue;

public class AspectTestService {
@ExtendMDC({ @MDCValue(value = "TestValue1", content = "TestContent1"),
@MDCValue(value = "TestValue2", content = "TestContent2"), @MDCValue("EmptyValue") })
@ExtendMDC({
@MDCValue(value = "TestValue1", content = "TestContent1"),
@MDCValue(value = "TestValue2", content = "TestContent2"), @MDCValue("EmptyValue")
})
public void testAnnotationDefinedValues() {
assertThat(MDC.get("TestValue1"), is(equalTo("TestContent1")));
assertThat(MDC.get("TestValue2"), is(equalTo("TestContent2")));
Expand All @@ -25,7 +28,8 @@ public void testMethodParameters(@MDCValue("ObjectValue") Object objectParam,
@MDCValue("StringValue") String stringParam, @MDCValue("IntegerValue") Integer integerParam,
@MDCValue("LongValue") Long longParam, @MDCValue("FloatValue") Float floatParam,
@MDCValue("DoubleValue") Double doubleParam, @MDCValue("BooleanValue") Boolean booleanParam,
String ignoredParam) {
String ignoredParam
) {
assertThat(MDC.get("ObjectValue"), is(equalTo(String.valueOf(objectParam))));
assertThat(MDC.get("StringValue"), is(equalTo(stringParam)));
assertThat(MDC.get("IntegerValue"), is(equalTo(String.valueOf(integerParam))));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.github.georgwittberger.extendmdc.aspect;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.MDC;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.assertNull;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = TestApplication.class)
@Import(TestAspectConfiguration.class)
@ContextConfiguration(classes = TestAspectConfiguration.class)
public class ExtendMDCAspectTests {
@Test
public void testAspectAddsAndRemovesAnnotationDefinedValues(ApplicationContext applicationContext) {
var aspectTestService= applicationContext.getBean(AspectTestService.class);

assertNull(MDC.get("TestValue1"));
assertNull(MDC.get("TestValue2"));
assertNull(MDC.get("EmptyValue"));
aspectTestService.testAnnotationDefinedValues();
assertNull(MDC.get("TestValue1"));
assertNull(MDC.get("TestValue2"));
assertNull(MDC.get("EmptyValue"));
}

@Test
public void testAspectAddsAndRemovesMethodParameters(ApplicationContext applicationContext) {
var aspectTestService= applicationContext.getBean(AspectTestService.class);

assertNull(MDC.get("ObjectValue"));
assertNull(MDC.get("StringValue"));
assertNull(MDC.get("IntegerValue"));
assertNull(MDC.get("LongValue"));
assertNull(MDC.get("FloatValue"));
assertNull(MDC.get("DoubleValue"));
assertNull(MDC.get("BooleanValue"));
aspectTestService.testMethodParameters(new Object(), "Test String", 1, 2L, 3.1F, 4.2, true, "Ignored String");
assertNull(MDC.get("ObjectValue"));
assertNull(MDC.get("StringValue"));
assertNull(MDC.get("IntegerValue"));
assertNull(MDC.get("LongValue"));
assertNull(MDC.get("FloatValue"));
assertNull(MDC.get("DoubleValue"));
assertNull(MDC.get("BooleanValue"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.georgwittberger.extendmdc.aspect;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication
@EnableAspectJAutoProxy
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.georgwittberger.extendmdc.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Aspect
@Configuration
public class TestAspectConfiguration {
@Bean
public ExtendMDCAspect extendMDCAspect() {
return new ExtendMDCAspect();
}

@Bean
public AspectTestService aspectTestService() { return new AspectTestService(); }
}
Loading

0 comments on commit b6e5dee

Please sign in to comment.