Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Oct 5, 2024
1 parent 32df079 commit b0c7d15
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public abstract class StringUtils {

private static final String CURRENT_PATH = ".";

private static final char EXTENSION_SEPARATOR = '.';
private static final char DOT_CHAR = '.';

private static final int DEFAULT_TRUNCATION_THRESHOLD = 100;

Expand Down Expand Up @@ -538,7 +538,7 @@ public static Object quoteIfString(@Nullable Object obj) {
* @param qualifiedName the qualified name
*/
public static String unqualify(String qualifiedName) {
return unqualify(qualifiedName, '.');
return unqualify(qualifiedName, DOT_CHAR);
}

/**
Expand Down Expand Up @@ -641,7 +641,7 @@ public static String getFilenameExtension(@Nullable String path) {
return null;
}

int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR);
int extIndex = path.lastIndexOf(DOT_CHAR);
if (extIndex == -1) {
return null;
}
Expand All @@ -661,7 +661,7 @@ public static String getFilenameExtension(@Nullable String path) {
* @return the path with stripped filename extension
*/
public static String stripFilenameExtension(String path) {
int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR);
int extIndex = path.lastIndexOf(DOT_CHAR);
if (extIndex == -1) {
return path;
}
Expand Down Expand Up @@ -724,7 +724,7 @@ public static String cleanPath(String path) {
String pathToUse = normalizedPath;

// Shortcut if there is no work to do
if (pathToUse.indexOf(EXTENSION_SEPARATOR) == -1) {
if (pathToUse.indexOf(DOT_CHAR) == -1) {
return pathToUse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void replaceDefinition(ConfigurableListableBeanFactory beanFactory, Over

if (!(beanFactory instanceof BeanDefinitionRegistry registry)) {
throw new IllegalStateException("Cannot process bean override with a BeanFactory " +
"that doesn't implement BeanDefinitionRegistry: " + beanFactory.getClass());
"that doesn't implement BeanDefinitionRegistry: " + beanFactory.getClass().getName());
}

// The following is a "pseudo" bean definition which MUST NOT be used to
Expand Down Expand Up @@ -176,10 +176,12 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, OverrideMetad
int candidateCount = candidateNames.size();
if (candidateCount != 1) {
Field field = overrideMetadata.getField();
throw new IllegalStateException("Unable to select a bean to override by wrapping: found " +
candidateCount + " bean instances of type " + overrideMetadata.getBeanType() +
" (as required by annotated field '" + field.getDeclaringClass().getSimpleName() +
"." + field.getName() + "')" + (candidateCount > 0 ? ": " + candidateNames : ""));
throw new IllegalStateException("""
Unable to select a bean to override by wrapping: found %d bean instances of type %s \
(as required by annotated field '%s.%s')%s"""
.formatted(candidateCount, overrideMetadata.getBeanType(),
field.getDeclaringClass().getSimpleName(), field.getName(),
(candidateCount > 0 ? ": " + candidateNames : "")));
}
beanName = BeanFactoryUtils.transformedBeanName(candidateNames.iterator().next());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class BeanOverrideContextCustomizer implements ContextCustomizer {
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
if (!(context instanceof BeanDefinitionRegistry registry)) {
throw new IllegalStateException("Cannot process bean overrides with an ApplicationContext " +
"that doesn't implement BeanDefinitionRegistry: " + context.getClass());
"that doesn't implement BeanDefinitionRegistry: " + context.getClass().getName());
}
registerInfrastructure(registry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BeanOverrideRegistrar implements BeanFactoryAware {
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if (!(beanFactory instanceof ConfigurableBeanFactory cbf)) {
throw new IllegalStateException("Cannot process bean override with a BeanFactory " +
"that doesn't implement ConfigurableBeanFactory: " + beanFactory.getClass());
"that doesn't implement ConfigurableBeanFactory: " + beanFactory.getClass().getName());
}
this.beanFactory = cbf;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,28 @@ public void prepareTestInstance(TestContext testContext) throws Exception {

@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
reinjectFieldsIfConfigured(testContext);
reinjectFieldsIfNecessary(testContext);
}

/**
* Process the test instance and make sure that fields flagged for bean
* overriding are processed.
* <p>Each field's value will be updated with the overridden bean instance.
* overriding are injected with the overridden bean instance.
*/
protected void injectFields(TestContext testContext) {
postProcessFields(testContext, (testMetadata, overrideRegistrar) -> overrideRegistrar.inject(
testMetadata.testInstance, testMetadata.overrideMetadata));
postProcessFields(testContext, (testMetadata, registrar) ->
registrar.inject(testMetadata.testInstance, testMetadata.overrideMetadata));
}

/**
* Process the test instance and make sure that fields flagged for bean
* overriding are processed.
* overriding are injected with the overridden bean instance, if necessary.
* <p>If a fresh instance is required, the field is nulled out and then
* re-injected with the overridden bean instance.
* <p>This method does nothing if the
* {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
* attribute is not present in the {@code TestContext}.
* attribute is not present in the {@code TestContext} with a value of {@link Boolean#TRUE}.
*/
protected void reinjectFieldsIfConfigured(TestContext testContext) throws Exception {
protected void reinjectFieldsIfNecessary(TestContext testContext) throws Exception {
if (Boolean.TRUE.equals(
testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {

Expand Down

0 comments on commit b0c7d15

Please sign in to comment.