|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* Copyright 2002-2016 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. |
|
|
|
@ -379,6 +379,50 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen |
|
|
|
context.close(); |
|
|
|
context.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void anonymousClassAsListener() { |
|
|
|
|
|
|
|
final Set<MyEvent> seenEvents = new HashSet<>(); |
|
|
|
|
|
|
|
StaticApplicationContext context = new StaticApplicationContext(); |
|
|
|
|
|
|
|
context.addApplicationListener(new ApplicationListener<MyEvent>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onApplicationEvent(MyEvent event) { |
|
|
|
|
|
|
|
seenEvents.add(event); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
context.refresh(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MyEvent event1 = new MyEvent(context); |
|
|
|
|
|
|
|
context.publishEvent(event1); |
|
|
|
|
|
|
|
context.publishEvent(new MyOtherEvent(context)); |
|
|
|
|
|
|
|
MyEvent event2 = new MyEvent(context); |
|
|
|
|
|
|
|
context.publishEvent(event2); |
|
|
|
|
|
|
|
assertSame(2, seenEvents.size()); |
|
|
|
|
|
|
|
assertTrue(seenEvents.contains(event1)); |
|
|
|
|
|
|
|
assertTrue(seenEvents.contains(event2)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void lambdaAsListener() { |
|
|
|
|
|
|
|
final Set<MyEvent> seenEvents = new HashSet<>(); |
|
|
|
|
|
|
|
StaticApplicationContext context = new StaticApplicationContext(); |
|
|
|
|
|
|
|
ApplicationListener<MyEvent> listener = seenEvents::add; |
|
|
|
|
|
|
|
context.addApplicationListener(listener); |
|
|
|
|
|
|
|
context.refresh(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MyEvent event1 = new MyEvent(context); |
|
|
|
|
|
|
|
context.publishEvent(event1); |
|
|
|
|
|
|
|
context.publishEvent(new MyOtherEvent(context)); |
|
|
|
|
|
|
|
MyEvent event2 = new MyEvent(context); |
|
|
|
|
|
|
|
context.publishEvent(event2); |
|
|
|
|
|
|
|
assertSame(2, seenEvents.size()); |
|
|
|
|
|
|
|
assertTrue(seenEvents.contains(event1)); |
|
|
|
|
|
|
|
assertTrue(seenEvents.contains(event2)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void beanPostProcessorPublishesEvents() { |
|
|
|
public void beanPostProcessorPublishesEvents() { |
|
|
|
GenericApplicationContext context = new GenericApplicationContext(); |
|
|
|
GenericApplicationContext context = new GenericApplicationContext(); |
|
|
|
@ -415,7 +459,7 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen |
|
|
|
|
|
|
|
|
|
|
|
public static class MyOrderedListener1 implements ApplicationListener<ApplicationEvent>, Ordered { |
|
|
|
public static class MyOrderedListener1 implements ApplicationListener<ApplicationEvent>, Ordered { |
|
|
|
|
|
|
|
|
|
|
|
public final Set<ApplicationEvent> seenEvents = new HashSet<ApplicationEvent>(); |
|
|
|
public final Set<ApplicationEvent> seenEvents = new HashSet<>(); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onApplicationEvent(ApplicationEvent event) { |
|
|
|
public void onApplicationEvent(ApplicationEvent event) { |
|
|
|
@ -452,14 +496,14 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onApplicationEvent(MyEvent event) { |
|
|
|
public void onApplicationEvent(MyEvent event) { |
|
|
|
assertTrue(otherListener.seenEvents.contains(event)); |
|
|
|
assertTrue(this.otherListener.seenEvents.contains(event)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class MyPayloadListener implements ApplicationListener<PayloadApplicationEvent> { |
|
|
|
public static class MyPayloadListener implements ApplicationListener<PayloadApplicationEvent> { |
|
|
|
|
|
|
|
|
|
|
|
public final Set<Object> seenPayloads = new HashSet<Object>(); |
|
|
|
public final Set<Object> seenPayloads = new HashSet<>(); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onApplicationEvent(PayloadApplicationEvent event) { |
|
|
|
public void onApplicationEvent(PayloadApplicationEvent event) { |
|
|
|
@ -470,7 +514,7 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen |
|
|
|
|
|
|
|
|
|
|
|
public static class MyNonSingletonListener implements ApplicationListener<ApplicationEvent> { |
|
|
|
public static class MyNonSingletonListener implements ApplicationListener<ApplicationEvent> { |
|
|
|
|
|
|
|
|
|
|
|
public static final Set<ApplicationEvent> seenEvents = new HashSet<ApplicationEvent>(); |
|
|
|
public static final Set<ApplicationEvent> seenEvents = new HashSet<>(); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onApplicationEvent(ApplicationEvent event) { |
|
|
|
public void onApplicationEvent(ApplicationEvent event) { |
|
|
|
@ -482,7 +526,7 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen |
|
|
|
@Order(5) |
|
|
|
@Order(5) |
|
|
|
public static class MyOrderedListener3 implements ApplicationListener<ApplicationEvent> { |
|
|
|
public static class MyOrderedListener3 implements ApplicationListener<ApplicationEvent> { |
|
|
|
|
|
|
|
|
|
|
|
public final Set<ApplicationEvent> seenEvents = new HashSet<ApplicationEvent>(); |
|
|
|
public final Set<ApplicationEvent> seenEvents = new HashSet<>(); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onApplicationEvent(ApplicationEvent event) { |
|
|
|
public void onApplicationEvent(ApplicationEvent event) { |
|
|
|
@ -503,7 +547,7 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onApplicationEvent(MyEvent event) { |
|
|
|
public void onApplicationEvent(MyEvent event) { |
|
|
|
assertTrue(otherListener.seenEvents.contains(event)); |
|
|
|
assertTrue(this.otherListener.seenEvents.contains(event)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|