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.application;
17  
18  import java.util.Locale;
19  
20  import junit.framework.TestCase;
21  
22  import org.easymock.EasyMock;
23  import org.springframework.context.support.StaticApplicationContext;
24  import org.springframework.context.support.StaticMessageSource;
25  import org.springframework.richclient.progress.NullProgressMonitor;
26  import org.springframework.richclient.progress.ProgressMonitor;
27  
28  /**
29   * This class provides a suite of unit tests for the
30   * {@link ProgressMonitoringBeanFactoryPostProcessor}.
31   * 
32   * @author Kevin Stembridge
33   * @since 0.3.0
34   * 
35   */
36  public class ProgressMonitoringBeanFactoryPostProcessorTests extends TestCase {
37  
38  	/**
39  	 * Confirms that the post-processor's constructor throws an
40  	 * IllegalArgumentException if a ProgressMonitor is not provided, but allows
41  	 * a null MessageSource.
42  	 */
43  	public void testConstructor() {
44  
45  		try {
46  			new ProgressMonitoringBeanFactoryPostProcessor(null, null);
47  			fail("Should have thrown an IllegalArgumentException");
48  		}
49  		catch (IllegalArgumentException e) {
50  			// do nothing, test succeeded
51  		}
52  
53  		new ProgressMonitoringBeanFactoryPostProcessor(new NullProgressMonitor(), null);
54  
55  	}
56  
57  	/**
58  	 * Confirms that the post processor correctly notifies a given progress
59  	 * monitor as the bean factory is loaded, providing the expected localized
60  	 * messages. The following assertions are made:
61  	 * 
62  	 * <ul>
63  	 * <li>The {@link ProgressMonitor#taskStarted(String, int)} method is
64  	 * called exactly once with a localized message, provided by the key
65  	 * {@link ProgressMonitoringBeanPostProcessor#LOADING_APP_CONTEXT_KEY}, and
66  	 * the number of singleton beans in the bean factory.</li>
67  	 * <li>The {@link ProgressMonitor#subTaskStarted(String)} method is called,
68  	 * with the localized message provided by
69  	 * {@link ProgressMonitoringBeanPostProcessor#LOADING_BEAN_KEY}, for each
70  	 * singleton bean defined in the bean factory being loaded.</li>
71  	 * <li>The {@link ProgressMonitor#worked(int)} method is called with the
72  	 * argument '1' the same number of times as there are singleton beans
73  	 * defined in the bean factory.</li>
74  	 * </ul>
75  	 */
76  	public void testLoadingBeansWithMessageSource() {
77  		String loadingAppCtxMessage = "Loading Application Context Message Test";
78  		int expectedSingletonBeanCount = 2;
79  		String beanName1 = "beanName1";
80  		String beanName2 = "beanName2";
81  		String beanName3 = "beanName3";
82  		String loadingBeanMessage = "LoadBeanTestMessage {0}";
83  		String expectedLoadBean1Message = "LoadBeanTestMessage beanName1";
84  		String expectedLoadBean2Message = "LoadBeanTestMessage beanName2";
85  
86  		StaticApplicationContext appCtx = new StaticApplicationContext();
87  		appCtx.registerSingleton(beanName1, Object.class);
88  		appCtx.registerSingleton(beanName2, Object.class);
89  		appCtx.registerPrototype(beanName3, Object.class);
90  
91  		StaticMessageSource messageSource = new StaticMessageSource();
92  
93  		messageSource.addMessage(ProgressMonitoringBeanFactoryPostProcessor.LOADING_APP_CONTEXT_KEY, Locale
94  				.getDefault(), loadingAppCtxMessage);
95  
96  		messageSource.addMessage(ProgressMonitoringBeanFactoryPostProcessor.LOADING_BEAN_KEY, Locale.getDefault(),
97  				loadingBeanMessage);
98  
99  		ProgressMonitor mockProgressMonitor = (ProgressMonitor) EasyMock.createStrictMock(ProgressMonitor.class);
100 		mockProgressMonitor.taskStarted(loadingAppCtxMessage, expectedSingletonBeanCount);
101 		mockProgressMonitor.subTaskStarted(expectedLoadBean1Message);
102 		mockProgressMonitor.worked(1);
103 		mockProgressMonitor.subTaskStarted(expectedLoadBean2Message);
104 		mockProgressMonitor.worked(1);
105 		EasyMock.replay(mockProgressMonitor);
106 
107 		ProgressMonitoringBeanFactoryPostProcessor processor = new ProgressMonitoringBeanFactoryPostProcessor(
108 				mockProgressMonitor, messageSource);
109 
110 		appCtx.addBeanFactoryPostProcessor(processor);
111 
112 		appCtx.refresh();
113 
114 		EasyMock.verify(mockProgressMonitor);
115 	}
116 
117 	/**
118 	 * Confirms that the post processor correctly notifies a given progress
119 	 * monitor as the bean factory is loaded. The following assertions are made:
120 	 * 
121 	 * <ul>
122 	 * <li>The {@link ProgressMonitor#taskStarted(String, int)} method is
123 	 * called exactly once with any message and the number of singleton beans in
124 	 * the bean factory.</li>
125 	 * <li>The {@link ProgressMonitor#subTaskStarted(String)} method is called,
126 	 * with the localized message provided by
127 	 * {@link ProgressMonitoringBeanPostProcessor#LOADING_BEAN_KEY}, for each
128 	 * singleton bean defined in the bean factory being loaded.</li>
129 	 * <li>The {@link ProgressMonitor#worked(int)} method is called with the
130 	 * argument '1' the same number of times as there are singleton beans
131 	 * defined in the bean factory.</li>
132 	 * </ul>
133 	 */
134 	public void testLoadingBeansWithoutMessageSource() {
135 		int expectedSingletonBeanCount = 2;
136 		String beanName1 = "beanName1";
137 		String beanName2 = "beanName2";
138 		String beanName3 = "beanName3";
139 
140 		StaticApplicationContext appCtx = new StaticApplicationContext();
141 		appCtx.registerSingleton(beanName1, Object.class);
142 		appCtx.registerSingleton(beanName2, Object.class);
143 		appCtx.registerPrototype(beanName3, Object.class);
144 
145 		ProgressMonitor mockProgressMonitor = (ProgressMonitor) EasyMock.createStrictMock(ProgressMonitor.class);
146 		mockProgressMonitor.taskStarted("Loading Application Context ...", expectedSingletonBeanCount);
147 		mockProgressMonitor.subTaskStarted("Loading " + beanName1 + " ...");
148 		mockProgressMonitor.worked(1);
149 		mockProgressMonitor.subTaskStarted("Loading " + beanName2 + " ...");
150 		mockProgressMonitor.worked(1);
151 		EasyMock.replay(mockProgressMonitor);
152 
153 		ProgressMonitoringBeanFactoryPostProcessor processor = new ProgressMonitoringBeanFactoryPostProcessor(
154 				mockProgressMonitor, null);
155 		appCtx.addBeanFactoryPostProcessor(processor);
156 
157 		appCtx.refresh();
158 
159 		EasyMock.verify(mockProgressMonitor);
160 	}
161 
162 }