Skip to content

Commit 8661da7

Browse files
authored
Remove checked GoogleApiException from IGoogleApiFactory (#2131)
1 parent 0736c6f commit 8661da7

12 files changed

Lines changed: 129 additions & 175 deletions

File tree

plugins/com.google.cloud.tools.eclipse.googleapis.test/src/com/google/cloud/tools/eclipse/googleapis/internal/GoogleApiFactoryTest.java

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@
3333
import com.google.api.services.appengine.v1.Appengine.Apps;
3434
import com.google.api.services.cloudresourcemanager.CloudResourceManager.Projects;
3535
import com.google.api.services.storage.Storage;
36-
import com.google.cloud.tools.eclipse.googleapis.GoogleApiException;
3736
import com.google.cloud.tools.eclipse.util.CloudToolsInfo;
3837
import com.google.common.cache.LoadingCache;
3938
import java.io.IOException;
40-
import java.util.concurrent.ExecutionException;
4139
import org.eclipse.core.net.proxy.IProxyChangeEvent;
4240
import org.eclipse.core.net.proxy.IProxyChangeListener;
4341
import org.eclipse.core.net.proxy.IProxyService;
@@ -55,56 +53,41 @@ public class GoogleApiFactoryTest {
5553
@Mock private JsonFactory jsonFactory;
5654
@Mock private IProxyService proxyService;
5755
@Mock private Credential credential;
58-
@Mock LoadingCache<GoogleApiUrl, HttpTransport> transportCache;
56+
@Mock private LoadingCache<GoogleApiUrl, HttpTransport> transportCache;
5957
@Mock private ProxyFactory proxyFactory;
6058
@Captor private ArgumentCaptor<IProxyChangeListener> proxyChangeListenerCaptor =
6159
ArgumentCaptor.forClass(IProxyChangeListener.class);
6260

6361
private GoogleApiFactory googleApiFactory;
6462

6563
@Before
66-
public void setUp() throws ExecutionException {
64+
public void setUp() {
6765
googleApiFactory = new GoogleApiFactory(proxyFactory);
68-
when(transportCache.get(any(GoogleApiUrl.class))).thenReturn(mock(HttpTransport.class));
66+
when(transportCache.getUnchecked(any(GoogleApiUrl.class)))
67+
.thenReturn(mock(HttpTransport.class));
6968
googleApiFactory.setTransportCache(transportCache);
7069
}
7170

7271
@Test
73-
public void testNewStorageApi_Url() throws GoogleApiException {
72+
public void testNewStorageApi_Url() {
7473
Storage storage = googleApiFactory.newStorageApi(mock(Credential.class));
7574
assertEquals("https://www.googleapis.com/", storage.getRootUrl());
7675
}
77-
76+
7877
@Test
79-
public void testNewAppsApi_userAgentIsSet() throws IOException, GoogleApiException {
78+
public void testNewAppsApi_userAgentIsSet() throws IOException {
8079
Apps api = googleApiFactory.newAppsApi(mock(Credential.class));
8180
assertThat(api.get("").getRequestHeaders().getUserAgent(),
8281
containsString(CloudToolsInfo.USER_AGENT));
8382
}
8483

85-
@Test(expected = GoogleApiException.class)
86-
public void testNewAppsApi_transportCacheErrorTranslatedToGoogleApiException()
87-
throws ExecutionException, GoogleApiException {
88-
when(transportCache.get(any(GoogleApiUrl.class)))
89-
.thenThrow(new ExecutionException(new Exception("test")));
90-
googleApiFactory.newAppsApi(mock(Credential.class));
91-
}
92-
9384
@Test
94-
public void testNewProjectsApi_userAgentIsSet() throws IOException, GoogleApiException {
85+
public void testNewProjectsApi_userAgentIsSet() throws IOException {
9586
Projects api = googleApiFactory.newProjectsApi(mock(Credential.class));
9687
assertThat(api.get("").getRequestHeaders().getUserAgent(),
9788
containsString(CloudToolsInfo.USER_AGENT));
9889
}
9990

100-
@Test(expected = GoogleApiException.class)
101-
public void testNewProjectsApi_transportCacheErrorTranslatedToGoogleApiException()
102-
throws ExecutionException, GoogleApiException {
103-
when(transportCache.get(any(GoogleApiUrl.class)))
104-
.thenThrow(new ExecutionException(new Exception("test")));
105-
googleApiFactory.newProjectsApi(mock(Credential.class));
106-
}
107-
10891
@Test
10992
public void testSetProxyService() {
11093
googleApiFactory.setProxyService(proxyService);

plugins/com.google.cloud.tools.eclipse.googleapis.test/src/com/google/cloud/tools/eclipse/googleapis/internal/GoogleApiFactoryWithProxyServerTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@
2828
import com.google.api.client.json.JsonFactory;
2929
import com.google.api.services.appengine.v1.Appengine.Apps;
3030
import com.google.api.services.cloudresourcemanager.CloudResourceManager.Projects;
31-
import com.google.cloud.tools.eclipse.googleapis.GoogleApiException;
3231
import com.google.cloud.tools.eclipse.util.CloudToolsInfo;
3332
import com.google.common.cache.LoadingCache;
3433
import java.io.IOException;
3534
import java.net.URI;
36-
import java.util.concurrent.ExecutionException;
3735
import org.eclipse.core.net.proxy.IProxyChangeListener;
3836
import org.eclipse.core.net.proxy.IProxyData;
3937
import org.eclipse.core.net.proxy.IProxyService;
@@ -51,28 +49,29 @@ public class GoogleApiFactoryWithProxyServerTest {
5149
@Mock private JsonFactory jsonFactory;
5250
@Mock private IProxyService proxyService;
5351
@Mock private Credential credential;
54-
@Mock LoadingCache<GoogleApiUrl, HttpTransport> transportCache;
52+
@Mock private LoadingCache<GoogleApiUrl, HttpTransport> transportCache;
5553
@Captor private ArgumentCaptor<IProxyChangeListener> proxyListenerCaptor =
5654
ArgumentCaptor.forClass(IProxyChangeListener.class);
5755

5856
private GoogleApiFactory googleApiFactory;
5957

6058
@Before
61-
public void setUp() throws ExecutionException {
59+
public void setUp() {
6260
googleApiFactory = new GoogleApiFactory();
63-
when(transportCache.get(any(GoogleApiUrl.class))).thenReturn(mock(HttpTransport.class));
61+
when(transportCache.getUnchecked(any(GoogleApiUrl.class)))
62+
.thenReturn(mock(HttpTransport.class));
6463
googleApiFactory.setTransportCache(transportCache);
6564
}
6665

6766
@Test
68-
public void testNewAppsApi_userAgentIsSet() throws IOException, GoogleApiException {
67+
public void testNewAppsApi_userAgentIsSet() throws IOException {
6968
Apps api = googleApiFactory.newAppsApi(mock(Credential.class));
7069
assertThat(api.get("").getRequestHeaders().getUserAgent(),
7170
containsString(CloudToolsInfo.USER_AGENT));
7271
}
7372

7473
@Test
75-
public void testNewProjectsApi_userAgentIsSet() throws IOException, GoogleApiException {
74+
public void testNewProjectsApi_userAgentIsSet() throws IOException {
7675
Projects api = googleApiFactory.newProjectsApi(mock(Credential.class));
7776
assertThat(api.get("").getRequestHeaders().getUserAgent(),
7877
containsString(CloudToolsInfo.USER_AGENT));

plugins/com.google.cloud.tools.eclipse.googleapis.test/src/com/google/cloud/tools/eclipse/googleapis/internal/ProxyFactoryTest.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.hamcrest.CoreMatchers.is;
2020
import static org.junit.Assert.assertThat;
21+
import static org.junit.Assert.fail;
2122
import static org.mockito.Matchers.any;
2223
import static org.mockito.Mockito.mock;
2324
import static org.mockito.Mockito.when;
@@ -41,94 +42,106 @@ public class ProxyFactoryTest {
4142

4243
@Mock private IProxyService proxyService;
4344

45+
private URI exampleUri;
4446
private ProxyFactory proxyFactory;
4547

4648
@Before
47-
public void setUp() throws Exception {
49+
public void setUp() throws URISyntaxException {
4850
proxyFactory = new ProxyFactory();
51+
exampleUri = new URI("https://example.com");
4952
}
5053

51-
@Test(expected = NullPointerException.class)
52-
public void testCreateProxy_nullArgument() throws URISyntaxException {
53-
proxyFactory.createProxy(null);
54+
@Test
55+
public void testCreateProxy_nullArgument() {
56+
try {
57+
proxyFactory.createProxy(null);
58+
fail();
59+
} catch (NullPointerException ex) {
60+
assertThat(ex.getMessage(), is("uri is null"));
61+
}
5462
}
5563

56-
@Test(expected = IllegalArgumentException.class)
64+
@Test
5765
public void testCreateProxy_httpSchemeIsUnsupported() throws URISyntaxException {
58-
proxyFactory.createProxy("http://exmaple.com");
66+
URI httpUri = new URI("http://example.com");
67+
try {
68+
proxyFactory.createProxy(httpUri);
69+
fail();
70+
} catch (IllegalArgumentException ex) {
71+
assertThat(ex.getMessage(), is("http is not a supported schema"));
72+
}
5973
}
6074

6175
@Test
62-
public void testCreateProxy_noProxyServiceCreatesDirectConnection() throws URISyntaxException {
63-
assertThat(proxyFactory.createProxy("https://exmaple.com").type(), is(Proxy.Type.DIRECT));
76+
public void testCreateProxy_noProxyServiceCreatesDirectConnection() {
77+
assertThat(proxyFactory.createProxy(exampleUri).type(), is(Proxy.Type.DIRECT));
6478
}
6579

6680
@Test
67-
public void testCreateProxy_noProxyDataCreatesDirectConnection() throws URISyntaxException {
81+
public void testCreateProxy_noProxyDataCreatesDirectConnection() {
6882
proxyFactory.setProxyService(proxyService);
6983
when(proxyService.select(any(URI.class))).thenReturn(new IProxyData[0]);
70-
assertThat(proxyFactory.createProxy("https://exmaple.com").type(), is(Proxy.Type.DIRECT));
84+
assertThat(proxyFactory.createProxy(exampleUri).type(), is(Proxy.Type.DIRECT));
7185
}
7286

7387
@Test
74-
public void testCreateProxy_httpsProxyData() throws URISyntaxException {
88+
public void testCreateProxy_httpsProxyData() {
7589
proxyFactory.setProxyService(proxyService);
7690
IProxyData proxyData = createProxyData(IProxyData.HTTPS_PROXY_TYPE, PROXY_HOST, PROXY_PORT);
7791
when(proxyService.select(any(URI.class))).thenReturn(new IProxyData[]{ proxyData });
7892

79-
assertThat(proxyFactory.createProxy("https://exmaple.com").type(), is(Proxy.Type.HTTP));
93+
assertThat(proxyFactory.createProxy(exampleUri).type(), is(Proxy.Type.HTTP));
8094
}
8195

8296
@Test
83-
public void testCreateProxy_socksProxyData() throws URISyntaxException {
97+
public void testCreateProxy_socksProxyData() {
8498
proxyFactory.setProxyService(proxyService);
8599
IProxyData proxyData = createProxyData(IProxyData.SOCKS_PROXY_TYPE, PROXY_HOST, PROXY_PORT);
86100
when(proxyService.select(any(URI.class))).thenReturn(new IProxyData[]{ proxyData });
87101

88-
assertThat(proxyFactory.createProxy("https://exmaple.com").type(), is(Proxy.Type.SOCKS));
102+
assertThat(proxyFactory.createProxy(exampleUri).type(), is(Proxy.Type.SOCKS));
89103
}
90104

91105
@Test
92-
public void testCreateProxy_unsupportedProxyData() throws URISyntaxException {
106+
public void testCreateProxy_unsupportedProxyData() {
93107
proxyFactory.setProxyService(proxyService);
94108
IProxyData proxyData = createProxyData(IProxyData.HTTP_PROXY_TYPE, PROXY_HOST, PROXY_PORT);
95109
when(proxyService.select(any(URI.class))).thenReturn(new IProxyData[]{ proxyData });
96110

97-
assertThat(proxyFactory.createProxy("https://exmaple.com").type(), is(Proxy.Type.DIRECT));
111+
assertThat(proxyFactory.createProxy(exampleUri).type(), is(Proxy.Type.DIRECT));
98112
}
99113

100114
@Test
101-
public void testCreateProxy_ifHttpsIsFirstItWillBeUsed() throws URISyntaxException {
115+
public void testCreateProxy_ifHttpsIsFirstItWillBeUsed() {
102116
proxyFactory.setProxyService(proxyService);
103117
IProxyData httpsProxyData = createProxyData(IProxyData.HTTPS_PROXY_TYPE, PROXY_HOST, PROXY_PORT);
104118
IProxyData socksProxyData = createProxyData(IProxyData.SOCKS_PROXY_TYPE, PROXY_HOST, PROXY_PORT);
105119
when(proxyService.select(any(URI.class)))
106120
.thenReturn(new IProxyData[]{ httpsProxyData, socksProxyData });
107121

108-
assertThat(proxyFactory.createProxy("https://exmaple.com").type(), is(Proxy.Type.HTTP));
122+
assertThat(proxyFactory.createProxy(exampleUri).type(), is(Proxy.Type.HTTP));
109123
}
110124

111125
@Test
112-
public void testCreateProxy_ifSocksIsFirstItWillBeUsed() throws URISyntaxException {
126+
public void testCreateProxy_ifSocksIsFirstItWillBeUsed() {
113127
proxyFactory.setProxyService(proxyService);
114128
IProxyData httpsProxyData = createProxyData(IProxyData.HTTPS_PROXY_TYPE, PROXY_HOST, PROXY_PORT);
115129
IProxyData socksProxyData = createProxyData(IProxyData.SOCKS_PROXY_TYPE, PROXY_HOST, PROXY_PORT);
116130
when(proxyService.select(any(URI.class)))
117131
.thenReturn(new IProxyData[]{ socksProxyData, httpsProxyData });
118132

119-
assertThat(proxyFactory.createProxy("https://exmaple.com").type(), is(Proxy.Type.SOCKS));
133+
assertThat(proxyFactory.createProxy(exampleUri).type(), is(Proxy.Type.SOCKS));
120134
}
121135

122136
@Test
123-
public void testCreateProxy_settingProxyServiceToNullCreatesDirectConnection()
124-
throws URISyntaxException {
137+
public void testCreateProxy_settingProxyServiceToNullCreatesDirectConnection() {
125138
proxyFactory.setProxyService(proxyService);
126139
IProxyData proxyData = createProxyData(IProxyData.HTTPS_PROXY_TYPE, PROXY_HOST, PROXY_PORT);
127140
when(proxyService.select(any(URI.class))).thenReturn(new IProxyData[]{ proxyData });
128141

129-
assertThat(proxyFactory.createProxy("https://exmaple.com").type(), is(Proxy.Type.HTTP));
142+
assertThat(proxyFactory.createProxy(exampleUri).type(), is(Proxy.Type.HTTP));
130143
proxyFactory.setProxyService(null);
131-
assertThat(proxyFactory.createProxy("https://exmaple.com").type(), is(Proxy.Type.DIRECT));
144+
assertThat(proxyFactory.createProxy(exampleUri).type(), is(Proxy.Type.DIRECT));
132145
}
133146

134147
private IProxyData createProxyData(String proxyType, String host, int port) {

plugins/com.google.cloud.tools.eclipse.googleapis.test/src/com/google/cloud/tools/eclipse/googleapis/internal/TransportCacheLoaderTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
package com.google.cloud.tools.eclipse.googleapis.internal;
1818

1919
import static org.junit.Assert.assertNotNull;
20-
import static org.mockito.Matchers.anyString;
20+
import static org.mockito.Matchers.any;
2121
import static org.mockito.Mockito.mock;
2222
import static org.mockito.Mockito.verify;
2323
import static org.mockito.Mockito.when;
2424

2525
import com.google.api.client.http.HttpTransport;
2626
import java.net.Proxy;
27+
import java.net.URI;
2728
import org.junit.Test;
2829
import org.junit.runner.RunWith;
2930
import org.mockito.Mock;
@@ -35,11 +36,12 @@ public class TransportCacheLoaderTest {
3536
@Mock private ProxyFactory proxyFactory;
3637

3738
@Test
38-
public void test() throws Exception {
39-
when(proxyFactory.createProxy(anyString())).thenReturn(mock(Proxy.class));
40-
HttpTransport load = new TransportCacheLoader(proxyFactory).load(GoogleApiUrl.APPENGINE_ADMIN_API);
39+
public void test() {
40+
when(proxyFactory.createProxy(any(URI.class))).thenReturn(mock(Proxy.class));
41+
HttpTransport load = new TransportCacheLoader(proxyFactory)
42+
.load(GoogleApiUrl.APPENGINE_ADMIN_API);
4143
assertNotNull(load);
42-
verify(proxyFactory).createProxy(GoogleApiUrl.APPENGINE_ADMIN_API.getUrl());
44+
verify(proxyFactory).createProxy(GoogleApiUrl.APPENGINE_ADMIN_API.toUri());
4345
}
4446

4547
}

plugins/com.google.cloud.tools.eclipse.googleapis/src/com/google/cloud/tools/eclipse/googleapis/GoogleApiException.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

plugins/com.google.cloud.tools.eclipse.googleapis/src/com/google/cloud/tools/eclipse/googleapis/IGoogleApiFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ public interface IGoogleApiFactory {
3232
/**
3333
* @return a Google Cloud Storage API client
3434
*/
35-
Storage newStorageApi(Credential credential) throws GoogleApiException;
36-
35+
Storage newStorageApi(Credential credential);
36+
3737
/**
3838
* @return an Appengine Apps API client
3939
*/
40-
Apps newAppsApi(Credential credential) throws GoogleApiException;
40+
Apps newAppsApi(Credential credential);
4141

4242
/**
4343
* @return a CloudResourceManager/Projects API client
4444
*/
45-
Projects newProjectsApi(Credential credential) throws GoogleApiException;
45+
Projects newProjectsApi(Credential credential);
4646

4747
}

0 commit comments

Comments
 (0)