ServerConfigurationParser { public ServerConfiguration parseConfig(HierarchicalConfiguration config) throws ConfigurationException { InetAddress address = null; int port = config.getInt("[@port]", 0); String listenRep = config.getString("[@listen]"); if(StringUtils.isNotBlank(listenRep)) { try { address = InetAddress.getByName(listenRep); } catch (UnknownHostException e) { log.error("failed to parse list address: " + listenRep, e); throw new ConfigurationException(e); } } return new ServerConfigurationImpl(new InetSocketAddress(address, port)); }  ServerConfiguration parseConfig(HierarchicalConfiguration config);  }
@Test public void testDefaultServerConfig() throws ConfigurationException { ServerConfiguration serverConfig = parser.parseConfig(config.configurationAt("Server(0)")); Assert.assertEquals(0, serverConfig.getListenAddress().getPort()); Assert.assertEquals("0.0.0.0", serverConfig.getListenAddress().getHostName()); }
@Test public void testPortOnlyServerConfig() throws ConfigurationException { ServerConfiguration serverConfig = parser.parseConfig(config.configurationAt("Server(1)")); Assert.assertEquals(17179, serverConfig.getListenAddress().getPort()); Assert.assertEquals("0.0.0.0", serverConfig.getListenAddress().getHostName()); }
@Test public void testAddressOnlyServerConfig() throws ConfigurationException { ServerConfiguration serverConfig = parser.parseConfig(config.configurationAt("Server(2)")); Assert.assertEquals(0, serverConfig.getListenAddress().getPort()); Assert.assertEquals("192.168.4.1", serverConfig.getListenAddress().getHostName()); }
@Test public void testAddressPortServerConfig() throws ConfigurationException { ServerConfiguration serverConfig = parser.parseConfig(config.configurationAt("Server(3)")); Assert.assertEquals(17179, serverConfig.getListenAddress().getPort()); Assert.assertEquals("192.168.4.1", serverConfig.getListenAddress().getHostName()); }