Browse Source

Polishing

pull/36079/head
Sam Brannen 4 weeks ago
parent
commit
7212fbe36a
  1. 46
      spring-context/src/test/java/org/springframework/context/event/EventPublicationInterceptorTests.java
  2. 2
      spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java

46
spring-context/src/test/java/org/springframework/context/event/EventPublicationInterceptorTests.java

@ -26,7 +26,6 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.testfixture.beans.ITestBean; import org.springframework.beans.testfixture.beans.ITestBean;
import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.test.TestEvent; import org.springframework.context.event.test.TestEvent;
import org.springframework.context.support.StaticApplicationContext; import org.springframework.context.support.StaticApplicationContext;
import org.springframework.context.testfixture.beans.TestApplicationListener; import org.springframework.context.testfixture.beans.TestApplicationListener;
@ -36,6 +35,8 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link EventPublicationInterceptor}.
*
* @author Dmitriy Kopylenko * @author Dmitriy Kopylenko
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rick Evans * @author Rick Evans
@ -47,46 +48,44 @@ class EventPublicationInterceptorTests {
@BeforeEach @BeforeEach
void setup() { void setup() {
ApplicationEventPublisher publisher = mock(); this.interceptor.setApplicationEventPublisher(mock());
this.interceptor.setApplicationEventPublisher(publisher);
} }
@Test @Test
void withNoApplicationEventPublisherSupplied() { void withNoApplicationEventPublisherSupplied() {
this.interceptor.setApplicationEventPublisher(null); this.interceptor.setApplicationEventPublisher(null);
assertThatIllegalArgumentException().isThrownBy(interceptor::afterPropertiesSet); assertThatIllegalArgumentException()
.isThrownBy(interceptor::afterPropertiesSet)
.withMessage("Property 'applicationEventPublisher' is required");
} }
@SuppressWarnings("unchecked") @SuppressWarnings({ "unchecked", "rawtypes" })
@Test @Test
void withNonApplicationEventClassSupplied() { void withNonApplicationEventClassSupplied() {
assertThatIllegalArgumentException().isThrownBy(() -> { assertThatIllegalArgumentException()
interceptor.setApplicationEventClass((Class) getClass()); .isThrownBy(() -> interceptor.setApplicationEventClass((Class) getClass()))
interceptor.afterPropertiesSet(); .withMessage("'applicationEventClass' needs to extend ApplicationEvent");
});
} }
@Test @Test
void withAbstractStraightApplicationEventClassSupplied() { void withAbstractStraightApplicationEventClassSupplied() {
assertThatIllegalArgumentException().isThrownBy(() -> { assertThatIllegalArgumentException()
interceptor.setApplicationEventClass(ApplicationEvent.class); .isThrownBy(() -> interceptor.setApplicationEventClass(ApplicationEvent.class))
interceptor.afterPropertiesSet(); .withMessage("'applicationEventClass' needs to extend ApplicationEvent");
});
} }
@Test @Test
void withApplicationEventClassThatDoesntExposeAValidCtor() { void withApplicationEventClassThatDoesntExposeAValidCtor() {
assertThatIllegalArgumentException().isThrownBy(() -> { assertThatIllegalArgumentException()
interceptor.setApplicationEventClass(TestEventWithNoValidOneArgObjectCtor.class); .isThrownBy(() -> interceptor.setApplicationEventClass(TestEventWithNoValidOneArgObjectCtor.class))
interceptor.afterPropertiesSet(); .withMessageContaining("does not have the required Object constructor");
});
} }
@Test @Test
void expectedBehavior() { void expectedBehavior() {
TestBean target = new TestBean(); TestBean target = new TestBean();
final TestApplicationListener listener = new TestApplicationListener(); TestApplicationListener listener = new TestApplicationListener();
class TestContext extends StaticApplicationContext { class TestContext extends StaticApplicationContext {
@Override @Override
@ -103,8 +102,7 @@ class EventPublicationInterceptorTests {
ctx.registerSingleton("otherListener", FactoryBeanTestListener.class); ctx.registerSingleton("otherListener", FactoryBeanTestListener.class);
ctx.refresh(); ctx.refresh();
EventPublicationInterceptor interceptor = EventPublicationInterceptor interceptor = ctx.getBean(EventPublicationInterceptor.class);
(EventPublicationInterceptor) ctx.getBean("publisher");
ProxyFactory factory = new ProxyFactory(target); ProxyFactory factory = new ProxyFactory(target);
factory.addAdvice(0, interceptor); factory.addAdvice(0, interceptor);
@ -115,14 +113,14 @@ class EventPublicationInterceptorTests {
// two events: ContextRefreshedEvent and TestEvent // two events: ContextRefreshedEvent and TestEvent
assertThat(listener.getEventCount()).as("Interceptor must have published 2 events").isEqualTo(2); assertThat(listener.getEventCount()).as("Interceptor must have published 2 events").isEqualTo(2);
TestApplicationListener otherListener = (TestApplicationListener) ctx.getBean("&otherListener"); TestApplicationListener otherListener = ctx.getBean("&otherListener", TestApplicationListener.class);
assertThat(otherListener.getEventCount()).as("Interceptor must have published 2 events").isEqualTo(2); assertThat(otherListener.getEventCount()).as("Interceptor must have published 2 events").isEqualTo(2);
ctx.close(); ctx.close();
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
static final class TestEventWithNoValidOneArgObjectCtor extends ApplicationEvent { static class TestEventWithNoValidOneArgObjectCtor extends ApplicationEvent {
public TestEventWithNoValidOneArgObjectCtor() { public TestEventWithNoValidOneArgObjectCtor() {
super(""); super("");
@ -130,10 +128,10 @@ class EventPublicationInterceptorTests {
} }
static class FactoryBeanTestListener extends TestApplicationListener implements FactoryBean<Object> { static class FactoryBeanTestListener extends TestApplicationListener implements FactoryBean<String> {
@Override @Override
public Object getObject() { public String getObject() {
return "test"; return "test";
} }

2
spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java

@ -373,8 +373,6 @@ class EnableTransactionManagementTests {
TestServiceWithRollback bean = ctx.getBean("testBean", TestServiceWithRollback.class); TestServiceWithRollback bean = ctx.getBean("testBean", TestServiceWithRollback.class);
CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class); CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
Method method1 = TestServiceWithRollback.class.getMethod("methodOne");
Method method2 = TestServiceWithRollback.class.getMethod("methodTwo");
assertThatException().isThrownBy(bean::methodOne); assertThatException().isThrownBy(bean::methodOne);
assertThatException().isThrownBy(bean::methodTwo); assertThatException().isThrownBy(bean::methodTwo);
assertThat(txManager.begun).isEqualTo(2); assertThat(txManager.begun).isEqualTo(2);

Loading…
Cancel
Save