@Test public void testReplaceDuplicateTokensGetSamePort() throws Exception { final int expectedPort = PortUtils.MINIMUM_PORT + 1; PortUtils.PortLocator portLocator = Mockito.mock(PortUtils.PortLocator.class); Mockito.when(portLocator.random()).thenAnswer(new Answer<Integer>() { public int callCount; @Override public Integer answer(InvocationOnMock invocation) throws Throwable { if (this.callCount++ == 0) { return expectedPort; } return expectedPort + callCount++; } }); Mockito.when(portLocator.specific(expectedPort)).thenReturn(expectedPort); PortUtils portUtils = new PortUtils(portLocator); String actual = portUtils.replacePortTokens("-Dvar1=${PORT_?} -Dvar2=${PORT_?}"); Assert.assertEquals(actual, "-Dvar1=1026 -Dvar2=1026"); }
public String replacePortTokens(String value) { BiMap<String, Optional<Integer>> portMappings = HashBiMap.create(); Matcher regexMatcher = PORT_REGEX.matcher(value); while (regexMatcher.find()) { String token = regexMatcher.group(0); if (!portMappings.containsKey(token)) { Optional<Integer> portStart = Optional.absent(); Optional<Integer> portEnd = Optional.absent(); String unboundedStart = regexMatcher.group(1); if (unboundedStart != null) { int requestedEndPort = Integer.parseInt(unboundedStart); Preconditions.checkArgument(requestedEndPort <= PortUtils.MAXIMUM_PORT); portEnd = Optional.of(requestedEndPort); } else { String unboundedEnd = regexMatcher.group(2); if (unboundedEnd != null) { int requestedStartPort = Integer.parseInt(unboundedEnd); Preconditions.checkArgument(requestedStartPort >= PortUtils.MINIMUM_PORT); portStart = Optional.of(requestedStartPort); } else { String absolute = regexMatcher.group(3); if (!"?".equals(absolute)) { int requestedPort = Integer.parseInt(absolute); Preconditions.checkArgument(requestedPort >= PortUtils.MINIMUM_PORT && requestedPort <= PortUtils.MAXIMUM_PORT); portStart = Optional.of(requestedPort); portEnd = Optional.of(requestedPort); } } } Optional<Integer> port = takePort(portStart, portEnd); portMappings.put(token, port); } } for (Map.Entry<String, Optional<Integer>> port : portMappings.entrySet()) { if (port.getValue().isPresent()) { value = value.replace(port.getKey(), port.getValue().get().toString()); } } return value; }
PortUtils { public String replacePortTokens(String value) { BiMap<String, Optional<Integer>> portMappings = HashBiMap.create(); Matcher regexMatcher = PORT_REGEX.matcher(value); while (regexMatcher.find()) { String token = regexMatcher.group(0); if (!portMappings.containsKey(token)) { Optional<Integer> portStart = Optional.absent(); Optional<Integer> portEnd = Optional.absent(); String unboundedStart = regexMatcher.group(1); if (unboundedStart != null) { int requestedEndPort = Integer.parseInt(unboundedStart); Preconditions.checkArgument(requestedEndPort <= PortUtils.MAXIMUM_PORT); portEnd = Optional.of(requestedEndPort); } else { String unboundedEnd = regexMatcher.group(2); if (unboundedEnd != null) { int requestedStartPort = Integer.parseInt(unboundedEnd); Preconditions.checkArgument(requestedStartPort >= PortUtils.MINIMUM_PORT); portStart = Optional.of(requestedStartPort); } else { String absolute = regexMatcher.group(3); if (!"?".equals(absolute)) { int requestedPort = Integer.parseInt(absolute); Preconditions.checkArgument(requestedPort >= PortUtils.MINIMUM_PORT && requestedPort <= PortUtils.MAXIMUM_PORT); portStart = Optional.of(requestedPort); portEnd = Optional.of(requestedPort); } } } Optional<Integer> port = takePort(portStart, portEnd); portMappings.put(token, port); } } for (Map.Entry<String, Optional<Integer>> port : portMappings.entrySet()) { if (port.getValue().isPresent()) { value = value.replace(port.getKey(), port.getValue().get().toString()); } } return value; } }
PortUtils { public String replacePortTokens(String value) { BiMap<String, Optional<Integer>> portMappings = HashBiMap.create(); Matcher regexMatcher = PORT_REGEX.matcher(value); while (regexMatcher.find()) { String token = regexMatcher.group(0); if (!portMappings.containsKey(token)) { Optional<Integer> portStart = Optional.absent(); Optional<Integer> portEnd = Optional.absent(); String unboundedStart = regexMatcher.group(1); if (unboundedStart != null) { int requestedEndPort = Integer.parseInt(unboundedStart); Preconditions.checkArgument(requestedEndPort <= PortUtils.MAXIMUM_PORT); portEnd = Optional.of(requestedEndPort); } else { String unboundedEnd = regexMatcher.group(2); if (unboundedEnd != null) { int requestedStartPort = Integer.parseInt(unboundedEnd); Preconditions.checkArgument(requestedStartPort >= PortUtils.MINIMUM_PORT); portStart = Optional.of(requestedStartPort); } else { String absolute = regexMatcher.group(3); if (!"?".equals(absolute)) { int requestedPort = Integer.parseInt(absolute); Preconditions.checkArgument(requestedPort >= PortUtils.MINIMUM_PORT && requestedPort <= PortUtils.MAXIMUM_PORT); portStart = Optional.of(requestedPort); portEnd = Optional.of(requestedPort); } } } Optional<Integer> port = takePort(portStart, portEnd); portMappings.put(token, port); } } for (Map.Entry<String, Optional<Integer>> port : portMappings.entrySet()) { if (port.getValue().isPresent()) { value = value.replace(port.getKey(), port.getValue().get().toString()); } } return value; } PortUtils(); @VisibleForTesting  PortUtils(PortLocator locator); }
PortUtils { public String replacePortTokens(String value) { BiMap<String, Optional<Integer>> portMappings = HashBiMap.create(); Matcher regexMatcher = PORT_REGEX.matcher(value); while (regexMatcher.find()) { String token = regexMatcher.group(0); if (!portMappings.containsKey(token)) { Optional<Integer> portStart = Optional.absent(); Optional<Integer> portEnd = Optional.absent(); String unboundedStart = regexMatcher.group(1); if (unboundedStart != null) { int requestedEndPort = Integer.parseInt(unboundedStart); Preconditions.checkArgument(requestedEndPort <= PortUtils.MAXIMUM_PORT); portEnd = Optional.of(requestedEndPort); } else { String unboundedEnd = regexMatcher.group(2); if (unboundedEnd != null) { int requestedStartPort = Integer.parseInt(unboundedEnd); Preconditions.checkArgument(requestedStartPort >= PortUtils.MINIMUM_PORT); portStart = Optional.of(requestedStartPort); } else { String absolute = regexMatcher.group(3); if (!"?".equals(absolute)) { int requestedPort = Integer.parseInt(absolute); Preconditions.checkArgument(requestedPort >= PortUtils.MINIMUM_PORT && requestedPort <= PortUtils.MAXIMUM_PORT); portStart = Optional.of(requestedPort); portEnd = Optional.of(requestedPort); } } } Optional<Integer> port = takePort(portStart, portEnd); portMappings.put(token, port); } } for (Map.Entry<String, Optional<Integer>> port : portMappings.entrySet()) { if (port.getValue().isPresent()) { value = value.replace(port.getKey(), port.getValue().get().toString()); } } return value; } PortUtils(); @VisibleForTesting  PortUtils(PortLocator locator); String replacePortTokens(String value); }
PortUtils { public String replacePortTokens(String value) { BiMap<String, Optional<Integer>> portMappings = HashBiMap.create(); Matcher regexMatcher = PORT_REGEX.matcher(value); while (regexMatcher.find()) { String token = regexMatcher.group(0); if (!portMappings.containsKey(token)) { Optional<Integer> portStart = Optional.absent(); Optional<Integer> portEnd = Optional.absent(); String unboundedStart = regexMatcher.group(1); if (unboundedStart != null) { int requestedEndPort = Integer.parseInt(unboundedStart); Preconditions.checkArgument(requestedEndPort <= PortUtils.MAXIMUM_PORT); portEnd = Optional.of(requestedEndPort); } else { String unboundedEnd = regexMatcher.group(2); if (unboundedEnd != null) { int requestedStartPort = Integer.parseInt(unboundedEnd); Preconditions.checkArgument(requestedStartPort >= PortUtils.MINIMUM_PORT); portStart = Optional.of(requestedStartPort); } else { String absolute = regexMatcher.group(3); if (!"?".equals(absolute)) { int requestedPort = Integer.parseInt(absolute); Preconditions.checkArgument(requestedPort >= PortUtils.MINIMUM_PORT && requestedPort <= PortUtils.MAXIMUM_PORT); portStart = Optional.of(requestedPort); portEnd = Optional.of(requestedPort); } } } Optional<Integer> port = takePort(portStart, portEnd); portMappings.put(token, port); } } for (Map.Entry<String, Optional<Integer>> port : portMappings.entrySet()) { if (port.getValue().isPresent()) { value = value.replace(port.getKey(), port.getValue().get().toString()); } } return value; } PortUtils(); @VisibleForTesting  PortUtils(PortLocator locator); String replacePortTokens(String value); static final int MINIMUM_PORT; static final int MAXIMUM_PORT; }