Skip to content

Commit

Permalink
Fix VERSION constant (#1355)
Browse files Browse the repository at this point in the history
* parse version parts from full version

* Add test for version parsing
  • Loading branch information
chingor13 committed Aug 12, 2019
1 parent 556eafb commit b4e774c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
Expand Up @@ -15,11 +15,14 @@
package com.google.api.client.googleapis;

import com.google.api.client.util.SecurityUtils;
import com.google.common.annotations.VisibleForTesting;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Utility class for the Google API Client Library.
Expand All @@ -29,42 +32,52 @@
*/
public final class GoogleUtils {

// NOTE: toString() so compiler thinks it isn't a constant, so it won't inline it
// {x-version-update-start:google-api-client:current}
/** Current release version. */
public static final String VERSION = "1.30.3-SNAPSHOT".toString();
// {x-version-update-end:google-api-client:current}

// NOTE: Integer instead of int so compiler thinks it isn't a constant, so it won't inline it
/**
* Major part of the current release version.
*
* @since 1.14
*/
public static final Integer MAJOR_VERSION = 1;
public static final Integer MAJOR_VERSION;

/**
* Minor part of the current release version.
*
* @since 1.14
*/
public static final Integer MINOR_VERSION = 26;
public static final Integer MINOR_VERSION;

/**
* Bug fix part of the current release version.
*
* @since 1.14
*/
public static final Integer BUGFIX_VERSION = 0;
public static final Integer BUGFIX_VERSION;

/** Current release version. */
// NOTE: toString() so compiler thinks it isn't a constant, so it won't inline it
public static final String VERSION = (MAJOR_VERSION + "." + MINOR_VERSION + "." + BUGFIX_VERSION
+ "-SNAPSHOT").toString();
@VisibleForTesting
static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(-SNAPSHOT)?");

static {
Matcher versionMatcher = VERSION_PATTERN.matcher(VERSION);
versionMatcher.find();
MAJOR_VERSION = Integer.parseInt(versionMatcher.group(1));
MINOR_VERSION = Integer.parseInt(versionMatcher.group(2));
BUGFIX_VERSION = Integer.parseInt(versionMatcher.group(3));
}

/** Cached value for {@link #getCertificateTrustStore()}. */
static KeyStore certTrustStore;

/**
* Returns the key store for trusted root certificates to use for Google APIs.
*
* <p>
* Value is cached, so subsequent access is fast.
* </p>
* <p>Value is cached, so subsequent access is fast.
*
* @since 1.14
*/
Expand All @@ -78,6 +91,5 @@ public static synchronized KeyStore getCertificateTrustStore()
return certTrustStore;
}

private GoogleUtils() {
}
private GoogleUtils() {}
}
Expand Up @@ -16,6 +16,8 @@

import java.security.KeyStore;
import java.util.Enumeration;
import java.util.regex.Matcher;

import junit.framework.TestCase;

/**
Expand All @@ -36,4 +38,22 @@ public void testGetCertificateTrustStore() throws Exception {
// has been added or removed
assertEquals(70, trustStore.size());
}

public void testVersionMatcher() {
String version = "1.30.3";
Matcher matcher = GoogleUtils.VERSION_PATTERN.matcher(version);
assertTrue(matcher.find());
assertEquals(1, Integer.parseInt(matcher.group(1)));
assertEquals(30, Integer.parseInt(matcher.group(2)));
assertEquals(3, Integer.parseInt(matcher.group(3)));
}

public void testVersionMatcherSnapshot() {
String version = "1.30.3-SNAPSHOT";
Matcher matcher = GoogleUtils.VERSION_PATTERN.matcher(version);
assertTrue(matcher.find());
assertEquals(1, Integer.parseInt(matcher.group(1)));
assertEquals(30, Integer.parseInt(matcher.group(2)));
assertEquals(3, Integer.parseInt(matcher.group(3)));
}
}

0 comments on commit b4e774c

Please sign in to comment.