Skip to content

Commit

Permalink
Introduce test for XML replaced-method element without explicit arg-type
Browse files Browse the repository at this point in the history
This commit introduces an integration test for the regression fixed in
the previous commit (cd64e66).

Closes gh-31826
  • Loading branch information
sbrannen committed Dec 13, 2023
1 parent cd64e66 commit 8d4deca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -56,6 +61,8 @@
import org.springframework.beans.testfixture.beans.IndexedTestBean;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.beans.testfixture.beans.factory.DummyFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.UrlResource;
Expand Down Expand Up @@ -1336,6 +1343,15 @@ void replaceMethodOverrideWithSetterInjection() {
assertThat(dos.lastArg).isEqualTo(s2);
}

@Test // gh-31826
void replaceNonOverloadedInterfaceMethodWithoutSpecifyingExplicitArgTypes() {
try (ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(DELEGATION_OVERRIDES_CONTEXT.getPath())) {
EchoService echoService = context.getBean(EchoService.class);
assertThat(echoService.echo("foo", "bar")).containsExactly("bar", "foo");
}
}

@Test
void lookupOverrideOneMethodWithConstructorInjection() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
Expand Down Expand Up @@ -1891,3 +1907,20 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
}

}

interface EchoService {

String[] echo(Object... objects);
}

class ReverseArrayMethodReplacer implements MethodReplacer {

@Override
public Object reimplement(Object obj, Method method, Object[] args) {
List<String> list = Arrays.stream((Object[]) args[0])
.map(Object::toString)
.collect(Collectors.toCollection(ArrayList::new));
Collections.reverse(list);
return list.toArray(String[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@

<bean id="replaceVoidMethod" parent="someParent"
class="org.springframework.beans.factory.xml.OverrideOneMethodSubclass">
</bean>

<bean id="replaceEchoMethod" class="org.springframework.beans.factory.xml.EchoService">
<!--
This method is not overloaded, so we don't need to specify any arg types
-->
<replaced-method name="echo" replacer="reverseArrayReplacer" />
</bean>

<bean id="reverseReplacer"
class="org.springframework.beans.factory.xml.ReverseMethodReplacer"/>

<bean id="reverseArrayReplacer"
class="org.springframework.beans.factory.xml.ReverseArrayMethodReplacer"/>

<bean id="fixedReplacer"
class="org.springframework.beans.factory.xml.FixedMethodReplacer"/>

Expand Down

0 comments on commit 8d4deca

Please sign in to comment.