Skip to content

Commit

Permalink
Add Locale conversion format configuration property
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Jul 31, 2023
1 parent 59141b9 commit 95da5f8
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 4 deletions.
1 change: 1 addition & 0 deletions junit-jupiter-engine/junit-jupiter-engine.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
api(platform(projects.junitBom))
api(projects.junitPlatformEngine)
api(projects.junitJupiterApi)
api(projects.junitJupiterParams)

compileOnlyApi(libs.apiguardian)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.io.TempDirFactory;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.converter.LocaleConversionFormat;

/**
* Caching implementation of the {@link JupiterConfiguration} API.
Expand Down Expand Up @@ -125,4 +126,10 @@ public Supplier<TempDirFactory> getDefaultTempDirFactorySupplier() {
key -> delegate.getDefaultTempDirFactorySupplier());
}

@Override
public LocaleConversionFormat getDefaultLocaleConversionFormat() {
return (LocaleConversionFormat) cache.computeIfAbsent(DEFAULT_LOCALE_CONVERSION_FORMAT_PROPERTY_NAME,
key -> delegate.getDefaultLocaleConversionFormat());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.jupiter.api.io.CleanupMode.ALWAYS;
import static org.junit.jupiter.api.io.TempDir.DEFAULT_CLEANUP_MODE_PROPERTY_NAME;
import static org.junit.jupiter.api.io.TempDir.DEFAULT_FACTORY_PROPERTY_NAME;
import static org.junit.jupiter.params.converter.LocaleConversionFormat.ISO_639;

import java.util.Optional;
import java.util.function.Function;
Expand All @@ -29,6 +30,7 @@
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.io.TempDirFactory;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.converter.LocaleConversionFormat;
import org.junit.platform.commons.util.ClassNamePatternFilterUtils;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.engine.ConfigurationParameters;
Expand Down Expand Up @@ -62,6 +64,9 @@ public class DefaultJupiterConfiguration implements JupiterConfiguration {
private static final InstantiatingConfigurationParameterConverter<TempDirFactory> tempDirFactoryConverter = //
new InstantiatingConfigurationParameterConverter<>(TempDirFactory.class, "temp dir factory");

private static final EnumConfigurationParameterConverter<LocaleConversionFormat> localeConversionFormatConverter = //
new EnumConfigurationParameterConverter<>(LocaleConversionFormat.class, "locale conversion format");

private final ConfigurationParameters configurationParameters;

public DefaultJupiterConfiguration(ConfigurationParameters configurationParameters) {
Expand Down Expand Up @@ -141,4 +146,10 @@ public Supplier<TempDirFactory> getDefaultTempDirFactorySupplier() {
return () -> supplier.get().orElse(TempDirFactory.Standard.INSTANCE);
}

@Override
public LocaleConversionFormat getDefaultLocaleConversionFormat() {
return localeConversionFormatConverter.get(configurationParameters,
DEFAULT_LOCALE_CONVERSION_FORMAT_PROPERTY_NAME, ISO_639);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.api.io.TempDirFactory;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.converter.LocaleConversionFormat;

/**
* @since 5.4
Expand All @@ -43,6 +44,7 @@ public interface JupiterConfiguration {
String DEFAULT_DISPLAY_NAME_GENERATOR_PROPERTY_NAME = DisplayNameGenerator.DEFAULT_GENERATOR_PROPERTY_NAME;
String DEFAULT_TEST_METHOD_ORDER_PROPERTY_NAME = MethodOrderer.DEFAULT_ORDER_PROPERTY_NAME;
String DEFAULT_TEST_CLASS_ORDER_PROPERTY_NAME = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME;
String DEFAULT_LOCALE_CONVERSION_FORMAT_PROPERTY_NAME = "junit.jupiter.params.arguments.conversion.locale.format";

Optional<String> getRawConfigurationParameter(String key);

Expand Down Expand Up @@ -70,4 +72,6 @@ public interface JupiterConfiguration {

Supplier<TempDirFactory> getDefaultTempDirFactorySupplier();

LocaleConversionFormat getDefaultLocaleConversionFormat();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
module org.junit.jupiter.engine {
requires static org.apiguardian.api;
requires org.junit.jupiter.api;
requires org.junit.jupiter.params.converter;
requires org.junit.platform.commons;
requires org.junit.platform.engine;
requires org.opentest4j;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.io.CleanupMode.NEVER;
import static org.junit.jupiter.params.converter.LocaleConversionFormat.ISO_639;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -95,7 +96,7 @@ void cachesDefaultDisplayNameGenerator() {
CustomDisplayNameGenerator customDisplayNameGenerator = new CustomDisplayNameGenerator();
when(delegate.getDefaultDisplayNameGenerator()).thenReturn(customDisplayNameGenerator);

// call `cache.getDefaultDisplayNameGenerator()` twice to verify the delegate method is called only once.
// call twice to verify the delegate method is called only once.
assertThat(cache.getDefaultDisplayNameGenerator()).isSameAs(customDisplayNameGenerator);
assertThat(cache.getDefaultDisplayNameGenerator()).isSameAs(customDisplayNameGenerator);

Expand All @@ -107,7 +108,7 @@ void cachesDefaultTestMethodOrderer() {
final Optional<MethodOrderer> methodOrderer = Optional.of(new MethodOrderer.MethodName());
when(delegate.getDefaultTestMethodOrderer()).thenReturn(methodOrderer);

// call `cache.getDefaultTestMethodOrderer()` twice to verify the delegate method is called only once.
// call twice to verify the delegate method is called only once.
assertThat(cache.getDefaultTestMethodOrderer()).isSameAs(methodOrderer);
assertThat(cache.getDefaultTestMethodOrderer()).isSameAs(methodOrderer);

Expand All @@ -118,7 +119,7 @@ void cachesDefaultTestMethodOrderer() {
void cachesDefaultTempDirCleanupMode() {
when(delegate.getDefaultTempDirCleanupMode()).thenReturn(NEVER);

// call `cache.getDefaultTempStrategyDirCleanupMode()` twice to verify the delegate method is called only once.
// call twice to verify the delegate method is called only once.
assertThat(cache.getDefaultTempDirCleanupMode()).isSameAs(NEVER);
assertThat(cache.getDefaultTempDirCleanupMode()).isSameAs(NEVER);

Expand All @@ -130,13 +131,24 @@ void cachesDefaultTempDirFactorySupplier() {
Supplier<TempDirFactory> supplier = mock();
when(delegate.getDefaultTempDirFactorySupplier()).thenReturn(supplier);

// call `cache.getDefaultTempDirFactorySupplier()` twice to verify the delegate method is called only once.
// call twice to verify the delegate method is called only once.
assertThat(cache.getDefaultTempDirFactorySupplier()).isSameAs(supplier);
assertThat(cache.getDefaultTempDirFactorySupplier()).isSameAs(supplier);

verify(delegate, only()).getDefaultTempDirFactorySupplier();
}

@Test
void cachesDefaultLocaleConversionFormat() {
when(delegate.getDefaultLocaleConversionFormat()).thenReturn(ISO_639);

// call twice to verify the delegate method is called only once.
assertThat(cache.getDefaultLocaleConversionFormat()).isSameAs(ISO_639);
assertThat(cache.getDefaultLocaleConversionFormat()).isSameAs(ISO_639);

verify(delegate, only()).getDefaultLocaleConversionFormat();
}

@Test
void doesNotCacheRawParameters() {
when(delegate.getRawConfigurationParameter("foo")).thenReturn(Optional.of("bar")).thenReturn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_METHOD;
import static org.junit.jupiter.api.io.CleanupMode.ALWAYS;
import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME;
import static org.junit.jupiter.params.converter.LocaleConversionFormat.ISO_639;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -34,6 +35,7 @@
import org.junit.jupiter.api.io.TempDirFactory;
import org.junit.jupiter.engine.Constants;
import org.junit.jupiter.engine.descriptor.CustomDisplayNameGenerator;
import org.junit.jupiter.params.converter.LocaleConversionFormat;
import org.junit.platform.commons.PreconditionViolationException;
import org.junit.platform.engine.ConfigurationParameters;

Expand Down Expand Up @@ -145,6 +147,13 @@ void shouldGetStandardAsDefaultTempDirFactorySupplierWithoutConfigParamSet() {
assertThat(supplier.get()).isSameAs(TempDirFactory.Standard.INSTANCE);
}

@Test
void shouldGetDefaultLocaleConversionFormatWithNoConfigParamSet() {
JupiterConfiguration configuration = new DefaultJupiterConfiguration(mock());
LocaleConversionFormat localeConversionFormat = configuration.getDefaultLocaleConversionFormat();
assertThat(localeConversionFormat).isEqualTo(ISO_639);
}

private void assertDefaultConfigParam(String configValue, Lifecycle expected) {
ConfigurationParameters configParams = mock();
when(configParams.get(KEY)).thenReturn(Optional.ofNullable(configValue));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2015-2023 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.jupiter.params.converter;

import static org.apiguardian.api.API.Status.INTERNAL;

import org.apiguardian.api.API;

/**
* Enumeration of {@link java.util.Locale} conversion formats.
*
* @since 5.11
*/
@API(status = INTERNAL, since = "5.11")
public enum LocaleConversionFormat {

/**
* The ISO 639 alpha-2 or alpha-3 language code format.
*
* @see java.util.Locale#Locale(String)
*/
ISO_639,

/**
* The IETF BCP 47 language tag format.
*
* @see java.util.Locale#forLanguageTag(String)
*/
BCP_47

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2015-2023 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.jupiter.params.converter;

import java.util.Locale;
import java.util.function.Function;

import org.junit.platform.commons.util.Preconditions;

class StringToLocaleConverter implements StringToObjectConverter {

private final Function<String, Locale> converter;

StringToLocaleConverter(LocaleConversionFormat format) {
Preconditions.notNull(format, "format must not be null");
this.converter = format == LocaleConversionFormat.ISO_639 ? Locale::new : Locale::forLanguageTag;
}

@Override
public boolean canConvert(Class<?> targetType) {
return targetType == Locale.class;
}

@Override
public Object convert(String source, Class<?> targetType) {
return converter.apply(source);
}

}

0 comments on commit 95da5f8

Please sign in to comment.