-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First attempt to use this library from a java 21 jigsaw project.
Signed-off-by: Sjoerd Talsma <[email protected]>
- Loading branch information
1 parent
9eab2e6
commit 00bd2b5
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
40 changes: 40 additions & 0 deletions
40
...e/src/main/java/nl/talsmasoftware/context/tests/jigsaw/locale/JigsawLocalePropagator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...c/test/java/nl/talsmasoftware/context/tests/jigsaw/locale/JigsawLocalePropagatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("_", "-")); | ||
} | ||
} |