Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.boot.actuate.data.elasticsearch.ElasticsearchReactiveHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.testsupport.junit.EnabledOnLocale;
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -87,6 +88,7 @@ void elasticsearchWithYellowStatusIsUp() {
}

@Test
@EnabledOnLocale(language = "en")
void elasticsearchIsDown() throws Exception {
this.server.shutdown();
Health health = this.healthIndicator.health().block(TIMEOUT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.boot.testsupport.junit.EnabledOnLocale;

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

Expand Down Expand Up @@ -125,6 +127,7 @@ void whenAotRunsWithReleaseSourcesAreGenerated(MavenBuild mavenBuild) {
}

@TestTemplate
@EnabledOnLocale(language = "en")
void whenAotRunsWithInvalidCompilerArgumentsCompileFails(MavenBuild mavenBuild) {
mavenBuild.project("aot-compiler-arguments")
.goals("package")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2012-present the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.testsupport.junit;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* {@code @EnabledOnLocale} annotation is used to conditionally enable a test method based
* on the specified locale attributes.
*
* @author Dmytro Nosan
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(EnabledOnLocaleCondition.class)
public @interface EnabledOnLocale {

/**
* Specifies the language code for which the test method should be enabled.
* @return the language code
*/
String language();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2012-present the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.testsupport.junit;

import java.util.Locale;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.support.AnnotationSupport;

/**
* An implementation of {@link ExecutionCondition} that conditionally enables or disables
* the execution of a test method or class based on the specified locale attributes.
*
* @author Dmytro Nosan
*/
class EnabledOnLocaleCondition implements ExecutionCondition {

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
return AnnotationSupport.findAnnotation(context.getElement(), EnabledOnLocale.class)
.map(this::evaluate)
.orElseGet(() -> ConditionEvaluationResult.enabled("No @EnabledOnLocale annotation found"));
}

private ConditionEvaluationResult evaluate(EnabledOnLocale annotation) {
Locale locale = Locale.getDefault();
String language = locale.getLanguage();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative, System.getProperty("user.language") can be used, but as far as I remember, Locale.getDefault() returns the same value.

if (!annotation.language().equals(language)) {
return ConditionEvaluationResult.disabled("Disabled on language: " + language);
}
return ConditionEvaluationResult.enabled("Enabled on language: " + language);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;

import org.springframework.boot.testsupport.junit.EnabledOnLocale;
import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactoryTests;
import org.springframework.boot.web.server.PortInUseException;
Expand Down Expand Up @@ -226,6 +227,7 @@ void referenceClearingIsDisabled() {
}

@Test
@EnabledOnLocale(language = "en")
void portClashOfPrimaryConnectorResultsInPortInUseException() throws Exception {
doWithBlockedPort((port) -> assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
AbstractReactiveWebServerFactory factory = getFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@

import org.springframework.boot.ssl.DefaultSslBundleRegistry;
import org.springframework.boot.testsupport.classpath.resources.WithPackageResources;
import org.springframework.boot.testsupport.junit.EnabledOnLocale;
import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.web.server.PortInUseException;
import org.springframework.boot.web.server.Shutdown;
Expand Down Expand Up @@ -367,6 +368,7 @@ void defaultUriEncoding() {
}

@Test
@EnabledOnLocale(language = "en")
void startupFailureDoesNotResultInUnstoppedThreadsBeingReported(CapturedOutput output) throws Exception {
super.portClashOfPrimaryConnectorResultsInPortInUseException();
assertThat(output).doesNotContain("appears to have started a thread named [main]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
import org.springframework.boot.system.ApplicationTemp;
import org.springframework.boot.testsupport.classpath.resources.ResourcePath;
import org.springframework.boot.testsupport.classpath.resources.WithPackageResources;
import org.springframework.boot.testsupport.junit.EnabledOnLocale;
import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
Expand Down Expand Up @@ -1071,6 +1072,7 @@ void serverHeaderIsDisabledByDefault() throws Exception {
}

@Test
@EnabledOnLocale(language = "en")
protected void portClashOfPrimaryConnectorResultsInPortInUseException() throws Exception {
doWithBlockedPort((port) -> {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
Expand All @@ -1083,6 +1085,7 @@ protected void portClashOfPrimaryConnectorResultsInPortInUseException() throws E
}

@Test
@EnabledOnLocale(language = "en")
protected void portClashOfSecondaryConnectorResultsInPortInUseException() throws Exception {
doWithBlockedPort((port) -> {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
Expand Down