KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > service > test > DefaultServiceSelectorTestCase


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.service.test;
52
53 import junit.framework.TestCase;
54 import org.apache.avalon.framework.service.ServiceException;
55 import org.apache.avalon.framework.service.DefaultServiceSelector;
56
57 /**
58  * Test the basic public methods of DefaultComponentSelector.
59  *
60  * @author <a HREF="mailto:rantene@hotmail.com">Ran Tene</a>
61  */

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

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