1616
1717package com .mongodb ;
1818
19+ import com .mongodb .lang .Nullable ;
1920import junit .framework .TestCase ;
2021import org .bson .BsonArray ;
2122import org .bson .BsonDocument ;
@@ -87,19 +88,20 @@ protected void testValidHostIdentifiers() {
8788 try {
8889 connectionString = new ConnectionString (input );
8990 } catch (Throwable t ) {
90- assertTrue (String .format ("Connection string '%s' should not have thrown an exception: %s" , input , t . toString ()), false );
91+ fail (String .format ("Connection string '%s' should not have thrown an exception: %s" , input , t ) );
9192 }
9293
9394 assertExpectedHosts (connectionString .getHosts ());
9495 }
9596
97+ @ SuppressWarnings ("ConstantConditions" )
9698 protected void testValidOptions () {
9799 ConnectionString connectionString = null ;
98100
99101 try {
100102 connectionString = new ConnectionString (input );
101103 } catch (Throwable t ) {
102- assertTrue (String .format ("Connection string '%s' should not have thrown an exception: %s" , input , t . toString ()), false );
104+ fail (String .format ("Connection string '%s' should not have thrown an exception: %s" , input , t ) );
103105 }
104106
105107 for (Map .Entry <String , BsonValue > option : definition .getDocument ("options" ).entrySet ()) {
@@ -116,63 +118,60 @@ protected void testValidOptions() {
116118 String actual = connectionString .getCredential ().getAuthenticationMechanism ().getMechanismName ();
117119 assertEquals (expected , actual );
118120 }
119- } else if (option .getKey ().toLowerCase (). equals ("retrywrites" )) {
121+ } else if (option .getKey ().equalsIgnoreCase ("retrywrites" )) {
120122 boolean expected = option .getValue ().asBoolean ().getValue ();
121123 assertEquals (expected , connectionString .getRetryWritesValue ().booleanValue ());
122- } else if (option .getKey ().toLowerCase (). equals ("replicaset" )) {
124+ } else if (option .getKey ().equalsIgnoreCase ("replicaset" )) {
123125 String expected = option .getValue ().asString ().getValue ();
124126 assertEquals (expected , connectionString .getRequiredReplicaSetName ());
125- } else if (option .getKey ().toLowerCase (). equals ("serverselectiontimeoutms" )) {
127+ } else if (option .getKey ().equalsIgnoreCase ("serverselectiontimeoutms" )) {
126128 int expected = option .getValue ().asInt32 ().getValue ();
127129 assertEquals (expected , connectionString .getServerSelectionTimeout ().intValue ());
128- } else if (option .getKey ().toLowerCase (). equals ("sockettimeoutms" )) {
130+ } else if (option .getKey ().equalsIgnoreCase ("sockettimeoutms" )) {
129131 int expected = option .getValue ().asInt32 ().getValue ();
130132 assertEquals (expected , connectionString .getSocketTimeout ().intValue ());
131- } else if (option .getKey ().equals ("wtimeoutms" )) {
133+ } else if (option .getKey ().equalsIgnoreCase ("wtimeoutms" )) {
132134 int expected = option .getValue ().asInt32 ().getValue ();
133135 assertEquals (expected , connectionString .getWriteConcern ().getWTimeout (TimeUnit .MILLISECONDS ).intValue ());
134- } else if (option .getKey ().toLowerCase (). equals ("connecttimeoutms" )) {
136+ } else if (option .getKey ().equalsIgnoreCase ("connecttimeoutms" )) {
135137 int expected = option .getValue ().asInt32 ().getValue ();
136138 assertEquals (expected , connectionString .getConnectTimeout ().intValue ());
137- } else if (option .getKey ().toLowerCase (). equals ("heartbeatfrequencyms" )) {
139+ } else if (option .getKey ().equalsIgnoreCase ("heartbeatfrequencyms" )) {
138140 int expected = option .getValue ().asInt32 ().getValue ();
139141 assertEquals (expected , connectionString .getHeartbeatFrequency ().intValue ());
140- } else if (option .getKey ().toLowerCase (). equals ("localthresholdms" )) {
142+ } else if (option .getKey ().equalsIgnoreCase ("localthresholdms" )) {
141143 int expected = option .getValue ().asInt32 ().getValue ();
142144 assertEquals (expected , connectionString .getLocalThreshold ().intValue ());
143- } else if (option .getKey ().toLowerCase (). equals ("maxidletimems" )) {
145+ } else if (option .getKey ().equalsIgnoreCase ("maxidletimems" )) {
144146 int expected = option .getValue ().asInt32 ().getValue ();
145147 assertEquals (expected , connectionString .getMaxConnectionIdleTime ().intValue ());
146- } else if (option .getKey ().toLowerCase (). equals ("tls" )) {
148+ } else if (option .getKey ().equalsIgnoreCase ("tls" )) {
147149 boolean expected = option .getValue ().asBoolean ().getValue ();
148150 assertEquals (expected , connectionString .getSslEnabled ().booleanValue ());
149- } else if (option .getKey ().toLowerCase (). equals ("tlsinsecure" )) {
151+ } else if (option .getKey ().equalsIgnoreCase ("tlsinsecure" )) {
150152 boolean expected = option .getValue ().asBoolean ().getValue ();
151153 assertEquals (expected , connectionString .getSslInvalidHostnameAllowed ().booleanValue ());
152- } else if (option .getKey ().toLowerCase (). equals ("readconcernlevel" )) {
154+ } else if (option .getKey ().equalsIgnoreCase ("readconcernlevel" )) {
153155 String expected = option .getValue ().asString ().getValue ();
154156 assertEquals (expected , connectionString .getReadConcern ().getLevel ().getValue ());
155- } else if (option .getKey ().toLowerCase (). equals ("w" )) {
157+ } else if (option .getKey ().equalsIgnoreCase ("w" )) {
156158 if (option .getValue ().isString ()) {
157159 String expected = option .getValue ().asString ().getValue ();
158160 assertEquals (expected , connectionString .getWriteConcern ().getWString ());
159161 } else {
160162 int expected = option .getValue ().asNumber ().intValue ();
161163 assertEquals (expected , connectionString .getWriteConcern ().getW ());
162164 }
163- } else if (option .getKey ().toLowerCase ().equals ("wtimeoutms" )) {
164- int expected = option .getValue ().asInt32 ().getValue ();
165- assertEquals (expected , connectionString .getWriteConcern ().getWTimeout (TimeUnit .MILLISECONDS ).intValue ());
166- } else if (option .getKey ().toLowerCase ().equals ("journal" )) {
165+ } else if (option .getKey ().equalsIgnoreCase ("journal" )) {
167166 boolean expected = option .getValue ().asBoolean ().getValue ();
168167 assertEquals (expected , connectionString .getWriteConcern ().getJournal ().booleanValue ());
169- } else if (option .getKey ().toLowerCase (). equals ("readpreference" )) {
168+ } else if (option .getKey ().equalsIgnoreCase ("readpreference" )) {
170169 String expected = option .getValue ().asString ().getValue ();
171170 assertEquals (expected , connectionString .getReadPreference ().getName ());
172- } else if (option .getKey ().toLowerCase (). equals ("readpreferencetags" )) {
171+ } else if (option .getKey ().equalsIgnoreCase ("readpreferencetags" )) {
173172 BsonArray expected = option .getValue ().asArray ();
174173 assertEquals (expected , connectionString .getReadPreference ().toDocument ().getArray ("tags" ));
175- } else if (option .getKey ().toLowerCase (). equals ("maxstalenessseconds" )) {
174+ } else if (option .getKey ().equalsIgnoreCase ("maxstalenessseconds" )) {
176175 int expected = option .getValue ().asNumber ().intValue ();
177176 assertEquals (expected , connectionString .getReadPreference ().toDocument ().getNumber ("maxStalenessSeconds" ).intValue ());
178177 } else if (option .getKey ().equals ("compressors" )) {
@@ -182,19 +181,19 @@ protected void testValidOptions() {
182181 String expected = expectedCompressorList .get (i ).asString ().getValue ();
183182 assertEquals (expected , connectionString .getCompressorList ().get (i ).getName ());
184183 }
185- } else if (option .getKey ().toLowerCase (). equals ("zlibcompressionlevel" )) {
184+ } else if (option .getKey ().equalsIgnoreCase ("zlibcompressionlevel" )) {
186185 int expected = option .getValue ().asNumber ().intValue ();
187186 assertEquals (expected , connectionString .getCompressorList ().get (0 ).getProperty ("level" , 0 ).intValue ());
188- } else if (option .getKey ().toLowerCase (). equals ("appname" )) {
187+ } else if (option .getKey ().equalsIgnoreCase ("appname" )) {
189188 String expected = option .getValue ().asString ().getValue ();
190189 assertEquals (expected , connectionString .getApplicationName ());
191- } else if (option .getKey ().toLowerCase (). equals ("authmechanism" )) {
190+ } else if (option .getKey ().equalsIgnoreCase ("authmechanism" )) {
192191 String expected = option .getValue ().asString ().getValue ();
193192 assertEquals (expected , connectionString .getCredential ().getMechanism ());
194- } else if (option .getKey ().toLowerCase (). equals ("authsource" )) {
193+ } else if (option .getKey ().equalsIgnoreCase ("authsource" )) {
195194 String expected = option .getValue ().asString ().getValue ();
196195 assertEquals (expected , connectionString .getCredential ().getSource ());
197- } else if (option .getKey ().toLowerCase (). equals ("authmechanismproperties" )) {
196+ } else if (option .getKey ().equalsIgnoreCase ("authmechanismproperties" )) {
198197 BsonDocument properties = option .getValue ().asDocument ();
199198 for (String cur : properties .keySet ()) {
200199 if (properties .get (cur ).isString ()) {
@@ -205,20 +204,20 @@ protected void testValidOptions() {
205204 assertEquals (expected , connectionString .getCredential ().getMechanismProperty (cur , (Boolean ) null ).booleanValue ());
206205 }
207206 }
208- } else if (option .getKey ().toLowerCase (). equals ("loadbalanced" )) {
207+ } else if (option .getKey ().equalsIgnoreCase ("loadbalanced" )) {
209208 Boolean expected = option .getValue ().asBoolean ().getValue ();
210209 assertEquals (expected , connectionString .isLoadBalanced ());
211- } else if (option .getKey ().toLowerCase (). equals ("directconnection" )) {
210+ } else if (option .getKey ().equalsIgnoreCase ("directconnection" )) {
212211 Boolean expected = option .getValue ().asBoolean ().getValue ();
213212 assertEquals (expected , connectionString .isDirectConnection ());
214- } else if (option .getKey ().toLowerCase (). equals ("maxpoolsize" )) {
213+ } else if (option .getKey ().equalsIgnoreCase ("maxpoolsize" )) {
215214 Integer expected = option .getValue ().asNumber ().intValue ();
216215 assertEquals (expected , connectionString .getMaxConnectionPoolSize ());
217- } else if (option .getKey ().toLowerCase (). equals ("minpoolsize" )) {
216+ } else if (option .getKey ().equalsIgnoreCase ("minpoolsize" )) {
218217 Integer expected = option .getValue ().asNumber ().intValue ();
219218 assertEquals (expected , connectionString .getMinConnectionPoolSize ());
220219 } else {
221- assertTrue (String .format ("Unsupported option '%s' in '%s'" , option .getKey (), input ), false );
220+ fail (String .format ("Unsupported option '%s' in '%s'" , option .getKey (), input ));
222221 }
223222 }
224223 }
@@ -233,7 +232,7 @@ protected void testValidAuth() {
233232 // We don't allow null passwords without setting the authentication mechanism.
234233 return ;
235234 } else {
236- assertTrue (String .format ("Connection string '%s' should not have throw an exception: %s" , input , t . toString ()), false );
235+ fail (String .format ("Connection string '%s' should not have throw an exception: %s" , input , t ) );
237236 }
238237 }
239238
@@ -266,7 +265,7 @@ protected void testValidAuth() {
266265 }
267266 }
268267
269- private void assertString (final String key , final String actual ) {
268+ private void assertString (final String key , @ Nullable final String actual ) {
270269 BsonValue expected = definition ;
271270 if (key .contains ("." )) {
272271 for (String subKey : key .split ("\\ ." )) {
@@ -277,12 +276,12 @@ private void assertString(final String key, final String actual) {
277276 }
278277
279278 if (expected .isNull ()) {
280- assertTrue (String .format ("%s should be null" , key ), actual == null );
279+ assertNull (String .format ("%s should be null" , key ), actual );
281280 } else if (expected .isString ()) {
282281 String expectedString = expected .asString ().getValue ();
283- assertTrue (String .format ("%s should be %s but was %s" , key , actual , expectedString ), actual . equals ( expectedString ) );
282+ assertEquals (String .format ("%s should be %s but was %s" , key , actual , expectedString ), actual , expectedString );
284283 } else {
285- assertTrue (String .format ("%s should be %s but was %s" , key , actual , expected ), false );
284+ fail (String .format ("%s should be %s but was %s" , key , actual , expected ));
286285 }
287286 }
288287
@@ -312,6 +311,7 @@ private void assertExpectedHosts(final List<String> hosts) {
312311 assertEquals (expectedHosts , cleanedHosts );
313312 }
314313
314+ @ SuppressWarnings ("ConstantConditions" )
315315 private String getAuthDB (final ConnectionString connectionString ) {
316316 if (connectionString .getCollection () != null ) {
317317 return connectionString .getDatabase () + "." + connectionString .getCollection ();
0 commit comments