### Question:
PortMappingParser { static PortMappingWithName parsePortMapping(final String portSpec) { final Matcher matcher = PATTERN.matcher(portSpec); if (!matcher.matches()) { throw new IllegalArgumentException("Bad port mapping: " + portSpec); } final String name = matcher.group("n"); final String ip = matcher.group("ip"); final int internal = Integer.parseInt(matcher.group("i")); final Integer external = nullOrInteger(matcher.group("e")); final String protocol = fromNullable(matcher.group("p")).or(TCP); return PortMappingWithName.create(name, PortMapping.builder() .ip(ip) .internalPort(internal) .externalPort(external) .protocol(protocol) .build()); } private  PortMappingParser();   }### Answer:
@Test public void testParsePortMappingGoodSpecs() throws Exception { for (final TestData d : GOOD_SPECS) { final PortMappingWithName mappingWithName = PortMappingParser.parsePortMapping(d.getSpec()); final PortMapping portMapping = mappingWithName.portMapping(); assertThat(mappingWithName.name(), equalTo(d.getName())); assertThat(portMapping.getInternalPort(), equalTo(d.getPortMapping().getInternalPort())); assertThat(portMapping.getExternalPort(), equalTo(d.getPortMapping().getExternalPort())); assertThat(portMapping.getProtocol(), equalTo(d.getPortMapping().getProtocol())); } }
@Test public void testParsePortMappingBadSpecs() throws Exception { for (final TestData d : BAD_SPECS) { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Bad port mapping: " + d.getSpec()); PortMappingParser.parsePortMapping(d.getSpec()); } }
@Test public void testParsePortMappingBadIp() throws Exception { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("999.999.999.999 is not a valid IP address."); final TestData d = TestData.partial("mail=999.999.999.999:23:23/tcp"); PortMappingParser.parsePortMapping(d.getSpec()); }