Polishing

This commit is contained in:
Sam Brannen
2023-07-15 12:53:12 +02:00
parent 8629182822
commit e6d360c1c6
11 changed files with 82 additions and 62 deletions

View File

@@ -27,7 +27,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator;
import org.springframework.dao.support.MapPersistenceExceptionTranslator;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.stereotype.Repository;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator;
import static org.assertj.core.api.Assertions.assertThat;
@@ -28,18 +27,17 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Rod Johnson
* @since 2.0
*/
public class ChainedPersistenceExceptionTranslatorTests {
class ChainedPersistenceExceptionTranslatorTests {
@Test
public void empty() {
void empty() {
ChainedPersistenceExceptionTranslator pet = new ChainedPersistenceExceptionTranslator();
//MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator();
RuntimeException in = new RuntimeException("in");
assertThat(DataAccessUtils.translateIfNecessary(in, pet)).isSameAs(in);
}
@Test
public void exceptionTranslationWithTranslation() {
void exceptionTranslationWithTranslation() {
MapPersistenceExceptionTranslator mpet1 = new MapPersistenceExceptionTranslator();
RuntimeException in1 = new RuntimeException("in");
InvalidDataAccessApiUsageException out1 = new InvalidDataAccessApiUsageException("out");

View File

@@ -20,15 +20,12 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.TypeMismatchDataAccessException;
@@ -40,10 +37,10 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Juergen Hoeller
* @since 20.10.2004
*/
public class DataAccessUtilsTests {
class DataAccessUtilsTests {
@Test
public void withEmptyCollection() {
void withEmptyCollection() {
Collection<String> col = new HashSet<>();
assertThat(DataAccessUtils.uniqueResult(col)).isNull();
@@ -73,7 +70,7 @@ public class DataAccessUtilsTests {
}
@Test
public void withTooLargeCollection() {
void withTooLargeCollection() {
Collection<String> col = new HashSet<>(2);
col.add("test1");
col.add("test2");
@@ -124,7 +121,7 @@ public class DataAccessUtilsTests {
}
@Test
public void withInteger() {
void withInteger() {
Collection<Integer> col = new HashSet<>(1);
col.add(5);
@@ -143,7 +140,7 @@ public class DataAccessUtilsTests {
}
@Test
public void withSameIntegerInstanceTwice() {
void withSameIntegerInstanceTwice() {
Integer i = 5;
Collection<Integer> col = new ArrayList<>(1);
col.add(i);
@@ -158,7 +155,7 @@ public class DataAccessUtilsTests {
}
@Test
public void withEquivalentIntegerInstanceTwice() {
void withEquivalentIntegerInstanceTwice() {
Collection<Integer> col = Arrays.asList(555, 555);
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class)
@@ -167,7 +164,7 @@ public class DataAccessUtilsTests {
}
@Test
public void withLong() {
void withLong() {
Collection<Long> col = new HashSet<>(1);
col.add(5L);
@@ -186,7 +183,7 @@ public class DataAccessUtilsTests {
}
@Test
public void withString() {
void withString() {
Collection<String> col = new HashSet<>(1);
col.add("test1");
@@ -208,7 +205,7 @@ public class DataAccessUtilsTests {
}
@Test
public void withDate() {
void withDate() {
Date date = new Date();
Collection<Date> col = new HashSet<>(1);
col.add(date);
@@ -232,14 +229,14 @@ public class DataAccessUtilsTests {
}
@Test
public void exceptionTranslationWithNoTranslation() {
void exceptionTranslationWithNoTranslation() {
MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator();
RuntimeException in = new RuntimeException();
assertThat(DataAccessUtils.translateIfNecessary(in, mpet)).isSameAs(in);
}
@Test
public void exceptionTranslationWithTranslation() {
void exceptionTranslationWithTranslation() {
MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator();
RuntimeException in = new RuntimeException("in");
InvalidDataAccessApiUsageException out = new InvalidDataAccessApiUsageException("out");
@@ -261,20 +258,4 @@ public class DataAccessUtilsTests {
return ex -> assertThat(ex.getExpectedSize()).as("expected size").isEqualTo(expectedSize);
}
public static class MapPersistenceExceptionTranslator implements PersistenceExceptionTranslator {
// in to out
private final Map<RuntimeException, RuntimeException> translations = new HashMap<>();
public void addTranslation(RuntimeException in, RuntimeException out) {
this.translations.put(in, out);
}
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
return (DataAccessException) translations.get(ex);
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2002-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.dao.support;
import java.util.HashMap;
import java.util.Map;
import org.springframework.dao.DataAccessException;
public class MapPersistenceExceptionTranslator implements PersistenceExceptionTranslator {
// in to out
private final Map<RuntimeException, RuntimeException> translations = new HashMap<>();
public void addTranslation(RuntimeException in, RuntimeException out) {
this.translations.put(in, out);
}
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
return (DataAccessException) this.translations.get(ex);
}
}