From f53ef655e423b2b6344f794b2f2ce821403fed4c Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Thu, 20 Feb 2014 11:22:23 -0600 Subject: [PATCH] BATCH-2175: Added the ability to inject lobType --- .../support/JobRepositoryFactoryBean.java | 46 ++++++++++++++++--- .../JobRepositoryFactoryBeanTests.java | 19 +++++++- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java index 1407429b2..ebea00ced 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2014 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,6 +18,7 @@ package org.springframework.batch.core.repository.support; import static org.springframework.batch.support.DatabaseType.SYBASE; +import java.lang.reflect.Field; import java.sql.Types; import javax.sql.DataSource; @@ -78,6 +79,15 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i private ExecutionContextSerializer serializer; + private Integer lobType; + + /** + * @param type a value from the {@link java.sql.Types} class to indicate the type to use for a CLOB + */ + public void setClobType(int type) { + this.lobType = type; + } + /** * A custom implementation of the {@link ExecutionContextSerializer}. * The default, if not injected, is the {@link XStreamExecutionContextStringSerializer}. @@ -178,6 +188,10 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i + "' is an unsupported database type. The supported database types are " + StringUtils.arrayToCommaDelimitedString(incrementerFactory.getSupportedIncrementerTypes())); + if(lobType != null) { + Assert.isTrue(isValidTypes(lobType), "lobType must be a value from the java.sql.Types class"); + } + super.afterPropertiesSet(); } @@ -235,13 +249,31 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i return dao; } - private int determineClobTypeToUse(String databaseType) { - if (SYBASE == DatabaseType.valueOf(databaseType.toUpperCase())) { - return Types.LONGVARCHAR; - } - else { - return Types.CLOB; + private int determineClobTypeToUse(String databaseType) throws Exception { + if(lobType != null) { + return lobType; + } else { + if (SYBASE == DatabaseType.valueOf(databaseType.toUpperCase())) { + return Types.LONGVARCHAR; + } + else { + return Types.CLOB; + } } } + private boolean isValidTypes(int value) throws Exception { + boolean result = false; + + for (Field field : Types.class.getFields()) { + int curValue = field.getInt(null); + if(curValue == value) { + result = true; + break; + } + } + + return result; + } + } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBeanTests.java index 7285707be..b848dcfc1 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBeanTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2013 the original author or authors. + * Copyright 2006-2014 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. @@ -16,6 +16,7 @@ package org.springframework.batch.core.repository.support; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; import static org.mockito.Mockito.mock; @@ -23,6 +24,7 @@ import static org.mockito.Mockito.when; import java.sql.Connection; import java.sql.DatabaseMetaData; +import java.sql.Types; import java.util.Map; import javax.sql.DataSource; @@ -241,7 +243,7 @@ public class JobRepositoryFactoryBeanTests { factory.getObject(); } - @Ignore //TODO - fix this test + @Ignore @Test public void testTransactionAttributesForCreateMethodNullHypothesis() throws Exception { testCreateRepository(); @@ -309,7 +311,20 @@ public class JobRepositoryFactoryBeanTests { // expected exception from DataSourceUtils assertEquals("No Statement specified", e.getMessage()); } + } + @Test(expected=IllegalArgumentException.class) + public void testInvalidCustomLobType() throws Exception { + factory.setClobType(Integer.MAX_VALUE); + testCreateRepository(); + } + + @Test + public void testCustomLobType() throws Exception { + factory.setClobType(Types.ARRAY); + testCreateRepository(); + JobRepository repository = (JobRepository) factory.getObject(); + assertNotNull(repository); } private static class StubIncrementer implements DataFieldMaxValueIncrementer {