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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -32,17 +34,22 @@ public final class MapDataSourceLookupTests {
private static final String DATA_SOURCE_NAME = "dataSource"; private static final String DATA_SOURCE_NAME = "dataSource";
@Rule
public final ExpectedException exception = ExpectedException.none();
@SuppressWarnings("unchecked")
@Test(expected=UnsupportedOperationException.class) @Test
public void testGetDataSourcesReturnsUnmodifiableMap() throws Exception { @SuppressWarnings({ "unchecked", "rawtypes" })
MapDataSourceLookup lookup = new MapDataSourceLookup(new HashMap<String, DataSource>()); public void getDataSourcesReturnsUnmodifiableMap() throws Exception {
MapDataSourceLookup lookup = new MapDataSourceLookup();
Map dataSources = lookup.getDataSources(); Map dataSources = lookup.getDataSources();
exception.expect(UnsupportedOperationException.class);
dataSources.put("", ""); dataSources.put("", "");
} }
@Test @Test
public void testLookupSunnyDay() throws Exception { public void lookupSunnyDay() throws Exception {
Map<String, DataSource> dataSources = new HashMap<String, DataSource>(); Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
StubDataSource expectedDataSource = new StubDataSource(); StubDataSource expectedDataSource = new StubDataSource();
dataSources.put(DATA_SOURCE_NAME, expectedDataSource); dataSources.put(DATA_SOURCE_NAME, expectedDataSource);
@ -54,7 +61,7 @@ public final class MapDataSourceLookupTests {
} }
@Test @Test
public void testSettingDataSourceMapToNullIsAnIdempotentOperation() throws Exception { public void setDataSourcesIsAnIdempotentOperation() throws Exception {
Map<String, DataSource> dataSources = new HashMap<String, DataSource>(); Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
StubDataSource expectedDataSource = new StubDataSource(); StubDataSource expectedDataSource = new StubDataSource();
dataSources.put(DATA_SOURCE_NAME, expectedDataSource); dataSources.put(DATA_SOURCE_NAME, expectedDataSource);
@ -67,7 +74,7 @@ public final class MapDataSourceLookupTests {
} }
@Test @Test
public void testAddingDataSourcePermitsOverride() throws Exception { public void addingDataSourcePermitsOverride() throws Exception {
Map<String, DataSource> dataSources = new HashMap<String, DataSource>(); Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
StubDataSource overridenDataSource = new StubDataSource(); StubDataSource overridenDataSource = new StubDataSource();
StubDataSource expectedDataSource = new StubDataSource(); StubDataSource expectedDataSource = new StubDataSource();
@ -80,19 +87,22 @@ public final class MapDataSourceLookupTests {
assertSame(expectedDataSource, dataSource); assertSame(expectedDataSource, dataSource);
} }
@SuppressWarnings("unchecked") @Test
@Test(expected=ClassCastException.class) @SuppressWarnings({ "unchecked", "rawtypes" })
public void testGetDataSourceWhereSuppliedMapHasNonDataSourceTypeUnderSpecifiedKey() throws Exception { public void getDataSourceWhereSuppliedMapHasNonDataSourceTypeUnderSpecifiedKey() throws Exception {
Map dataSources = new HashMap<String, DataSource>(); Map dataSources = new HashMap();
dataSources.put(DATA_SOURCE_NAME, new Object()); dataSources.put(DATA_SOURCE_NAME, new Object());
MapDataSourceLookup lookup = new MapDataSourceLookup(); MapDataSourceLookup lookup = new MapDataSourceLookup(dataSources);
lookup.setDataSources(dataSources);
exception.expect(ClassCastException.class);
lookup.getDataSource(DATA_SOURCE_NAME); lookup.getDataSource(DATA_SOURCE_NAME);
} }
@Test(expected=DataSourceLookupFailureException.class) @Test
public void testGetDataSourceWhereSuppliedMapHasNoEntryForSpecifiedKey() throws Exception { public void getDataSourceWhereSuppliedMapHasNoEntryForSpecifiedKey() throws Exception {
MapDataSourceLookup lookup = new MapDataSourceLookup(); MapDataSourceLookup lookup = new MapDataSourceLookup();
exception.expect(DataSourceLookupFailureException.class);
lookup.getDataSource(DATA_SOURCE_NAME); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 java.sql.SQLException;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -29,20 +28,16 @@ import org.springframework.jdbc.BadSqlGrammarException;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
* Tests for custom translator. * Tests for custom {@link SQLExceptionTranslator}.
* *
* @author Thomas Risberg * @author Thomas Risberg
*/ */
public class CustomSQLExceptionTranslatorRegistrarTests { public class CustomSQLExceptionTranslatorRegistrarTests {
@Before
public void setUp() {
new ClassPathXmlApplicationContext("test-custom-translators-context.xml",
CustomSQLExceptionTranslatorRegistrarTests.class);
}
@Test @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"); SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(); SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
@ -61,5 +56,6 @@ public class CustomSQLExceptionTranslatorRegistrarTests {
DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3)); DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
assertNull("Should not have been translated", exFor3); assertNull("Should not have been translated", exFor3);
} }
}
} }