KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > test > MetaInfoEntryTestCase


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.fortress.test;
19
20 import junit.framework.TestCase;
21 import org.apache.avalon.fortress.MetaInfoEntry;
22 import org.apache.avalon.fortress.RoleEntry;
23 import org.apache.avalon.fortress.util.Service;
24 import org.apache.avalon.fortress.impl.handler.FactoryComponentHandler;
25 import org.apache.avalon.fortress.impl.handler.PerThreadComponentHandler;
26 import org.apache.avalon.fortress.impl.handler.PoolableComponentHandler;
27 import org.apache.avalon.fortress.impl.handler.ThreadSafeComponentHandler;
28 import org.apache.avalon.fortress.test.data.BaseRole;
29 import org.apache.avalon.fortress.test.data.Component1;
30 import org.apache.avalon.fortress.test.data.Role1;
31 import org.apache.avalon.fortress.test.data.Role2;
32
33 import java.util.*;
34
35 /**
36  * MetaInfoEntryTestCase does tests the meta info entry class
37  *
38  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
39  * @version CVS Revision: 1.1 $
40  */

41 public class MetaInfoEntryTestCase extends TestCase
42 {
43     private Class JavaDoc m_componentClass;
44     private Properties m_properties;
45     private List m_dependencies;
46     private Map m_lifecycleMap;
47
48     public MetaInfoEntryTestCase( String JavaDoc name )
49     {
50         super( name );
51     }
52
53     public void setUp()
54     {
55         m_componentClass = Component1.class;
56         m_properties = new Properties();
57         m_properties.setProperty( "x-avalon.name", "component1" );
58         m_properties.setProperty( "x-avalon.lifestyle", "singleton" );
59
60         m_dependencies = new ArrayList();
61
62         Map lifecycleMap = new HashMap();
63         lifecycleMap.put( "singleton", ThreadSafeComponentHandler.class );
64         lifecycleMap.put( "thread", PerThreadComponentHandler.class );
65         lifecycleMap.put( "pooled", PoolableComponentHandler.class );
66         lifecycleMap.put( "transient", FactoryComponentHandler.class );
67
68         m_lifecycleMap = Collections.unmodifiableMap( lifecycleMap );
69     }
70
71     public void testFullySpecified() throws Exception JavaDoc
72     {
73         MetaInfoEntry entry = new MetaInfoEntry( m_componentClass, m_properties, m_dependencies );
74         checkMetaInfoEntry( entry, ThreadSafeComponentHandler.class, "component1", false );
75     }
76
77     public void testAutoDiscovery() throws Exception JavaDoc
78     {
79         m_properties.remove( "x-avalon.lifestyle" );
80         m_properties.remove( "x-avalon.name" );
81         m_properties.setProperty( "fortress.handler", ThreadSafeComponentHandler.class.getName() );
82         m_componentClass = MetaInfoEntry.class;
83
84         MetaInfoEntry entry = new MetaInfoEntry( m_componentClass, m_properties, m_dependencies );
85
86         checkMetaInfoEntry( entry, ThreadSafeComponentHandler.class, "meta-info-entry", false );
87     }
88
89     public void testLifestyleMarkers() throws Exception JavaDoc
90     {
91         String JavaDoc name = "component1";
92
93         Iterator it = m_lifecycleMap.keySet().iterator();
94         while ( it.hasNext() )
95         {
96             String JavaDoc type = (String JavaDoc) it.next();
97             m_properties.setProperty( "x-avalon.lifestyle", type );
98             MetaInfoEntry entry = new MetaInfoEntry( m_componentClass, m_properties, m_dependencies );
99             checkMetaInfoEntry( entry, (Class JavaDoc) m_lifecycleMap.get( type ), name, false );
100         }
101     }
102
103     public void testRoleEntryParent() throws Exception JavaDoc
104     {
105         RoleEntry roleEntry = new RoleEntry( Role1.class.getName(), "component1",
106             m_componentClass, ThreadSafeComponentHandler.class );
107
108         MetaInfoEntry entry = new MetaInfoEntry( roleEntry );
109
110         checkMetaInfoEntry( entry, ThreadSafeComponentHandler.class, "component1", true );
111     }
112
113     public void testNullPointerException() throws Exception JavaDoc
114     {
115         try
116         {
117             new MetaInfoEntry( null );
118             fail( "Did not throw an exception" );
119         }
120         catch ( NullPointerException JavaDoc npe )
121         {
122             // SUCCESS!
123
}
124         catch ( Exception JavaDoc e )
125         {
126             fail( "Threw wrong exception type: " + e.getClass().getName() );
127         }
128
129         try
130         {
131             new MetaInfoEntry( null, m_properties, m_dependencies );
132             fail( "Did not throw an exception" );
133         }
134         catch ( NullPointerException JavaDoc npe )
135         {
136             // SUCCESS!
137
}
138         catch ( Exception JavaDoc e )
139         {
140             fail( "Threw wrong exception type: " + e.getClass().getName() );
141         }
142
143         try
144         {
145             new MetaInfoEntry( m_componentClass, null, m_dependencies );
146             fail( "Did not throw an exception" );
147         }
148         catch ( NullPointerException JavaDoc npe )
149         {
150             // SUCCESS!
151
}
152         catch ( Exception JavaDoc e )
153         {
154             fail( "Threw wrong exception type: " + e.getClass().getName() );
155         }
156
157         try
158         {
159             new MetaInfoEntry( m_componentClass, m_properties, null );
160             fail( "Did not throw an exception" );
161         }
162         catch ( NullPointerException JavaDoc npe )
163         {
164             // SUCCESS!
165
}
166         catch ( Exception JavaDoc e )
167         {
168             fail( "Threw wrong exception type: " + e.getClass().getName() );
169         }
170
171         try
172         {
173             MetaInfoEntry entry = new MetaInfoEntry( m_componentClass, m_properties, m_dependencies );
174             entry.addRole( null );
175             fail( "Did not throw an exception" );
176         }
177         catch ( NullPointerException JavaDoc npe )
178         {
179             // SUCCESS!
180
}
181         catch ( Exception JavaDoc e )
182         {
183             fail( "Threw wrong exception type: " + e.getClass().getName() );
184         }
185
186         try
187         {
188             MetaInfoEntry entry = new MetaInfoEntry( m_componentClass, m_properties, m_dependencies );
189             entry.addRole( Role1.class.getName() );
190             entry.containsRole( null );
191             fail( "Did not throw an exception" );
192         }
193         catch ( NullPointerException JavaDoc npe )
194         {
195             // SUCCESS!
196
}
197         catch ( Exception JavaDoc e )
198         {
199             fail( "Threw wrong exception type: " + e.getClass().getName() );
200         }
201     }
202
203     public void testCreateShortName()
204     {
205         String JavaDoc start = "Regular";
206         String JavaDoc end = "regular";
207
208         assertEquals( end, MetaInfoEntry.createShortName(start));
209
210         start = "TwoWords";
211         end = "two-words";
212
213         assertEquals( end, MetaInfoEntry.createShortName(start));
214
215         start = "MANYcaps";
216         end = "manycaps";
217
218         assertEquals( end, MetaInfoEntry.createShortName( start ) );
219
220         start = "MANYcapsAndWords";
221         end = "manycaps-and-words";
222
223         assertEquals( end, MetaInfoEntry.createShortName( start ) );
224     }
225
226     private void checkMetaInfoEntry( MetaInfoEntry entry, Class JavaDoc handler, String JavaDoc name, boolean oneRole )
227     {
228         assertEquals( m_componentClass, entry.getComponentClass() );
229         assertEquals( name, entry.getConfigurationName() );
230         assertEquals( handler, entry.getHandlerClass() );
231
232         if ( oneRole )
233         {
234             checkSize( 1, entry.getRoles() );
235             // only one test does this
236
}
237         else
238         {
239             checkSize( 0, entry.getRoles() );
240             entry.addRole( Role1.class.getName() );
241             checkSize( 1, entry.getRoles() );
242             entry.addRole( BaseRole.class.getName() );
243             checkSize( 2, entry.getRoles() );
244             entry.makeReadOnly();
245
246             assertTrue( entry.containsRole( BaseRole.class.getName() ) );
247         }
248
249         assertTrue( entry.containsRole( Role1.class.getName() ) );
250
251         try
252         {
253             entry.addRole( Role2.class.getName() );
254             fail( "Should not allow Role2 to be added" );
255         }
256         catch ( SecurityException JavaDoc se )
257         {
258             // SUCCESS!
259
}
260         catch ( Exception JavaDoc e )
261         {
262             fail( "Threw the wrong exception: " + e.getMessage() );
263         }
264
265         assertTrue( !entry.containsRole( Role2.class.getName() ) );
266     }
267
268     private void checkSize( int numRoles, Iterator roles )
269     {
270         int i = 0;
271         while ( roles.hasNext() )
272         {
273             i++;
274             roles.next();
275         }
276
277         assertEquals( numRoles, i );
278     }
279 }
280
Popular Tags