1717package com .mongodb .connection ;
1818
1919import com .mongodb .ConnectionString ;
20- import com .mongodb .MongoClientException ;
2120import com .mongodb .ServerAddress ;
2221import com .mongodb .annotations .Immutable ;
2322import com .mongodb .annotations .NotThreadSafe ;
@@ -519,14 +518,13 @@ public String getShortDescription() {
519518 }
520519
521520 private ClusterSettings (final Builder builder ) {
522- // TODO: Unit test this
523521 if (builder .srvHost != null ) {
524522 if (builder .srvHost .contains (":" )) {
525523 throw new IllegalArgumentException ("The srvHost can not contain a host name that specifies a port" );
526524 }
527525
528- if (builder .hosts . get ( 0 ). getHost () .split ("\\ ." ).length < 3 ) {
529- throw new MongoClientException (format ("An SRV host name '%s' was provided that does not contain at least three parts. "
526+ if (builder .srvHost .split ("\\ ." ).length < 3 ) {
527+ throw new IllegalArgumentException (format ("An SRV host name '%s' was provided that does not contain at least three parts. "
530528 + "It must contain a hostname, domain name and a top level domain." , builder .hosts .get (0 ).getHost ()));
531529 }
532530 }
@@ -535,10 +533,6 @@ private ClusterSettings(final Builder builder) {
535533 throw new IllegalArgumentException ("Multiple hosts cannot be specified when using ClusterType.STANDALONE." );
536534 }
537535
538- if (builder .mode != null && builder .mode == ClusterConnectionMode .SINGLE && builder .hosts .size () > 1 ) {
539- throw new IllegalArgumentException ("Can not directly connect to more than one server" );
540- }
541-
542536 if (builder .requiredReplicaSetName != null ) {
543537 if (builder .requiredClusterType == ClusterType .UNKNOWN ) {
544538 builder .requiredClusterType = ClusterType .REPLICA_SET ;
@@ -550,7 +544,18 @@ private ClusterSettings(final Builder builder) {
550544
551545 srvHost = builder .srvHost ;
552546 hosts = builder .hosts ;
553- mode = builder .mode != null ? builder .mode : hosts .size () == 1 ? ClusterConnectionMode .SINGLE : ClusterConnectionMode .MULTIPLE ;
547+ if (srvHost != null ) {
548+ if (builder .mode == ClusterConnectionMode .SINGLE ) {
549+ throw new IllegalArgumentException ("An SRV host name was provided but the connection mode is not MULTIPLE" );
550+ }
551+ mode = ClusterConnectionMode .MULTIPLE ;
552+ } else {
553+ if (builder .mode == ClusterConnectionMode .SINGLE && builder .hosts .size () > 1 ) {
554+ throw new IllegalArgumentException ("Can not directly connect to more than one server" );
555+ }
556+
557+ mode = builder .mode != null ? builder .mode : hosts .size () == 1 ? ClusterConnectionMode .SINGLE : ClusterConnectionMode .MULTIPLE ;
558+ }
554559 requiredReplicaSetName = builder .requiredReplicaSetName ;
555560 requiredClusterType = builder .requiredClusterType ;
556561 localThresholdMS = builder .localThresholdMS ;
0 commit comments