MongoUtil { protected static List<ServerAddress> parseAddresses(String addressStr) { List<ServerAddress> addresses = new ArrayList<>(); if (addressStr != null) { addressStr = addressStr.trim(); for (String address : ADDRESS_DELIMITER_PATTERN.split(addressStr)) { String hostAndPort = null; if (address.startsWith("[")) { hostAndPort = address; } else { int index = address.indexOf("/["); if (index >= 0) { if ((index + 2) < address.length()) { hostAndPort = address.substring(index + 1); } else { continue; } } else { index = address.indexOf("/"); if (index >= 0) { if ((index + 1) < address.length()) { hostAndPort = address.substring(index + 1); } else { hostAndPort = ServerAddress.defaultHost(); } } else { hostAndPort = address; } } } ServerAddress newAddress = parseAddress(hostAndPort); if (newAddress != null) { addresses.add(newAddress); } } } return addresses; } 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 shouldParseServerAddressesWithoutPort() { addresses = MongoUtil.parseAddresses("host1,host2,[::1/128],host4"); assertThat(addresses.size()).isEqualTo(4); assertThat(addresses.get(0).getHost()).isEqualTo("host1"); assertThat(addresses.get(0).getPort()).isEqualTo(ServerAddress.defaultPort()); assertThat(addresses.get(1).getHost()).isEqualTo("host2"); assertThat(addresses.get(1).getPort()).isEqualTo(ServerAddress.defaultPort()); assertThat(addresses.get(2).getHost()).isEqualTo("::1/128"); assertThat(addresses.get(2).getPort()).isEqualTo(ServerAddress.defaultPort()); assertThat(addresses.get(3).getHost()).isEqualTo("host4"); assertThat(addresses.get(3).getPort()).isEqualTo(ServerAddress.defaultPort()); }
@Test public void shouldParseServerAddressesWithPort() { addresses = MongoUtil.parseAddresses("host1:2111,host2:3111,[ff02::2:ff00:0/104]:4111,host4:5111"); assertThat(addresses.size()).isEqualTo(4); assertThat(addresses.get(0).getHost()).isEqualTo("host1"); assertThat(addresses.get(0).getPort()).isEqualTo(2111); assertThat(addresses.get(1).getHost()).isEqualTo("host2"); assertThat(addresses.get(1).getPort()).isEqualTo(3111); assertThat(addresses.get(2).getHost()).isEqualTo("ff02::2:ff00:0/104"); assertThat(addresses.get(2).getPort()).isEqualTo(4111); assertThat(addresses.get(3).getHost()).isEqualTo("host4"); assertThat(addresses.get(3).getPort()).isEqualTo(5111); }
@Test public void shouldParseServerAddressesWithReplicaSetNameAndWithoutPort() { addresses = MongoUtil.parseAddresses("replicaSetName/host1,host2,[::1/128],host4"); assertThat(addresses.size()).isEqualTo(4); assertThat(addresses.get(0).getHost()).isEqualTo("host1"); assertThat(addresses.get(0).getPort()).isEqualTo(ServerAddress.defaultPort()); assertThat(addresses.get(1).getHost()).isEqualTo("host2"); assertThat(addresses.get(1).getPort()).isEqualTo(ServerAddress.defaultPort()); assertThat(addresses.get(2).getHost()).isEqualTo("::1/128"); assertThat(addresses.get(2).getPort()).isEqualTo(ServerAddress.defaultPort()); assertThat(addresses.get(3).getHost()).isEqualTo("host4"); assertThat(addresses.get(3).getPort()).isEqualTo(ServerAddress.defaultPort()); }
@Test public void shouldParseServerAddressesWithReplicaSetNameAndWithPort() { addresses = MongoUtil.parseAddresses("replicaSetName/host1:2111,host2:3111,[ff02::2:ff00:0/104]:4111,host4:5111"); assertThat(addresses.size()).isEqualTo(4); assertThat(addresses.get(0).getHost()).isEqualTo("host1"); assertThat(addresses.get(0).getPort()).isEqualTo(2111); assertThat(addresses.get(1).getHost()).isEqualTo("host2"); assertThat(addresses.get(1).getPort()).isEqualTo(3111); assertThat(addresses.get(2).getHost()).isEqualTo("ff02::2:ff00:0/104"); assertThat(addresses.get(2).getPort()).isEqualTo(4111); assertThat(addresses.get(3).getHost()).isEqualTo("host4"); assertThat(addresses.get(3).getPort()).isEqualTo(5111); }
@Test public void shouldParseServerIPv6AddressesWithReplicaSetNameAndWithoutPort() { addresses = MongoUtil.parseAddresses("replicaSetName/[::1/128],host2,[ff02::2:ff00:0/104],host4"); assertThat(addresses.size()).isEqualTo(4); assertThat(addresses.get(0).getHost()).isEqualTo("::1/128"); assertThat(addresses.get(0).getPort()).isEqualTo(ServerAddress.defaultPort()); assertThat(addresses.get(1).getHost()).isEqualTo("host2"); assertThat(addresses.get(1).getPort()).isEqualTo(ServerAddress.defaultPort()); assertThat(addresses.get(2).getHost()).isEqualTo("ff02::2:ff00:0/104"); assertThat(addresses.get(2).getPort()).isEqualTo(ServerAddress.defaultPort()); assertThat(addresses.get(3).getHost()).isEqualTo("host4"); assertThat(addresses.get(3).getPort()).isEqualTo(ServerAddress.defaultPort()); }
@Test public void shouldParseServerIPv6AddressesWithReplicaSetNameAndWithPort() { addresses = MongoUtil.parseAddresses("replicaSetName/[::1/128]:2111,host2:3111,[ff02::2:ff00:0/104]:4111,host4:5111"); assertThat(addresses.size()).isEqualTo(4); assertThat(addresses.get(0).getHost()).isEqualTo("::1/128"); assertThat(addresses.get(0).getPort()).isEqualTo(2111); assertThat(addresses.get(1).getHost()).isEqualTo("host2"); assertThat(addresses.get(1).getPort()).isEqualTo(3111); assertThat(addresses.get(2).getHost()).isEqualTo("ff02::2:ff00:0/104"); assertThat(addresses.get(2).getPort()).isEqualTo(4111); assertThat(addresses.get(3).getHost()).isEqualTo("host4"); assertThat(addresses.get(3).getPort()).isEqualTo(5111); }
@Test public void shouldNotParseServerAddressesWithReplicaSetNameAndOpenBracket() { addresses = MongoUtil.parseAddresses("replicaSetName/["); assertThat(addresses.size()).isEqualTo(0); }
@Test public void shouldNotParseServerAddressesWithReplicaSetNameAndNoAddress() { addresses = MongoUtil.parseAddresses("replicaSetName/"); assertThat(addresses.size()).isEqualTo(1); assertThat(addresses.get(0).getHost()).isEqualTo(ServerAddress.defaultHost()); assertThat(addresses.get(0).getPort()).isEqualTo(ServerAddress.defaultPort()); }