|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.context.event; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import org.springframework.context.ApplicationContext; |
| 25 | +import org.springframework.context.PayloadApplicationEvent; |
| 26 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 27 | +import org.springframework.stereotype.Component; |
| 28 | + |
| 29 | +import static org.junit.Assert.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Juergen Hoeller |
| 33 | + */ |
| 34 | +public class PayloadApplicationEventTests { |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testEventClassWithInterface() { |
| 38 | + ApplicationContext ac = new AnnotationConfigApplicationContext(Listener.class); |
| 39 | + MyEventClass event = new MyEventClass<>(this, "xyz"); |
| 40 | + ac.publishEvent(event); |
| 41 | + assertTrue(ac.getBean(Listener.class).events.contains(event)); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public interface Auditable { |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public static class MyEventClass<GT> extends PayloadApplicationEvent<GT> implements Auditable { |
| 50 | + |
| 51 | + public MyEventClass(Object source, GT payload) { |
| 52 | + super(source, payload); |
| 53 | + } |
| 54 | + |
| 55 | + public String toString() { |
| 56 | + return "Payload: " + getPayload(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + @Component |
| 62 | + public static class Listener { |
| 63 | + |
| 64 | + public final List<Auditable> events = new ArrayList<>(); |
| 65 | + |
| 66 | + @EventListener |
| 67 | + public void onEvent(Auditable event) { |
| 68 | + events.add(event); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments