MongoUtil { public static String replicaSetUsedIn(String addresses) { if (addresses.startsWith("[")) { return null; } int index = addresses.indexOf('/'); if (index < 0) { return null; } return addresses.substring(0, index); } 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 shouldParseReplicaSetName() { assertThat(MongoUtil.replicaSetUsedIn("rs0/")).isEqualTo("rs0"); assertThat(MongoUtil.replicaSetUsedIn("rs0/localhost")).isEqualTo("rs0"); assertThat(MongoUtil.replicaSetUsedIn("rs0/[::1/128]")).isEqualTo("rs0"); }
@Test public void shouldNotParseReplicaSetName() { assertThat(MongoUtil.replicaSetUsedIn("")).isNull(); assertThat(MongoUtil.replicaSetUsedIn("localhost")).isNull(); assertThat(MongoUtil.replicaSetUsedIn("[::1/128]")).isNull(); }