Protocol { public static Protocol decode(String protocolText) { if (HTTP_1_1.text.equals(protocolText)) { return HTTP_1_1; } else if (HTTP_1_0.text.equals(protocolText)) { return HTTP_1_0; } else { Matcher matcher = RE_PROTOCOL.matcher(protocolText); if (!matcher.matches()) { throw new IllegalArgumentException("Unknown protocol format: " + protocolText); } return new Protocol( matcher.group(1), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3))); } } private  Protocol(String name, int majorVersion, int minorVersion); final String name(); final int majorVersion(); final int minorVersion(); final String text(); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); static Protocol decode(String protocolText); static final Protocol HTTP_1_0; static final Protocol HTTP_1_1; }
@Test public void testCommonMethods() { Protocol protocol1 = Protocol.decode("HTTP/1.0"); Protocol protocol2 = Protocol.decode("HTTP/1.0"); Protocol protocol3 = Protocol.decode("HTTP/1.1"); TestUtil.testCommonMethods(protocol1, protocol2, protocol3); }
@Test(expected = IllegalArgumentException.class) public void constructor_emptyName_exception() { Protocol.decode("/1.1"); }
@Test(expected = IllegalArgumentException.class) public void constructor_negativeMajorVersion_exception() { Protocol.decode("HTTP/-1.1"); }
@Test(expected = IllegalArgumentException.class) public void constructor_negativeMinorVersion_exception() { Protocol.decode("HTTP/1.-1"); }
@Test public void decode_known() { assertSame(Protocol.HTTP_1_0, Protocol.decode("HTTP/1.0")); assertSame(Protocol.HTTP_1_1, Protocol.decode("HTTP/1.1")); }
@Test(expected = IllegalArgumentException.class) public void decode_nameOnly_exception() { Protocol.decode("foo"); }
@Test(expected = IllegalArgumentException.class) public void decode_noSlash_exception() { Protocol.decode("HTTP 1.1"); }
@Test(expected = IllegalArgumentException.class) public void decode_noMajor_exception() { Protocol.decode("FTP/.3"); }
@Test(expected = IllegalArgumentException.class) public void decode_noMinor_exception() { Protocol.decode("FTP/17"); }