PropertiesEndpointGroup extends DynamicEndpointGroup { public static PropertiesEndpointGroup of(ClassLoader classLoader, String resourceName, String endpointKeyPrefix) { return builder(classLoader, resourceName, endpointKeyPrefix).build(); } PropertiesEndpointGroup(EndpointSelectionStrategy selectionStrategy, List<Endpoint> endpoints);   PropertiesEndpointGroup(EndpointSelectionStrategy selectionStrategy,
                            Path path, String endpointKeyPrefix, int defaultPort); static PropertiesEndpointGroup of(ClassLoader classLoader, String resourceName,
                                             String endpointKeyPrefix); static PropertiesEndpointGroup of(Properties properties, String endpointKeyPrefix); static PropertiesEndpointGroup of(Path path, String endpointKeyPrefix); static PropertiesEndpointGroupBuilder builder(ClassLoader classLoader, String resourceName,
                                                         String endpointKeyPrefix); static PropertiesEndpointGroupBuilder builder(Properties properties, String endpointKeyPrefix); static PropertiesEndpointGroupBuilder builder(Path path, String endpointKeyPrefix);  }
@Test public void propertiesWithoutDefaultPort() { final PropertiesEndpointGroup endpointGroup = PropertiesEndpointGroup.of(PROPS, "serverA.hosts"); assertThat(endpointGroup.endpoints()).containsExactlyInAnyOrder(Endpoint.parse("127.0.0.1:8080"), Endpoint.parse("127.0.0.1:8081"), Endpoint.parse("127.0.0.1")); }
@Test public void resourceWithoutDefaultPort() { final PropertiesEndpointGroup endpointGroup = PropertiesEndpointGroup.of( getClass().getClassLoader(), "server-list.properties", "serverA.hosts"); assertThat(endpointGroup.endpoints()).containsExactlyInAnyOrder(Endpoint.parse("127.0.0.1:8080"), Endpoint.parse("127.0.0.1:8081"), Endpoint.parse("127.0.0.1")); }
@Test public void pathWithoutDefaultPort() { final URL resourceUrl = getClass().getClassLoader().getResource("server-list.properties"); assert resourceUrl != null; final Path resourcePath = new File(resourceUrl.getFile()).toPath(); final PropertiesEndpointGroup endpointGroup = PropertiesEndpointGroup.of( resourcePath, "serverA.hosts"); assertThat(endpointGroup.endpoints()).containsExactlyInAnyOrder(Endpoint.parse("127.0.0.1:8080"), Endpoint.parse("127.0.0.1:8081"), Endpoint.parse("127.0.0.1")); endpointGroup.close(); }
@Test public void testWithPrefixThatEndsWithDot() { final PropertiesEndpointGroup endpointGroup = PropertiesEndpointGroup.of( getClass().getClassLoader(), "server-list.properties", "serverA.hosts."); assertThat(endpointGroup.endpoints()).containsExactlyInAnyOrder(Endpoint.parse("127.0.0.1:8080"), Endpoint.parse("127.0.0.1:8081"), Endpoint.parse("127.0.0.1")); }
@Test public void propertiesFileUpdatesCorrectly() throws Exception { final File file = folder.newFile("temp-file.properties"); PrintWriter printWriter = new PrintWriter(file); Properties props = new Properties(); props.setProperty("serverA.hosts.0", "127.0.0.1:8080"); props.store(printWriter, ""); printWriter.close(); final PropertiesEndpointGroup endpointGroupA = PropertiesEndpointGroup.of( file.toPath(), "serverA.hosts"); await().untilAsserted(() -> assertThat(endpointGroupA.endpoints()).hasSize(1)); printWriter = new PrintWriter(file); props = new Properties(); props.setProperty("serverA.hosts.0", "127.0.0.1:8080"); props.setProperty("serverA.hosts.1", "127.0.0.1:8081"); props.store(printWriter, ""); printWriter.close(); await().untilAsserted(() -> assertThat(endpointGroupA.endpoints()).hasSize(2)); endpointGroupA.close(); }
@Test public void duplicateResourceUrl() throws IOException { final File file = folder.newFile("temp-file.properties"); final PropertiesEndpointGroup propertiesEndpointGroupA = PropertiesEndpointGroup.of(file.toPath(), "serverA.hosts"); final PropertiesEndpointGroup propertiesEndpointGroupB = PropertiesEndpointGroup.of(file.toPath(), "serverA.hosts"); propertiesEndpointGroupA.close(); propertiesEndpointGroupB.close(); }
@Test public void propertiesFileRestart() throws Exception { final File file = folder.newFile("temp-file.properties"); PrintWriter printWriter = new PrintWriter(file); Properties props = new Properties(); props.setProperty("serverA.hosts.0", "127.0.0.1:8080"); props.store(printWriter, ""); printWriter.close(); final PropertiesEndpointGroup endpointGroupA = PropertiesEndpointGroup.of( file.toPath(), "serverA.hosts"); await().untilAsserted(() -> assertThat(endpointGroupA.endpoints()).hasSize(1)); endpointGroupA.close(); final PropertiesEndpointGroup endpointGroupB = PropertiesEndpointGroup.of( file.toPath(), "serverB.hosts"); await().untilAsserted(() -> assertThat(endpointGroupB.endpoints()).isEmpty()); printWriter = new PrintWriter(file); props = new Properties(); props.setProperty("serverB.hosts.0", "127.0.0.1:8080"); props.setProperty("serverB.hosts.1", "127.0.0.1:8081"); props.store(printWriter, ""); printWriter.close(); await().untilAsserted(() -> assertThat(endpointGroupB.endpoints()).hasSize(2)); endpointGroupB.close(); }
@Test public void endpointChangePropagatesToListeners() throws Exception { final File file = folder.newFile("temp-file.properties"); PrintWriter printWriter = new PrintWriter(file); Properties props = new Properties(); props.setProperty("serverA.hosts.0", "127.0.0.1:8080"); props.setProperty("serverA.hosts.1", "127.0.0.1:8081"); props.store(printWriter, ""); printWriter.close(); final PropertiesEndpointGroup propertiesEndpointGroup = PropertiesEndpointGroup.of( file.toPath(), "serverA.hosts"); final EndpointGroup fallbackEndpointGroup = Endpoint.of("127.0.0.1", 8081); final EndpointGroup endpointGroup = propertiesEndpointGroup.orElse(fallbackEndpointGroup); await().untilAsserted(() -> assertThat(endpointGroup.endpoints()).hasSize(2)); printWriter = new PrintWriter(file); props = new Properties(); props.store(printWriter, ""); printWriter.close(); await().untilAsserted(() -> assertThat(endpointGroup.endpoints()).hasSize(1)); printWriter = new PrintWriter(file); props = new Properties(); props.setProperty("serverA.hosts.0", "127.0.0.1:8080"); props.setProperty("serverA.hosts.1", "127.0.0.1:8081"); props.setProperty("serverA.hosts.2", "127.0.0.1:8082"); props.store(printWriter, ""); printWriter.close(); await().untilAsserted(() -> assertThat(endpointGroup.endpoints()).hasSize(3)); propertiesEndpointGroup.close(); }