Skip to content

Commit 9ab4062

Browse files
committed
Clean up warnings in tests in spring-jdbc
1 parent d54aab2 commit 9ab4062

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

spring-jdbc/src/test/java/org/springframework/jdbc/datasource/lookup/MapDataSourceLookupTests.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,9 @@
2020
import java.util.Map;
2121
import javax.sql.DataSource;
2222

23+
import org.junit.Rule;
2324
import org.junit.Test;
25+
import org.junit.rules.ExpectedException;
2426

2527
import static org.junit.Assert.*;
2628

@@ -32,17 +34,22 @@ public final class MapDataSourceLookupTests {
3234

3335
private static final String DATA_SOURCE_NAME = "dataSource";
3436

37+
@Rule
38+
public final ExpectedException exception = ExpectedException.none();
3539

36-
@SuppressWarnings("unchecked")
37-
@Test(expected=UnsupportedOperationException.class)
38-
public void testGetDataSourcesReturnsUnmodifiableMap() throws Exception {
39-
MapDataSourceLookup lookup = new MapDataSourceLookup(new HashMap<String, DataSource>());
40+
41+
@Test
42+
@SuppressWarnings({ "unchecked", "rawtypes" })
43+
public void getDataSourcesReturnsUnmodifiableMap() throws Exception {
44+
MapDataSourceLookup lookup = new MapDataSourceLookup();
4045
Map dataSources = lookup.getDataSources();
46+
47+
exception.expect(UnsupportedOperationException.class);
4148
dataSources.put("", "");
4249
}
4350

4451
@Test
45-
public void testLookupSunnyDay() throws Exception {
52+
public void lookupSunnyDay() throws Exception {
4653
Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
4754
StubDataSource expectedDataSource = new StubDataSource();
4855
dataSources.put(DATA_SOURCE_NAME, expectedDataSource);
@@ -54,7 +61,7 @@ public void testLookupSunnyDay() throws Exception {
5461
}
5562

5663
@Test
57-
public void testSettingDataSourceMapToNullIsAnIdempotentOperation() throws Exception {
64+
public void setDataSourcesIsAnIdempotentOperation() throws Exception {
5865
Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
5966
StubDataSource expectedDataSource = new StubDataSource();
6067
dataSources.put(DATA_SOURCE_NAME, expectedDataSource);
@@ -67,7 +74,7 @@ public void testSettingDataSourceMapToNullIsAnIdempotentOperation() throws Excep
6774
}
6875

6976
@Test
70-
public void testAddingDataSourcePermitsOverride() throws Exception {
77+
public void addingDataSourcePermitsOverride() throws Exception {
7178
Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
7279
StubDataSource overridenDataSource = new StubDataSource();
7380
StubDataSource expectedDataSource = new StubDataSource();
@@ -80,19 +87,22 @@ public void testAddingDataSourcePermitsOverride() throws Exception {
8087
assertSame(expectedDataSource, dataSource);
8188
}
8289

83-
@SuppressWarnings("unchecked")
84-
@Test(expected=ClassCastException.class)
85-
public void testGetDataSourceWhereSuppliedMapHasNonDataSourceTypeUnderSpecifiedKey() throws Exception {
86-
Map dataSources = new HashMap<String, DataSource>();
90+
@Test
91+
@SuppressWarnings({ "unchecked", "rawtypes" })
92+
public void getDataSourceWhereSuppliedMapHasNonDataSourceTypeUnderSpecifiedKey() throws Exception {
93+
Map dataSources = new HashMap();
8794
dataSources.put(DATA_SOURCE_NAME, new Object());
88-
MapDataSourceLookup lookup = new MapDataSourceLookup();
89-
lookup.setDataSources(dataSources);
95+
MapDataSourceLookup lookup = new MapDataSourceLookup(dataSources);
96+
97+
exception.expect(ClassCastException.class);
9098
lookup.getDataSource(DATA_SOURCE_NAME);
9199
}
92100

93-
@Test(expected=DataSourceLookupFailureException.class)
94-
public void testGetDataSourceWhereSuppliedMapHasNoEntryForSpecifiedKey() throws Exception {
101+
@Test
102+
public void getDataSourceWhereSuppliedMapHasNoEntryForSpecifiedKey() throws Exception {
95103
MapDataSourceLookup lookup = new MapDataSourceLookup();
104+
105+
exception.expect(DataSourceLookupFailureException.class);
96106
lookup.getDataSource(DATA_SOURCE_NAME);
97107
}
98108

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.sql.SQLException;
2020

21-
import org.junit.Before;
2221
import org.junit.Test;
2322

2423
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -29,37 +28,34 @@
2928
import static org.junit.Assert.*;
3029

3130
/**
32-
* Tests for custom translator.
31+
* Tests for custom {@link SQLExceptionTranslator}.
3332
*
3433
* @author Thomas Risberg
3534
*/
3635
public class CustomSQLExceptionTranslatorRegistrarTests {
3736

38-
@Before
39-
public void setUp() {
40-
new ClassPathXmlApplicationContext("test-custom-translators-context.xml",
41-
CustomSQLExceptionTranslatorRegistrarTests.class);
42-
}
43-
4437
@Test
45-
public void testCustomErrorCodeTranslation() {
38+
public void customErrorCodeTranslation() {
39+
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
40+
"test-custom-translators-context.xml", CustomSQLExceptionTranslatorRegistrarTests.class)) {
4641

47-
SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
48-
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
49-
sext.setSqlErrorCodes(codes);
42+
SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
43+
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
44+
sext.setSqlErrorCodes(codes);
5045

51-
DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
52-
assertNotNull("Should have been translated", exFor4200);
53-
assertTrue("Should have been instance of BadSqlGrammarException",
46+
DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
47+
assertNotNull("Should have been translated", exFor4200);
48+
assertTrue("Should have been instance of BadSqlGrammarException",
5449
BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass()));
5550

56-
DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
57-
assertNotNull("Should have been translated", exFor2);
58-
assertTrue("Should have been instance of TransientDataAccessResourceException",
51+
DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
52+
assertNotNull("Should have been translated", exFor2);
53+
assertTrue("Should have been instance of TransientDataAccessResourceException",
5954
TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass()));
6055

61-
DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
62-
assertNull("Should not have been translated", exFor3);
56+
DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
57+
assertNull("Should not have been translated", exFor3);
58+
}
6359
}
6460

6561
}

0 commit comments

Comments
 (0)