diff --git a/api-common-java/pom.xml b/api-common-java/pom.xml index fe6403145a..5551e0ebed 100644 --- a/api-common-java/pom.xml +++ b/api-common-java/pom.xml @@ -69,9 +69,21 @@ - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + + + org.junit.jupiter + junit-jupiter-params + ${junit.version} + test + + + org.junit.vintage + junit-vintage-engine + ${junit.version} 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..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 @@ -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 { +class ApiFuturesTest { @Test - public void testAddCallback() throws Exception { + 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,24 @@ 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 +212,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 19b10af472..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 @@ -31,12 +31,12 @@ 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 { +class ApiServiceTest { @Test - public void testNoopService() { + void testNoopService() { ApiService service = new AbstractApiService() { @Override @@ -50,12 +50,12 @@ protected void doStart() { } }; service.startAsync().awaitRunning(); - Assert.assertTrue(service.isRunning()); + Assertions.assertTrue(service.isRunning()); service.stopAsync().awaitTerminated(); } @Test - public void testFailingService() { + void testFailingService() { final AtomicReference savedFailure = new AtomicReference<>(); ApiService service = new AbstractApiService() { @@ -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..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 @@ -31,12 +31,12 @@ 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 { +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 a4cff9b086..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 @@ -34,11 +34,12 @@ 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 { +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 +49,7 @@ public void testSet() throws Exception { } @Test - public void testCancel() throws Exception { + void testCancel() { SettableApiFuture future = SettableApiFuture.create(); Truth.assertThat(future.isDone()).isFalse(); Truth.assertThat(future.isCancelled()).isFalse(); @@ -57,15 +58,19 @@ 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 + void testException() { + Assertions.assertThrows( + ExecutionException.class, + () -> { + SettableApiFuture future = SettableApiFuture.create(); + future.setException(new Exception()); + future.get(); + }); } @Test - public void testListener() throws Exception { + 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..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 @@ -32,40 +32,35 @@ 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(); +class PathTemplateTest { // Match // ===== @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(); @@ -75,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(); @@ -85,7 +80,7 @@ public void matchWithHostName() { } @Test - public void matchWithHostNameAndProtocol() { + void matchWithHostNameAndProtocol() { PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone}"); Map match = template.match( @@ -97,7 +92,7 @@ public void matchWithHostNameAndProtocol() { } @Test - public void matchWithHostNameAndProtocolWithTemplateStartWithBinding() { + void matchWithHostNameAndProtocolWithTemplateStartWithBinding() { PathTemplate template = PathTemplate.create("{project}/zones/{zone}"); Map match = template.match( @@ -109,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=**}"); @@ -136,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(); @@ -145,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(); @@ -154,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"); @@ -186,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"); @@ -194,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"); } @@ -217,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 = @@ -256,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 = @@ -295,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"); @@ -307,21 +302,29 @@ public void complexResourceIdEqualsWildcard() { } @Test - public void complexResourceIdEqualsPathWildcard() { - thrown.expect(ValidationException.class); - PathTemplate template = PathTemplate.create("projects/{project=*}/zones/{zone_a=**}~{zone_b}"); - thrown.expectMessage( + @Disabled("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")); - - template = PathTemplate.create("projects/{project=*}/zones/{zone_a}.{zone_b=**}"); - thrown.expectMessage( + "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")); + "parse error: wildcard path not allowed in complex ID resource '%s'", "zone_b"), + exception.getMessage()); } @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(); @@ -337,39 +340,52 @@ public void complexResourceIdMissingMatches() { } @Test - public void complexResourceIdNoSeparator() { - thrown.expect(ValidationException.class); - PathTemplate.create("projects/{project}/zones/{zone_a}{zone_b}"); - thrown.expectMessage( + 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}")); - - PathTemplate.create("projects/{project}/zones/{zone_a}_{zone_b}{zone_c}"); - thrown.expectMessage( + "{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}")); - } - - @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( - String.format( - "parse error: invalid complex resource ID delimiter character in '%s'", - String.format("{zone_a}%s{zone_b}", invalidDelimiter))); - } + "{zone_a}_{zone_b}{zone_c}"), + exception.getMessage()); + } + + @Disabled("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", ",", "{", ")"); } @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}"); @@ -398,7 +414,7 @@ public void complexResourceIdMixedSeparators() { } @Test - public void collectionWildcardMatchingInParent() { + void collectionWildcardMatchingInParent() { PathTemplate template = PathTemplate.create("v1/publishers/-/books/{book}"); Map match = template.match( @@ -411,19 +427,21 @@ public void collectionWildcardMatchingInParent() { } @Test - public void collectionWildcardMatchingInvalid() { - thrown.expect(ValidationException.class); - PathTemplate.create("v1/publishers/{publisher}/books/-"); - } + 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( @@ -458,7 +476,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); @@ -478,57 +496,77 @@ 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}")); - - 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( + void complexResourceBasicInvalidIds() { + 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()); + } + + @Test + void complexResourceMultipleDelimiters() { + 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}")); - - PathTemplate.create("projects/*/zones/{zone_a}~{zone_b}..{zone_c}"); - thrown.expectMessage( + "{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}")); - + "{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, ""); @@ -543,7 +581,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); @@ -552,18 +590,19 @@ public void validateSuccess() { } @Test - public void validateFailure() { - thrown.expect(ValidationException.class); + void validateFailure() { 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 - public void validateMatchSuccess() { + void validateMatchSuccess() { String templateString = "buckets/*/objects/{object_id}"; String pathString = "buckets/bucket/objects/object"; PathTemplate template = PathTemplate.create(templateString); @@ -573,62 +612,65 @@ public void validateMatchSuccess() { } @Test - public void validateMatchFailure() { - thrown.expect(ValidationException.class); + void validateMatchFailure() { 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 // =========== @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() { - thrown.expect(ValidationException.class); + void instantiateFailWhenTooFewVariables() { 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 - 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( @@ -637,43 +679,46 @@ public void instantiateWithHostName() { } @Test - public void instantiateEscapeUnsafeCharNoEncoding() { - thrown.expect(ValidationException.class); - thrown.expectMessage("Invalid character \"/\" in path section \"f/o/o\"."); + void instantiateEscapeUnsafeCharNoEncoding() { 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 - 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"); @@ -681,7 +726,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"); @@ -693,7 +738,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}"); @@ -717,7 +762,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 = @@ -736,7 +781,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"); @@ -744,7 +789,7 @@ public void instantiateWithCustomVerbs() { } @Test - public void instantiateWithASegmentStartsWithADelimiter() { + void instantiateWithASegmentStartsWithADelimiter() { PathTemplate pathTemplate = PathTemplate.create( "v1beta1/{parent=projects/*/locations/*/clusters/*}/.well-known/openid-configuration"); @@ -754,17 +799,20 @@ 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}")); + void instantiateWithASegmentContainingComplexResourceNamesAndStartsWithADelimiter() { + 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 - public void - instantiateWithASegmentContainingNoComplexResourceNamesAndStartsWithMultipleDelimiters() { + void instantiateWithASegmentContainingNoComplexResourceNamesAndStartsWithMultipleDelimiters() { PathTemplate pathTemplate = PathTemplate.create( "v1beta1/{parent=projects/*/locations/*/clusters/*}/.-~well-known/openid-configuration"); @@ -774,21 +822,25 @@ 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'", ".")); + void instantiateWithASegmentOnlyContainingOneDelimiter() { + 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 - 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"); @@ -798,17 +850,20 @@ 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}.")); + void instantiateWithASegmentContainingComplexResourceNamesAndEndsWithADelimiter() { + 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 - public void - instantiateWithASegmentContainingNoComplexResourceNamesAndEndsWithMultipleDelimiters() { + void instantiateWithASegmentContainingNoComplexResourceNamesAndEndsWithMultipleDelimiters() { PathTemplate pathTemplate = PathTemplate.create( "v1beta1/{parent=projects/*/locations/*/clusters/*}/well-known.-~/openid-configuration"); @@ -821,20 +876,20 @@ public void instantiateWithASegmentContainingComplexResourceNamesAndEndsWithADel // ===== @Test - public void testMultiplePathWildcardFailure() { - thrown.expect(IllegalArgumentException.class); - PathTemplate.create("bar/**/{name=foo/**}:verb"); + 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 bb143932ff..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 @@ -31,19 +31,16 @@ 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 { +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 c0615da416..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 @@ -30,23 +30,21 @@ 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.Assertions; +import org.junit.jupiter.api.Test; /** Tests for {@link UntypedResourceNameTest}. */ -@RunWith(JUnit4.class) -public class UntypedResourceNameTest { +class UntypedResourceNameTest { private static final String NAME_STRING = "sunshine"; private static final String EMPTY_STRING = ""; @Test - public void testGetFieldValues() { + void testGetFieldValues() { assertTrue(UntypedResourceName.isParsableFrom(NAME_STRING)); UntypedResourceName fooName = UntypedResourceName.parse(NAME_STRING); @@ -58,7 +56,7 @@ public void testGetFieldValues() { } @Test - public void testInsertIntoFieldValuesMap() { + void testInsertIntoFieldValuesMap() { UntypedResourceName fooName = UntypedResourceName.parse(NAME_STRING); Map fieldValuesMap = fooName.getFieldValuesMap(); @@ -82,11 +80,8 @@ public void testInsertIntoFieldValuesMap() { } @Test - public void testNullName() { + void testNullName() { assertFalse(UntypedResourceName.isParsableFrom(null)); - try { - UntypedResourceName fooName = UntypedResourceName.parse(null); - } catch (NullPointerException e) { - } + Assertions.assertThrows(NullPointerException.class, () -> UntypedResourceName.parse(null)); } }