MongoUtil { public static ServerAddress parseAddress(String addressStr) { if (addressStr != null) { addressStr = addressStr.trim(); Matcher matcher = ADDRESS_PATTERN.matcher(addressStr); if (!matcher.matches()) { matcher = IPV6_ADDRESS_PATTERN.matcher(addressStr); } if (matcher.matches()) { String host = matcher.group(1); String port = matcher.group(3); if (port == null) { return new ServerAddress(host.trim()); } return new ServerAddress(host.trim(), Integer.parseInt(port)); } } return null; } private  MongoUtil(); static String replicaSetUsedIn(String addresses); static void forEachDatabaseName(MongoClient client, Consumer<String> operation); static void forEachCollectionNameInDatabase(MongoClient client, String databaseName, Consumer<String> operation); static void forEach(MongoIterable<T> iterable, Consumer<T> operation); static void onDatabase(MongoClient client, String dbName, Consumer<MongoDatabase> dbOperation); static void onCollection(MongoClient client, String dbName, String collectionName,
                                    Consumer<MongoCollection<Document>> collectionOperation); static void onCollectionDocuments(MongoClient client, String dbName, String collectionName,
                                             BlockingConsumer<Document> documentOperation); static boolean contains(MongoIterable<String> iterable, String match); static boolean contains(MongoIterable<T> iterable, Predicate<T> matcher); static ServerAddress parseAddress(String addressStr); static String getOplogSessionTransactionId(Document oplogEvent); static final Pattern ADDRESS_DELIMITER_PATTERN; }
@Test public void shouldParseIPv4ServerAddressWithoutPort() { address = MongoUtil.parseAddress("localhost"); assertThat(address.getHost()).isEqualTo("localhost"); assertThat(address.getPort()).isEqualTo(ServerAddress.defaultPort()); }
@Test public void shouldParseIPv4ServerAddressWithoPort() { address = MongoUtil.parseAddress("localhost:28017"); assertThat(address.getHost()).isEqualTo("localhost"); assertThat(address.getPort()).isEqualTo(28017); }
@Test public void shouldParseIPv6ServerAddressWithoutPort() { address = MongoUtil.parseAddress("[::1/128]"); assertThat(address.getHost()).isEqualTo("::1/128"); assertThat(address.getPort()).isEqualTo(ServerAddress.defaultPort()); }
@Test public void shouldParseIPv6ServerAddressWithPort() { address = MongoUtil.parseAddress("[::1/128]:28017"); assertThat(address.getHost()).isEqualTo("::1/128"); assertThat(address.getPort()).isEqualTo(28017); }