KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > util > test > LifecycleExtensionManagerTestCase


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.util.test;
19
20 import junit.framework.TestCase;
21 import org.apache.avalon.fortress.util.LifecycleExtensionManager;
22 import org.apache.avalon.framework.logger.NullLogger;
23 import org.apache.avalon.framework.context.DefaultContext;
24 import org.apache.avalon.framework.context.Context;
25 import org.apache.avalon.lifecycle.Creator;
26 import org.apache.avalon.lifecycle.Accessor;
27
28 import java.util.Iterator JavaDoc;
29
30 /**
31  * LifecycleExtensionManagerTestCase does XYZ
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version CVS $ Revision: 1.1 $
35  */

36 public class LifecycleExtensionManagerTestCase extends TestCase
37 {
38     private LifecycleExtensionManager m_manager;
39
40     public LifecycleExtensionManagerTestCase( String JavaDoc name )
41     {
42         super( name );
43     }
44
45     public void setUp()
46     {
47         m_manager = new LifecycleExtensionManager();
48         m_manager.enableLogging(new NullLogger());
49     }
50
51     public void testCreators()
52     {
53         TestCreator testCreator = new TestCreator(0);
54         assertEquals(0, m_manager.creatorExtensionsCount());
55
56         m_manager.addCreatorExtension(testCreator);
57
58         assertEquals(1, m_manager.creatorExtensionsCount());
59         assertEquals( testCreator, m_manager.getCreatorExtension( 0 ) );
60
61         int count = 0;
62         Iterator JavaDoc it = m_manager.creatorExtensionsIterator();
63         while (it.hasNext())
64         {
65             count++;
66             TestCreator creator = (TestCreator)it.next();
67             assertEquals(testCreator, creator);
68             assertEquals( testCreator.m_id, creator.m_id );
69         }
70         assertEquals(1, count);
71
72         TestCreator second = new TestCreator( 1 );
73         m_manager.insertCreatorExtension( 0, second );
74
75         assertEquals( 2, m_manager.creatorExtensionsCount() );
76         assertEquals( second.m_id, ( (TestCreator) m_manager.getCreatorExtension( 0 ) ).m_id );
77         assertEquals( testCreator.m_id, ( (TestCreator) m_manager.getCreatorExtension( 1 ) ).m_id );
78
79         m_manager.removeCreatorExtension( 0 );
80         assertEquals( 1, m_manager.creatorExtensionsCount() );
81         assertEquals( testCreator.m_id, ( (TestCreator) m_manager.getCreatorExtension( 0 ) ).m_id );
82
83         m_manager.clearCreatorExtensions();
84         assertEquals(0, m_manager.creatorExtensionsCount());
85     }
86
87     public void testAccessors()
88     {
89         TestAccessor testAccessor = new TestAccessor(0);
90         assertEquals( 0, m_manager.accessorExtensionsCount() );
91
92         m_manager.addAccessorExtension( testAccessor );
93
94         assertEquals(1, m_manager.accessorExtensionsCount());
95         assertEquals( testAccessor, m_manager.getAccessorExtension( 0 ) );
96
97         int count = 0;
98         Iterator JavaDoc it = m_manager.accessorExtensionsIterator();
99         while ( it.hasNext() )
100         {
101             count++;
102             TestAccessor accessor = (TestAccessor) it.next();
103             assertEquals( testAccessor, accessor );
104             assertEquals( testAccessor.m_id, accessor.m_id);
105         }
106         assertEquals( 1, count );
107
108         TestAccessor second = new TestAccessor(1);
109         m_manager.insertAccessorExtension(0, second);
110
111         assertEquals( 2, m_manager.accessorExtensionsCount());
112         assertEquals( second.m_id, ( (TestAccessor) m_manager.getAccessorExtension( 0 ) ).m_id );
113         assertEquals( testAccessor.m_id, ( (TestAccessor) m_manager.getAccessorExtension( 1 ) ).m_id );
114
115         m_manager.removeAccessorExtension(0);
116         assertEquals( 1, m_manager.accessorExtensionsCount() );
117         assertEquals( testAccessor.m_id, ( (TestAccessor) m_manager.getAccessorExtension( 0 ) ).m_id );
118
119         m_manager.clearAccessorExtensions();
120         assertEquals( 0, m_manager.accessorExtensionsCount());
121     }
122
123     public void testLifecycle() throws Exception JavaDoc
124     {
125         Accessor testAccessor = new TestAccessor(1);
126         Creator testCreator = new TestCreator(1);
127         m_manager.addCreatorExtension( testCreator );
128         m_manager.addAccessorExtension( testAccessor );
129
130         TestComponent component = new TestComponent();
131         Context context = new DefaultContext();
132         m_manager.executeCreationExtensions( component, context );
133         m_manager.executeAccessExtensions( component, context );
134         m_manager.executeReleaseExtensions( component, context );
135         m_manager.executeDestructionExtensions( component, context );
136     }
137 }
138
Popular Tags