Avoid reflection for instantiating MockServerContainerContextCustomizer

Since MockServerContainerContextCustomizerFactory already has a
`webSocketPresent` check, there should be no need to instantiate
MockServerContainerContextCustomizer via reflection.

See gh-29028
Closes gh-29035
This commit is contained in:
Sam Brannen 2022-08-28 17:43:45 +02:00
parent 92b582701e
commit c24e98e51b
1 changed files with 2 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.test.context.web.socket;
import java.util.List; import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextConfigurationAttributes; import org.springframework.test.context.ContextConfigurationAttributes;
@ -39,9 +38,6 @@ class MockServerContainerContextCustomizerFactory implements ContextCustomizerFa
private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME = private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME =
"org.springframework.test.context.web.WebAppConfiguration"; "org.springframework.test.context.web.WebAppConfiguration";
private static final String MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME =
"org.springframework.test.context.web.socket.MockServerContainerContextCustomizer";
private static final boolean webSocketPresent = ClassUtils.isPresent("jakarta.websocket.server.ServerContainer", private static final boolean webSocketPresent = ClassUtils.isPresent("jakarta.websocket.server.ServerContainer",
MockServerContainerContextCustomizerFactory.class.getClassLoader()); MockServerContainerContextCustomizerFactory.class.getClassLoader());
@ -52,17 +48,8 @@ class MockServerContainerContextCustomizerFactory implements ContextCustomizerFa
List<ContextConfigurationAttributes> configAttributes) { List<ContextConfigurationAttributes> configAttributes) {
if (webSocketPresent && isAnnotatedWithWebAppConfiguration(testClass)) { if (webSocketPresent && isAnnotatedWithWebAppConfiguration(testClass)) {
try { return new MockServerContainerContextCustomizer();
Class<?> clazz = ClassUtils.forName(MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME,
getClass().getClassLoader());
return (ContextCustomizer) BeanUtils.instantiateClass(clazz);
} }
catch (Throwable ex) {
throw new IllegalStateException("Failed to enable WebSocket test support; could not load class: " +
MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME, ex);
}
}
// Else, nothing to customize // Else, nothing to customize
return null; return null;
} }