Skip to content

Commit

Permalink
GH-2946 Remove spring.binders from TestBinder
Browse files Browse the repository at this point in the history
Resolves #2946
  • Loading branch information
olegz committed May 8, 2024
1 parent c2754de commit ee3faf6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,33 @@
*/
class ActuatorBindingsTest {

private static ClassLoader createClassLoader(String[] additionalClasspathDirectories) throws IOException {
URL[] urls = ObjectUtils.isEmpty(additionalClasspathDirectories) ? new URL[0]
: new URL[additionalClasspathDirectories.length];
if (!ObjectUtils.isEmpty(additionalClasspathDirectories)) {
for (int i = 0; i < additionalClasspathDirectories.length; i++) {
urls[i] = new URL(new ClassPathResource(additionalClasspathDirectories[i])
.getURL().toString() + "/");
}
}
return new URLClassLoader(urls,
ActuatorBindingsTest.class.getClassLoader());
}


/*
* Even though this test performs some simple assertions, the main purpose for it is to validate that
* it does not result in recursive exception described in https://github.com/spring-cloud/spring-cloud-stream/issues/2253
*/
@Test
void actuatorDoesNotCauseInfiniteRecursion() {
void actuatorDoesNotCauseInfiniteRecursion() throws Exception {
ClassLoader classLoader = createClassLoader(new String[] { "binder1" });
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
TestChannelBinderConfiguration.getCompleteConfiguration(Bindings.class))
.resourceLoader(new DefaultResourceLoader(classLoader))
.web(WebApplicationType.NONE).run("--spring.jmx.enabled=false",
"--spring.cloud.function.definition=consume",
"--spring.cloud.stream.bindings.consume-in-0.binder=integration1",
"--spring.jackson.visibility.field=ANY" // see https://github.com/spring-cloud/spring-cloud-stream/issues/2253
// we need the above just to verify that such action does not
// interfere with instance of ObjectMapper inside of BindingsLifecycleController
Expand All @@ -69,18 +86,6 @@ void actuatorDoesNotCauseInfiniteRecursion() {
}
}

private static ClassLoader createClassLoader(String[] additionalClasspathDirectories) throws IOException {
URL[] urls = ObjectUtils.isEmpty(additionalClasspathDirectories) ? new URL[0]
: new URL[additionalClasspathDirectories.length];
if (!ObjectUtils.isEmpty(additionalClasspathDirectories)) {
for (int i = 0; i < additionalClasspathDirectories.length; i++) {
urls[i] = new URL(new ClassPathResource(additionalClasspathDirectories[i])
.getURL().toString() + "/");
}
}
return new URLClassLoader(urls,
ActuatorBindingsTest.class.getClassLoader());
}

// Following three tests are verifying the behavior for
// https://github.com/spring-cloud/spring-cloud-stream/commit/3abf06345ad1ed57dea161b35503eba107feb04a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.springframework.cloud.stream.function.edgecases;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.function.Consumer;

import org.junit.jupiter.api.Test;
Expand All @@ -25,7 +28,10 @@
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.messaging.Message;
import org.springframework.util.ObjectUtils;

/**
* This test validates that the issue https://github.com/spring-cloud/spring-cloud-stream/issues/1801
Expand All @@ -36,15 +42,29 @@
*/
class GH1801Test {

private static ClassLoader createClassLoader(String[] additionalClasspathDirectories) throws IOException {
URL[] urls = ObjectUtils.isEmpty(additionalClasspathDirectories) ? new URL[0]
: new URL[additionalClasspathDirectories.length];
if (!ObjectUtils.isEmpty(additionalClasspathDirectories)) {
for (int i = 0; i < additionalClasspathDirectories.length; i++) {
urls[i] = new URL(new ClassPathResource(additionalClasspathDirectories[i]).getURL().toString() + "/");
}
}
return new URLClassLoader(urls, GH1801Test.class.getClassLoader());
}

@Test
void verifyNoNPEWhenFactoryMethodNull() {
SampleBootApplication.main("--spring.cloud.stream.defaultBinder=integration");
void verifyNoNPEWhenFactoryMethodNull() throws Exception {
SampleBootApplication.main("--spring.cloud.stream.defaultBinder=integration1");
}

@SpringBootApplication
public static class SampleBootApplication {
public static void main(String... args) {
new SpringApplicationBuilder(SampleBootApplication.class).web(WebApplicationType.NONE).run(args);
public static void main(String... args) throws Exception {
ClassLoader classLoader = createClassLoader(new String[] { "binder1" });
new SpringApplicationBuilder(SampleBootApplication.class)
.resourceLoader(new DefaultResourceLoader(classLoader))
.web(WebApplicationType.NONE).run(args);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
integration1:\
org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration
integration2:\
org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration

This file was deleted.

0 comments on commit ee3faf6

Please sign in to comment.