diff --git a/src/main/java/com/google/api/generator/gapic/composer/utils/PackageChecker.java b/src/main/java/com/google/api/generator/gapic/composer/utils/PackageChecker.java index 9765df0425..18a50a622e 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/utils/PackageChecker.java +++ b/src/main/java/com/google/api/generator/gapic/composer/utils/PackageChecker.java @@ -20,6 +20,8 @@ import java.util.regex.Pattern; public class PackageChecker { + + // Nothing to initialize. private PackageChecker() {} /** @@ -30,13 +32,18 @@ public static boolean isGaApi(String pakkage) { String[] packageComponents = pakkage.split("\\."); Preconditions.checkState( packageComponents.length > 0, "No subcomponents found in Java package %s", pakkage); - String versionComponent = packageComponents[packageComponents.length - 1]; - Matcher matcher = Pattern.compile("^v[0-9]+").matcher(versionComponent); - Preconditions.checkState( - matcher.find(), - "No version component found in last subpackage %s of %s", - versionComponent, - pakkage); + String versionComponent = null; + Matcher matcher = null; + boolean isFound = false; + for (int i = packageComponents.length - 1; i >= 0; i--) { + versionComponent = packageComponents[i]; + matcher = Pattern.compile("^v[0-9]+").matcher(versionComponent); + isFound = matcher.find(); + if (isFound) { + break; + } + } + Preconditions.checkState(isFound, "No version component found in package %s", pakkage); String versionSubstr = versionComponent.replace(matcher.group(), ""); return Strings.isNullOrEmpty(versionSubstr) || (!versionSubstr.contains("alpha") && !versionSubstr.contains("beta")); diff --git a/src/test/java/com/google/api/generator/gapic/composer/utils/PackageCheckerTest.java b/src/test/java/com/google/api/generator/gapic/composer/utils/PackageCheckerTest.java index 323f5e0100..7e514599d3 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/utils/PackageCheckerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/utils/PackageCheckerTest.java @@ -23,6 +23,8 @@ public class PackageCheckerTest { @Test public void isGaApi_normalExpectedPackageStructure() { + assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v1.services")); + assertTrue(PackageChecker.isGaApi("com.google.cloud.v1.foobar.services")); assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v1")); assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v999")); assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v12345a")); @@ -40,7 +42,7 @@ public void isGaApi_normalExpectedPackageStructure() { public void isGaApi_invalidPackageStructure() { assertThrows( IllegalStateException.class, - () -> PackageChecker.isGaApi("com.google.cloud.dataproc.v1.foobar")); + () -> PackageChecker.isGaApi("com.google.cloud.dataproc.foo.bar")); assertThrows(IllegalStateException.class, () -> PackageChecker.isGaApi("")); } }