Clean up warnings in tests in spring-jdbc

This commit is contained in:
Sam Brannen 2015-08-19 23:29:57 +02:00
parent d54aab2338
commit 9ab4062317
2 changed files with 43 additions and 37 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,7 +20,9 @@ import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.*;
@ -32,17 +34,22 @@ public final class MapDataSourceLookupTests {
private static final String DATA_SOURCE_NAME = "dataSource";
@Rule
public final ExpectedException exception = ExpectedException.none();
@SuppressWarnings("unchecked")
@Test(expected=UnsupportedOperationException.class)
public void testGetDataSourcesReturnsUnmodifiableMap() throws Exception {
MapDataSourceLookup lookup = new MapDataSourceLookup(new HashMap<String, DataSource>());
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void getDataSourcesReturnsUnmodifiableMap() throws Exception {
MapDataSourceLookup lookup = new MapDataSourceLookup();
Map dataSources = lookup.getDataSources();
exception.expect(UnsupportedOperationException.class);
dataSources.put("", "");
}
@Test
public void testLookupSunnyDay() throws Exception {
public void lookupSunnyDay() throws Exception {
Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
StubDataSource expectedDataSource = new StubDataSource();
dataSources.put(DATA_SOURCE_NAME, expectedDataSource);
@ -54,7 +61,7 @@ public final class MapDataSourceLookupTests {
}
@Test
public void testSettingDataSourceMapToNullIsAnIdempotentOperation() throws Exception {
public void setDataSourcesIsAnIdempotentOperation() throws Exception {
Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
StubDataSource expectedDataSource = new StubDataSource();
dataSources.put(DATA_SOURCE_NAME, expectedDataSource);
@ -67,7 +74,7 @@ public final class MapDataSourceLookupTests {
}
@Test
public void testAddingDataSourcePermitsOverride() throws Exception {
public void addingDataSourcePermitsOverride() throws Exception {
Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
StubDataSource overridenDataSource = new StubDataSource();
StubDataSource expectedDataSource = new StubDataSource();
@ -80,19 +87,22 @@ public final class MapDataSourceLookupTests {
assertSame(expectedDataSource, dataSource);
}
@SuppressWarnings("unchecked")
@Test(expected=ClassCastException.class)
public void testGetDataSourceWhereSuppliedMapHasNonDataSourceTypeUnderSpecifiedKey() throws Exception {
Map dataSources = new HashMap<String, DataSource>();
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void getDataSourceWhereSuppliedMapHasNonDataSourceTypeUnderSpecifiedKey() throws Exception {
Map dataSources = new HashMap();
dataSources.put(DATA_SOURCE_NAME, new Object());
MapDataSourceLookup lookup = new MapDataSourceLookup();
lookup.setDataSources(dataSources);
MapDataSourceLookup lookup = new MapDataSourceLookup(dataSources);
exception.expect(ClassCastException.class);
lookup.getDataSource(DATA_SOURCE_NAME);
}
@Test(expected=DataSourceLookupFailureException.class)
public void testGetDataSourceWhereSuppliedMapHasNoEntryForSpecifiedKey() throws Exception {
@Test
public void getDataSourceWhereSuppliedMapHasNoEntryForSpecifiedKey() throws Exception {
MapDataSourceLookup lookup = new MapDataSourceLookup();
exception.expect(DataSourceLookupFailureException.class);
lookup.getDataSource(DATA_SOURCE_NAME);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.jdbc.support;
import java.sql.SQLException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -29,37 +28,34 @@ import org.springframework.jdbc.BadSqlGrammarException;
import static org.junit.Assert.*;
/**
* Tests for custom translator.
* Tests for custom {@link SQLExceptionTranslator}.
*
* @author Thomas Risberg
*/
public class CustomSQLExceptionTranslatorRegistrarTests {
@Before
public void setUp() {
new ClassPathXmlApplicationContext("test-custom-translators-context.xml",
CustomSQLExceptionTranslatorRegistrarTests.class);
}
@Test
public void testCustomErrorCodeTranslation() {
public void customErrorCodeTranslation() {
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"test-custom-translators-context.xml", CustomSQLExceptionTranslatorRegistrarTests.class)) {
SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
sext.setSqlErrorCodes(codes);
SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
sext.setSqlErrorCodes(codes);
DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
assertNotNull("Should have been translated", exFor4200);
assertTrue("Should have been instance of BadSqlGrammarException",
DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
assertNotNull("Should have been translated", exFor4200);
assertTrue("Should have been instance of BadSqlGrammarException",
BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass()));
DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
assertNotNull("Should have been translated", exFor2);
assertTrue("Should have been instance of TransientDataAccessResourceException",
DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
assertNotNull("Should have been translated", exFor2);
assertTrue("Should have been instance of TransientDataAccessResourceException",
TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass()));
DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
assertNull("Should not have been translated", exFor3);
DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
assertNull("Should not have been translated", exFor3);
}
}
}