KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > impl > lookup > test > FortressServiceManagerTestCase


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.impl.lookup.test;
19
20 import junit.framework.TestCase;
21 import org.apache.avalon.fortress.Container;
22 import org.apache.avalon.fortress.impl.AbstractContainer;
23 import org.apache.avalon.fortress.impl.lookup.FortressServiceManager;
24 import org.apache.avalon.fortress.impl.lookup.FortressServiceSelector;
25 import org.apache.avalon.fortress.impl.test.TestComponentHandler;
26 import org.apache.avalon.fortress.test.data.Role1;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceSelector;
29
30 /**
31  * FortressServiceManagerTestCase 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 FortressServiceManagerTestCase extends TestCase
37 {
38     TestContainer m_container = new TestContainer();
39
40     public FortressServiceManagerTestCase( String JavaDoc name )
41     {
42         super( name );
43     }
44
45     public void testServiceManager() throws Exception JavaDoc
46     {
47         FortressServiceManager manager = new FortressServiceManager( m_container, null );
48
49         m_container.setExpectedKey( Role1.ROLE );
50
51         assertTrue( manager.hasService( Role1.ROLE ) );
52         assertNotNull( manager.lookup( Role1.ROLE ) );
53
54         String JavaDoc hint = "test";
55         m_container.setExpectedHint( hint );
56         assertTrue( manager.hasService( Role1.ROLE + "/" + hint ) );
57         assertNotNull( manager.lookup( Role1.ROLE + "/" + hint ) );
58
59         m_container.setExpectedHint( AbstractContainer.SELECTOR_ENTRY );
60         assertTrue( manager.hasService( Role1.ROLE + "Selector" ) );
61         assertNotNull( manager.lookup( Role1.ROLE + "Selector" ) );
62
63         ServiceSelector selector = (ServiceSelector) manager.lookup( Role1.ROLE + "Selector" );
64         m_container.setExpectedHint( hint );
65         assertTrue( selector.isSelectable( hint ) );
66         assertNotNull( selector.select( hint ) );
67     }
68
69     public void testServiceSelector() throws Exception JavaDoc
70     {
71         FortressServiceSelector selector = new FortressServiceSelector( m_container, Role1.ROLE );
72
73         m_container.setExpectedKey( Role1.ROLE );
74
75         String JavaDoc hint = "test";
76         m_container.setExpectedHint( hint );
77         assertTrue( selector.isSelectable( hint ) );
78         assertNotNull( selector.select( hint ) );
79     }
80 }
81
82 class TestContainer implements Container
83 {
84     private String JavaDoc m_key;
85     private Object JavaDoc m_hint = AbstractContainer.DEFAULT_ENTRY;
86     private TestComponentHandler m_component;
87     private FortressServiceSelector m_selector;
88
89     public TestContainer()
90     {
91         m_component = new TestComponentHandler();
92     }
93
94     public void setExpectedKey( String JavaDoc key )
95     {
96         m_key = key;
97         m_selector = new FortressServiceSelector( this, m_key );
98     }
99
100     public void setExpectedHint( Object JavaDoc hint )
101     {
102         m_hint = hint;
103     }
104
105     public Object JavaDoc get( String JavaDoc key, Object JavaDoc hint ) throws ServiceException
106     {
107         if ( exists( key, hint ) )
108         {
109             if ( hint.equals( AbstractContainer.SELECTOR_ENTRY ) )
110             {
111                 return m_selector;
112             }
113             else
114             {
115                 return m_component;
116             }
117         }
118
119         throw new ServiceException( m_key, "Unexpected key/hint combo" );
120     }
121
122     public boolean has( String JavaDoc key, Object JavaDoc hint )
123     {
124         if ( exists( key, hint ) )
125         {
126             return true;
127         }
128
129         return false;
130     }
131
132     private boolean exists( String JavaDoc key, Object JavaDoc hint )
133     {
134         boolean exists = false;
135
136         if ( m_key.equals( key ) )
137         {
138             if ( null == m_hint )
139             {
140                 exists = hint == null;
141             }
142             else
143             {
144                 exists = m_hint.equals( hint );
145             }
146         }
147
148         return exists;
149     }
150 }
151
Popular Tags