Skip to content

Commit 722137c

Browse files
SNOW-1887451 Replace try catch with assertThrows in tests (#2118)
1 parent d237048 commit 722137c

File tree

57 files changed

+1691
-2196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1691
-2196
lines changed

src/test/java/net/snowflake/client/TestUtil.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import static org.hamcrest.Matchers.matchesPattern;
66
import static org.junit.jupiter.api.Assertions.assertEquals;
77
import static org.junit.jupiter.api.Assertions.assertNotNull;
8-
import static org.junit.jupiter.api.Assertions.fail;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
99

1010
import java.sql.SQLException;
1111
import java.sql.Statement;
@@ -51,12 +51,8 @@ public static boolean isSchemaGeneratedInTests(String schema) {
5151
* @param testCode the code that will run and throws exception
5252
*/
5353
public static void assertSFException(int errorCode, TestRunInterface testCode) {
54-
try {
55-
testCode.run();
56-
fail();
57-
} catch (SFException e) {
58-
assertThat(e.getVendorCode(), is(errorCode));
59-
}
54+
SFException e = assertThrows(SFException.class, testCode::run);
55+
assertThat(e.getVendorCode(), is(errorCode));
6056
}
6157

6258
/** Functional interface used to run a piece of code which throws SFException */
@@ -137,12 +133,8 @@ public interface MethodRaisesSQLException {
137133
}
138134

139135
public static void expectSnowflakeLoggedFeatureNotSupportedException(MethodRaisesSQLException f) {
140-
try {
141-
f.run();
142-
fail("must raise exception");
143-
} catch (SQLException ex) {
144-
assertEquals(ex.getClass().getSimpleName(), "SnowflakeLoggedFeatureNotSupportedException");
145-
}
136+
SQLException ex = assertThrows(SQLException.class, f::run);
137+
assertEquals(ex.getClass().getSimpleName(), "SnowflakeLoggedFeatureNotSupportedException");
146138
}
147139

148140
/**

src/test/java/net/snowflake/client/config/SFClientConfigParserTest.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import static net.snowflake.client.jdbc.SnowflakeUtil.systemUnsetEnv;
1010
import static org.junit.jupiter.api.Assertions.assertEquals;
1111
import static org.junit.jupiter.api.Assertions.assertNull;
12+
import static org.junit.jupiter.api.Assertions.assertThrows;
1213
import static org.junit.jupiter.api.Assertions.assertTrue;
13-
import static org.junit.jupiter.api.Assertions.fail;
1414
import static org.mockito.Mockito.mockStatic;
1515

1616
import java.io.IOException;
@@ -63,27 +63,19 @@ public void testLoadSFClientConfigValidPathWithUnknownProperties() throws IOExce
6363
@Test
6464
public void testLoadSFClientConfigInValidPath() {
6565
String configFilePath = "InvalidPath";
66-
SFClientConfig config = null;
67-
try {
68-
SFClientConfigParser.loadSFClientConfig(configFilePath);
69-
fail("testLoadSFClientConfigInValidPath"); // this will not be reached!
70-
} catch (IOException e) {
71-
// do nothing
72-
}
66+
assertThrows(IOException.class, () -> SFClientConfigParser.loadSFClientConfig(configFilePath));
7367
}
7468

7569
@Test
7670
public void testLoadSFClientConfigInValidJson() {
77-
try {
78-
String invalidJson = "invalidJson";
79-
configFilePath = Paths.get("config.json");
80-
Files.write(configFilePath, invalidJson.getBytes());
81-
SFClientConfigParser.loadSFClientConfig(configFilePath.toString());
82-
83-
fail("testLoadSFClientConfigInValidJson");
84-
} catch (IOException e) {
85-
// DO Nothing
86-
}
71+
assertThrows(
72+
IOException.class,
73+
() -> {
74+
String invalidJson = "invalidJson";
75+
configFilePath = Paths.get("config.json");
76+
Files.write(configFilePath, invalidJson.getBytes());
77+
SFClientConfigParser.loadSFClientConfig(configFilePath.toString());
78+
});
8779
}
8880

8981
@Test

src/test/java/net/snowflake/client/config/SFPermissionsTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.snowflake.client.config;
22

3-
import static org.junit.jupiter.api.Assertions.fail;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
44

55
import java.io.IOException;
66
import java.nio.file.Files;
@@ -55,8 +55,6 @@ public void testLogDirectoryPermissions(String permission, boolean isSucceed) th
5555
Files.setPosixFilePermissions(configFilePath, PosixFilePermissions.fromString(permission));
5656
Boolean result =
5757
SFClientConfigParser.checkGroupOthersWritePermissions(configFilePath.toString());
58-
if (isSucceed != result) {
59-
fail("testLogDirectoryPermissions failed. Expected " + isSucceed);
60-
}
58+
assertEquals(isSucceed, result);
6159
}
6260
}

src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
import static org.junit.jupiter.api.Assertions.assertFalse;
66
import static org.junit.jupiter.api.Assertions.assertNull;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
78
import static org.junit.jupiter.api.Assertions.assertTrue;
89

910
import com.amazonaws.ClientConfiguration;
@@ -28,11 +29,13 @@ public class CoreUtilsMiscellaneousTest {
2829
*/
2930
@Test
3031
public void testSnowflakeAssertTrue() {
31-
try {
32-
AssertUtil.assertTrue(1 == 0, "Numbers do not match");
33-
} catch (SFException e) {
34-
assertEquals("JDBC driver internal error: Numbers do not match.", e.getMessage());
35-
}
32+
SFException e =
33+
assertThrows(
34+
SFException.class,
35+
() -> {
36+
AssertUtil.assertTrue(1 == 0, "Numbers do not match");
37+
});
38+
assertEquals("JDBC driver internal error: Numbers do not match.", e.getMessage());
3639
}
3740

3841
/** Test that Constants.getOS function is working as expected */
@@ -135,11 +138,13 @@ public void testSetSessionlessProxyForS3() throws SnowflakeSQLException {
135138
assertEquals("testuser", clientConfig.getProxyUsername());
136139
// Test that exception is thrown when port number is invalid
137140
props.put("proxyPort", "invalidnumber");
138-
try {
139-
S3HttpUtil.setSessionlessProxyForS3(props, clientConfig);
140-
} catch (SnowflakeSQLException e) {
141-
assertEquals((int) ErrorCode.INVALID_PROXY_PROPERTIES.getMessageCode(), e.getErrorCode());
142-
}
141+
SnowflakeSQLException e =
142+
assertThrows(
143+
SnowflakeSQLException.class,
144+
() -> {
145+
S3HttpUtil.setSessionlessProxyForS3(props, clientConfig);
146+
});
147+
assertEquals((int) ErrorCode.INVALID_PROXY_PROPERTIES.getMessageCode(), e.getErrorCode());
143148
}
144149

145150
@Test
@@ -175,11 +180,13 @@ public void testSetSessionlessProxyForAzure() throws SnowflakeSQLException {
175180
assertEquals(new InetSocketAddress("localhost", 8084), proxy.address());
176181
// Test that exception is thrown when port number is invalid
177182
props.put("proxyPort", "invalidnumber");
178-
try {
179-
HttpUtil.setSessionlessProxyForAzure(props, op);
180-
} catch (SnowflakeSQLException e) {
181-
assertEquals((int) ErrorCode.INVALID_PROXY_PROPERTIES.getMessageCode(), e.getErrorCode());
182-
}
183+
SnowflakeSQLException e =
184+
assertThrows(
185+
SnowflakeSQLException.class,
186+
() -> {
187+
HttpUtil.setSessionlessProxyForAzure(props, op);
188+
});
189+
assertEquals((int) ErrorCode.INVALID_PROXY_PROPERTIES.getMessageCode(), e.getErrorCode());
183190
}
184191

185192
@Test
@@ -334,11 +341,13 @@ public void testConvertProxyPropertiesToHttpClientKey() throws SnowflakeSQLExcep
334341

335342
// Test that exception is thrown when port number is invalid
336343
props.put("proxyPort", "invalidnumber");
337-
try {
338-
settingsKey = SnowflakeUtil.convertProxyPropertiesToHttpClientKey(mode, props);
339-
} catch (SnowflakeSQLException e) {
340-
assertEquals((int) ErrorCode.INVALID_PROXY_PROPERTIES.getMessageCode(), e.getErrorCode());
341-
}
344+
SnowflakeSQLException e =
345+
assertThrows(
346+
SnowflakeSQLException.class,
347+
() -> {
348+
SnowflakeUtil.convertProxyPropertiesToHttpClientKey(mode, props);
349+
});
350+
assertEquals((int) ErrorCode.INVALID_PROXY_PROPERTIES.getMessageCode(), e.getErrorCode());
342351
}
343352

344353
@Test

src/test/java/net/snowflake/client/core/HttpUtilLatestIT.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package net.snowflake.client.core;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.fail;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
55

66
import java.io.IOException;
77
import java.net.SocketTimeoutException;
@@ -11,6 +11,7 @@
1111
import org.apache.http.impl.client.CloseableHttpClient;
1212
import org.hamcrest.CoreMatchers;
1313
import org.hamcrest.MatcherAssert;
14+
import org.junit.jupiter.api.AfterEach;
1415
import org.junit.jupiter.api.BeforeEach;
1516
import org.junit.jupiter.api.Tag;
1617
import org.junit.jupiter.api.Test;
@@ -26,6 +27,12 @@ public void resetHttpClientsCache() {
2627
HttpUtil.httpClient.clear();
2728
}
2829

30+
@AfterEach
31+
public void resetHttpTimeouts() {
32+
HttpUtil.setConnectionTimeout(60000);
33+
HttpUtil.setSocketTimeout(300000);
34+
}
35+
2936
/** Added in > 3.14.5 */
3037
@Test
3138
public void shouldGetDefaultConnectionAndSocketTimeouts() {
@@ -43,14 +50,12 @@ public void shouldOverrideConnectionAndSocketTimeouts() {
4350

4451
CloseableHttpClient httpClient =
4552
HttpUtil.getHttpClient(new HttpClientSettingsKey(OCSPMode.INSECURE));
46-
try {
47-
httpClient.execute(new HttpGet(HANG_WEBSERVER_ADDRESS));
48-
fail("Request should fail with exception");
49-
} catch (IOException e) {
50-
MatcherAssert.assertThat(e, CoreMatchers.instanceOf(SocketTimeoutException.class));
51-
} finally {
52-
HttpUtil.setConnectionTimeout(60000);
53-
HttpUtil.setSocketTimeout(300000);
54-
}
53+
IOException e =
54+
assertThrows(
55+
IOException.class,
56+
() -> {
57+
httpClient.execute(new HttpGet(HANG_WEBSERVER_ADDRESS));
58+
});
59+
MatcherAssert.assertThat(e, CoreMatchers.instanceOf(SocketTimeoutException.class));
5560
}
5661
}

src/test/java/net/snowflake/client/core/SFSessionPropertyTest.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import static org.hamcrest.CoreMatchers.endsWith;
44
import static org.hamcrest.CoreMatchers.is;
55
import static org.hamcrest.MatcherAssert.assertThat;
6-
import static org.junit.jupiter.api.Assertions.fail;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
77

88
import net.snowflake.client.jdbc.ErrorCode;
99
import org.junit.jupiter.api.Test;
@@ -22,12 +22,13 @@ public void testCheckApplicationName() throws SFException {
2222
}
2323

2424
for (String invalid : invalidApplicationName) {
25-
try {
26-
SFSessionProperty.checkPropertyValue(SFSessionProperty.APPLICATION, invalid);
27-
fail();
28-
} catch (SFException e) {
29-
assertThat(e.getVendorCode(), is(ErrorCode.INVALID_PARAMETER_VALUE.getMessageCode()));
30-
}
25+
SFException e =
26+
assertThrows(
27+
SFException.class,
28+
() -> {
29+
SFSessionProperty.checkPropertyValue(SFSessionProperty.APPLICATION, invalid);
30+
});
31+
assertThat(e.getVendorCode(), is(ErrorCode.INVALID_PARAMETER_VALUE.getMessageCode()));
3132
}
3233
}
3334

@@ -42,12 +43,14 @@ public void testCustomSuffixForUserAgentHeaders() {
4243

4344
@Test
4445
public void testInvalidMaxRetries() {
45-
try {
46-
SFSessionProperty.checkPropertyValue(SFSessionProperty.MAX_HTTP_RETRIES, "invalidValue");
47-
fail("testInvalidMaxRetries");
48-
} catch (SFException e) {
49-
assertThat(e.getVendorCode(), is(ErrorCode.INVALID_PARAMETER_VALUE.getMessageCode()));
50-
}
46+
SFException e =
47+
assertThrows(
48+
SFException.class,
49+
() -> {
50+
SFSessionProperty.checkPropertyValue(
51+
SFSessionProperty.MAX_HTTP_RETRIES, "invalidValue");
52+
});
53+
assertThat(e.getVendorCode(), is(ErrorCode.INVALID_PARAMETER_VALUE.getMessageCode()));
5154
}
5255

5356
@Test
@@ -61,12 +64,14 @@ public void testvalidMaxRetries() throws SFException {
6164

6265
@Test
6366
public void testInvalidPutGetMaxRetries() {
64-
try {
65-
SFSessionProperty.checkPropertyValue(SFSessionProperty.PUT_GET_MAX_RETRIES, "invalidValue");
66-
fail("testInvalidMaxRetries");
67-
} catch (SFException e) {
68-
assertThat(e.getVendorCode(), is(ErrorCode.INVALID_PARAMETER_VALUE.getMessageCode()));
69-
}
67+
SFException e =
68+
assertThrows(
69+
SFException.class,
70+
() -> {
71+
SFSessionProperty.checkPropertyValue(
72+
SFSessionProperty.PUT_GET_MAX_RETRIES, "invalidValue");
73+
});
74+
assertThat(e.getVendorCode(), is(ErrorCode.INVALID_PARAMETER_VALUE.getMessageCode()));
7075
}
7176

7277
@Test

src/test/java/net/snowflake/client/core/SessionUtilExternalBrowserTest.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import static org.hamcrest.CoreMatchers.equalTo;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
56
import static org.junit.jupiter.api.Assertions.assertTrue;
6-
import static org.junit.jupiter.api.Assertions.fail;
77
import static org.mockito.Mockito.mock;
88
import static org.mockito.Mockito.mockStatic;
99
import static org.mockito.Mockito.when;
@@ -193,12 +193,13 @@ public void testSessionUtilExternalBrowserFail() throws Throwable {
193193

194194
SessionUtilExternalBrowser sub =
195195
FakeSessionUtilExternalBrowser.createInstance(loginInput, false);
196-
try {
197-
sub.authenticate();
198-
fail("should have failed with an exception.");
199-
} catch (SnowflakeSQLException ex) {
200-
MatcherAssert.assertThat("Error is expected", ex.getErrorCode(), equalTo(123456));
201-
}
196+
SnowflakeSQLException ex =
197+
assertThrows(
198+
SnowflakeSQLException.class,
199+
() -> {
200+
sub.authenticate();
201+
});
202+
MatcherAssert.assertThat("Error is expected", ex.getErrorCode(), equalTo(123456));
202203
}
203204
}
204205

@@ -218,11 +219,13 @@ public void testBuildDefaultHandler() throws URISyntaxException {
218219
public void testInvalidSSOUrl() {
219220
SessionUtilExternalBrowser.DefaultAuthExternalBrowserHandlers handler =
220221
new SessionUtilExternalBrowser.DefaultAuthExternalBrowserHandlers();
221-
try {
222-
handler.openBrowser("file://invalidUrl");
223-
} catch (SFException ex) {
224-
assertTrue(ex.getMessage().contains("Invalid SSOUrl found"));
225-
}
222+
SFException ex =
223+
assertThrows(
224+
SFException.class,
225+
() -> {
226+
handler.openBrowser("file://invalidUrl");
227+
});
228+
assertTrue(ex.getMessage().contains("Invalid SSOUrl found"));
226229
}
227230

228231
/**
@@ -275,11 +278,12 @@ public void testExternalBrowserTimeout() throws Exception {
275278
ds.setPortNumber(Integer.parseInt(params.get("port")));
276279
ds.setUser(params.get("user"));
277280
ds.setBrowserResponseTimeout(10);
278-
try {
279-
ds.getConnection();
280-
fail();
281-
} catch (SnowflakeSQLLoggedException e) {
282-
assertTrue(e.getMessage().contains("External browser authentication failed"));
283-
}
281+
SnowflakeSQLLoggedException e =
282+
assertThrows(
283+
SnowflakeSQLLoggedException.class,
284+
() -> {
285+
ds.getConnection();
286+
});
287+
assertTrue(e.getMessage().contains("External browser authentication failed"));
284288
}
285289
}

0 commit comments

Comments
 (0)