|
16 | 16 |
|
17 | 17 | import static org.junit.Assert.assertEquals; |
18 | 18 | import static org.junit.Assert.assertFalse; |
| 19 | +import static org.junit.Assert.assertNotEquals; |
19 | 20 | import static org.junit.Assert.assertNotNull; |
20 | 21 | import static org.junit.Assert.assertNull; |
21 | 22 | import static org.junit.Assert.assertThat; |
@@ -338,13 +339,74 @@ public void exceptionTransformedToGoogleAdsException() { |
338 | 339 | } |
339 | 340 | } |
340 | 341 |
|
| 342 | + /** Ensure that can set endpoint on default transport channel. */ |
341 | 343 | @Test |
342 | 344 | public void transportChannelProvider_defaultRequiresEndpoint() { |
343 | 345 | assertTrue( |
344 | 346 | "Default TransportChannelProvider must accept endpoint.", |
345 | 347 | GoogleAdsClient.newBuilder().getTransportChannelProvider().needsEndpoint()); |
346 | 348 | } |
347 | 349 |
|
| 350 | + /** Ensure that hashCode doesn't collide for a test instance. */ |
| 351 | + @Test |
| 352 | + public void hashCode_doesNotCollide() { |
| 353 | + GoogleAdsClient clientA = |
| 354 | + GoogleAdsClient.newBuilder() |
| 355 | + .setCredentials(fakeCredentials) |
| 356 | + .setDeveloperToken(DEVELOPER_TOKEN) |
| 357 | + .build(); |
| 358 | + GoogleAdsClient clientB = |
| 359 | + GoogleAdsClient.newBuilder() |
| 360 | + .setCredentials(fakeCredentials) |
| 361 | + .setDeveloperToken(DEVELOPER_TOKEN + "asdf") |
| 362 | + .build(); |
| 363 | + // Granted there could be hash collisions, but this should not happen for this test case. |
| 364 | + assertNotEquals( |
| 365 | + "Clients with distinct params should have distinct hashCodes", |
| 366 | + clientA.hashCode(), |
| 367 | + clientB.hashCode()); |
| 368 | + } |
| 369 | + |
| 370 | + /** Ensures that equals is true for A == B. */ |
| 371 | + @Test |
| 372 | + public void equals_equalIfSameInstance() { |
| 373 | + GoogleAdsClient client = |
| 374 | + GoogleAdsClient.newBuilder() |
| 375 | + .setCredentials(fakeCredentials) |
| 376 | + .setDeveloperToken(DEVELOPER_TOKEN) |
| 377 | + .build(); |
| 378 | + assertEquals("same instance should be equal", client, client); |
| 379 | + } |
| 380 | + |
| 381 | + /** Ensures that equals is false for A != B, with same config. */ |
| 382 | + @Test |
| 383 | + public void equals_notEqualIfDifferentInstance() { |
| 384 | + GoogleAdsClient clientA = |
| 385 | + GoogleAdsClient.newBuilder() |
| 386 | + .setCredentials(fakeCredentials) |
| 387 | + .setDeveloperToken(DEVELOPER_TOKEN) |
| 388 | + .build(); |
| 389 | + GoogleAdsClient clientB = |
| 390 | + GoogleAdsClient.newBuilder() |
| 391 | + .setCredentials(fakeCredentials) |
| 392 | + .setDeveloperToken(DEVELOPER_TOKEN) |
| 393 | + .build(); |
| 394 | + assertNotEquals("different instances should not be equal", clientA, clientB); |
| 395 | + } |
| 396 | + |
| 397 | + /** Ensures that toString returns a nonnull value with length() > 0. */ |
| 398 | + @Test |
| 399 | + public void toString_returnsNotNull() { |
| 400 | + GoogleAdsClient client = |
| 401 | + GoogleAdsClient.newBuilder() |
| 402 | + .setCredentials(fakeCredentials) |
| 403 | + .setDeveloperToken(DEVELOPER_TOKEN) |
| 404 | + .build(); |
| 405 | + String toString = client.toString(); |
| 406 | + assertNotNull("toString should return a non-null string", toString); |
| 407 | + assertFalse("toString should return a non-empty string", toString.isEmpty()); |
| 408 | + } |
| 409 | + |
348 | 410 | /** Creates an GoogleAdsClient using mock credentials. */ |
349 | 411 | private GoogleAdsClient createTestGoogleAdsClient() { |
350 | 412 | return GoogleAdsClient.newBuilder() |
|
0 commit comments