Skip to content

Commit

Permalink
First attempt to use this library from a java 21 jigsaw project.
Browse files Browse the repository at this point in the history
Signed-off-by: Sjoerd Talsma <[email protected]>
  • Loading branch information
sjoerdtalsma committed Dec 13, 2024
1 parent 9eab2e6 commit 00bd2b5
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<module>timers/context-timer-opentracing</module>

<module>tests/test-j8-locale</module>
<module>tests/test-j21-locale</module>
</modules>

<licenses>
Expand Down
87 changes: 87 additions & 0 deletions tests/test-j21-locale/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2016-2024 Talsma ICT
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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.talsmasoftware.context</groupId>
<artifactId>context-propagation</artifactId>
<version>2.0.0-RC2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<!-- Artifact identification -->
<groupId>nl.talsmasoftware.context.tests</groupId>
<artifactId>test-j21-locale</artifactId>
<name>Test for Locale propagation in Jigsaw project</name>
<packaging>jar</packaging>

<properties>
<project.moduleName>${project.groupId}.springsecurity</project.moduleName>
<root.basedir>${project.parent.basedir}</root.basedir>
<project.jdk.version>21</project.jdk.version>
<maven.compiler.release>21</maven.compiler.release>
</properties>

<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>context-propagation-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.parent.groupId}.managers</groupId>
<artifactId>context-manager-locale</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>module-javadoc</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4 changes: 4 additions & 0 deletions tests/test-j21-locale/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module nl.talsmasoftware.context.tests.jigsaw.locale {
requires nl.talsmasoftware.context.api;
requires nl.talsmasoftware.context.managers.locale;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2016-2024 Talsma ICT
*
* 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 nl.talsmasoftware.context.tests.jigsaw.locale;

import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.api.ContextSnapshot.Reactivation;
import nl.talsmasoftware.context.managers.locale.CurrentLocaleHolder;

import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference;

public class JigsawLocalePropagator {

public static Locale getPropagatedLocaleFromNewThread() throws InterruptedException {
final ContextSnapshot snapshot = ContextSnapshot.capture();
final AtomicReference<Locale> result = new AtomicReference<>();
final Thread thread = new Thread(() -> {
try (Reactivation reactivation = snapshot.reactivate()) {
result.set(CurrentLocaleHolder.get().orElse(null));
}
});
thread.start();
thread.join(10000L);
return result.get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2016-2024 Talsma ICT
*
* 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 nl.talsmasoftware.context.tests.jigsaw.locale;

import nl.talsmasoftware.context.managers.locale.CurrentLocaleHolder;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Locale;

import static org.assertj.core.api.Assertions.assertThat;

class JigsawLocalePropagatorTest {

@ParameterizedTest
@ValueSource(strings = {"nl-NL", "en-GB", "de-DE"})
void testLocalePropagation(String locale) throws InterruptedException {
// given
final Locale currentLocale = parseLocale(locale);
CurrentLocaleHolder.set(currentLocale);

// when
final Locale result = JigsawLocalePropagator.getPropagatedLocaleFromNewThread();

// then
assertThat(result).isEqualTo(currentLocale);
}

private static Locale parseLocale(String locale) {
return Locale.forLanguageTag(locale.replace("_", "-"));
}
}

0 comments on commit 00bd2b5

Please sign in to comment.