001 /* 002 * Copyright 2002-2005 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.test; 017 018 import junit.framework.TestCase; 019 020 import org.apache.commons.logging.Log; 021 import org.apache.commons.logging.LogFactory; 022 import org.springframework.context.ConfigurableApplicationContext; 023 import org.springframework.context.support.StaticApplicationContext; 024 import org.springframework.context.support.StaticMessageSource; 025 import org.springframework.richclient.application.Application; 026 import org.springframework.richclient.application.ApplicationServicesLocator; 027 import org.springframework.richclient.application.config.ApplicationLifecycleAdvisor; 028 import org.springframework.richclient.application.config.DefaultApplicationLifecycleAdvisor; 029 import org.springframework.richclient.application.support.DefaultApplicationServices; 030 031 /** 032 * Convenient base implementation for Spring Rich test cases; automatically configures the 033 * application services singleton and provides hooks for common test case setup 034 * requirements. 035 * 036 * @author Oliver Hutchison 037 */ 038 public abstract class SpringRichTestCase extends TestCase { 039 040 /** 041 * Logger available to subclasses. 042 */ 043 protected final Log logger = LogFactory.getLog(getClass()); 044 045 protected DefaultApplicationServices applicationServices; 046 047 protected final void setUp() throws Exception { 048 try { 049 Application.load(null); 050 ConfigurableApplicationContext applicationContext = createApplicationContext(); 051 applicationServices = new DefaultApplicationServices(applicationContext); 052 new ApplicationServicesLocator(applicationServices); 053 054 final ApplicationLifecycleAdvisor advisor = createApplicationLifecycleAdvisor(); 055 final Application application = new Application(advisor); 056 advisor.setApplication(application); 057 058 Application.instance().setApplicationContext(applicationContext); 059 applicationServices.setApplicationContext(applicationContext); 060 061 registerBasicServices(applicationServices); 062 registerAdditionalServices(applicationServices); 063 064 applicationContext.refresh(); 065 doSetUp(); 066 } catch( Exception e ) { 067 Application.load(null); 068 throw e; 069 } 070 } 071 072 /** 073 * returns the application context to use for testing 074 * 075 * overwrite to specify a different application context 076 * 077 * @return this implementation returns an instance of StaticApplicationContext 078 */ 079 protected ConfigurableApplicationContext createApplicationContext() { 080 return new StaticApplicationContext(); 081 } 082 083 /** 084 * Subclasses may override this to return a custom 085 * ApplicationLifecycleAdvisor. 086 */ 087 protected ApplicationLifecycleAdvisor createApplicationLifecycleAdvisor() { 088 return new DefaultApplicationLifecycleAdvisor(); 089 } 090 091 092 093 /** 094 * Register the application services needed for our tests. 095 */ 096 protected void registerBasicServices( DefaultApplicationServices applicationServices ) { 097 applicationServices.setMessageSource(new StaticMessageSource()); 098 } 099 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 }