DefaultConfigFactory implements ConfigFactory { @Override public ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat) { ConfigRepository configRepository = createLocalConfigRepository(namespace); switch (configFileFormat) { case Properties: return new PropertiesConfigFile(namespace, configRepository); case XML: return new XmlConfigFile(namespace, configRepository); case JSON: return new JsonConfigFile(namespace, configRepository); case YAML: return new YamlConfigFile(namespace, configRepository); case YML: return new YmlConfigFile(namespace, configRepository); case TXT: return new TxtConfigFile(namespace, configRepository); } return null; } DefaultConfigFactory(); @Override Config create(String namespace); @Override ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat);  }
@Test public void testCreateConfigFile() throws Exception { String someNamespace = "someName"; String anotherNamespace = "anotherName"; String yetAnotherNamespace = "yetAnotherNamespace"; Properties someProperties = new Properties(); LocalFileConfigRepository someLocalConfigRepo = mock(LocalFileConfigRepository.class); when(someLocalConfigRepo.getConfig()).thenReturn(someProperties); doReturn(someLocalConfigRepo).when(defaultConfigFactory).createLocalConfigRepository(someNamespace); doReturn(someLocalConfigRepo).when(defaultConfigFactory).createLocalConfigRepository(anotherNamespace); doReturn(someLocalConfigRepo).when(defaultConfigFactory).createLocalConfigRepository(yetAnotherNamespace); ConfigFile propertyConfigFile = defaultConfigFactory.createConfigFile(someNamespace, ConfigFileFormat.Properties); ConfigFile xmlConfigFile = defaultConfigFactory.createConfigFile(anotherNamespace, ConfigFileFormat.XML); ConfigFile jsonConfigFile = defaultConfigFactory.createConfigFile(yetAnotherNamespace, ConfigFileFormat.JSON); ConfigFile ymlConfigFile = defaultConfigFactory.createConfigFile(someNamespace, ConfigFileFormat.YML); ConfigFile yamlConfigFile = defaultConfigFactory.createConfigFile(someNamespace, ConfigFileFormat.YAML); assertThat("Should create PropertiesConfigFile for properties format", propertyConfigFile, is(instanceOf( PropertiesConfigFile.class))); assertEquals(someNamespace, propertyConfigFile.getNamespace()); assertThat("Should create XmlConfigFile for xml format", xmlConfigFile, is(instanceOf( XmlConfigFile.class))); assertEquals(anotherNamespace, xmlConfigFile.getNamespace()); assertThat("Should create JsonConfigFile for json format", jsonConfigFile, is(instanceOf( JsonConfigFile.class))); assertEquals(yetAnotherNamespace, jsonConfigFile.getNamespace()); assertThat("Should create YmlConfigFile for yml format", ymlConfigFile, is(instanceOf( YmlConfigFile.class))); assertEquals(someNamespace, ymlConfigFile.getNamespace()); assertThat("Should create YamlConfigFile for yaml format", yamlConfigFile, is(instanceOf( YamlConfigFile.class))); assertEquals(someNamespace, yamlConfigFile.getNamespace()); }