1 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 36 public class FortressServiceManagerTestCase extends TestCase 37 { 38 TestContainer m_container = new TestContainer(); 39 40 public FortressServiceManagerTestCase( String name ) 41 { 42 super( name ); 43 } 44 45 public void testServiceManager() throws Exception 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 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 70 { 71 FortressServiceSelector selector = new FortressServiceSelector( m_container, Role1.ROLE ); 72 73 m_container.setExpectedKey( Role1.ROLE ); 74 75 String 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 m_key; 85 private Object 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 key ) 95 { 96 m_key = key; 97 m_selector = new FortressServiceSelector( this, m_key ); 98 } 99 100 public void setExpectedHint( Object hint ) 101 { 102 m_hint = hint; 103 } 104 105 public Object get( String key, Object 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 key, Object hint ) 123 { 124 if ( exists( key, hint ) ) 125 { 126 return true; 127 } 128 129 return false; 130 } 131 132 private boolean exists( String key, Object 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 |