16
16
17
17
package com .mongodb ;
18
18
19
+ import com .mongodb .lang .Nullable ;
19
20
import junit .framework .TestCase ;
20
21
import org .bson .BsonArray ;
21
22
import org .bson .BsonDocument ;
@@ -87,19 +88,20 @@ protected void testValidHostIdentifiers() {
87
88
try {
88
89
connectionString = new ConnectionString (input );
89
90
} 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 ) );
91
92
}
92
93
93
94
assertExpectedHosts (connectionString .getHosts ());
94
95
}
95
96
97
+ @ SuppressWarnings ("ConstantConditions" )
96
98
protected void testValidOptions () {
97
99
ConnectionString connectionString = null ;
98
100
99
101
try {
100
102
connectionString = new ConnectionString (input );
101
103
} 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 ) );
103
105
}
104
106
105
107
for (Map .Entry <String , BsonValue > option : definition .getDocument ("options" ).entrySet ()) {
@@ -116,63 +118,60 @@ protected void testValidOptions() {
116
118
String actual = connectionString .getCredential ().getAuthenticationMechanism ().getMechanismName ();
117
119
assertEquals (expected , actual );
118
120
}
119
- } else if (option .getKey ().toLowerCase (). equals ("retrywrites" )) {
121
+ } else if (option .getKey ().equalsIgnoreCase ("retrywrites" )) {
120
122
boolean expected = option .getValue ().asBoolean ().getValue ();
121
123
assertEquals (expected , connectionString .getRetryWritesValue ().booleanValue ());
122
- } else if (option .getKey ().toLowerCase (). equals ("replicaset" )) {
124
+ } else if (option .getKey ().equalsIgnoreCase ("replicaset" )) {
123
125
String expected = option .getValue ().asString ().getValue ();
124
126
assertEquals (expected , connectionString .getRequiredReplicaSetName ());
125
- } else if (option .getKey ().toLowerCase (). equals ("serverselectiontimeoutms" )) {
127
+ } else if (option .getKey ().equalsIgnoreCase ("serverselectiontimeoutms" )) {
126
128
int expected = option .getValue ().asInt32 ().getValue ();
127
129
assertEquals (expected , connectionString .getServerSelectionTimeout ().intValue ());
128
- } else if (option .getKey ().toLowerCase (). equals ("sockettimeoutms" )) {
130
+ } else if (option .getKey ().equalsIgnoreCase ("sockettimeoutms" )) {
129
131
int expected = option .getValue ().asInt32 ().getValue ();
130
132
assertEquals (expected , connectionString .getSocketTimeout ().intValue ());
131
- } else if (option .getKey ().equals ("wtimeoutms" )) {
133
+ } else if (option .getKey ().equalsIgnoreCase ("wtimeoutms" )) {
132
134
int expected = option .getValue ().asInt32 ().getValue ();
133
135
assertEquals (expected , connectionString .getWriteConcern ().getWTimeout (TimeUnit .MILLISECONDS ).intValue ());
134
- } else if (option .getKey ().toLowerCase (). equals ("connecttimeoutms" )) {
136
+ } else if (option .getKey ().equalsIgnoreCase ("connecttimeoutms" )) {
135
137
int expected = option .getValue ().asInt32 ().getValue ();
136
138
assertEquals (expected , connectionString .getConnectTimeout ().intValue ());
137
- } else if (option .getKey ().toLowerCase (). equals ("heartbeatfrequencyms" )) {
139
+ } else if (option .getKey ().equalsIgnoreCase ("heartbeatfrequencyms" )) {
138
140
int expected = option .getValue ().asInt32 ().getValue ();
139
141
assertEquals (expected , connectionString .getHeartbeatFrequency ().intValue ());
140
- } else if (option .getKey ().toLowerCase (). equals ("localthresholdms" )) {
142
+ } else if (option .getKey ().equalsIgnoreCase ("localthresholdms" )) {
141
143
int expected = option .getValue ().asInt32 ().getValue ();
142
144
assertEquals (expected , connectionString .getLocalThreshold ().intValue ());
143
- } else if (option .getKey ().toLowerCase (). equals ("maxidletimems" )) {
145
+ } else if (option .getKey ().equalsIgnoreCase ("maxidletimems" )) {
144
146
int expected = option .getValue ().asInt32 ().getValue ();
145
147
assertEquals (expected , connectionString .getMaxConnectionIdleTime ().intValue ());
146
- } else if (option .getKey ().toLowerCase (). equals ("tls" )) {
148
+ } else if (option .getKey ().equalsIgnoreCase ("tls" )) {
147
149
boolean expected = option .getValue ().asBoolean ().getValue ();
148
150
assertEquals (expected , connectionString .getSslEnabled ().booleanValue ());
149
- } else if (option .getKey ().toLowerCase (). equals ("tlsinsecure" )) {
151
+ } else if (option .getKey ().equalsIgnoreCase ("tlsinsecure" )) {
150
152
boolean expected = option .getValue ().asBoolean ().getValue ();
151
153
assertEquals (expected , connectionString .getSslInvalidHostnameAllowed ().booleanValue ());
152
- } else if (option .getKey ().toLowerCase (). equals ("readconcernlevel" )) {
154
+ } else if (option .getKey ().equalsIgnoreCase ("readconcernlevel" )) {
153
155
String expected = option .getValue ().asString ().getValue ();
154
156
assertEquals (expected , connectionString .getReadConcern ().getLevel ().getValue ());
155
- } else if (option .getKey ().toLowerCase (). equals ("w" )) {
157
+ } else if (option .getKey ().equalsIgnoreCase ("w" )) {
156
158
if (option .getValue ().isString ()) {
157
159
String expected = option .getValue ().asString ().getValue ();
158
160
assertEquals (expected , connectionString .getWriteConcern ().getWString ());
159
161
} else {
160
162
int expected = option .getValue ().asNumber ().intValue ();
161
163
assertEquals (expected , connectionString .getWriteConcern ().getW ());
162
164
}
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" )) {
167
166
boolean expected = option .getValue ().asBoolean ().getValue ();
168
167
assertEquals (expected , connectionString .getWriteConcern ().getJournal ().booleanValue ());
169
- } else if (option .getKey ().toLowerCase (). equals ("readpreference" )) {
168
+ } else if (option .getKey ().equalsIgnoreCase ("readpreference" )) {
170
169
String expected = option .getValue ().asString ().getValue ();
171
170
assertEquals (expected , connectionString .getReadPreference ().getName ());
172
- } else if (option .getKey ().toLowerCase (). equals ("readpreferencetags" )) {
171
+ } else if (option .getKey ().equalsIgnoreCase ("readpreferencetags" )) {
173
172
BsonArray expected = option .getValue ().asArray ();
174
173
assertEquals (expected , connectionString .getReadPreference ().toDocument ().getArray ("tags" ));
175
- } else if (option .getKey ().toLowerCase (). equals ("maxstalenessseconds" )) {
174
+ } else if (option .getKey ().equalsIgnoreCase ("maxstalenessseconds" )) {
176
175
int expected = option .getValue ().asNumber ().intValue ();
177
176
assertEquals (expected , connectionString .getReadPreference ().toDocument ().getNumber ("maxStalenessSeconds" ).intValue ());
178
177
} else if (option .getKey ().equals ("compressors" )) {
@@ -182,19 +181,19 @@ protected void testValidOptions() {
182
181
String expected = expectedCompressorList .get (i ).asString ().getValue ();
183
182
assertEquals (expected , connectionString .getCompressorList ().get (i ).getName ());
184
183
}
185
- } else if (option .getKey ().toLowerCase (). equals ("zlibcompressionlevel" )) {
184
+ } else if (option .getKey ().equalsIgnoreCase ("zlibcompressionlevel" )) {
186
185
int expected = option .getValue ().asNumber ().intValue ();
187
186
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" )) {
189
188
String expected = option .getValue ().asString ().getValue ();
190
189
assertEquals (expected , connectionString .getApplicationName ());
191
- } else if (option .getKey ().toLowerCase (). equals ("authmechanism" )) {
190
+ } else if (option .getKey ().equalsIgnoreCase ("authmechanism" )) {
192
191
String expected = option .getValue ().asString ().getValue ();
193
192
assertEquals (expected , connectionString .getCredential ().getMechanism ());
194
- } else if (option .getKey ().toLowerCase (). equals ("authsource" )) {
193
+ } else if (option .getKey ().equalsIgnoreCase ("authsource" )) {
195
194
String expected = option .getValue ().asString ().getValue ();
196
195
assertEquals (expected , connectionString .getCredential ().getSource ());
197
- } else if (option .getKey ().toLowerCase (). equals ("authmechanismproperties" )) {
196
+ } else if (option .getKey ().equalsIgnoreCase ("authmechanismproperties" )) {
198
197
BsonDocument properties = option .getValue ().asDocument ();
199
198
for (String cur : properties .keySet ()) {
200
199
if (properties .get (cur ).isString ()) {
@@ -205,20 +204,20 @@ protected void testValidOptions() {
205
204
assertEquals (expected , connectionString .getCredential ().getMechanismProperty (cur , (Boolean ) null ).booleanValue ());
206
205
}
207
206
}
208
- } else if (option .getKey ().toLowerCase (). equals ("loadbalanced" )) {
207
+ } else if (option .getKey ().equalsIgnoreCase ("loadbalanced" )) {
209
208
Boolean expected = option .getValue ().asBoolean ().getValue ();
210
209
assertEquals (expected , connectionString .isLoadBalanced ());
211
- } else if (option .getKey ().toLowerCase (). equals ("directconnection" )) {
210
+ } else if (option .getKey ().equalsIgnoreCase ("directconnection" )) {
212
211
Boolean expected = option .getValue ().asBoolean ().getValue ();
213
212
assertEquals (expected , connectionString .isDirectConnection ());
214
- } else if (option .getKey ().toLowerCase (). equals ("maxpoolsize" )) {
213
+ } else if (option .getKey ().equalsIgnoreCase ("maxpoolsize" )) {
215
214
Integer expected = option .getValue ().asNumber ().intValue ();
216
215
assertEquals (expected , connectionString .getMaxConnectionPoolSize ());
217
- } else if (option .getKey ().toLowerCase (). equals ("minpoolsize" )) {
216
+ } else if (option .getKey ().equalsIgnoreCase ("minpoolsize" )) {
218
217
Integer expected = option .getValue ().asNumber ().intValue ();
219
218
assertEquals (expected , connectionString .getMinConnectionPoolSize ());
220
219
} 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 ));
222
221
}
223
222
}
224
223
}
@@ -233,7 +232,7 @@ protected void testValidAuth() {
233
232
// We don't allow null passwords without setting the authentication mechanism.
234
233
return ;
235
234
} 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 ) );
237
236
}
238
237
}
239
238
@@ -266,7 +265,7 @@ protected void testValidAuth() {
266
265
}
267
266
}
268
267
269
- private void assertString (final String key , final String actual ) {
268
+ private void assertString (final String key , @ Nullable final String actual ) {
270
269
BsonValue expected = definition ;
271
270
if (key .contains ("." )) {
272
271
for (String subKey : key .split ("\\ ." )) {
@@ -277,12 +276,12 @@ private void assertString(final String key, final String actual) {
277
276
}
278
277
279
278
if (expected .isNull ()) {
280
- assertTrue (String .format ("%s should be null" , key ), actual == null );
279
+ assertNull (String .format ("%s should be null" , key ), actual );
281
280
} else if (expected .isString ()) {
282
281
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 );
284
283
} 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 ));
286
285
}
287
286
}
288
287
@@ -312,6 +311,7 @@ private void assertExpectedHosts(final List<String> hosts) {
312
311
assertEquals (expectedHosts , cleanedHosts );
313
312
}
314
313
314
+ @ SuppressWarnings ("ConstantConditions" )
315
315
private String getAuthDB (final ConnectionString connectionString ) {
316
316
if (connectionString .getCollection () != null ) {
317
317
return connectionString .getDatabase () + "." + connectionString .getCollection ();
0 commit comments