Search in sources :

Example 11 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class NativeAuthenticatorUnitTest method before.

@Before
public void before() {
    connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
    connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
    credential = MongoCredential.createMongoCRCredential("台北", "database", "Táiběi".toCharArray());
    subject = new NativeAuthenticator(this.credential);
}
Also used : ServerAddress(com.mongodb.ServerAddress) Before(org.junit.Before)

Example 12 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class ServerDescriptionTest method testBuilder.

@Test
public void testBuilder() throws UnknownHostException {
    IllegalArgumentException exception = new IllegalArgumentException();
    ServerDescription serverDescription = builder().address(new ServerAddress("localhost:27018")).type(ServerType.REPLICA_SET_PRIMARY).tagSet(new TagSet(new Tag("dc", "ny"))).setName("test").maxDocumentSize(100).roundTripTime(50000, java.util.concurrent.TimeUnit.NANOSECONDS).primary("localhost:27017").canonicalAddress("localhost:27018").hosts(new HashSet<String>(asList("localhost:27017", "localhost:27018", "localhost:27019", "localhost:27020"))).arbiters(new HashSet<String>(asList("localhost:27019"))).passives(new HashSet<String>(asList("localhost:27020"))).ok(true).state(CONNECTED).version(new ServerVersion(asList(2, 4, 1))).minWireVersion(1).maxWireVersion(2).electionId(new ObjectId("123412341234123412341234")).setVersion(new Integer(2)).lastWriteDate(new Date(1234L)).lastUpdateTimeNanos(40000L).exception(exception).build();
    assertEquals(new ServerAddress("localhost:27018"), serverDescription.getAddress());
    assertTrue(serverDescription.isOk());
    assertEquals(CONNECTED, serverDescription.getState());
    assertEquals(REPLICA_SET_PRIMARY, serverDescription.getType());
    assertTrue(serverDescription.isReplicaSetMember());
    assertFalse(serverDescription.isShardRouter());
    assertFalse(serverDescription.isStandAlone());
    assertTrue(serverDescription.isPrimary());
    assertFalse(serverDescription.isSecondary());
    assertEquals(50000, serverDescription.getRoundTripTimeNanos(), 0L);
    assertEquals(100, serverDescription.getMaxDocumentSize());
    assertEquals("localhost:27017", serverDescription.getPrimary());
    assertEquals("localhost:27018", serverDescription.getCanonicalAddress());
    assertEquals(new HashSet<String>(asList("localhost:27017", "localhost:27018", "localhost:27019", "localhost:27020")), serverDescription.getHosts());
    assertEquals(new TagSet(new Tag("dc", "ny")), serverDescription.getTagSet());
    assertEquals(new HashSet<String>(asList("localhost:27019")), serverDescription.getArbiters());
    assertEquals(new HashSet<String>(asList("localhost:27020")), serverDescription.getPassives());
    assertEquals("test", serverDescription.getSetName());
    assertEquals(new ServerVersion(asList(2, 4, 1)), serverDescription.getVersion());
    assertEquals(1, serverDescription.getMinWireVersion());
    assertEquals(2, serverDescription.getMaxWireVersion());
    assertEquals(new ObjectId("123412341234123412341234"), serverDescription.getElectionId());
    assertEquals(new Integer(2), serverDescription.getSetVersion());
    assertEquals(new Date(1234), serverDescription.getLastWriteDate());
    assertEquals(40000L, serverDescription.getLastUpdateTime(TimeUnit.NANOSECONDS));
    assertEquals(exception, serverDescription.getException());
}
Also used : ObjectId(org.bson.types.ObjectId) TagSet(com.mongodb.TagSet) ServerAddress(com.mongodb.ServerAddress) Tag(com.mongodb.Tag) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class ServerDescriptionTest method testHasTags.

@Test
public void testHasTags() throws UnknownHostException {
    ServerDescription serverDescription = builder().address(new ServerAddress()).type(ServerType.SHARD_ROUTER).ok(false).state(CONNECTED).build();
    assertFalse(serverDescription.hasTags(new TagSet(asList(new Tag("dc", "ny")))));
    serverDescription = builder().address(new ServerAddress()).type(ServerType.SHARD_ROUTER).ok(true).state(CONNECTED).build();
    assertTrue(serverDescription.hasTags(new TagSet(asList(new Tag("dc", "ny")))));
    serverDescription = builder().address(new ServerAddress()).type(ServerType.STANDALONE).ok(true).state(CONNECTED).build();
    assertTrue(serverDescription.hasTags(new TagSet(asList(new Tag("dc", "ny")))));
    serverDescription = builder().address(new ServerAddress()).type(REPLICA_SET_PRIMARY).ok(true).state(CONNECTED).build();
    assertTrue(serverDescription.hasTags(new TagSet()));
    serverDescription = builder().address(new ServerAddress()).type(REPLICA_SET_PRIMARY).ok(true).tagSet(new TagSet(asList(new Tag("dc", "ca")))).state(CONNECTED).build();
    assertFalse(serverDescription.hasTags(new TagSet(asList(new Tag("dc", "ny")))));
    serverDescription = builder().address(new ServerAddress()).type(REPLICA_SET_PRIMARY).ok(true).tagSet(new TagSet(asList(new Tag("rack", "1")))).state(CONNECTED).build();
    assertFalse(serverDescription.hasTags(new TagSet(asList(new Tag("rack", "2")))));
    serverDescription = builder().address(new ServerAddress()).type(REPLICA_SET_PRIMARY).ok(true).tagSet(new TagSet(asList(new Tag("rack", "1")))).state(CONNECTED).build();
    assertTrue(serverDescription.hasTags(new TagSet(asList(new Tag("rack", "1")))));
}
Also used : TagSet(com.mongodb.TagSet) ServerAddress(com.mongodb.ServerAddress) Tag(com.mongodb.Tag) Test(org.junit.Test)

Example 14 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class ServerDescriptionTest method testObjectOverridesWithUnequalException.

@Test
public void testObjectOverridesWithUnequalException() throws UnknownHostException {
    ServerDescription.Builder builder1 = builder().state(CONNECTING).address(new ServerAddress()).exception(new IllegalArgumentException("This is illegal"));
    ServerDescription.Builder builder2 = builder().state(CONNECTING).address(new ServerAddress()).exception(new IllegalArgumentException("This is also illegal"));
    ServerDescription.Builder builder3 = builder().state(CONNECTING).address(new ServerAddress()).exception(new IllegalStateException("This is illegal"));
    ServerDescription.Builder builder4 = builder().state(CONNECTING).address(new ServerAddress());
    assertThat(builder1.build(), not(builder2.build()));
    assertThat(builder1.build().hashCode(), not(builder2.build().hashCode()));
    assertThat(builder1.build(), not(builder3.build()));
    assertThat(builder1.build().hashCode(), not(builder3.build().hashCode()));
    assertThat(builder1.build(), not(builder4.build()));
    assertThat(builder1.build().hashCode(), not(builder4.build().hashCode()));
    assertThat(builder4.build(), not(builder3.build()));
    assertThat(builder4.build().hashCode(), not(builder3.build().hashCode()));
}
Also used : ServerAddress(com.mongodb.ServerAddress) Test(org.junit.Test)

Example 15 with ServerAddress

use of com.mongodb.ServerAddress in project presto by prestodb.

the class MongoClientConfig method buildSeeds.

private List<ServerAddress> buildSeeds(Iterable<String> hostPorts) {
    ImmutableList.Builder<ServerAddress> builder = ImmutableList.builder();
    for (String hostPort : hostPorts) {
        List<String> values = PORT_SPLITTER.splitToList(hostPort);
        checkArgument(values.size() == 1 || values.size() == 2, "Invalid ServerAddress format. Requires host[:port]");
        try {
            if (values.size() == 1) {
                builder.add(new ServerAddress(values.get(0)));
            } else {
                builder.add(new ServerAddress(values.get(0), Integer.parseInt(values.get(1))));
            }
        } catch (NumberFormatException e) {
            throw Throwables.propagate(e);
        }
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ServerAddress(com.mongodb.ServerAddress)

Aggregations

ServerAddress (com.mongodb.ServerAddress)58 Test (org.junit.Test)22 MongoClient (com.mongodb.MongoClient)13 MongoCredential (com.mongodb.MongoCredential)9 ArrayList (java.util.ArrayList)9 Before (org.junit.Before)9 TagSet (com.mongodb.TagSet)4 ClusterDescription (com.mongodb.connection.ClusterDescription)4 ClusterSettings (com.mongodb.connection.ClusterSettings)4 MongoClient (com.mongodb.reactivestreams.client.MongoClient)4 Tag (com.mongodb.Tag)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 ServerDescription (com.mongodb.connection.ServerDescription)3 UnknownHostException (java.net.UnknownHostException)3 Date (java.util.Date)3 ChunkInfo (org.apache.drill.exec.store.mongo.common.ChunkInfo)3 BsonDocument (org.bson.BsonDocument)3 DataXException (com.alibaba.datax.common.exception.DataXException)2 ConnectionString (com.mongodb.ConnectionString)2 MongoClientURI (com.mongodb.MongoClientURI)2