@Test void testMatchesIPWithHostUnknown() throws Exception { // Given String unknownHost = "unknownHost"; assertThrows( UnknownHostException.class, () -> InetAddress.getByName( unknownHost ) ); String host1 = InetAddress.getLocalHost().getHostAddress(); HostnamePort hostnamePortSinglePort = new HostnamePort( host1 + ":1234" ); HostnamePort hostnamePortWithRange = new HostnamePort( host1 + ":1234-1236" ); // When & Then // should return false if matched with any unknown host assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost + ":1234") ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost + ":1234") ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost + ":1235") ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost + ":1236") ) ); }
@Test void testMatchesNullHostWithIP() throws Exception { HostnamePort hostnamePortSinglePort = new HostnamePort( ":1234" ); String host1IP = InetAddress.getLocalHost().getHostAddress(); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host1IP + ":1234" ) ) ); }
@Test void testMatchesNullHostWithKnownHost() throws Exception { HostnamePort hostnamePortSinglePort = new HostnamePort( ":1234" ); String host1 = InetAddress.getLocalHost().getHostName(); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host1 + ":1234" ) ) ); }
@Test void testMatchesKnownHostWithHostUnknown() throws Exception { // Given String host1 = InetAddress.getLocalHost().getHostName(); String unknownHost = "unknownHost"; assertThrows( UnknownHostException.class, () -> InetAddress.getByName( unknownHost ) ); HostnamePort hostnamePortSinglePort = new HostnamePort( host1 + ":1234" ); HostnamePort hostnamePortWithRange = new HostnamePort( host1 + ":1234-1236" ); // When & Then // should return false if matched with any unknown host assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost + ":1234") ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost + ":1234") ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost + ":1235") ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost + ":1236") ) ); }
@Test void testMatchesIP() { // Given HostnamePort hostnamePortSinglePort = new HostnamePort( "1.2.3.4:1234" ); HostnamePort hostnamePortWithRange = new HostnamePort( "1.2.3.4:1234-1236" ); // When & Then // Should match, same host and port assertTrue( hostnamePortSinglePort.matches( URI.create( "ha://1.2.3.4:1234" ) ) ); // Should fail, different host or port assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://1.2.3.4:1235" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://5.6.7.8:1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://5.6.7.8:1235" ) ) ); // Should fail, no port assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://1.2.3.4" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://5.6.7.8" ) ) ); // Should match, port in range and host the same assertTrue( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4:1234" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4:1235" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4:1236" ) ) ); // Should not match, different host assertFalse( hostnamePortWithRange.matches( URI.create( "ha://5.6.7.8:1234" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://5.6.7.8:1235" ) ) ); // Should not match, port outside of range assertFalse( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4:1233" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4:1237" ) ) ); // Should not match, no port assertFalse( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://5.6.7.8" ) ) ); }
assertTrue( hostnamePortSinglePort.matches( URI.create( "ha://" + host1 + ":1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host1 + ":1235" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host2 + ":1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host2 + ":1235" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host1 ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host2 ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + host1 + ":1234" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + host1 + ":1235" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + host1 + ":1236" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host2 + ":1234" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host2 + ":1235" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host1 + ":1233" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host1 + ":1237" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host1 ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host2 ) ) );
assertTrue( hostnamePortSinglePort.matches( URI.create( "ha://" + hostname1 + ":1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + hostname1 + ":1235" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host2 + ":1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host2 + ":1235" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host1 ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host2 ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + hostname1 + ":1234" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + hostname1 + ":1235" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + hostname1 + ":1236" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host2 + ":1234" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host2 + ":1235" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + hostname1 + ":1233" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + hostname1 + ":1237" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + hostname1 ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host2 ) ) );
assertTrue( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost1 + ":1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost1 + ":1235" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost2 + ":1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost2 + ":1235" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost1 + "" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost2 + "" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://1.2.3.4:1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + knownHost + ":1234" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost1 + ":1234" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost1 + ":1235" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost1 + ":1236" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost2 + ":1234" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost2 + ":1235" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost1 + ":1233" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost1 + ":1237" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost1 ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost2 ) ) );
assertTrue( hostnamePortSinglePort.matches( URI.create( "ha://" + host1 + ":1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host1 + ":1235" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + hostname2 + ":1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + hostname2 + ":1235" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host1 ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + hostname2 ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + host1 + ":1234" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + host1 + ":1235" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://" + host1 + ":1236" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + hostname2 + ":1234" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + hostname2 + ":1235" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host1 + ":1233" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host1 + ":1237" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + host1 ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + hostname2 ) ) );
@Test void testMatchesNullHostWithUnknownHost() { // Given HostnamePort hostnamePortSinglePort = new HostnamePort( ":1234" ); String unknownHost = "unknownHost"; assertThrows( UnknownHostException.class, () -> InetAddress.getByName( unknownHost ) ); // When & Then assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost + ":1234" ) ) ); }