Improve type safety of transactional collections by reducing visibility of private features

This commit is contained in:
dsyer
2008-09-02 07:52:22 +00:00
parent 3c098f6cb8
commit 1a6d7202ec
11 changed files with 40 additions and 43 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.batch.core.launch;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
@@ -43,9 +42,7 @@ public class EmptyItemWriter<T> implements ItemWriter<T>, InitializingBean {
List<Object> list;
public void afterPropertiesSet() throws Exception {
TransactionAwareProxyFactory<List<Object>> factory = new TransactionAwareProxyFactory<List<Object>>(
new ArrayList<Object>());
list = factory.createInstance();
list = TransactionAwareProxyFactory.createTransactionalList();
}
public void setFailurePoint(int failurePoint) {

View File

@@ -8,7 +8,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.List;
import java.util.Map;
/**
* Abstract {@link org.springframework.batch.item.ItemReader} for to extend when reading database records in a paging

View File

@@ -50,7 +50,7 @@ public class TransactionAwareProxyFactory<T> {
private T target;
public TransactionAwareProxyFactory(T target) {
private TransactionAwareProxyFactory(T target) {
super();
this.target = begin(target);
}
@@ -99,7 +99,7 @@ public class TransactionAwareProxyFactory<T> {
}
}
public T createInstance() {
private T createInstance() {
ProxyFactory factory = new ProxyFactory(target);
factory.addAdvice(new MethodInterceptor() {
@@ -136,16 +136,31 @@ public class TransactionAwareProxyFactory<T> {
return (Map<K,V>) new TransactionAwareProxyFactory(new HashMap<K,V>()).createInstance();
}
@SuppressWarnings("unchecked")
public static <K,V> Map<K,V> createTransactionalMap(Map<K,V> map) {
return (Map<K,V>) new TransactionAwareProxyFactory(map).createInstance();
}
@SuppressWarnings("unchecked")
public static <T> Set<T> createTransactionalSet() {
return (Set<T>) new TransactionAwareProxyFactory(new HashSet<T>()).createInstance();
}
@SuppressWarnings("unchecked")
public static <T> Set<T> createTransactionalSet(Set<T> set) {
return (Set<T>) new TransactionAwareProxyFactory(set).createInstance();
}
@SuppressWarnings("unchecked")
public static <T> List<T> createTransactionalList() {
return (List<T>) new TransactionAwareProxyFactory(new ArrayList<T>()).createInstance();
}
@SuppressWarnings("unchecked")
public static <T> List<T> createTransactionalList(List<T> list) {
return (List<T>) new TransactionAwareProxyFactory(list).createInstance();
}
private class TargetSynchronization extends TransactionSynchronizationAdapter {
T cache;

View File

@@ -4,15 +4,11 @@ import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.sample.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import javax.sql.DataSource;
/**

View File

@@ -5,7 +5,6 @@ import org.junit.internal.runners.JUnit4ClassRunner;
import org.springframework.batch.item.sample.Foo;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.database.support.IbatisKeyCollector;
import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
import org.springframework.core.io.ClassPathResource;
import com.ibatis.sqlmap.client.SqlMapClient;

View File

@@ -3,13 +3,11 @@ package org.springframework.batch.item.database;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.batch.item.sample.Foo;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import javax.persistence.EntityManagerFactory;
import java.util.Collections;
import java.sql.ResultSet;
import java.sql.SQLException;

View File

@@ -5,12 +5,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.batch.item.sample.Foo;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.annotation.Autowired;
import com.ibatis.sqlmap.client.SqlMapClient;
import javax.persistence.EntityManagerFactory;
import java.util.Collections;

View File

@@ -32,17 +32,12 @@ import org.springframework.transaction.support.TransactionTemplate;
public class TransactionAwareListItemReaderTests extends TestCase {
// TransactionAwareListItemProvider provider = new
// TransactionAwareListItemProvider(Arrays.asList(new String[] { "a",
// "b", "c" }));
ListItemReader<String> reader;
private ListItemReader<String> reader;
@SuppressWarnings("unchecked")
protected void setUp() throws Exception {
super.setUp();
TransactionAwareProxyFactory factory = new TransactionAwareProxyFactory(Arrays.asList(new String[] { "a", "b",
"c" }));
reader = new ListItemReader<String>((List<String>) factory.createInstance());
reader = new ListItemReader<String>(TransactionAwareProxyFactory.createTransactionalList(Arrays.asList("a", "b", "c")));
}
public void testNext() throws Exception {

View File

@@ -27,15 +27,12 @@ import org.springframework.transaction.support.TransactionTemplate;
public class TransactionAwareListFactoryTests extends TestCase {
TransactionAwareProxyFactory<List<String>> factory = new TransactionAwareProxyFactory<List<String>>(Arrays.asList(new String[] { "foo",
"bar", "spam" }));
TransactionTemplate transactionTemplate = new TransactionTemplate(new ResourcelessTransactionManager());
List<String> list;
protected void setUp() throws Exception {
list = (List<String>) factory.createInstance();
list = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList("foo", "bar", "spam"));
}
public void testAdd() {

View File

@@ -27,8 +27,6 @@ import org.springframework.transaction.support.TransactionTemplate;
public class TransactionAwareMapFactoryTests extends TestCase {
TransactionAwareProxyFactory<Map<String, String>> factory;
TransactionTemplate transactionTemplate = new TransactionTemplate(new ResourcelessTransactionManager());
Map<String, String> map;
@@ -38,8 +36,7 @@ public class TransactionAwareMapFactoryTests extends TestCase {
seed.put("foo", "oof");
seed.put("bar", "bar");
seed.put("spam", "maps");
factory = new TransactionAwareProxyFactory<Map<String, String>>(seed);
map = (Map<String, String>) factory.createInstance();
map = TransactionAwareProxyFactory.createTransactionalMap(seed);
}
public void testAdd() {

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.support.transaction;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -23,31 +24,39 @@ import java.util.Set;
import junit.framework.TestCase;
public class TransactionAwareProxyFactoryTests extends TestCase {
public void testCreateList() throws Exception {
List<String> list = TransactionAwareProxyFactory.createTransactionalList();
list.add("foo");
assertEquals(1, list.size());
}
public void testCreateListWithValues() throws Exception {
List<String> list = TransactionAwareProxyFactory.createTransactionalList(Collections.singletonList("foo"));
assertEquals(1, list.size());
}
public void testCreateSet() throws Exception {
Set<String> set = TransactionAwareProxyFactory.createTransactionalSet();
set.add("foo");
assertEquals(1, set.size());
}
public void testCreateSetWithValues() throws Exception {
Set<String> list = TransactionAwareProxyFactory.createTransactionalSet(Collections.singleton("foo"));
assertEquals(1, list.size());
}
public void testCreateMap() throws Exception {
Map<String, String> map = TransactionAwareProxyFactory.createTransactionalMap();
map.put("foo", "bar");
assertEquals(1, map.size());
}
public void testCreateUnsupported() throws Exception {
try {
new TransactionAwareProxyFactory<Object>(new Object()).createInstance();
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
}
public void testCreateMapWithValues() throws Exception {
Map<String, String> map = TransactionAwareProxyFactory.createTransactionalMap(Collections.singletonMap("foo",
"bar"));
assertEquals(1, map.size());
}
}