001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.config;
017    
018    import junit.framework.TestCase;
019    
020    import org.easymock.EasyMock;
021    import org.springframework.beans.factory.BeanFactory;
022    import org.springframework.context.ApplicationEvent;
023    import org.springframework.context.ApplicationListener;
024    import org.springframework.context.event.SimpleApplicationEventMulticaster;
025    import org.springframework.context.support.StaticApplicationContext;
026    import org.springframework.richclient.application.Application;
027    import org.springframework.richclient.application.ApplicationWindow;
028    import org.springframework.richclient.application.config.DefaultApplicationLifecycleAdvisor;
029    
030    /**
031     * Test cases for {@link DefaultApplicationLifecycleAdvisor}
032     */
033    public class DefaultApplicationLifecycleAdvisorTests extends TestCase {
034    
035        public void testApplicationEventNotification() throws Exception {
036            TestAdvisor advisor = new TestAdvisor();
037            advisor.afterPropertiesSet();
038            Application.load(null);
039            new Application(advisor);
040            StaticApplicationContext applicationContext = new StaticApplicationContext();
041            Application.instance().setApplicationContext(applicationContext);
042            applicationContext.registerSingleton( "eventMulticaster", SimpleApplicationEventMulticaster.class );
043            applicationContext.refresh();
044    
045            advisor.setWindowCommandBarDefinitions(
046                "org/springframework/richclient/config/app-lifecycle-test-ctx.xml");
047    
048            // Fire up test.....
049            advisor.testInit();
050    
051            TestCommand cmd = (TestCommand)advisor.getBeanFactory().getBean("testCommand");
052            assertEquals("Command must be notified of refresh", 1, cmd.getCount());
053    
054            advisor.onApplicationEvent(new ApplicationEvent(this) {});
055            assertEquals("Command must be notified", 2, cmd.getCount());
056        }
057    
058        // ===============================================================================
059        // Helper classes:
060    
061        public static class TestAdvisor extends DefaultApplicationLifecycleAdvisor {
062            public TestAdvisor() {
063                setStartingPageId("whatever");
064                ApplicationWindow window = (ApplicationWindow) EasyMock.createMock(ApplicationWindow.class);
065                setOpeningWindow(window);
066            }
067            
068    
069            public void testInit() {
070                initNewWindowCommandBarFactory();
071            }
072    
073            public BeanFactory getBeanFactory() {
074                return getCommandBarFactory();
075            }
076            
077            
078        }
079    
080        public static class TestCommand implements ApplicationListener {
081    
082            int count = 0;
083    
084            public void onApplicationEvent(ApplicationEvent event) {
085                count++;
086            }
087    
088            public int getCount() {
089                return count;
090            }
091        }
092    }