DefaultConfigFactoryManager implements ConfigFactoryManager { @Override public ConfigFactory getFactory(String namespace) { ConfigFactory factory = m_registry.getFactory(namespace); if (factory != null) { return factory; } factory = m_factories.get(namespace); if (factory != null) { return factory; } factory = ApolloInjector.getInstance(ConfigFactory.class, namespace); if (factory != null) { return factory; } factory = ApolloInjector.getInstance(ConfigFactory.class); m_factories.put(namespace, factory); return factory; } DefaultConfigFactoryManager(); @Override ConfigFactory getFactory(String namespace);  }
@Test public void testGetFactoryFromRegistry() throws Exception { ConfigFactory result = defaultConfigFactoryManager.getFactory(MockConfigRegistry.NAMESPACE_REGISTERED); assertEquals(MockConfigRegistry.REGISTERED_CONFIGFACTORY, result); }
@Test public void testGetFactoryFromNamespace() throws Exception { String someNamespace = "someName"; MockInjector.setInstance(ConfigFactory.class, someNamespace, new SomeConfigFactory()); ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace); assertThat("When namespace is registered, should return the registerd config factory", result, instanceOf(SomeConfigFactory.class)); }
@Test public void testGetFactoryFromNamespaceMultipleTimes() throws Exception { String someNamespace = "someName"; MockInjector.setInstance(ConfigFactory.class, someNamespace, new SomeConfigFactory()); ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace); ConfigFactory anotherResult = defaultConfigFactoryManager.getFactory(someNamespace); assertThat( "Get config factory with the same namespace multiple times should returnt the same instance", anotherResult, equalTo(result)); }
@Test public void testGetFactoryFromDefault() throws Exception { String someNamespace = "someName"; MockInjector.setInstance(ConfigFactory.class, new AnotherConfigFactory()); ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace); assertThat("When namespace is not registered, should return the default config factory", result, instanceOf(AnotherConfigFactory.class)); }