1   /*
2    * Copyright 2002-2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.config;
17  
18  import junit.framework.TestCase;
19  
20  import org.easymock.EasyMock;
21  import org.springframework.beans.factory.BeanFactory;
22  import org.springframework.context.ApplicationEvent;
23  import org.springframework.context.ApplicationListener;
24  import org.springframework.context.event.SimpleApplicationEventMulticaster;
25  import org.springframework.context.support.StaticApplicationContext;
26  import org.springframework.richclient.application.Application;
27  import org.springframework.richclient.application.ApplicationWindow;
28  import org.springframework.richclient.application.config.DefaultApplicationLifecycleAdvisor;
29  
30  /**
31   * Test cases for {@link DefaultApplicationLifecycleAdvisor}
32   */
33  public class DefaultApplicationLifecycleAdvisorTests extends TestCase {
34  
35      public void testApplicationEventNotification() throws Exception {
36          TestAdvisor advisor = new TestAdvisor();
37          advisor.afterPropertiesSet();
38          Application.load(null);
39          new Application(advisor);
40          StaticApplicationContext applicationContext = new StaticApplicationContext();
41          Application.instance().setApplicationContext(applicationContext);
42          applicationContext.registerSingleton( "eventMulticaster", SimpleApplicationEventMulticaster.class );
43          applicationContext.refresh();
44  
45          advisor.setWindowCommandBarDefinitions(
46              "org/springframework/richclient/config/app-lifecycle-test-ctx.xml");
47  
48          // Fire up test.....
49          advisor.testInit();
50  
51          TestCommand cmd = (TestCommand)advisor.getBeanFactory().getBean("testCommand");
52          assertEquals("Command must be notified of refresh", 1, cmd.getCount());
53  
54          advisor.onApplicationEvent(new ApplicationEvent(this) {});
55          assertEquals("Command must be notified", 2, cmd.getCount());
56      }
57  
58      // ===============================================================================
59      // Helper classes:
60  
61      public static class TestAdvisor extends DefaultApplicationLifecycleAdvisor {
62          public TestAdvisor() {
63              setStartingPageId("whatever");
64              ApplicationWindow window = (ApplicationWindow) EasyMock.createMock(ApplicationWindow.class);
65              setOpeningWindow(window);
66          }
67          
68  
69          public void testInit() {
70              initNewWindowCommandBarFactory();
71          }
72  
73          public BeanFactory getBeanFactory() {
74              return getCommandBarFactory();
75          }
76          
77          
78      }
79  
80      public static class TestCommand implements ApplicationListener {
81  
82          int count = 0;
83  
84          public void onApplicationEvent(ApplicationEvent event) {
85              count++;
86          }
87  
88          public int getCount() {
89              return count;
90          }
91      }
92  }