1 | package com.ether; |
---|
2 | |
---|
3 | import org.apache.commons.logging.Log; |
---|
4 | import org.apache.commons.logging.LogFactory; |
---|
5 | import org.junit.After; |
---|
6 | import org.junit.AfterClass; |
---|
7 | import org.junit.Assert; |
---|
8 | import org.junit.Before; |
---|
9 | import org.springframework.context.ApplicationContext; |
---|
10 | import org.springframework.context.support.AbstractApplicationContext; |
---|
11 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
---|
12 | |
---|
13 | public abstract class TestHelper<BEAN> |
---|
14 | { |
---|
15 | protected TestHelper(final String beanContextName, final String beanName, final String[] otherContextNames) { |
---|
16 | _beanContextName = beanContextName; |
---|
17 | _beanName = beanName; |
---|
18 | _otherContextNames = otherContextNames; |
---|
19 | } |
---|
20 | |
---|
21 | protected abstract void beforeBeanTest() throws Exception; |
---|
22 | |
---|
23 | protected abstract void afterBeanTest() throws Exception; |
---|
24 | |
---|
25 | @Before |
---|
26 | public final void beforeTest() throws Exception |
---|
27 | { |
---|
28 | Assert.assertNotNull(getApplicationContext()); |
---|
29 | Assert.assertNotNull(getBean()); |
---|
30 | beforeBeanTest(); |
---|
31 | } |
---|
32 | |
---|
33 | @After |
---|
34 | public final void afterTest() throws Exception |
---|
35 | { |
---|
36 | afterBeanTest(); |
---|
37 | _bean = null; |
---|
38 | } |
---|
39 | |
---|
40 | @AfterClass |
---|
41 | public static void afterAllTest() throws Exception |
---|
42 | { |
---|
43 | try |
---|
44 | { |
---|
45 | if (_applicationContext != null) _applicationContext.close(); |
---|
46 | } |
---|
47 | finally |
---|
48 | { |
---|
49 | _applicationContext = null; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Builds context names from 2 context name arrays. |
---|
55 | * |
---|
56 | * @param baseContextNames : the base context names. |
---|
57 | * @param otherContextNames : the other context names. |
---|
58 | * @return a context name array. |
---|
59 | */ |
---|
60 | protected static String[] buildContextNames( |
---|
61 | final String[] baseContextNames, final String... otherContextNames) |
---|
62 | { |
---|
63 | int contextNameSize = baseContextNames == null ? 0 : baseContextNames.length; |
---|
64 | contextNameSize += otherContextNames == null ? 0 : otherContextNames.length; |
---|
65 | |
---|
66 | final String[] contextNames = new String[contextNameSize]; |
---|
67 | |
---|
68 | if (baseContextNames != null) |
---|
69 | System.arraycopy(baseContextNames, 0, contextNames, 0, baseContextNames.length); |
---|
70 | |
---|
71 | if (otherContextNames != null) |
---|
72 | { |
---|
73 | if (baseContextNames != null) |
---|
74 | System.arraycopy(otherContextNames, 0, contextNames, baseContextNames.length, otherContextNames.length); |
---|
75 | else |
---|
76 | System.arraycopy(otherContextNames, 0, contextNames, 0, otherContextNames.length); |
---|
77 | } |
---|
78 | |
---|
79 | return contextNames; |
---|
80 | } |
---|
81 | |
---|
82 | protected final ApplicationContext getApplicationContext() |
---|
83 | { |
---|
84 | if (_applicationContext == null) |
---|
85 | { |
---|
86 | final String[] contextNames; |
---|
87 | |
---|
88 | if (_otherContextNames != null) |
---|
89 | { |
---|
90 | if (_beanContextName != null) |
---|
91 | contextNames = buildContextNames(_otherContextNames, _beanContextName); |
---|
92 | else |
---|
93 | contextNames = buildContextNames(_otherContextNames); |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | contextNames = new String[] { _beanContextName }; |
---|
98 | } |
---|
99 | |
---|
100 | _applicationContext = new ClassPathXmlApplicationContext(contextNames); |
---|
101 | } |
---|
102 | |
---|
103 | return _applicationContext; |
---|
104 | } |
---|
105 | |
---|
106 | @SuppressWarnings({ "unchecked" }) |
---|
107 | protected final BEAN getBean() |
---|
108 | { |
---|
109 | if (_bean == null) |
---|
110 | _bean = (BEAN) getApplicationContext().getBean(_beanName); |
---|
111 | |
---|
112 | return _bean; |
---|
113 | } |
---|
114 | |
---|
115 | private static final Log LOGGER = LogFactory.getLog( TestHelper.class ); |
---|
116 | |
---|
117 | private static AbstractApplicationContext _applicationContext; |
---|
118 | |
---|
119 | private String _beanContextName; |
---|
120 | private String _beanName; |
---|
121 | private String[] _otherContextNames; |
---|
122 | private BEAN _bean; |
---|
123 | } |
---|