From 4b1caed0edb4b3140fd72b2cd22bb1db15c9d198 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Thu, 16 May 2024 11:22:51 -0400 Subject: [PATCH 1/8] test: use Junit5 in api-common --- api-common-java/pom.xml | 15 +- .../com/google/api/core/ApiFuturesTest.java | 4 +- .../com/google/api/core/ApiServiceTest.java | 12 +- .../core/ListenableFutureToApiFutureTest.java | 2 +- .../api/core/SettableApiFutureTest.java | 20 +- .../api/pathtemplate/PathTemplateTest.java | 288 ++++++++++-------- .../TemplatedResourceNameTest.java | 6 +- .../UntypedResourceNameTest.java | 14 +- 8 files changed, 205 insertions(+), 156 deletions(-) diff --git a/api-common-java/pom.xml b/api-common-java/pom.xml index c8740cd068..1fbb0bf8a1 100644 --- a/api-common-java/pom.xml +++ b/api-common-java/pom.xml @@ -69,9 +69,18 @@ - junit - junit - 4.13.2 + org.junit.platform + junit-platform-launcher + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.vintage + junit-vintage-engine test diff --git a/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java b/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java index 34dc41dda2..20d90b1b29 100644 --- a/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java +++ b/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java @@ -38,12 +38,12 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicInteger; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class ApiFuturesTest { @Test - public void testAddCallback() throws Exception { + public void testAddCallback() { final AtomicInteger flag = new AtomicInteger(); SettableApiFuture future = SettableApiFuture.create(); ApiFutures.addCallback( diff --git a/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java b/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java index 19b10af472..46ef0f2147 100644 --- a/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java +++ b/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java @@ -31,8 +31,8 @@ import com.google.common.util.concurrent.MoreExecutors; import java.util.concurrent.atomic.AtomicReference; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class ApiServiceTest { @Test @@ -50,7 +50,7 @@ protected void doStart() { } }; service.startAsync().awaitRunning(); - Assert.assertTrue(service.isRunning()); + Assertions.assertTrue(service.isRunning()); service.stopAsync().awaitTerminated(); } @@ -86,8 +86,8 @@ public void failed(ApiService.State from, Throwable failure) { // Expected } - Assert.assertEquals(service.state(), ApiService.State.FAILED); - Assert.assertEquals(savedFailure.get().getMessage(), "this service always fails"); - Assert.assertEquals(service.failureCause().getMessage(), "this service always fails"); + Assertions.assertEquals(service.state(), ApiService.State.FAILED); + Assertions.assertEquals(savedFailure.get().getMessage(), "this service always fails"); + Assertions.assertEquals(service.failureCause().getMessage(), "this service always fails"); } } diff --git a/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java b/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java index 9bc06dae07..8c6d556cea 100644 --- a/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java +++ b/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java @@ -31,7 +31,7 @@ import com.google.common.truth.Truth; import com.google.common.util.concurrent.SettableFuture; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class ListenableFutureToApiFutureTest { diff --git a/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java b/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java index a4cff9b086..d1e54a954e 100644 --- a/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java +++ b/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java @@ -30,11 +30,11 @@ package com.google.api.core; import com.google.common.truth.Truth; -import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class SettableApiFutureTest { @Test @@ -48,7 +48,7 @@ public void testSet() throws Exception { } @Test - public void testCancel() throws Exception { + public void testCancel() { SettableApiFuture future = SettableApiFuture.create(); Truth.assertThat(future.isDone()).isFalse(); Truth.assertThat(future.isCancelled()).isFalse(); @@ -57,15 +57,17 @@ public void testCancel() throws Exception { Truth.assertThat(future.isCancelled()).isTrue(); } - @Test(expected = ExecutionException.class) - public void testException() throws Exception { - SettableApiFuture future = SettableApiFuture.create(); - future.setException(new Exception()); - future.get(); + @Test + public void testException() { + Assertions.assertThrows(Exception.class, () -> { + SettableApiFuture future = SettableApiFuture.create(); + future.setException(new Exception()); + future.get(); + }); } @Test - public void testListener() throws Exception { + public void testListener() { final AtomicInteger flag = new AtomicInteger(); SettableApiFuture future = SettableApiFuture.create(); future.addListener( diff --git a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java index 47747a1159..6db9db225d 100644 --- a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java +++ b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java @@ -32,23 +32,18 @@ import com.google.common.collect.ImmutableMap; import com.google.common.truth.Truth; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import java.util.Map; import java.util.Set; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; +import java.util.stream.Stream; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** Tests for {@link PathTemplate}. */ -@RunWith(JUnit4.class) public class PathTemplateTest { - @Rule public ExpectedException thrown = ExpectedException.none(); - // Match // ===== @@ -307,17 +302,16 @@ public void complexResourceIdEqualsWildcard() { } @Test + @Disabled + //TODO: This test was passing erroneously, see https://github.com/googleapis/sdk-platform-java/issues/2778 public void complexResourceIdEqualsPathWildcard() { - thrown.expect(ValidationException.class); - PathTemplate template = PathTemplate.create("projects/{project=*}/zones/{zone_a=**}~{zone_b}"); - thrown.expectMessage( - String.format( - "parse error: wildcard path not allowed in complex ID resource '%s'", "zone_a")); + Exception exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/{project=*}/zones/{zone_a=**}~{zone_b}")); + Assertions.assertEquals(String.format( + "parse error: wildcard path not allowed in complex ID resource '%s'", "zone_a"), exception.getMessage()); - template = PathTemplate.create("projects/{project=*}/zones/{zone_a}.{zone_b=**}"); - thrown.expectMessage( - String.format( - "parse error: wildcard path not allowed in complex ID resource '%s'", "zone_b")); + exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/{project=*}/zones/{zone_a}.{zone_b=**}")); + Assertions.assertEquals(String.format( + "parse error: wildcard path not allowed in complex ID resource '%s'", "zone_b"), exception.getMessage()); } @Test @@ -338,35 +332,35 @@ public void complexResourceIdMissingMatches() { @Test public void complexResourceIdNoSeparator() { - thrown.expect(ValidationException.class); - PathTemplate.create("projects/{project}/zones/{zone_a}{zone_b}"); - thrown.expectMessage( - String.format( - "parse error: missing or 2+ consecutive delimiter characters in '%s'", - "{zone_a}{zone_b}")); - - PathTemplate.create("projects/{project}/zones/{zone_a}_{zone_b}{zone_c}"); - thrown.expectMessage( - String.format( - "parse error: missing or 2+ consecutive delimiter characters in '%s'", - "{zone_a}_{zone_b}{zone_c}")); - } - - @Test - public void complexResourceIdInvalidDelimiter() { - thrown.expect(ValidationException.class); - // Not a comprehensive set of invalid delimiters, please check the class's defined pattern. - List someInvalidDelimiters = - new ArrayList<>(Arrays.asList("|", "!", "@", "a", "1", ",", "{", ")")); - for (String invalidDelimiter : someInvalidDelimiters) { - PathTemplate.create( - String.format("projects/{project=*}/zones/{zone_a}%s{zone_b}", invalidDelimiter)); - thrown.expectMessage( + Exception exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/{project}/zones/{zone_a}{zone_b}")); + Assertions.assertEquals(String.format( + "parse error: missing or 2+ consecutive delimiter characters in '%s'", + "{zone_a}{zone_b}"), exception.getMessage()); + + exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/{project}/zones/{zone_a}_{zone_b}{zone_c}")); + Assertions.assertEquals(String.format( + "parse error: missing or 2+ consecutive delimiter characters in '%s'", + "{zone_a}_{zone_b}{zone_c}"), exception.getMessage()); + } + + @Disabled + // TODO: This test was passing erroneously, see https://github.com/googleapis/sdk-platform-java/issues/2776 + @ParameterizedTest + @MethodSource("invalidDelimiters") + public void complexResourceIdInvalidDelimiter(String invalidDelimiter) { + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create(String.format("projects/{project=*}/zones/{zone_a}%s{zone_b}", invalidDelimiter)) + ); + Assertions.assertEquals( String.format( "parse error: invalid complex resource ID delimiter character in '%s'", - String.format("{zone_a}%s{zone_b}", invalidDelimiter))); + String.format("zone_a}%s{zone_b}", invalidDelimiter)), + exception.getMessage() + ); + } + static Stream invalidDelimiters() { + return Stream.of("|", "!", "@", "a", "1", ",", "{", ")"); } - } @Test public void complexResourceIdMixedSeparators() { @@ -412,9 +406,10 @@ public void collectionWildcardMatchingInParent() { @Test public void collectionWildcardMatchingInvalid() { - thrown.expect(ValidationException.class); - PathTemplate.create("v1/publishers/{publisher}/books/-"); - } + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("v1/publishers/{publisher}/books/-") + ); + }; @Test public void complexResourceIdPubSubDeletedTopic() { @@ -479,56 +474,83 @@ public void complexResourcePathTemplateVariables() { @Test public void complexResourceBasicInvalidIds() { - thrown.expect(ValidationException.class); - PathTemplate.create("projects/*/zones/~{zone_a}"); - thrown.expectMessage( - String.format("parse error: invalid begin or end character in '%s'", "~{zone_a}")); - - PathTemplate.create("projects/*/zones/{zone_a}~"); - thrown.expectMessage( - String.format("parse error: invalid begin or end character in '%s'", "{zone_a}~")); - - PathTemplate.create("projects/*/zones/.{zone_a}"); - thrown.expectMessage( - String.format("parse error: invalid begin or end character in '%s'", ".{zone_a}")); - - PathTemplate.create("projects/*/zones/{zone_a}."); - thrown.expectMessage( - String.format("parse error: invalid begin or end character in '%s'", "{zone_a}.")); - - PathTemplate.create("projects/*/zones/-{zone_a}"); - thrown.expectMessage( - String.format("parse error: invalid begin or end character in '%s'", "-{zone_a}")); + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("projects/*/zones/~{zone_a}") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", "~{zone_a}"), + exception.getMessage() + ); + exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("projects/*/zones/{zone_a}~") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", "{zone_a}~"), + exception.getMessage() + ); + exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("projects/*/zones/.{zone_a}") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", ".{zone_a}"), + exception.getMessage() + ); + exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("projects/*/zones/{zone_a}.") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", "{zone_a}."), + exception.getMessage() + ); + exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("projects/*/zones/-{zone_a}") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", "-{zone_a}"), + exception.getMessage() + ); + exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("projects/*/zones/{zone_a}-") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", "{zone_a}-"), + exception.getMessage() + ); + exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("projects/*/zones/_{zone_a}") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", "_{zone_a}"), + exception.getMessage() + ); + exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("projects/*/zones/{zone_a}_") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", "{zone_a}_"), + exception.getMessage() + ); - PathTemplate.create("projects/*/zones/{zone_a}-"); - thrown.expectMessage( - String.format("parse error: invalid begin or end character in '%s'", "{zone_a}-")); - - PathTemplate.create("projects/*/zones/_{zone_a}"); - thrown.expectMessage( - String.format("parse error: invalid begin or end character in '%s'", "{zone_a}_")); - - PathTemplate.create("projects/*/zones/{zone_a}_"); - thrown.expectMessage( - String.format("parse error: invalid begin or end character in '%s'", "{zone_a}_")); } @Test public void complexResourceMultipleDelimiters() { - thrown.expect(ValidationException.class); - - PathTemplate.create("projects/*/zones/{zone_a}~.{zone_b}"); - thrown.expectMessage( - String.format( - "parse error: missing or 2+ consecutive delimiter characters in '%s'", - "{zone_a}~.{zone_b}")); - - PathTemplate.create("projects/*/zones/{zone_a}~{zone_b}..{zone_c}"); - thrown.expectMessage( - String.format( - "parse error: missing or 2+ consecutive delimiter characters in '%s'", - "{zone_a}~{zone_b}..{zone_c}")); - + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("projects/*/zones/{zone_a}~.{zone_b}") + ); + Assertions.assertEquals( + String.format( "parse error: missing or 2+ consecutive delimiter characters in '%s'", + "{zone_a}~.{zone_b}"), + exception.getMessage() + ); + exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("projects/*/zones/{zone_a}~{zone_b}..{zone_c}") + ); + Assertions.assertEquals( + String.format("parse error: missing or 2+ consecutive delimiter characters in '%s'", + "{zone_a}~{zone_b}..{zone_c}"), + exception.getMessage() + ); String pathString = "projects/project_123/zones/lorum~ipsum"; PathTemplate template = PathTemplate.create("projects/*/zones/{zone_.~-a}~{zone_b}"); template.validate(pathString, ""); @@ -553,13 +575,16 @@ public void validateSuccess() { @Test public void validateFailure() { - thrown.expect(ValidationException.class); String templateString = "buckets/*/objects/*"; String pathString = "buckets/bucket/invalid/object"; - thrown.expectMessage( - String.format("Parameter \"%s\" must be in the form \"%s\"", pathString, templateString)); PathTemplate template = PathTemplate.create(templateString); - template.validate(pathString, ""); + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + template.validate(pathString, "") + ); + Assertions.assertEquals( + String.format(": Parameter \"%s\" must be in the form \"%s\"", pathString, templateString), + exception.getMessage() + ); } @Test @@ -574,13 +599,16 @@ public void validateMatchSuccess() { @Test public void validateMatchFailure() { - thrown.expect(ValidationException.class); String templateString = "buckets/*/objects/*"; String pathString = "buckets/bucket/invalid/object"; - thrown.expectMessage( - String.format("Parameter \"%s\" must be in the form \"%s\"", pathString, templateString)); PathTemplate template = PathTemplate.create(templateString); - template.validatedMatch(pathString, ""); + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + template.validatedMatch(pathString, "") + ); + Assertions.assertEquals( + String.format(": Parameter \"%s\" must be in the form \"%s\"", pathString, templateString), + exception.getMessage() + ); } // Instantiate @@ -609,9 +637,10 @@ public void instantiateNotEscapeForUnboundedWildcard() { @Test public void instantiateFailWhenTooFewVariables() { - thrown.expect(ValidationException.class); PathTemplate template = PathTemplate.create("buckets/*/*/*/objects/*"); - template.instantiate("$0", "f", "1", "o"); + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + template.instantiate("$0", "f", "1", "o") + ); } @Test @@ -638,10 +667,14 @@ public void instantiateWithHostName() { @Test public void instantiateEscapeUnsafeCharNoEncoding() { - thrown.expect(ValidationException.class); - thrown.expectMessage("Invalid character \"/\" in path section \"f/o/o\"."); PathTemplate template = PathTemplate.createWithoutUrlEncoding("buckets/*/objects/*"); - template.instantiate("$0", "f/o/o", "$1", "b/a/r"); + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + template.instantiate("$0", "f/o/o", "$1", "b/a/r") + ); + Assertions.assertEquals( + String.format("Invalid character \"/\" in path section \"f/o/o\"."), + exception.getMessage() + ); } @Test @@ -755,11 +788,14 @@ public void instantiateWithASegmentStartsWithADelimiter() { @Test public void instantiateWithASegmentContainingComplexResourceNamesAndStartsWithADelimiter() { - thrown.expect(ValidationException.class); - PathTemplate.create( - "v1beta1/{parent=projects/*/locations/*/clusters/*}/.{well}-{known}/openid-configuration"); - thrown.expectMessage( - String.format("parse error: invalid begin or end character in '%s'", ".{well}-{known}")); + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create( + "v1beta1/{parent=projects/*/locations/*/clusters/*}/.{well}-{known}/openid-configuration") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", ".{well}-{known}"), + exception.getMessage() + ); } @Test @@ -775,9 +811,13 @@ public void instantiateWithASegmentContainingComplexResourceNamesAndStartsWithAD @Test public void instantiateWithASegmentOnlyContainingOneDelimiter() { - thrown.expect(ValidationException.class); - PathTemplate.create("v1/publishers/{publisher}/books/."); - thrown.expectMessage(String.format("parse error: invalid begin or end character in '%s'", ".")); + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create("v1/publishers/{publisher}/books/.") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", "."), + exception.getMessage() + ); } @Test @@ -799,11 +839,14 @@ public void instantiateWithASegmentEndsWithADelimiter() { @Test public void instantiateWithASegmentContainingComplexResourceNamesAndEndsWithADelimiter() { - thrown.expect(ValidationException.class); - PathTemplate.create( - "v1beta1/{parent=projects/*/locations/*/clusters/*}/{well}-{known}./openid-configuration"); - thrown.expectMessage( - String.format("parse error: invalid begin or end character in '%s'", "{well}-{known}.")); + ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> + PathTemplate.create( + "v1beta1/{parent=projects/*/locations/*/clusters/*}/{well}-{known}./openid-configuration") + ); + Assertions.assertEquals( + String.format("parse error: invalid begin or end character in '%s'", "{well}-{known}."), + exception.getMessage() + ); } @Test @@ -822,8 +865,9 @@ public void instantiateWithASegmentContainingComplexResourceNamesAndEndsWithADel @Test public void testMultiplePathWildcardFailure() { - thrown.expect(IllegalArgumentException.class); - PathTemplate.create("bar/**/{name=foo/**}:verb"); + Assertions.assertThrows(IllegalArgumentException.class, () -> + PathTemplate.create("bar/**/{name=foo/**}:verb") + ); } @Test diff --git a/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java b/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java index bb143932ff..3d2c833edb 100644 --- a/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java +++ b/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java @@ -31,15 +31,11 @@ package com.google.api.pathtemplate; import com.google.common.truth.Truth; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - +import org.junit.jupiter.api.Test; /** * Tests for {@link TemplatedResourceName}. As resource names are mostly a wrapper around path * templates, not much needs to be done here. */ -@RunWith(JUnit4.class) public class TemplatedResourceNameTest { @Test diff --git a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java index c0615da416..e132c44263 100644 --- a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java +++ b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java @@ -30,17 +30,15 @@ package com.google.api.resourcenames; import static junit.framework.TestCase.fail; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Map; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; +import org.junit.jupiter.api.Test; /** Tests for {@link UntypedResourceNameTest}. */ -@RunWith(JUnit4.class) + public class UntypedResourceNameTest { private static final String NAME_STRING = "sunshine"; private static final String EMPTY_STRING = ""; @@ -85,7 +83,7 @@ public void testInsertIntoFieldValuesMap() { public void testNullName() { assertFalse(UntypedResourceName.isParsableFrom(null)); try { - UntypedResourceName fooName = UntypedResourceName.parse(null); + UntypedResourceName.parse(null); } catch (NullPointerException e) { } } From 1177b5f5215e5aa8b9170032b9656aded90bbc47 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Thu, 16 May 2024 11:54:24 -0400 Subject: [PATCH 2/8] remove public keyword from testing methods --- .../com/google/api/core/ApiFuturesTest.java | 38 ++--- .../com/google/api/core/ApiServiceTest.java | 4 +- .../core/ListenableFutureToApiFutureTest.java | 2 +- .../api/core/SettableApiFutureTest.java | 8 +- .../api/pathtemplate/PathTemplateTest.java | 130 +++++++++--------- .../TemplatedResourceNameTest.java | 2 +- .../UntypedResourceNameTest.java | 6 +- 7 files changed, 91 insertions(+), 99 deletions(-) diff --git a/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java b/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java index 20d90b1b29..6c6fcee4a3 100644 --- a/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java +++ b/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java @@ -43,7 +43,7 @@ public class ApiFuturesTest { @Test - public void testAddCallback() { + void testAddCallback() { final AtomicInteger flag = new AtomicInteger(); SettableApiFuture future = SettableApiFuture.create(); ApiFutures.addCallback( @@ -65,7 +65,7 @@ public void onFailure(Throwable t) { } @Test - public void testCatch() throws Exception { + void testCatch() throws Exception { SettableApiFuture future = SettableApiFuture.create(); ApiFuture fallback = ApiFutures.catching( @@ -83,7 +83,7 @@ public Integer apply(Exception ex) { } @Test - public void testCatchAsync() throws Exception { + void testCatchAsync() throws Exception { SettableApiFuture future = SettableApiFuture.create(); ApiFuture fallback = ApiFutures.catchingAsync( @@ -101,7 +101,7 @@ public ApiFuture apply(Exception ex) { } @Test - public void testTransform() throws Exception { + void testTransform() throws Exception { SettableApiFuture inputFuture = SettableApiFuture.create(); ApiFuture transformedFuture = ApiFutures.transform( @@ -118,7 +118,7 @@ public String apply(Integer input) { } @Test - public void testTransformWithExecutor() throws Exception { + void testTransformWithExecutor() throws Exception { SettableApiFuture inputFuture = SettableApiFuture.create(); final AtomicInteger counter = new AtomicInteger(0); ApiFuture transformedFuture = @@ -143,7 +143,7 @@ public void execute(Runnable command) { } @Test - public void testAllAsList() throws Exception { + void testAllAsList() throws Exception { SettableApiFuture inputFuture1 = SettableApiFuture.create(); SettableApiFuture inputFuture2 = SettableApiFuture.create(); ApiFuture> listFuture = @@ -154,7 +154,7 @@ public void testAllAsList() throws Exception { } @Test - public void successfulAllAsList() throws Exception { + void successfulAllAsList() throws Exception { SettableApiFuture inputFuture1 = SettableApiFuture.create(); SettableApiFuture inputFuture2 = SettableApiFuture.create(); ApiFuture> listFuture = @@ -165,7 +165,7 @@ public void successfulAllAsList() throws Exception { } @Test - public void testTransformAsync() throws Exception { + void testTransformAsync() throws Exception { ApiFuture inputFuture = ApiFutures.immediateFuture(0); ApiFuture outputFuture = ApiFutures.transformAsync( @@ -181,31 +181,23 @@ public ApiFuture apply(Integer input) { } @Test - public void testTransformAsyncWithExecutor() throws Exception { + void testTransformAsyncWithExecutor() throws Exception { ApiFuture inputFuture = ApiFutures.immediateFuture(0); final AtomicInteger counter = new AtomicInteger(0); ApiFuture outputFuture = ApiFutures.transformAsync( inputFuture, - new ApiAsyncFunction() { - @Override - public ApiFuture apply(Integer input) { - return ApiFutures.immediateFuture(input + 1); - } - }, - new Executor() { - @Override - public void execute(Runnable command) { - counter.incrementAndGet(); - command.run(); - } + (ApiAsyncFunction) input -> ApiFutures.immediateFuture(input + 1), + (Executor) command -> { + counter.incrementAndGet(); + command.run(); }); assertThat(outputFuture.get()).isEqualTo(1); assertThat(counter.get()).isEqualTo(1); } @Test - public void testImmediateFailedFuture() throws InterruptedException { + void testImmediateFailedFuture() throws InterruptedException { ApiFuture future = ApiFutures.immediateFailedFuture(new IllegalArgumentException("The message")); IllegalArgumentException exception = null; @@ -219,7 +211,7 @@ public void testImmediateFailedFuture() throws InterruptedException { } @Test - public void testImmediateCancelledFuture() throws InterruptedException, ExecutionException { + void testImmediateCancelledFuture() throws InterruptedException, ExecutionException { ApiFuture future = ApiFutures.immediateCancelledFuture(); CancellationException exception = null; try { diff --git a/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java b/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java index 46ef0f2147..05c2552980 100644 --- a/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java +++ b/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java @@ -36,7 +36,7 @@ public class ApiServiceTest { @Test - public void testNoopService() { + void testNoopService() { ApiService service = new AbstractApiService() { @Override @@ -55,7 +55,7 @@ protected void doStart() { } @Test - public void testFailingService() { + void testFailingService() { final AtomicReference savedFailure = new AtomicReference<>(); ApiService service = new AbstractApiService() { diff --git a/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java b/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java index 8c6d556cea..854f355f37 100644 --- a/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java +++ b/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java @@ -36,7 +36,7 @@ public class ListenableFutureToApiFutureTest { @Test - public void testGet() throws Exception { + void testGet() throws Exception { SettableFuture future = SettableFuture.create(); ListenableFutureToApiFuture apiFuture = new ListenableFutureToApiFuture<>(future); future.set(3); diff --git a/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java b/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java index d1e54a954e..c9a8847d9f 100644 --- a/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java +++ b/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java @@ -38,7 +38,7 @@ public class SettableApiFutureTest { @Test - public void testSet() throws Exception { + void testSet() throws Exception { SettableApiFuture future = SettableApiFuture.create(); Truth.assertThat(future.isDone()).isFalse(); future.set(42); @@ -48,7 +48,7 @@ public void testSet() throws Exception { } @Test - public void testCancel() { + void testCancel() { SettableApiFuture future = SettableApiFuture.create(); Truth.assertThat(future.isDone()).isFalse(); Truth.assertThat(future.isCancelled()).isFalse(); @@ -58,7 +58,7 @@ public void testCancel() { } @Test - public void testException() { + void testException() { Assertions.assertThrows(Exception.class, () -> { SettableApiFuture future = SettableApiFuture.create(); future.setException(new Exception()); @@ -67,7 +67,7 @@ public void testException() { } @Test - public void testListener() { + void testListener() { final AtomicInteger flag = new AtomicInteger(); SettableApiFuture future = SettableApiFuture.create(); future.addListener( diff --git a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java index 6db9db225d..a11ddb3fc9 100644 --- a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java +++ b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java @@ -48,19 +48,19 @@ public class PathTemplateTest { // ===== @Test - public void matchAtomicResourceName() { + void matchAtomicResourceName() { PathTemplate template = PathTemplate.create("buckets/*/*/objects/*"); assertPositionalMatch(template.match("buckets/f/o/objects/bar"), "f", "o", "bar"); } @Test - public void matchTemplateWithUnboundedWildcard() { + void matchTemplateWithUnboundedWildcard() { PathTemplate template = PathTemplate.create("buckets/*/objects/**"); assertPositionalMatch(template.match("buckets/foo/objects/bar/baz"), "foo", "bar/baz"); } @Test - public void matchWithForcedHostName() { + void matchWithForcedHostName() { PathTemplate template = PathTemplate.create("buckets/*/objects/*"); Map match = template.matchFromFullName("somewhere.io/buckets/b/objects/o"); Truth.assertThat(match).isNotNull(); @@ -70,7 +70,7 @@ public void matchWithForcedHostName() { } @Test - public void matchWithHostName() { + void matchWithHostName() { PathTemplate template = PathTemplate.create("buckets/*/objects/*"); Map match = template.match("//somewhere.io/buckets/b/objects/o"); Truth.assertThat(match).isNotNull(); @@ -80,7 +80,7 @@ public void matchWithHostName() { } @Test - public void matchWithHostNameAndProtocol() { + void matchWithHostNameAndProtocol() { PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone}"); Map match = template.match( @@ -92,7 +92,7 @@ public void matchWithHostNameAndProtocol() { } @Test - public void matchWithHostNameAndProtocolWithTemplateStartWithBinding() { + void matchWithHostNameAndProtocolWithTemplateStartWithBinding() { PathTemplate template = PathTemplate.create("{project}/zones/{zone}"); Map match = template.match( @@ -104,7 +104,7 @@ public void matchWithHostNameAndProtocolWithTemplateStartWithBinding() { } @Test - public void pathWildcards_matchZeroOrMoreSegments() { + void pathWildcards_matchZeroOrMoreSegments() { PathTemplate start = PathTemplate.create("{glob=**}/b"); PathTemplate middle = PathTemplate.create("a/{glob=**}/b"); PathTemplate end = PathTemplate.create("a/{glob=**}"); @@ -131,7 +131,7 @@ public void pathWildcards_matchZeroOrMoreSegments() { } @Test - public void pathWildcard_canMatchTheEmptyString() { + void pathWildcard_canMatchTheEmptyString() { PathTemplate template = PathTemplate.create("{glob=**}"); Truth.assertThat(template.match("").get("glob")).isEmpty(); @@ -140,7 +140,7 @@ public void pathWildcard_canMatchTheEmptyString() { } @Test - public void matchWithCustomMethod() { + void matchWithCustomMethod() { PathTemplate template = PathTemplate.create("buckets/*/objects/*:custom"); Map match = template.match("buckets/b/objects/o:custom"); Truth.assertThat(match).isNotNull(); @@ -149,31 +149,31 @@ public void matchWithCustomMethod() { } @Test - public void matchFailWhenPathMismatch() { + void matchFailWhenPathMismatch() { PathTemplate template = PathTemplate.create("buckets/*/*/objects/*"); Truth.assertThat(template.match("buckets/f/o/o/objects/bar")).isNull(); } @Test - public void matchFailWhenPathTooShort() { + void matchFailWhenPathTooShort() { PathTemplate template = PathTemplate.create("buckets/*/*/objects/*"); Truth.assertThat(template.match("buckets/f/o/objects")).isNull(); } @Test - public void matchFailWhenPathTooLong() { + void matchFailWhenPathTooLong() { PathTemplate template = PathTemplate.create("buckets/*/*/objects/*"); Truth.assertThat(template.match("buckets/f/o/objects/too/long")).isNull(); } @Test - public void matchWithUnboundInMiddle() { + void matchWithUnboundInMiddle() { PathTemplate template = PathTemplate.create("bar/**/foo/*"); assertPositionalMatch(template.match("bar/foo/foo/foo/bar"), "foo/foo", "bar"); } @Test - public void matchWithNamedBindings() { + void matchWithNamedBindings() { PathTemplate template = PathTemplate.create("projects/*/{instance_id=instances/*}/**"); Map actual = template.match("projects/proj_foo/instances/instance_bar/table/table_baz"); @@ -181,7 +181,7 @@ public void matchWithNamedBindings() { } @Test - public void matchFailWithNamedBindingsWhenPathMismatches() { + void matchFailWithNamedBindingsWhenPathMismatches() { PathTemplate template = PathTemplate.create("projects/*/{instance_id=instances/*}/**"); Map actual = template.match("projects/proj_foo/instances_fail/instance_bar/table/table_baz"); @@ -189,21 +189,21 @@ public void matchFailWithNamedBindingsWhenPathMismatches() { } @Test - public void matchWithNamedBindingsThatHasOnlyWildcard() { + void matchWithNamedBindingsThatHasOnlyWildcard() { PathTemplate template = PathTemplate.create("profiles/{routing_id=*}"); Map actual = template.match("profiles/prof_qux"); Truth.assertThat(actual).containsEntry("routing_id", "prof_qux"); } @Test - public void matchFailWithNamedBindingsThatHasOnlyWildcardWhenPathMismatches() { + void matchFailWithNamedBindingsThatHasOnlyWildcardWhenPathMismatches() { PathTemplate template = PathTemplate.create("profiles/{routing_id=*}"); Map actual = template.match("profiles/prof_qux/fail"); Truth.assertThat(actual).isNull(); } @Test - public void matchWithCustomVerbs() { + void matchWithCustomVerbs() { PathTemplate template = PathTemplate.create("**:foo"); assertPositionalMatch(template.match("a/b/c:foo"), "a/b/c"); } @@ -212,7 +212,7 @@ public void matchWithCustomVerbs() { // ======== @Test - public void complexResourceIdBasicCases() { + void complexResourceIdBasicCases() { // Separate by "~". PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone_a}~{zone_b}"); Map match = @@ -251,7 +251,7 @@ public void complexResourceIdBasicCases() { } @Test - public void complexResourceIdCustomVerb() { + void complexResourceIdCustomVerb() { // Separate by "~". PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone_a}~{zone_b}:hello"); Map match = @@ -290,7 +290,7 @@ public void complexResourceIdCustomVerb() { } @Test - public void complexResourceIdEqualsWildcard() { + void complexResourceIdEqualsWildcard() { PathTemplate template = PathTemplate.create("projects/{project=*}/zones/{zone_a=*}~{zone_b=*}"); Map match = template.match("projects/project-123/zones/europe-west3-c~us-east3-a"); @@ -304,7 +304,7 @@ public void complexResourceIdEqualsWildcard() { @Test @Disabled //TODO: This test was passing erroneously, see https://github.com/googleapis/sdk-platform-java/issues/2778 - public void complexResourceIdEqualsPathWildcard() { + void complexResourceIdEqualsPathWildcard() { Exception exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/{project=*}/zones/{zone_a=**}~{zone_b}")); Assertions.assertEquals(String.format( "parse error: wildcard path not allowed in complex ID resource '%s'", "zone_a"), exception.getMessage()); @@ -315,7 +315,7 @@ public void complexResourceIdEqualsPathWildcard() { } @Test - public void complexResourceIdMissingMatches() { + void complexResourceIdMissingMatches() { PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone_a}~{zone_b}"); Truth.assertThat(template.match("projects/project-123/zones/europe-west3-c")).isNull(); @@ -331,7 +331,7 @@ public void complexResourceIdMissingMatches() { } @Test - public void complexResourceIdNoSeparator() { + void complexResourceIdNoSeparator() { Exception exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/{project}/zones/{zone_a}{zone_b}")); Assertions.assertEquals(String.format( "parse error: missing or 2+ consecutive delimiter characters in '%s'", @@ -347,7 +347,7 @@ public void complexResourceIdNoSeparator() { // TODO: This test was passing erroneously, see https://github.com/googleapis/sdk-platform-java/issues/2776 @ParameterizedTest @MethodSource("invalidDelimiters") - public void complexResourceIdInvalidDelimiter(String invalidDelimiter) { + void complexResourceIdInvalidDelimiter(String invalidDelimiter) { ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create(String.format("projects/{project=*}/zones/{zone_a}%s{zone_b}", invalidDelimiter)) ); @@ -363,7 +363,7 @@ static Stream invalidDelimiters() { } @Test - public void complexResourceIdMixedSeparators() { + void complexResourceIdMixedSeparators() { // Separate by a mix of delimiters. PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone_a}~{zone_b}.{zone_c}-{zone_d}"); @@ -392,7 +392,7 @@ public void complexResourceIdMixedSeparators() { } @Test - public void collectionWildcardMatchingInParent() { + void collectionWildcardMatchingInParent() { PathTemplate template = PathTemplate.create("v1/publishers/-/books/{book}"); Map match = template.match( @@ -405,20 +405,20 @@ public void collectionWildcardMatchingInParent() { } @Test - public void collectionWildcardMatchingInvalid() { + void collectionWildcardMatchingInvalid() { ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("v1/publishers/{publisher}/books/-") ); }; @Test - public void complexResourceIdPubSubDeletedTopic() { + void complexResourceIdPubSubDeletedTopic() { PathTemplate template = PathTemplate.create("_deleted-topic_"); Truth.assertThat(template).isNotNull(); } @Test - public void complexResourceIdInParent() { + void complexResourceIdInParent() { // One parent has a complex resource ID. PathTemplate template = PathTemplate.create( @@ -453,7 +453,7 @@ public void complexResourceIdInParent() { } @Test - public void complexResourcePathTemplateVariables() { + void complexResourcePathTemplateVariables() { String pattern = "projects/{foo}_{bar}/zones/{zone_a}-{zone_b}_{zone_c}/machines/{cell1}.{cell2}"; PathTemplate template = PathTemplate.create(pattern); @@ -473,7 +473,7 @@ public void complexResourcePathTemplateVariables() { } @Test - public void complexResourceBasicInvalidIds() { + void complexResourceBasicInvalidIds() { ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/*/zones/~{zone_a}") ); @@ -534,7 +534,7 @@ public void complexResourceBasicInvalidIds() { } @Test - public void complexResourceMultipleDelimiters() { + void complexResourceMultipleDelimiters() { ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/*/zones/{zone_a}~.{zone_b}") ); @@ -565,7 +565,7 @@ public void complexResourceMultipleDelimiters() { // ======== @Test - public void validateSuccess() { + void validateSuccess() { String templateString = "buckets/*/objects/*"; String pathString = "buckets/bucket/objects/object"; PathTemplate template = PathTemplate.create(templateString); @@ -574,7 +574,7 @@ public void validateSuccess() { } @Test - public void validateFailure() { + void validateFailure() { String templateString = "buckets/*/objects/*"; String pathString = "buckets/bucket/invalid/object"; PathTemplate template = PathTemplate.create(templateString); @@ -588,7 +588,7 @@ public void validateFailure() { } @Test - public void validateMatchSuccess() { + void validateMatchSuccess() { String templateString = "buckets/*/objects/{object_id}"; String pathString = "buckets/bucket/objects/object"; PathTemplate template = PathTemplate.create(templateString); @@ -598,7 +598,7 @@ public void validateMatchSuccess() { } @Test - public void validateMatchFailure() { + void validateMatchFailure() { String templateString = "buckets/*/objects/*"; String pathString = "buckets/bucket/invalid/object"; PathTemplate template = PathTemplate.create(templateString); @@ -615,28 +615,28 @@ public void validateMatchFailure() { // =========== @Test - public void instantiateAtomicResource() { + void instantiateAtomicResource() { PathTemplate template = PathTemplate.create("buckets/*/*/*/objects/*"); String url = template.instantiate("$0", "f", "$1", "o", "$2", "o", "$3", "bar"); Truth.assertThat(url).isEqualTo("buckets/f/o/o/objects/bar"); } @Test - public void instantiateEscapeUnsafeChar() { + void instantiateEscapeUnsafeChar() { PathTemplate template = PathTemplate.create("buckets/*/objects/*"); Truth.assertThat(template.instantiate("$0", "f/o/o", "$1", "b/a/r")) .isEqualTo("buckets/f%2Fo%2Fo/objects/b%2Fa%2Fr"); } @Test - public void instantiateNotEscapeForUnboundedWildcard() { + void instantiateNotEscapeForUnboundedWildcard() { PathTemplate template = PathTemplate.create("buckets/*/objects/**"); Truth.assertThat(template.instantiate("$0", "f/o/o", "$1", "b/a/r")) .isEqualTo("buckets/f%2Fo%2Fo/objects/b/a/r"); } @Test - public void instantiateFailWhenTooFewVariables() { + void instantiateFailWhenTooFewVariables() { PathTemplate template = PathTemplate.create("buckets/*/*/*/objects/*"); ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> template.instantiate("$0", "f", "1", "o") @@ -644,20 +644,20 @@ public void instantiateFailWhenTooFewVariables() { } @Test - public void instantiateWithUnboundInMiddle() { + void instantiateWithUnboundInMiddle() { PathTemplate template = PathTemplate.create("bar/**/foo/*"); Truth.assertThat(template.instantiate("$0", "1/2", "$1", "3")).isEqualTo("bar/1/2/foo/3"); } @Test - public void instantiatePartial() { + void instantiatePartial() { PathTemplate template = PathTemplate.create("bar/*/foo/*"); String instance = template.instantiatePartial(ImmutableMap.of("$0", "_1")); Truth.assertThat(instance).isEqualTo("bar/_1/foo/*"); } @Test - public void instantiateWithHostName() { + void instantiateWithHostName() { PathTemplate template = PathTemplate.create("bar/*"); String instance = template.instantiate( @@ -666,7 +666,7 @@ public void instantiateWithHostName() { } @Test - public void instantiateEscapeUnsafeCharNoEncoding() { + void instantiateEscapeUnsafeCharNoEncoding() { PathTemplate template = PathTemplate.createWithoutUrlEncoding("buckets/*/objects/*"); ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> template.instantiate("$0", "f/o/o", "$1", "b/a/r") @@ -678,35 +678,35 @@ public void instantiateEscapeUnsafeCharNoEncoding() { } @Test - public void instantiateNotEscapeForUnboundedWildcardNoEncoding() { + void instantiateNotEscapeForUnboundedWildcardNoEncoding() { PathTemplate template = PathTemplate.createWithoutUrlEncoding("buckets/*/objects/**"); Truth.assertThat(template.instantiate("$0", "foo", "$1", "b/a/r")) .isEqualTo("buckets/foo/objects/b/a/r"); } @Test - public void instantiateWithGoogProject() { + void instantiateWithGoogProject() { PathTemplate template = PathTemplate.create("projects/{project}"); String instance = template.instantiate(ImmutableMap.of("project", "google.com:test-proj")); Truth.assertThat(instance).isEqualTo("projects/google.com%3Atest-proj"); } @Test - public void instantiateWithGoogProjectNoEncoding() { + void instantiateWithGoogProjectNoEncoding() { PathTemplate template = PathTemplate.createWithoutUrlEncoding("projects/{project}"); String instance = template.instantiate(ImmutableMap.of("project", "google.com:test-proj")); Truth.assertThat(instance).isEqualTo("projects/google.com:test-proj"); } @Test - public void instantiateWithUnusualCharactersNoEncoding() { + void instantiateWithUnusualCharactersNoEncoding() { PathTemplate template = PathTemplate.createWithoutUrlEncoding("bar/*"); String instance = template.instantiate(ImmutableMap.of("$0", "asdf:;`~,.<>[]!@#$%^&*()")); Truth.assertThat(instance).isEqualTo("bar/asdf:;`~,.<>[]!@#$%^&*()"); } @Test - public void instantiateWithComplexResourceId_basic() { + void instantiateWithComplexResourceId_basic() { PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone_a}~{zone_b}"); String instance = template.instantiate("project", "a/b/c", "zone_a", "apple", "zone_b", "baseball"); @@ -714,7 +714,7 @@ public void instantiateWithComplexResourceId_basic() { } @Test - public void instantiateWithComplexResourceId_customVerb() { + void instantiateWithComplexResourceId_customVerb() { PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone_a}~{zone_b}:hello"); String instance = template.instantiate("project", "a/b/c", "zone_a", "apple", "zone_b", "baseball"); @@ -726,7 +726,7 @@ public void instantiateWithComplexResourceId_customVerb() { } @Test - public void instantiateWithComplexResourceId_mixedSeparators() { + void instantiateWithComplexResourceId_mixedSeparators() { PathTemplate template = PathTemplate.create( "projects/{project}/zones/{zone_a}~{zone_b}.{zone_c}-{zone_d}~{zone_e}"); @@ -750,7 +750,7 @@ public void instantiateWithComplexResourceId_mixedSeparators() { } @Test - public void instantiateWithComplexResourceId_mixedSeparatorsInParent() { + void instantiateWithComplexResourceId_mixedSeparatorsInParent() { PathTemplate template = PathTemplate.create("projects/{project_a}~{project_b}.{project_c}/zones/{zone_a}~{zone_b}"); String instance = @@ -769,7 +769,7 @@ public void instantiateWithComplexResourceId_mixedSeparatorsInParent() { } @Test - public void instantiateWithCustomVerbs() { + void instantiateWithCustomVerbs() { PathTemplate template = PathTemplate.create("/v1/{name=operations/**}:cancel"); String templateInstance = template.instantiate("name", "operations/3373707"); Truth.assertThat(templateInstance).isEqualTo("v1/operations/3373707:cancel"); @@ -777,7 +777,7 @@ public void instantiateWithCustomVerbs() { } @Test - public void instantiateWithASegmentStartsWithADelimiter() { + void instantiateWithASegmentStartsWithADelimiter() { PathTemplate pathTemplate = PathTemplate.create( "v1beta1/{parent=projects/*/locations/*/clusters/*}/.well-known/openid-configuration"); @@ -787,7 +787,7 @@ public void instantiateWithASegmentStartsWithADelimiter() { } @Test - public void instantiateWithASegmentContainingComplexResourceNamesAndStartsWithADelimiter() { + void instantiateWithASegmentContainingComplexResourceNamesAndStartsWithADelimiter() { ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create( "v1beta1/{parent=projects/*/locations/*/clusters/*}/.{well}-{known}/openid-configuration") @@ -799,7 +799,7 @@ public void instantiateWithASegmentContainingComplexResourceNamesAndStartsWithAD } @Test - public void + void instantiateWithASegmentContainingNoComplexResourceNamesAndStartsWithMultipleDelimiters() { PathTemplate pathTemplate = PathTemplate.create( @@ -810,7 +810,7 @@ public void instantiateWithASegmentContainingComplexResourceNamesAndStartsWithAD } @Test - public void instantiateWithASegmentOnlyContainingOneDelimiter() { + void instantiateWithASegmentOnlyContainingOneDelimiter() { ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("v1/publishers/{publisher}/books/.") ); @@ -821,14 +821,14 @@ public void instantiateWithASegmentOnlyContainingOneDelimiter() { } @Test - public void instantiateWithASegmentOnlyContainingOneCharacter() { + void instantiateWithASegmentOnlyContainingOneCharacter() { PathTemplate pathTemplate = PathTemplate.create("v1/publishers/{publisher}/books/a"); String pattern = "v1/publishers/o'reilly/books/a"; Truth.assertThat(pathTemplate.matches(pattern)).isTrue(); } @Test - public void instantiateWithASegmentEndsWithADelimiter() { + void instantiateWithASegmentEndsWithADelimiter() { PathTemplate pathTemplate = PathTemplate.create( "v1beta1/{parent=projects/*/locations/*/clusters/*}/well-known./openid-configuration"); @@ -838,7 +838,7 @@ public void instantiateWithASegmentEndsWithADelimiter() { } @Test - public void instantiateWithASegmentContainingComplexResourceNamesAndEndsWithADelimiter() { + void instantiateWithASegmentContainingComplexResourceNamesAndEndsWithADelimiter() { ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create( "v1beta1/{parent=projects/*/locations/*/clusters/*}/{well}-{known}./openid-configuration") @@ -850,7 +850,7 @@ public void instantiateWithASegmentContainingComplexResourceNamesAndEndsWithADel } @Test - public void + void instantiateWithASegmentContainingNoComplexResourceNamesAndEndsWithMultipleDelimiters() { PathTemplate pathTemplate = PathTemplate.create( @@ -864,21 +864,21 @@ public void instantiateWithASegmentContainingComplexResourceNamesAndEndsWithADel // ===== @Test - public void testMultiplePathWildcardFailure() { + void testMultiplePathWildcardFailure() { Assertions.assertThrows(IllegalArgumentException.class, () -> PathTemplate.create("bar/**/{name=foo/**}:verb") ); } @Test - public void testTemplateWithSimpleBinding() { + void testTemplateWithSimpleBinding() { PathTemplate template = PathTemplate.create("/v1/messages/{message_id}"); String url = template.instantiate("message_id", "mymessage"); Truth.assertThat(url).isEqualTo("v1/messages/mymessage"); } @Test - public void testTemplateWithMultipleSimpleBindings() { + void testTemplateWithMultipleSimpleBindings() { PathTemplate template = PathTemplate.create("v1/shelves/{shelf}/books/{book}"); String url = template.instantiate("shelf", "s1", "book", "b1"); Truth.assertThat(url).isEqualTo("v1/shelves/s1/books/b1"); diff --git a/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java b/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java index 3d2c833edb..bb3fe90d5d 100644 --- a/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java +++ b/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java @@ -39,7 +39,7 @@ public class TemplatedResourceNameTest { @Test - public void resourceNameMethods() { + void resourceNameMethods() { PathTemplate template = PathTemplate.create("buckets/*/objects/**"); TemplatedResourceName name = TemplatedResourceName.create(template, "buckets/b/objects/1/2"); Truth.assertThat(name.toString()).isEqualTo("buckets/b/objects/1/2"); diff --git a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java index e132c44263..bbe331ff0b 100644 --- a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java +++ b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java @@ -44,7 +44,7 @@ public class UntypedResourceNameTest { private static final String EMPTY_STRING = ""; @Test - public void testGetFieldValues() { + void testGetFieldValues() { assertTrue(UntypedResourceName.isParsableFrom(NAME_STRING)); UntypedResourceName fooName = UntypedResourceName.parse(NAME_STRING); @@ -56,7 +56,7 @@ public void testGetFieldValues() { } @Test - public void testInsertIntoFieldValuesMap() { + void testInsertIntoFieldValuesMap() { UntypedResourceName fooName = UntypedResourceName.parse(NAME_STRING); Map fieldValuesMap = fooName.getFieldValuesMap(); @@ -80,7 +80,7 @@ public void testInsertIntoFieldValuesMap() { } @Test - public void testNullName() { + void testNullName() { assertFalse(UntypedResourceName.isParsableFrom(null)); try { UntypedResourceName.parse(null); From d1225587713d108c1d5d955e12fe2c5e77cdadfa Mon Sep 17 00:00:00 2001 From: Alice Li Date: Thu, 16 May 2024 15:05:01 -0400 Subject: [PATCH 3/8] add version to pom.xml --- api-common-java/pom.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api-common-java/pom.xml b/api-common-java/pom.xml index f2ed551746..5f725e8971 100644 --- a/api-common-java/pom.xml +++ b/api-common-java/pom.xml @@ -71,16 +71,19 @@ org.junit.platform junit-platform-launcher + ${junit.version} test org.junit.jupiter junit-jupiter-engine + ${junit.version} test org.junit.vintage junit-vintage-engine + ${junit.version} test From f63f3b922a61e978f7e38b192e60f47f8dd3458a Mon Sep 17 00:00:00 2001 From: Alice Li Date: Thu, 16 May 2024 15:28:09 -0400 Subject: [PATCH 4/8] update pom.xml --- api-common-java/pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api-common-java/pom.xml b/api-common-java/pom.xml index 5f725e8971..5551e0ebed 100644 --- a/api-common-java/pom.xml +++ b/api-common-java/pom.xml @@ -69,14 +69,14 @@ - org.junit.platform - junit-platform-launcher + org.junit.jupiter + junit-jupiter-engine ${junit.version} test org.junit.jupiter - junit-jupiter-engine + junit-jupiter-params ${junit.version} test From 2016e407355b7f3071cbfd03b97ab03aa6678ed7 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Thu, 16 May 2024 15:30:03 -0400 Subject: [PATCH 5/8] fix lint errors --- .../com/google/api/core/ApiFuturesTest.java | 9 +- .../api/core/SettableApiFutureTest.java | 12 +- .../api/pathtemplate/PathTemplateTest.java | 273 +++++++++--------- .../TemplatedResourceNameTest.java | 1 + .../UntypedResourceNameTest.java | 1 - 5 files changed, 157 insertions(+), 139 deletions(-) diff --git a/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java b/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java index 6c6fcee4a3..eaff30e5e7 100644 --- a/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java +++ b/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java @@ -188,10 +188,11 @@ void testTransformAsyncWithExecutor() throws Exception { ApiFutures.transformAsync( inputFuture, (ApiAsyncFunction) input -> ApiFutures.immediateFuture(input + 1), - (Executor) command -> { - counter.incrementAndGet(); - command.run(); - }); + (Executor) + command -> { + counter.incrementAndGet(); + command.run(); + }); assertThat(outputFuture.get()).isEqualTo(1); assertThat(counter.get()).isEqualTo(1); } diff --git a/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java b/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java index c9a8847d9f..52c0db990a 100644 --- a/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java +++ b/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java @@ -59,11 +59,13 @@ void testCancel() { @Test void testException() { - Assertions.assertThrows(Exception.class, () -> { - SettableApiFuture future = SettableApiFuture.create(); - future.setException(new Exception()); - future.get(); - }); + Assertions.assertThrows( + Exception.class, + () -> { + SettableApiFuture future = SettableApiFuture.create(); + future.setException(new Exception()); + future.get(); + }); } @Test diff --git a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java index a11ddb3fc9..468814cf94 100644 --- a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java +++ b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java @@ -303,15 +303,26 @@ void complexResourceIdEqualsWildcard() { @Test @Disabled - //TODO: This test was passing erroneously, see https://github.com/googleapis/sdk-platform-java/issues/2778 + // TODO: This test was passing erroneously, see + // https://github.com/googleapis/sdk-platform-java/issues/2778 void complexResourceIdEqualsPathWildcard() { - Exception exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/{project=*}/zones/{zone_a=**}~{zone_b}")); - Assertions.assertEquals(String.format( - "parse error: wildcard path not allowed in complex ID resource '%s'", "zone_a"), exception.getMessage()); - - exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/{project=*}/zones/{zone_a}.{zone_b=**}")); - Assertions.assertEquals(String.format( - "parse error: wildcard path not allowed in complex ID resource '%s'", "zone_b"), exception.getMessage()); + Exception exception = + Assertions.assertThrows( + ValidationException.class, + () -> PathTemplate.create("projects/{project=*}/zones/{zone_a=**}~{zone_b}")); + Assertions.assertEquals( + String.format( + "parse error: wildcard path not allowed in complex ID resource '%s'", "zone_a"), + exception.getMessage()); + + exception = + Assertions.assertThrows( + ValidationException.class, + () -> PathTemplate.create("projects/{project=*}/zones/{zone_a}.{zone_b=**}")); + Assertions.assertEquals( + String.format( + "parse error: wildcard path not allowed in complex ID resource '%s'", "zone_b"), + exception.getMessage()); } @Test @@ -332,35 +343,50 @@ void complexResourceIdMissingMatches() { @Test void complexResourceIdNoSeparator() { - Exception exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/{project}/zones/{zone_a}{zone_b}")); - Assertions.assertEquals(String.format( - "parse error: missing or 2+ consecutive delimiter characters in '%s'", - "{zone_a}{zone_b}"), exception.getMessage()); - - exception = Assertions.assertThrows(ValidationException.class, () -> PathTemplate.create("projects/{project}/zones/{zone_a}_{zone_b}{zone_c}")); - Assertions.assertEquals(String.format( - "parse error: missing or 2+ consecutive delimiter characters in '%s'", - "{zone_a}_{zone_b}{zone_c}"), exception.getMessage()); + Exception exception = + Assertions.assertThrows( + ValidationException.class, + () -> PathTemplate.create("projects/{project}/zones/{zone_a}{zone_b}")); + Assertions.assertEquals( + String.format( + "parse error: missing or 2+ consecutive delimiter characters in '%s'", + "{zone_a}{zone_b}"), + exception.getMessage()); + + exception = + Assertions.assertThrows( + ValidationException.class, + () -> PathTemplate.create("projects/{project}/zones/{zone_a}_{zone_b}{zone_c}")); + Assertions.assertEquals( + String.format( + "parse error: missing or 2+ consecutive delimiter characters in '%s'", + "{zone_a}_{zone_b}{zone_c}"), + exception.getMessage()); } @Disabled - // TODO: This test was passing erroneously, see https://github.com/googleapis/sdk-platform-java/issues/2776 + // TODO: This test was passing erroneously, see + // https://github.com/googleapis/sdk-platform-java/issues/2776 @ParameterizedTest @MethodSource("invalidDelimiters") void complexResourceIdInvalidDelimiter(String invalidDelimiter) { - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create(String.format("projects/{project=*}/zones/{zone_a}%s{zone_b}", invalidDelimiter)) - ); - Assertions.assertEquals( - String.format( - "parse error: invalid complex resource ID delimiter character in '%s'", - String.format("zone_a}%s{zone_b}", invalidDelimiter)), - exception.getMessage() - ); - } - static Stream invalidDelimiters() { - return Stream.of("|", "!", "@", "a", "1", ",", "{", ")"); - } + ValidationException exception = + Assertions.assertThrows( + ValidationException.class, + () -> + PathTemplate.create( + String.format( + "projects/{project=*}/zones/{zone_a}%s{zone_b}", invalidDelimiter))); + Assertions.assertEquals( + String.format( + "parse error: invalid complex resource ID delimiter character in '%s'", + String.format("zone_a}%s{zone_b}", invalidDelimiter)), + exception.getMessage()); + } + + static Stream invalidDelimiters() { + return Stream.of("|", "!", "@", "a", "1", ",", "{", ")"); + } @Test void complexResourceIdMixedSeparators() { @@ -406,9 +432,10 @@ void collectionWildcardMatchingInParent() { @Test void collectionWildcardMatchingInvalid() { - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("v1/publishers/{publisher}/books/-") - ); + ValidationException exception = + Assertions.assertThrows( + ValidationException.class, + () -> PathTemplate.create("v1/publishers/{publisher}/books/-")); }; @Test @@ -474,83 +501,76 @@ void complexResourcePathTemplateVariables() { @Test void complexResourceBasicInvalidIds() { - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("projects/*/zones/~{zone_a}") - ); + ValidationException exception = + Assertions.assertThrows( + ValidationException.class, () -> PathTemplate.create("projects/*/zones/~{zone_a}")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", "~{zone_a}"), - exception.getMessage() - ); - exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("projects/*/zones/{zone_a}~") - ); + exception.getMessage()); + exception = + Assertions.assertThrows( + ValidationException.class, () -> PathTemplate.create("projects/*/zones/{zone_a}~")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", "{zone_a}~"), - exception.getMessage() - ); - exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("projects/*/zones/.{zone_a}") - ); + exception.getMessage()); + exception = + Assertions.assertThrows( + ValidationException.class, () -> PathTemplate.create("projects/*/zones/.{zone_a}")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", ".{zone_a}"), - exception.getMessage() - ); - exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("projects/*/zones/{zone_a}.") - ); + exception.getMessage()); + exception = + Assertions.assertThrows( + ValidationException.class, () -> PathTemplate.create("projects/*/zones/{zone_a}.")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", "{zone_a}."), - exception.getMessage() - ); - exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("projects/*/zones/-{zone_a}") - ); + exception.getMessage()); + exception = + Assertions.assertThrows( + ValidationException.class, () -> PathTemplate.create("projects/*/zones/-{zone_a}")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", "-{zone_a}"), - exception.getMessage() - ); - exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("projects/*/zones/{zone_a}-") - ); + exception.getMessage()); + exception = + Assertions.assertThrows( + ValidationException.class, () -> PathTemplate.create("projects/*/zones/{zone_a}-")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", "{zone_a}-"), - exception.getMessage() - ); - exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("projects/*/zones/_{zone_a}") - ); + exception.getMessage()); + exception = + Assertions.assertThrows( + ValidationException.class, () -> PathTemplate.create("projects/*/zones/_{zone_a}")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", "_{zone_a}"), - exception.getMessage() - ); - exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("projects/*/zones/{zone_a}_") - ); + exception.getMessage()); + exception = + Assertions.assertThrows( + ValidationException.class, () -> PathTemplate.create("projects/*/zones/{zone_a}_")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", "{zone_a}_"), - exception.getMessage() - ); - + exception.getMessage()); } @Test void complexResourceMultipleDelimiters() { - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("projects/*/zones/{zone_a}~.{zone_b}") - ); + ValidationException exception = + Assertions.assertThrows( + ValidationException.class, + () -> PathTemplate.create("projects/*/zones/{zone_a}~.{zone_b}")); Assertions.assertEquals( - String.format( "parse error: missing or 2+ consecutive delimiter characters in '%s'", + String.format( + "parse error: missing or 2+ consecutive delimiter characters in '%s'", "{zone_a}~.{zone_b}"), - exception.getMessage() - ); - exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("projects/*/zones/{zone_a}~{zone_b}..{zone_c}") - ); + exception.getMessage()); + exception = + Assertions.assertThrows( + ValidationException.class, + () -> PathTemplate.create("projects/*/zones/{zone_a}~{zone_b}..{zone_c}")); Assertions.assertEquals( - String.format("parse error: missing or 2+ consecutive delimiter characters in '%s'", + String.format( + "parse error: missing or 2+ consecutive delimiter characters in '%s'", "{zone_a}~{zone_b}..{zone_c}"), - exception.getMessage() - ); + exception.getMessage()); String pathString = "projects/project_123/zones/lorum~ipsum"; PathTemplate template = PathTemplate.create("projects/*/zones/{zone_.~-a}~{zone_b}"); template.validate(pathString, ""); @@ -578,13 +598,11 @@ void validateFailure() { String templateString = "buckets/*/objects/*"; String pathString = "buckets/bucket/invalid/object"; PathTemplate template = PathTemplate.create(templateString); - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - template.validate(pathString, "") - ); + ValidationException exception = + Assertions.assertThrows(ValidationException.class, () -> template.validate(pathString, "")); Assertions.assertEquals( String.format(": Parameter \"%s\" must be in the form \"%s\"", pathString, templateString), - exception.getMessage() - ); + exception.getMessage()); } @Test @@ -602,13 +620,12 @@ void validateMatchFailure() { String templateString = "buckets/*/objects/*"; String pathString = "buckets/bucket/invalid/object"; PathTemplate template = PathTemplate.create(templateString); - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - template.validatedMatch(pathString, "") - ); + ValidationException exception = + Assertions.assertThrows( + ValidationException.class, () -> template.validatedMatch(pathString, "")); Assertions.assertEquals( String.format(": Parameter \"%s\" must be in the form \"%s\"", pathString, templateString), - exception.getMessage() - ); + exception.getMessage()); } // Instantiate @@ -638,9 +655,9 @@ void instantiateNotEscapeForUnboundedWildcard() { @Test void instantiateFailWhenTooFewVariables() { PathTemplate template = PathTemplate.create("buckets/*/*/*/objects/*"); - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - template.instantiate("$0", "f", "1", "o") - ); + ValidationException exception = + Assertions.assertThrows( + ValidationException.class, () -> template.instantiate("$0", "f", "1", "o")); } @Test @@ -668,13 +685,12 @@ void instantiateWithHostName() { @Test void instantiateEscapeUnsafeCharNoEncoding() { PathTemplate template = PathTemplate.createWithoutUrlEncoding("buckets/*/objects/*"); - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - template.instantiate("$0", "f/o/o", "$1", "b/a/r") - ); + ValidationException exception = + Assertions.assertThrows( + ValidationException.class, () -> template.instantiate("$0", "f/o/o", "$1", "b/a/r")); Assertions.assertEquals( String.format("Invalid character \"/\" in path section \"f/o/o\"."), - exception.getMessage() - ); + exception.getMessage()); } @Test @@ -788,19 +804,19 @@ void instantiateWithASegmentStartsWithADelimiter() { @Test void instantiateWithASegmentContainingComplexResourceNamesAndStartsWithADelimiter() { - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create( - "v1beta1/{parent=projects/*/locations/*/clusters/*}/.{well}-{known}/openid-configuration") - ); + ValidationException exception = + Assertions.assertThrows( + ValidationException.class, + () -> + PathTemplate.create( + "v1beta1/{parent=projects/*/locations/*/clusters/*}/.{well}-{known}/openid-configuration")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", ".{well}-{known}"), - exception.getMessage() - ); + exception.getMessage()); } @Test - void - instantiateWithASegmentContainingNoComplexResourceNamesAndStartsWithMultipleDelimiters() { + void instantiateWithASegmentContainingNoComplexResourceNamesAndStartsWithMultipleDelimiters() { PathTemplate pathTemplate = PathTemplate.create( "v1beta1/{parent=projects/*/locations/*/clusters/*}/.-~well-known/openid-configuration"); @@ -811,13 +827,13 @@ void instantiateWithASegmentContainingComplexResourceNamesAndStartsWithADelimite @Test void instantiateWithASegmentOnlyContainingOneDelimiter() { - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create("v1/publishers/{publisher}/books/.") - ); + ValidationException exception = + Assertions.assertThrows( + ValidationException.class, + () -> PathTemplate.create("v1/publishers/{publisher}/books/.")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", "."), - exception.getMessage() - ); + exception.getMessage()); } @Test @@ -839,19 +855,19 @@ void instantiateWithASegmentEndsWithADelimiter() { @Test void instantiateWithASegmentContainingComplexResourceNamesAndEndsWithADelimiter() { - ValidationException exception = Assertions.assertThrows(ValidationException.class, () -> - PathTemplate.create( - "v1beta1/{parent=projects/*/locations/*/clusters/*}/{well}-{known}./openid-configuration") - ); + ValidationException exception = + Assertions.assertThrows( + ValidationException.class, + () -> + PathTemplate.create( + "v1beta1/{parent=projects/*/locations/*/clusters/*}/{well}-{known}./openid-configuration")); Assertions.assertEquals( String.format("parse error: invalid begin or end character in '%s'", "{well}-{known}."), - exception.getMessage() - ); + exception.getMessage()); } @Test - void - instantiateWithASegmentContainingNoComplexResourceNamesAndEndsWithMultipleDelimiters() { + void instantiateWithASegmentContainingNoComplexResourceNamesAndEndsWithMultipleDelimiters() { PathTemplate pathTemplate = PathTemplate.create( "v1beta1/{parent=projects/*/locations/*/clusters/*}/well-known.-~/openid-configuration"); @@ -865,9 +881,8 @@ void instantiateWithASegmentContainingComplexResourceNamesAndEndsWithADelimiter( @Test void testMultiplePathWildcardFailure() { - Assertions.assertThrows(IllegalArgumentException.class, () -> - PathTemplate.create("bar/**/{name=foo/**}:verb") - ); + Assertions.assertThrows( + IllegalArgumentException.class, () -> PathTemplate.create("bar/**/{name=foo/**}:verb")); } @Test diff --git a/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java b/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java index bb3fe90d5d..d086a81f44 100644 --- a/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java +++ b/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java @@ -32,6 +32,7 @@ import com.google.common.truth.Truth; import org.junit.jupiter.api.Test; + /** * Tests for {@link TemplatedResourceName}. As resource names are mostly a wrapper around path * templates, not much needs to be done here. diff --git a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java index bbe331ff0b..eb00a16eaa 100644 --- a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java +++ b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java @@ -38,7 +38,6 @@ import org.junit.jupiter.api.Test; /** Tests for {@link UntypedResourceNameTest}. */ - public class UntypedResourceNameTest { private static final String NAME_STRING = "sunshine"; private static final String EMPTY_STRING = ""; From be95a80a87e75c2440b04ef384ca99546dce6306 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Thu, 16 May 2024 15:54:45 -0400 Subject: [PATCH 6/8] make test classes package-private --- .../test/java/com/google/api/core/ApiFuturesTest.java | 2 +- .../test/java/com/google/api/core/ApiServiceTest.java | 2 +- .../api/core/ListenableFutureToApiFutureTest.java | 2 +- .../com/google/api/core/SettableApiFutureTest.java | 2 +- .../com/google/api/pathtemplate/PathTemplateTest.java | 10 +++------- .../api/pathtemplate/TemplatedResourceNameTest.java | 2 +- .../api/resourcenames/UntypedResourceNameTest.java | 2 +- 7 files changed, 9 insertions(+), 13 deletions(-) diff --git a/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java b/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java index eaff30e5e7..7019ab17e7 100644 --- a/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java +++ b/api-common-java/src/test/java/com/google/api/core/ApiFuturesTest.java @@ -40,7 +40,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; -public class ApiFuturesTest { +class ApiFuturesTest { @Test void testAddCallback() { diff --git a/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java b/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java index 05c2552980..8bbfe489a2 100644 --- a/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java +++ b/api-common-java/src/test/java/com/google/api/core/ApiServiceTest.java @@ -34,7 +34,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class ApiServiceTest { +class ApiServiceTest { @Test void testNoopService() { ApiService service = diff --git a/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java b/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java index 854f355f37..707f7aae0f 100644 --- a/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java +++ b/api-common-java/src/test/java/com/google/api/core/ListenableFutureToApiFutureTest.java @@ -33,7 +33,7 @@ import com.google.common.util.concurrent.SettableFuture; import org.junit.jupiter.api.Test; -public class ListenableFutureToApiFutureTest { +class ListenableFutureToApiFutureTest { @Test void testGet() throws Exception { diff --git a/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java b/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java index 52c0db990a..11629837f1 100644 --- a/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java +++ b/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java @@ -36,7 +36,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class SettableApiFutureTest { +class SettableApiFutureTest { @Test void testSet() throws Exception { SettableApiFuture future = SettableApiFuture.create(); diff --git a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java index 468814cf94..08d357f733 100644 --- a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java +++ b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java @@ -42,7 +42,7 @@ import org.junit.jupiter.params.provider.MethodSource; /** Tests for {@link PathTemplate}. */ -public class PathTemplateTest { +class PathTemplateTest { // Match // ===== @@ -302,9 +302,7 @@ void complexResourceIdEqualsWildcard() { } @Test - @Disabled - // TODO: This test was passing erroneously, see - // https://github.com/googleapis/sdk-platform-java/issues/2778 + @Disabled ("https://github.com/googleapis/sdk-platform-java/issues/2778") void complexResourceIdEqualsPathWildcard() { Exception exception = Assertions.assertThrows( @@ -364,9 +362,7 @@ void complexResourceIdNoSeparator() { exception.getMessage()); } - @Disabled - // TODO: This test was passing erroneously, see - // https://github.com/googleapis/sdk-platform-java/issues/2776 + @Disabled ("https://github.com/googleapis/sdk-platform-java/issues/2776") @ParameterizedTest @MethodSource("invalidDelimiters") void complexResourceIdInvalidDelimiter(String invalidDelimiter) { diff --git a/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java b/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java index d086a81f44..2a7fd6ede4 100644 --- a/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java +++ b/api-common-java/src/test/java/com/google/api/pathtemplate/TemplatedResourceNameTest.java @@ -37,7 +37,7 @@ * Tests for {@link TemplatedResourceName}. As resource names are mostly a wrapper around path * templates, not much needs to be done here. */ -public class TemplatedResourceNameTest { +class TemplatedResourceNameTest { @Test void resourceNameMethods() { diff --git a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java index eb00a16eaa..1f974b566b 100644 --- a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java +++ b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java @@ -38,7 +38,7 @@ import org.junit.jupiter.api.Test; /** Tests for {@link UntypedResourceNameTest}. */ -public class UntypedResourceNameTest { +class UntypedResourceNameTest { private static final String NAME_STRING = "sunshine"; private static final String EMPTY_STRING = ""; From fde9b3069c2fbe1133e46085e6c1f7204d302d20 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Thu, 16 May 2024 16:25:02 -0400 Subject: [PATCH 7/8] refactor --- .../java/com/google/api/core/SettableApiFutureTest.java | 3 ++- .../api/resourcenames/UntypedResourceNameTest.java | 9 ++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java b/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java index 11629837f1..0278b0b933 100644 --- a/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java +++ b/api-common-java/src/test/java/com/google/api/core/SettableApiFutureTest.java @@ -30,6 +30,7 @@ package com.google.api.core; import com.google.common.truth.Truth; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -60,7 +61,7 @@ void testCancel() { @Test void testException() { Assertions.assertThrows( - Exception.class, + ExecutionException.class, () -> { SettableApiFuture future = SettableApiFuture.create(); future.setException(new Exception()); diff --git a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java index 1f974b566b..8b625ad14c 100644 --- a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java +++ b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java @@ -33,8 +33,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; - import java.util.Map; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** Tests for {@link UntypedResourceNameTest}. */ @@ -81,9 +81,8 @@ void testInsertIntoFieldValuesMap() { @Test void testNullName() { assertFalse(UntypedResourceName.isParsableFrom(null)); - try { - UntypedResourceName.parse(null); - } catch (NullPointerException e) { - } + Assertions.assertThrows( + NullPointerException.class, + () -> UntypedResourceName.parse(null)); } } From 7130f152b51516192ca6d8ddf0587ba0810cf0cd Mon Sep 17 00:00:00 2001 From: Alice Li Date: Thu, 16 May 2024 16:36:19 -0400 Subject: [PATCH 8/8] fix lint --- .../java/com/google/api/pathtemplate/PathTemplateTest.java | 4 ++-- .../google/api/resourcenames/UntypedResourceNameTest.java | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java index 08d357f733..a51bdff91b 100644 --- a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java +++ b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java @@ -302,7 +302,7 @@ void complexResourceIdEqualsWildcard() { } @Test - @Disabled ("https://github.com/googleapis/sdk-platform-java/issues/2778") + @Disabled("https://github.com/googleapis/sdk-platform-java/issues/2778") void complexResourceIdEqualsPathWildcard() { Exception exception = Assertions.assertThrows( @@ -362,7 +362,7 @@ void complexResourceIdNoSeparator() { exception.getMessage()); } - @Disabled ("https://github.com/googleapis/sdk-platform-java/issues/2776") + @Disabled("https://github.com/googleapis/sdk-platform-java/issues/2776") @ParameterizedTest @MethodSource("invalidDelimiters") void complexResourceIdInvalidDelimiter(String invalidDelimiter) { diff --git a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java index 8b625ad14c..6fa28c49eb 100644 --- a/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java +++ b/api-common-java/src/test/java/com/google/api/resourcenames/UntypedResourceNameTest.java @@ -33,6 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.Map; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -81,8 +82,6 @@ void testInsertIntoFieldValuesMap() { @Test void testNullName() { assertFalse(UntypedResourceName.isParsableFrom(null)); - Assertions.assertThrows( - NullPointerException.class, - () -> UntypedResourceName.parse(null)); + Assertions.assertThrows(NullPointerException.class, () -> UntypedResourceName.parse(null)); } }