1   /*
2    * Copyright 2002-2005 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.test;
17  
18  import junit.framework.TestCase;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.springframework.context.ConfigurableApplicationContext;
23  import org.springframework.context.support.StaticApplicationContext;
24  import org.springframework.context.support.StaticMessageSource;
25  import org.springframework.richclient.application.Application;
26  import org.springframework.richclient.application.ApplicationServicesLocator;
27  import org.springframework.richclient.application.config.ApplicationLifecycleAdvisor;
28  import org.springframework.richclient.application.config.DefaultApplicationLifecycleAdvisor;
29  import org.springframework.richclient.application.support.DefaultApplicationServices;
30  
31  /**
32   * Convenient base implementation for Spring Rich test cases; automatically configures the
33   * application services singleton and provides hooks for common test case setup
34   * requirements.
35   * 
36   * @author Oliver Hutchison
37   */
38  public abstract class SpringRichTestCase extends TestCase {
39  
40      /**
41       * Logger available to subclasses.
42       */
43      protected final Log logger = LogFactory.getLog(getClass());
44  
45      protected DefaultApplicationServices applicationServices;
46  
47      protected final void setUp() throws Exception {
48          try {
49              Application.load(null);
50              ConfigurableApplicationContext applicationContext = createApplicationContext();
51              applicationServices = new DefaultApplicationServices(applicationContext);
52              new ApplicationServicesLocator(applicationServices);
53  
54              final ApplicationLifecycleAdvisor advisor = createApplicationLifecycleAdvisor();
55              final Application application = new Application(advisor);
56              advisor.setApplication(application);
57              
58              Application.instance().setApplicationContext(applicationContext);
59              applicationServices.setApplicationContext(applicationContext);
60  
61              registerBasicServices(applicationServices);
62              registerAdditionalServices(applicationServices);
63  
64              applicationContext.refresh();
65              doSetUp();
66          } catch( Exception e ) {
67              Application.load(null);
68              throw e;
69          }
70      }
71  
72  	/**
73       * returns the application context to use for testing
74       * 
75       * overwrite to specify a different application context
76       * 
77       * @return this implementation returns an instance of StaticApplicationContext
78       */
79      protected ConfigurableApplicationContext createApplicationContext() {
80          return new StaticApplicationContext();
81      }
82  
83      /**
84       * Subclasses may override this to return a custom
85       * ApplicationLifecycleAdvisor.
86       */
87      protected ApplicationLifecycleAdvisor createApplicationLifecycleAdvisor() {
88          return new DefaultApplicationLifecycleAdvisor();
89      }
90      
91      
92  
93      /**
94       * Register the application services needed for our tests.
95       */
96      protected void registerBasicServices( DefaultApplicationServices applicationServices ) {
97          applicationServices.setMessageSource(new StaticMessageSource());
98      }
99  
100     /**
101      * May be implemented in subclasses that need to register services with the global
102      * application services instance.
103      */
104     protected void registerAdditionalServices( DefaultApplicationServices applicationServices ) {
105     }
106 
107     /**
108      * Get the application services instance.
109      */
110     protected DefaultApplicationServices getApplicationServices() {
111         return applicationServices;
112     }
113 
114     /**
115      * Tear down method invoked by JUnit framework. Derived classes should put their work
116      * in {@link #doTearDown()}.
117      */
118     protected final void tearDown() throws Exception {
119         try {
120             doTearDown();
121         } finally {
122             Application.load(null);
123         }
124     }
125 
126     /**
127      * Should be implemented in subclasses as an alternative to the final method
128      * {@link #setUp()}
129      * 
130      * @throws Exception
131      */
132     protected void doSetUp() throws Exception {
133     }
134 
135     /**
136      * Should be implemented in subclasses as an alternative to the final method
137      * {@link #tearDown()}
138      */
139     protected void doTearDown() throws Exception {
140     }
141 }