-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: use Thread context ClassLoader for loading I18n ResourceBundle #19791
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4bcd731
fix: use Thread context ClassLoader for loading I18n ResourceBundle
platosha ef97f92
fix: make sure DefaultInstantiator also uses thread context ClassLoad…
platosha 4a9a309
test(vaadin-spring): make sure DefaultI18NProviderFactory uses contex…
platosha da67716
test(vaadin-spring): cleanup and fix i18N test interference
platosha 868435a
test(vaadin-spring): more i18n test interference fixes
platosha b71ca94
test(vaadin-spring): i18n test inteference different tactic
platosha 9a7375f
chore: formatting
platosha 00c9119
Merge branch 'main' into fix/ap/hilla-2554
platosha b4626db
add NotThreadSafe to test
caalador 791a02c
Merge branch 'main' into fix/ap/hilla-2554
caalador 1a46e92
Merge branch 'main' into fix/ap/hilla-2554
caalador File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
11 changes: 11 additions & 0 deletions
11
vaadin-spring/src/test/java/com/vaadin/flow/spring/i18n/DefaultI18NProviderFactorySuite.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,11 @@ | ||
package com.vaadin.flow.spring.i18n; | ||
|
||
import net.jcip.annotations.NotThreadSafe; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
|
||
@RunWith(Suite.class) | ||
@NotThreadSafe | ||
@Suite.SuiteClasses({ DefaultI18NProviderFactoryTest.class }) | ||
public class DefaultI18NProviderFactorySuite { | ||
} |
114 changes: 114 additions & 0 deletions
114
vaadin-spring/src/test/java/com/vaadin/flow/spring/i18n/DefaultI18NProviderFactoryTest.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,114 @@ | ||
package com.vaadin.flow.spring.i18n; | ||
|
||
import com.vaadin.flow.di.Instantiator; | ||
import com.vaadin.flow.i18n.DefaultI18NProvider; | ||
import com.vaadin.flow.i18n.I18NProvider; | ||
import com.vaadin.flow.spring.VaadinApplicationConfiguration; | ||
import com.vaadin.flow.spring.instantiator.SpringInstantiatorTest; | ||
import jakarta.servlet.ServletException; | ||
import net.jcip.annotations.NotThreadSafe; | ||
import org.junit.*; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.MockedConstruction; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.*; | ||
import org.springframework.core.io.DefaultResourceLoader; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.Locale; | ||
import java.util.Properties; | ||
|
||
@RunWith(SpringRunner.class) | ||
@Import(VaadinApplicationConfiguration.class) | ||
@NotThreadSafe | ||
public class DefaultI18NProviderFactoryTest { | ||
|
||
@Autowired | ||
private ApplicationContext context; | ||
|
||
static private ClassLoader originalClassLoader; | ||
|
||
static private ClassLoader testClassLoader; | ||
|
||
static private TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
static volatile private MockedConstruction<PathMatchingResourcePatternResolver> pathMatchingResourcePatternResolverMockedConstruction; | ||
|
||
@BeforeClass | ||
static public void setup() throws IOException { | ||
originalClassLoader = Thread.currentThread().getContextClassLoader(); | ||
|
||
temporaryFolder.create(); | ||
File resources = temporaryFolder.newFolder(); | ||
|
||
File translations = new File(resources, | ||
DefaultI18NProvider.BUNDLE_FOLDER); | ||
translations.mkdirs(); | ||
|
||
File defaultTranslation = new File(translations, | ||
DefaultI18NProvider.BUNDLE_FILENAME + ".properties"); | ||
Files.writeString(defaultTranslation.toPath(), "title=Default lang", | ||
StandardCharsets.UTF_8, StandardOpenOption.CREATE); | ||
|
||
testClassLoader = new URLClassLoader( | ||
new URL[] { resources.toURI().toURL() }, | ||
DefaultI18NProviderFactory.class.getClassLoader()); | ||
Thread.currentThread().setContextClassLoader(testClassLoader); | ||
|
||
Resource translationResource = new DefaultResourceLoader() | ||
.getResource(DefaultI18NProvider.BUNDLE_FOLDER + "/" | ||
+ DefaultI18NProvider.BUNDLE_FILENAME + ".properties"); | ||
|
||
pathMatchingResourcePatternResolverMockedConstruction = Mockito | ||
.mockConstruction(PathMatchingResourcePatternResolver.class, | ||
(mock, context) -> { | ||
Mockito.when(mock.getPathMatcher()) | ||
.thenCallRealMethod(); | ||
Mockito.when(mock.getResources(Mockito.anyString())) | ||
.thenAnswer(invocationOnMock -> { | ||
String pattern = invocationOnMock | ||
.getArgument(0); | ||
Assert.assertEquals( | ||
"classpath*:/vaadin-i18n/*.properties", | ||
pattern); | ||
return new Resource[] { | ||
translationResource }; | ||
}); | ||
}); | ||
} | ||
|
||
@AfterClass | ||
static public void teardown() throws Exception { | ||
pathMatchingResourcePatternResolverMockedConstruction.close(); | ||
Thread.currentThread().setContextClassLoader(originalClassLoader); | ||
} | ||
|
||
@Test | ||
public void create_usesThreadContextClassLoader() throws ServletException { | ||
Instantiator instantiator = getInstantiator(context); | ||
I18NProvider i18NProvider = instantiator.getI18NProvider(); | ||
|
||
Assert.assertNotNull(i18NProvider); | ||
Assert.assertTrue(i18NProvider instanceof DefaultI18NProvider); | ||
Assert.assertEquals("Default lang", | ||
i18NProvider.getTranslation("title", Locale.getDefault())); | ||
} | ||
|
||
private static Instantiator getInstantiator(ApplicationContext context) | ||
throws ServletException { | ||
return SpringInstantiatorTest.getService(context, new Properties()) | ||
.getInstantiator(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should perhaps be marked with
@NotThreadSafe
as we use a set current in the test