KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package org.apache.avalon.fortress.impl.lookup.test;
21
22 import junit.framework.TestCase;
23 import org.apache.avalon.fortress.Container;
24 import org.apache.avalon.fortress.impl.AbstractContainer;
25 import org.apache.avalon.fortress.impl.lookup.FortressServiceManager;
26 import org.apache.avalon.fortress.impl.lookup.FortressServiceSelector;
27 import org.apache.avalon.fortress.impl.test.TestComponentHandler;
28 import org.apache.avalon.fortress.test.data.Role1;
29 import org.apache.avalon.framework.service.ServiceException;
30 import org.apache.avalon.framework.service.ServiceSelector;
31
32 public class TestContainer implements Container
33 {
34     private String JavaDoc m_key;
35     private Object JavaDoc m_hint = AbstractContainer.DEFAULT_ENTRY;
36     private TestComponentHandler m_component;
37     private FortressServiceSelector m_selector;
38
39     public TestContainer() {
40         m_component = new TestComponentHandler();
41     }
42
43     public void setExpectedKey( String JavaDoc key )
44     {
45         m_key = key;
46         m_selector = new FortressServiceSelector( this, m_key );
47     }
48
49     public void setExpectedHint( Object JavaDoc hint )
50     {
51         m_hint = hint;
52     }
53
54     public Object JavaDoc get( String JavaDoc key, Object JavaDoc hint ) throws ServiceException
55     {
56         if ( exists( key, hint ) )
57         {
58             if ( hint.equals( AbstractContainer.SELECTOR_ENTRY ) )
59             {
60                 return m_selector;
61             }
62             else
63             {
64                 return m_component;
65             }
66         }
67
68         throw new ServiceException( m_key, "Unexpected key/hint combo" );
69     }
70
71     public boolean has( String JavaDoc key, Object JavaDoc hint )
72     {
73         if ( exists( key, hint ) )
74         {
75             return true;
76         }
77
78         return false;
79     }
80
81     private boolean exists( String JavaDoc key, Object JavaDoc hint )
82     {
83         boolean exists = false;
84
85         if ( m_key.equals( key ) )
86         {
87             if ( null == m_hint )
88             {
89                 exists = hint == null;
90             }
91             else
92             {
93                 exists = m_hint.equals( hint );
94             }
95         }
96
97         return exists;
98     }
99 }
100
Popular Tags