KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > component > test > DefaultComponentSelectorTestCase


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Apache Avalon", "Avalon Excalibur", "Avalon
26     Framework" and "Apache Software Foundation" must not be used to endorse
27     or promote products derived from this software without prior written
28     permission. For written permission, please contact apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation and was originally created by
47  Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
48  Software Foundation, please see <http://www.apache.org/>.
49  
50 */

51 package org.apache.avalon.framework.component.test;
52
53 import junit.framework.TestCase;
54 import org.apache.avalon.framework.component.Component;
55 import org.apache.avalon.framework.component.ComponentException;
56 import org.apache.avalon.framework.component.DefaultComponentSelector;
57
58 /**
59  * Test the basic public methods of DefaultComponentSelector.
60  *
61  * @author <a HREF="mailto:rantene@hotmail.com">Ran Tene</a>
62  */

63 public final class DefaultComponentSelectorTestCase
64     extends TestCase
65 {
66     class FeatureComponent
67         implements Component
68     {
69         Object JavaDoc m_feature;
70         public FeatureComponent( final Object JavaDoc feature )
71         {
72             m_feature = feature;
73         }
74
75         public Object JavaDoc getFeature()
76         {
77             return m_feature;
78         }
79     }
80
81     class Hint
82     {
83         String JavaDoc m_name;
84
85         public Hint( final String JavaDoc name )
86         {
87             m_name = name;
88         }
89
90         public String JavaDoc getName()
91         {
92             return m_name;
93         }
94     }
95
96     private DefaultComponentSelector m_componentSelector;
97     protected boolean m_exceptionThrown;
98
99     public DefaultComponentSelectorTestCase()
100     {
101         this("DefaultComponentSelector Test Case");
102     }
103
104     public DefaultComponentSelectorTestCase( final String JavaDoc name )
105     {
106         super( name );
107     }
108
109     protected void setUp()
110         throws Exception JavaDoc
111     {
112         m_componentSelector = new DefaultComponentSelector();
113         m_exceptionThrown =false;
114     }
115
116     protected void tearDown()
117         throws Exception JavaDoc
118     {
119         m_componentSelector = null;
120     }
121
122     /**
123      * lookup contract:
124      * return the component that was put with this hint
125      * if no compnent exist for hint
126      * throw ComponentException
127      */

128     public void testlookup()
129         throws Exception JavaDoc
130     {
131         Hint hintA = new Hint("a");
132         Hint hintB = new Hint("b");
133         m_componentSelector.put(hintA,new FeatureComponent(hintA));
134         m_componentSelector.put(hintB,new FeatureComponent(hintB));
135         FeatureComponent fComponent = (FeatureComponent)m_componentSelector.select(hintA);
136         assertEquals( hintA, fComponent.getFeature() );
137         Object JavaDoc o = null;
138         try
139         {
140             o = (FeatureComponent)m_componentSelector.select(new Hint("no component"));
141         }
142         catch (ComponentException ce)
143         {
144             m_exceptionThrown = true;
145         }
146         if (o == null)
147             assertTrue("ComponentException was not thrown when component was not found by lookup." ,m_exceptionThrown );
148         else
149             assertTrue("component was found by lookup ,when there was no component.",false);
150     }
151
152     public void testhasComponent()
153         throws Exception JavaDoc
154     {
155         Hint hintA = new Hint("a");
156         Hint hintB = new Hint("b");
157         m_componentSelector.put(hintA,new FeatureComponent(hintA));
158         assertTrue(m_componentSelector.hasComponent(hintA));
159         assertTrue(!m_componentSelector.hasComponent(hintB));
160     }
161
162     //makeReadOnly contract:put after makeReadOnly throws IllegalStateException
163
public void testmakeReadOnly()
164         throws Exception JavaDoc
165     {
166         Hint hintA = new Hint("a");
167         Hint hintB = new Hint("b");
168         //before read only
169
m_componentSelector.put(hintA,new FeatureComponent(hintA));
170         FeatureComponent fComponent = (FeatureComponent)m_componentSelector.select(hintA);
171         assertEquals( hintA, fComponent.getFeature() );
172         m_componentSelector.makeReadOnly();
173         //after read only
174
try
175         {
176             m_componentSelector.put(hintB,new FeatureComponent(hintB));
177         }
178         catch (IllegalStateException JavaDoc se)
179         {
180             m_exceptionThrown = true;
181         }
182         assertTrue("IllegalStateException was not thrown in put after makeReadOnly." , m_exceptionThrown );
183     }
184 }
185
186
187
188
189
190
191
Popular Tags