Skip to content

Commit

Permalink
Avoid reflection for instantiating MockServerContainerContextCustomizer
Browse files Browse the repository at this point in the history
Since MockServerContainerContextCustomizerFactory already has a
`webSocketPresent` check, there should be no need to instantiate
MockServerContainerContextCustomizer via reflection.

See gh-29028
Closes gh-29035
  • Loading branch information
sbrannen committed Aug 28, 2022
1 parent 92b5827 commit c24e98e
Showing 1 changed file with 2 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextConfigurationAttributes;
Expand All @@ -39,9 +38,6 @@ class MockServerContainerContextCustomizerFactory implements ContextCustomizerFa
private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME =
"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",
MockServerContainerContextCustomizerFactory.class.getClassLoader());

Expand All @@ -52,17 +48,8 @@ public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {

if (webSocketPresent && isAnnotatedWithWebAppConfiguration(testClass)) {
try {
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);
}
return new MockServerContainerContextCustomizer();
}

// Else, nothing to customize
return null;
}
Expand Down

0 comments on commit c24e98e

Please sign in to comment.