Skip to content

Commit ad5335a

Browse files
authored
Cause CloudToolsInfo.getToolsVersion() to return feature version (#1489)
1 parent 0f02a2b commit ad5335a

3 files changed

Lines changed: 64 additions & 8 deletions

File tree

plugins/com.google.cloud.tools.eclipse.util.test/src/com/google/cloud/tools/eclipse/util/CloudToolsInfoTest.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,46 @@
1616

1717
package com.google.cloud.tools.eclipse.util;
1818

19+
import org.eclipse.core.runtime.IBundleGroup;
20+
import org.eclipse.core.runtime.IBundleGroupProvider;
1921
import org.junit.Assert;
2022
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.Mock;
25+
import org.mockito.Mockito;
26+
import org.mockito.runners.MockitoJUnitRunner;
2127

28+
@RunWith(MockitoJUnitRunner.class)
2229
public class CloudToolsInfoTest {
30+
@Mock
31+
public IBundleGroupProvider bundleGroupProvider;
32+
@Mock
33+
public IBundleGroup bundleGroup;
2334

2435
@Test
25-
public void testGetToolsVersion() {
26-
Assert.assertTrue(CloudToolsInfo.getToolsVersion().startsWith("0.1.0."));
36+
public void testGetToolsVersion_featureId() {
37+
Assert.assertEquals("com.google.cloud.tools.eclipse.suite.e45.feature",
38+
CloudToolsInfo.CLOUD_TOOLS_FOR_ECLIPSE_FEATURE_ID);
39+
}
40+
41+
@Test
42+
public void testGetToolsVersion_noFeature() {
43+
Assert.assertEquals("0.0.0", CloudToolsInfo.getToolsVersion(new IBundleGroupProvider[0]));
44+
}
45+
46+
@Test
47+
public void testGetToolsVersion_hasFeature() {
48+
Mockito.when(bundleGroupProvider.getBundleGroups())
49+
.thenReturn(new IBundleGroup[] {bundleGroup});
50+
Mockito.when(bundleGroup.getIdentifier())
51+
.thenReturn(CloudToolsInfo.CLOUD_TOOLS_FOR_ECLIPSE_FEATURE_ID);
52+
Mockito.when(bundleGroup.getVersion()).thenReturn("123.456.789");
53+
Assert.assertEquals("123.456.789",
54+
CloudToolsInfo.getToolsVersion(new IBundleGroupProvider[] {bundleGroupProvider}));
2755
}
2856

2957
@Test
3058
public void testUserAgent() {
31-
Assert.assertTrue(CloudToolsInfo.USER_AGENT.startsWith("gcloud-eclipse-tools/0.1.0"));
59+
Assert.assertTrue(CloudToolsInfo.USER_AGENT.startsWith("gcloud-eclipse-tools/"));
3260
}
3361
}

plugins/com.google.cloud.tools.eclipse.util.test/src/com/google/cloud/tools/eclipse/util/io/HttpUtilWithServerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void testSendPostMultipart() throws IOException {
4343
assertEquals(200, HttpUtil.sendPostMultipart(server.getAddress(), parameters));
4444
assertEquals("POST", server.getRequestMethod());
4545
assertTrue(server.getRequestHeaders().get("User-Agent")
46-
.startsWith("gcloud-eclipse-tools/0.1.0."));
46+
.startsWith("gcloud-eclipse-tools/"));
4747
assertTrue(server.getRequestHeaders().get("Content-Type")
4848
.startsWith("multipart/form-data; boundary="));
4949

plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/CloudToolsInfo.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,48 @@
1616

1717
package com.google.cloud.tools.eclipse.util;
1818

19-
import org.osgi.framework.FrameworkUtil;
19+
import com.google.common.annotations.VisibleForTesting;
20+
import java.util.logging.Logger;
21+
import org.eclipse.core.runtime.IBundleGroup;
22+
import org.eclipse.core.runtime.IBundleGroupProvider;
23+
import org.eclipse.core.runtime.Platform;
2024

2125
/**
2226
* Provides generic information about the plug-in, such as a name to be used for usage
2327
* reporting and the current version, etc.
2428
*/
2529
public class CloudToolsInfo {
30+
private static final Logger logger = Logger.getLogger(CloudToolsInfo.class.getName());
31+
32+
/**
33+
* Our main feature identifier, used for branding.
34+
*/
35+
@VisibleForTesting
36+
static final String CLOUD_TOOLS_FOR_ECLIPSE_FEATURE_ID =
37+
"com.google.cloud.tools.eclipse.suite.e45.feature";
2638

2739
// Don't change the value; this name is used as an originating "application" of usage metrics.
28-
public static String METRICS_NAME = "gcloud-eclipse-tools";
40+
public static final String METRICS_NAME = "gcloud-eclipse-tools";
2941

30-
public static String USER_AGENT = METRICS_NAME + "/" + getToolsVersion();
42+
public static final String USER_AGENT = METRICS_NAME + "/" + getToolsVersion();
3143

44+
/** Return the version of associated Cloud Tools for Eclipse feature, or 0.0.0 if unknown. */
3245
public static String getToolsVersion() {
33-
return FrameworkUtil.getBundle(CloudToolsInfo.class).getVersion().toString();
46+
return getToolsVersion(Platform.getBundleGroupProviders());
47+
}
48+
49+
@VisibleForTesting
50+
static String getToolsVersion(IBundleGroupProvider[] bundleGroupProviders) {
51+
for (IBundleGroupProvider provider : bundleGroupProviders) {
52+
for (IBundleGroup feature : provider.getBundleGroups()) {
53+
if (CLOUD_TOOLS_FOR_ECLIPSE_FEATURE_ID.equals(feature.getIdentifier())) {
54+
return feature.getVersion();
55+
}
56+
}
57+
}
58+
// May not have been installed with via a feature. Although we could report the bundle version,
59+
// that may result in a confusing versions.
60+
logger.fine("Feature not found: " + CLOUD_TOOLS_FOR_ECLIPSE_FEATURE_ID);
61+
return "0.0.0";
3462
}
3563
}

0 commit comments

Comments
 (0)