KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > test > AbstractSpringContextTests


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.test;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.springframework.context.ConfigurableApplicationContext;
23 import org.springframework.util.Assert;
24 import org.springframework.util.ObjectUtils;
25
26 /**
27  * Superclass for JUnit test cases using Spring
28  * {@link org.springframework.context.ApplicationContext ApplicationContexts}.
29  *
30  * <p>Maintains a static cache of contexts by key. This has significant performance
31  * benefit if initializing the context would take time. While initializing a
32  * Spring context itself is very quick, some beans in a context, such as
33  * a LocalSessionFactoryBean for working with Hibernate, may take some time
34  * to initialize. Hence it often makes sense to do that initializing once.
35  *
36  * <p>Any ApplicationContext created by this class will be asked to register a JVM
37  * shutdown hook for itself. Unless the context gets closed early, all context
38  * instances will be automatically closed on JVM shutdown. This allows for freeing
39  * external resources held by beans within the context, e.g. temporary files.
40  *
41  * <p>Normally you won't extend this class directly but rather extend one of
42  * its subclasses.
43  *
44  * @author Rod Johnson
45  * @author Juergen Hoeller
46  * @since 1.1.1
47  * @see AbstractDependencyInjectionSpringContextTests
48  * @see AbstractTransactionalSpringContextTests
49  * @see AbstractTransactionalDataSourceSpringContextTests
50  */

51 public abstract class AbstractSpringContextTests extends ConditionalTestCase {
52
53     /**
54      * Map of context keys returned by subclasses of this class, to
55      * Spring contexts. This needs to be static, as JUnit tests are
56      * destroyed and recreated between running individual test methods.
57      */

58     private static Map JavaDoc contextKeyToContextMap = new HashMap JavaDoc();
59
60
61     /**
62      * Default constructor for AbstractSpringContextTests.
63      */

64     public AbstractSpringContextTests() {
65     }
66
67     /**
68      * Constructor for AbstractSpringContextTests with a JUnit name.
69      */

70     public AbstractSpringContextTests(String JavaDoc name) {
71         super(name);
72     }
73
74
75     /**
76      * Explicitly add an ApplicationContext instance under a given key.
77      * <p>This is not meant to be used by subclasses. It is rather exposed
78      * for special test suite environments.
79      * @param key the context key
80      * @param context the ApplicationContext instance
81      */

82     public final void addContext(Object JavaDoc key, ConfigurableApplicationContext context) {
83         Assert.notNull(context, "ApplicationContext must not be null");
84         contextKeyToContextMap.put(contextKeyString(key), context);
85     }
86
87     /**
88      * Return whether there is a cached context for the given key.
89      * @param contextKey the context key
90      */

91     protected final boolean hasCachedContext(Object JavaDoc contextKey) {
92         return contextKeyToContextMap.containsKey(contextKey);
93     }
94
95     /**
96      * Obtain an ApplicationContext for the given key, potentially cached.
97      * @param key the context key
98      * @return the corresponding ApplicationContext instance (potentially cached)
99      */

100     protected final ConfigurableApplicationContext getContext(Object JavaDoc key) throws Exception JavaDoc {
101         String JavaDoc keyString = contextKeyString(key);
102         ConfigurableApplicationContext ctx =
103                 (ConfigurableApplicationContext) contextKeyToContextMap.get(keyString);
104         if (ctx == null) {
105             ctx = loadContext(key);
106             ctx.registerShutdownHook();
107             contextKeyToContextMap.put(keyString, ctx);
108         }
109         return ctx;
110     }
111
112     /**
113      * Mark the context with the given key as dirty. This will cause the
114      * cached context to be reloaded before the next test case is executed.
115      * <p>Call this method only if you change the state of a singleton
116      * bean, potentially affecting future tests.
117      */

118     protected final void setDirty(Object JavaDoc contextKey) {
119         String JavaDoc keyString = contextKeyString(contextKey);
120         ConfigurableApplicationContext ctx =
121                 (ConfigurableApplicationContext) contextKeyToContextMap.remove(keyString);
122         if (ctx != null) {
123             ctx.close();
124         }
125     }
126
127
128     /**
129      * Subclasses can override this to return a String representation of
130      * their context key for use in logging.
131      * @param contextKey the context key
132      */

133     protected String JavaDoc contextKeyString(Object JavaDoc contextKey) {
134         return ObjectUtils.nullSafeToString(contextKey);
135     }
136
137     /**
138      * Load a new ApplicationContext for the given key.
139      * <p>To be implemented by subclasses.
140      * @param key the context key
141      * @return the corresponding ApplicationContext instance (new)
142      */

143     protected abstract ConfigurableApplicationContext loadContext(Object JavaDoc key) throws Exception JavaDoc;
144
145 }
146
Popular Tags