content: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.alibaba.spring.context.event;
  
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.BeansException;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  import org.springframework.context.ApplicationEvent;
  import org.springframework.context.ApplicationEventPublisher;
  import org.springframework.context.ApplicationEventPublisherAware;
  import org.springframework.context.ApplicationListener;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.test.context.support.AnnotationConfigContextLoader;
  
  import static org.junit.Assert.assertEquals;
  
  /**
   * {@link DeferredApplicationEventPublisher} Test
   *
   * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
   * @since 1.0.4
   */
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(
          classes = TestConfig.class,
          loader = AnnotationConfigContextLoader.class
  )
  public class DeferredApplicationEventPublisherTest {
  
      @Autowired
      private TestConfig testConfig;
  
      @Test
      public void test() {
          assertEquals("Hello,World", testConfig.getTestEvent().getSource());
      }
  
  }
  
  class TestEvent extends ApplicationEvent {
  
      public TestEvent(String source) {
          super(source);
      }
  }
  
  class TestConfig implements BeanFactoryPostProcessor, ApplicationEventPublisherAware, ApplicationListener<TestEvent> {
  
      private TestEvent testEvent;
  
      private ApplicationEventPublisher applicationEventPublisher;
  
      @Override
      public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
          this.applicationEventPublisher = new DeferredApplicationEventPublisher(applicationEventPublisher);
      }
  
      @Override
      public void onApplicationEvent(TestEvent event) {
          testEvent = event;
      }
  
      public TestEvent getTestEvent() {
          return testEvent;
      }
  
      @Override
      public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
          applicationEventPublisher.publishEvent(new TestEvent("Hello,World"));
      }
  }