SnmpConfigurationParser { public SnmpConfiguration parseConfiguration(HierarchicalConfiguration config) throws ConfigurationException { SnmpConfigurationImpl impl = new SnmpConfigurationImpl(); try { String addr = config.getString("[@targetAddress]"); if(StringUtils.isBlank(addr)) throw new ConfigurationException("empty SNMP address given"); impl.setAddress(InetAddress.getByName(addr)); } catch (UnknownHostException e) { throw new ConfigurationException("cannot parse easybox IP address", e); } impl.setCommunity(config.getString("[@community]")); if(StringUtils.isBlank(impl.getCommunity())) throw new ConfigurationException("empty SNMP community name given"); impl.setLocalPort(config.getInt("[@localPort]", 0)); return impl; }  SnmpConfiguration parseConfiguration(HierarchicalConfiguration config);  }
@Test public void testCorrectConfiguration() throws Exception { SnmpConfiguration snmp = parser.parseConfiguration(config.configurationAt("SnmpConfiguration(0)")); Assert.assertEquals(InetAddress.getByAddress(new byte[] { (byte)0xc0, (byte)0xa8, 0x01, 0x01 }), snmp.getTargetAddress()); Assert.assertEquals("public", snmp.getCommunity()); Assert.assertEquals(16161, snmp.getLocalPort()); }
@Test(expected=ConfigurationException.class) public void testMissingAddress() throws Exception { parser.parseConfiguration(config.configurationAt("SnmpConfiguration(1)")); }
@Test(expected=ConfigurationException.class) public void testMissingCommunity() throws Exception { parser.parseConfiguration(config.configurationAt("SnmpConfiguration(2)")); }
@Test(expected=ConfigurationException.class) public void testBlankCommunity() throws Exception { parser.parseConfiguration(config.configurationAt("SnmpConfiguration(3)")); }