1 package org.apache.maven.shared.release;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.fail;
23
24 import java.io.File;
25 import java.io.InputStream;
26
27 import org.codehaus.plexus.ContainerConfiguration;
28 import org.codehaus.plexus.DefaultContainerConfiguration;
29 import org.codehaus.plexus.DefaultPlexusContainer;
30 import org.codehaus.plexus.PlexusContainer;
31 import org.codehaus.plexus.PlexusContainerException;
32 import org.codehaus.plexus.configuration.PlexusConfiguration;
33 import org.codehaus.plexus.context.Context;
34 import org.codehaus.plexus.context.DefaultContext;
35 import org.junit.After;
36 import org.junit.Before;
37
38
39
40
41
42
43 public abstract class PlexusJUnit4TestCase
44 {
45 private PlexusContainer container;
46
47 private static String basedir;
48
49 @Before
50 public void setUp()
51 throws Exception
52 {
53 basedir = getBasedir();
54 }
55
56 protected void setupContainer()
57 {
58
59
60
61
62 final DefaultContext context = new DefaultContext();
63
64 context.put( "basedir", getBasedir() );
65
66 customizeContext( context );
67
68 final boolean hasPlexusHome = context.contains( "plexus.home" );
69
70 if ( !hasPlexusHome )
71 {
72 final File f = getTestFile( "target/plexus-home" );
73
74 if ( !f.isDirectory() )
75 {
76 f.mkdir();
77 }
78
79 context.put( "plexus.home", f.getAbsolutePath() );
80 }
81
82
83
84
85
86 final String config = getCustomConfigurationName();
87
88 final ContainerConfiguration containerConfiguration =
89 new DefaultContainerConfiguration().setName( "test" ).setContext( context.getContextData() ).setClassPathCaching( true );
90
91 if ( config != null )
92 {
93 containerConfiguration.setContainerConfiguration( config );
94 }
95 else
96 {
97 final String resource = getConfigurationName( null );
98
99 containerConfiguration.setContainerConfiguration( resource );
100 }
101
102 customizeContainerConfiguration( containerConfiguration );
103
104 try
105 {
106 container = new DefaultPlexusContainer( containerConfiguration );
107 }
108 catch ( final PlexusContainerException e )
109 {
110 e.printStackTrace();
111 fail( "Failed to create plexus container." );
112 }
113 }
114
115
116
117
118
119
120 protected void customizeContainerConfiguration( final ContainerConfiguration containerConfiguration )
121 {
122 }
123
124 protected void customizeContext( final Context context )
125 {
126 }
127
128 protected PlexusConfiguration customizeComponentConfiguration()
129 {
130 return null;
131 }
132
133 @After
134 public void tearDown()
135 throws Exception
136 {
137 if ( container != null )
138 {
139 container.dispose();
140
141 container = null;
142 }
143 }
144
145 protected PlexusContainer getContainer()
146 {
147 if ( container == null )
148 {
149 setupContainer();
150 }
151
152 return container;
153 }
154
155 protected InputStream getConfiguration()
156 throws Exception
157 {
158 return getConfiguration( null );
159 }
160
161 @SuppressWarnings( "unused" )
162 protected InputStream getConfiguration( final String subname )
163 throws Exception
164 {
165 return getResourceAsStream( getConfigurationName( subname ) );
166 }
167
168 protected String getCustomConfigurationName()
169 {
170 return null;
171 }
172
173
174
175
176
177
178
179
180
181 protected String getConfigurationName( final String subname )
182 {
183 return getClass().getName().replace( '.', '/' ) + ".xml";
184 }
185
186 protected InputStream getResourceAsStream( final String resource )
187 {
188 return getClass().getResourceAsStream( resource );
189 }
190
191 protected ClassLoader getClassLoader()
192 {
193 return getClass().getClassLoader();
194 }
195
196
197
198
199
200 protected Object lookup( final String componentKey )
201 throws Exception
202 {
203 return getContainer().lookup( componentKey );
204 }
205
206 protected Object lookup( final String role, final String roleHint )
207 throws Exception
208 {
209 return getContainer().lookup( role, roleHint );
210 }
211
212 protected <T> T lookup( final Class<T> componentClass )
213 throws Exception
214 {
215 return getContainer().lookup( componentClass );
216 }
217
218 protected <T> T lookup( final Class<T> componentClass, final String roleHint )
219 throws Exception
220 {
221 return getContainer().lookup( componentClass, roleHint );
222 }
223
224 protected void release( final Object component )
225 throws Exception
226 {
227 getContainer().release( component );
228 }
229
230
231
232
233
234 public static File getTestFile( final String path )
235 {
236 return new File( getBasedir(), path );
237 }
238
239 @SuppressWarnings( "hiding" )
240 public static File getTestFile( final String basedir, final String path )
241 {
242 File basedirFile = new File( basedir );
243
244 if ( !basedirFile.isAbsolute() )
245 {
246 basedirFile = getTestFile( basedir );
247 }
248
249 return new File( basedirFile, path );
250 }
251
252 public static String getTestPath( final String path )
253 {
254 return getTestFile( path ).getAbsolutePath();
255 }
256
257 @SuppressWarnings( "hiding" )
258 public static String getTestPath( final String basedir, final String path )
259 {
260 return getTestFile( basedir, path ).getAbsolutePath();
261 }
262
263 public static String getBasedir()
264 {
265 if ( basedir != null )
266 {
267 return basedir;
268 }
269
270 basedir = System.getProperty( "basedir" );
271
272 if ( basedir == null )
273 {
274 basedir = new File( "" ).getAbsolutePath();
275 }
276
277 return basedir;
278 }
279
280 public String getTestConfiguration()
281 {
282 return getTestConfiguration( getClass() );
283 }
284
285 public static String getTestConfiguration( final Class<?> clazz )
286 {
287 final String s = clazz.getName().replace( '.', '/' );
288
289 return s.substring( 0, s.indexOf( "$" ) ) + ".xml";
290 }
291 }