Skip to content

Commit 83bca6d

Browse files
committed
Throw IllegalStateException from afterPropertiesSet
Consistently use Assert.state in the afterPropertiesSet() methods to throw IllegalStateException instead of IllegalArgumentException when some properties are missing and/or invalid. Issue spring-projects#2244
1 parent 75b1492 commit 83bca6d

File tree

98 files changed

+258
-250
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+258
-250
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -283,6 +283,6 @@ private void doUnregister(String jobName) {
283283

284284
@Override
285285
public void afterPropertiesSet() {
286-
Assert.notNull(jobRegistry, "Job registry could not be null.");
286+
Assert.state(jobRegistry != null, "Job registry could not be null.");
287287
}
288288
}

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -102,7 +102,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
102102
*/
103103
@Override
104104
public void afterPropertiesSet() throws Exception {
105-
Assert.notNull(jobRegistry, "JobRegistry must not be null");
105+
Assert.state(jobRegistry != null, "JobRegistry must not be null");
106106
}
107107

108108
/**

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/SimpleFlowFactoryBean.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import org.springframework.beans.factory.FactoryBean;
3434
import org.springframework.beans.factory.InitializingBean;
3535
import org.springframework.util.Assert;
36+
import org.springframework.util.StringUtils;
3637

3738
/**
3839
* Convenience factory for SimpleFlow instances for use in XML namespace. It
@@ -98,7 +99,7 @@ public void setStateTransitions(List<StateTransition> stateTransitions) {
9899
*/
99100
@Override
100101
public void afterPropertiesSet() throws Exception {
101-
Assert.hasText(name, "The flow must have a name");
102+
Assert.state(StringUtils.hasText(name), "The flow must have a name");
102103

103104
if(flowType == null) {
104105
flowType = SimpleFlow.class;

spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -121,7 +121,7 @@ public void setLobHandler(LobHandler lobHandler) {
121121
@Override
122122
public void afterPropertiesSet() throws Exception {
123123

124-
Assert.notNull(dataSource, "DataSource must not be null.");
124+
Assert.state(dataSource != null, "DataSource must not be null.");
125125

126126
if (jdbcOperations == null) {
127127
jdbcOperations = new JdbcTemplate(dataSource);

spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -117,7 +117,7 @@ public void setJobParametersValidator(
117117
*/
118118
@Override
119119
public void afterPropertiesSet() throws Exception {
120-
Assert.notNull(jobRepository, "JobRepository must be set");
120+
Assert.state(jobRepository != null, "JobRepository must be set");
121121
}
122122

123123
/**

spring-batch-core/src/main/java/org/springframework/batch/core/job/CompositeJobParametersValidator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2018 the original author or authors.
2+
* Copyright 2011-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,8 +60,8 @@ public void setValidators(List<JobParametersValidator> validators) {
6060

6161
@Override
6262
public void afterPropertiesSet() throws Exception {
63-
Assert.notNull(validators, "The 'validators' may not be null");
64-
Assert.notEmpty(validators, "The 'validators' may not be empty");
63+
Assert.state(validators != null, "The 'validators' may not be null");
64+
Assert.state(!validators.isEmpty(), "The 'validators' may not be empty");
6565
}
6666

6767

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -105,10 +105,10 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
105105
*/
106106
@Override
107107
public void afterPropertiesSet() throws Exception {
108-
Assert.notNull(jobLauncher, "JobLauncher must be provided");
109-
Assert.notNull(jobRegistry, "JobLocator must be provided");
110-
Assert.notNull(jobExplorer, "JobExplorer must be provided");
111-
Assert.notNull(jobRepository, "JobRepository must be provided");
108+
Assert.state(jobLauncher != null, "JobLauncher must be provided");
109+
Assert.state(jobRegistry != null, "JobLocator must be provided");
110+
Assert.state(jobExplorer != null, "JobExplorer must be provided");
111+
Assert.state(jobRepository != null, "JobRepository must be provided");
112112
}
113113

114114
/**

spring-batch-core/src/main/java/org/springframework/batch/core/listener/AbstractListenerFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -197,7 +197,7 @@ public void setMetaDataMap(Map<String, String> metaDataMap) {
197197

198198
@Override
199199
public void afterPropertiesSet() throws Exception {
200-
Assert.notNull(delegate, "Delegate must not be null");
200+
Assert.state(delegate != null, "Delegate must not be null");
201201
}
202202

203203
/**

spring-batch-core/src/main/java/org/springframework/batch/core/listener/ExecutionContextPromotionListener.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.beans.factory.InitializingBean;
2626
import org.springframework.lang.Nullable;
2727
import org.springframework.util.Assert;
28+
import org.springframework.util.ObjectUtils;
2829

2930
/**
3031
* This class can be used to automatically promote items from the {@link Step}
@@ -76,10 +77,10 @@ public ExitStatus afterStep(StepExecution stepExecution) {
7677

7778
@Override
7879
public void afterPropertiesSet() throws Exception {
79-
Assert.notNull(this.keys, "The 'keys' property must be provided");
80-
Assert.notEmpty(this.keys, "The 'keys' property must not be empty");
81-
Assert.notNull(this.statuses, "The 'statuses' property must be provided");
82-
Assert.notEmpty(this.statuses, "The 'statuses' property must not be empty");
80+
Assert.state(this.keys != null, "The 'keys' property must be provided");
81+
Assert.state(!ObjectUtils.isEmpty(this.keys), "The 'keys' property must not be empty");
82+
Assert.state(this.statuses != null, "The 'statuses' property must be provided");
83+
Assert.state(!ObjectUtils.isEmpty(this.statuses), "The 'statuses' property must not be empty");
8384
}
8485

8586
/**

spring-batch-core/src/main/java/org/springframework/batch/core/partition/support/PartitionStep.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,8 +81,8 @@ public void setStepExecutionSplitter(StepExecutionSplitter stepExecutionSplitter
8181
*/
8282
@Override
8383
public void afterPropertiesSet() throws Exception {
84-
Assert.notNull(stepExecutionSplitter, "StepExecutionSplitter must be provided");
85-
Assert.notNull(partitionHandler, "PartitionHandler must be provided");
84+
Assert.state(stepExecutionSplitter != null, "StepExecutionSplitter must be provided");
85+
Assert.state(partitionHandler != null, "PartitionHandler must be provided");
8686
super.afterPropertiesSet();
8787
}
8888

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/AbstractJdbcBatchMetadataDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,7 +81,7 @@ public void setClobTypeToUse(int clobTypeToUse) {
8181

8282
@Override
8383
public void afterPropertiesSet() throws Exception {
84-
Assert.notNull(jdbcTemplate, "JdbcOperations is required");
84+
Assert.state(jdbcTemplate != null, "JdbcOperations is required");
8585
}
8686

8787
}

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -121,7 +121,7 @@ public void setJobExecutionIncrementer(DataFieldMaxValueIncrementer jobExecution
121121
@Override
122122
public void afterPropertiesSet() throws Exception {
123123
super.afterPropertiesSet();
124-
Assert.notNull(jobExecutionIncrementer, "The jobExecutionIncrementer must not be null.");
124+
Assert.state(jobExecutionIncrementer != null, "The jobExecutionIncrementer must not be null.");
125125
}
126126

127127
@Override

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobInstanceDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -325,7 +325,7 @@ public void setJobInstanceIncrementer(DataFieldMaxValueIncrementer jobInstanceIn
325325
@Override
326326
public void afterPropertiesSet() throws Exception {
327327
super.afterPropertiesSet();
328-
Assert.notNull(jobInstanceIncrementer, "jobInstanceIncrementer is required");
328+
Assert.state(jobInstanceIncrementer != null, "jobInstanceIncrementer is required");
329329
}
330330

331331
/**

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void setStepExecutionIncrementer(DataFieldMaxValueIncrementer stepExecuti
130130
@Override
131131
public void afterPropertiesSet() throws Exception {
132132
super.afterPropertiesSet();
133-
Assert.notNull(stepExecutionIncrementer, "StepExecutionIncrementer cannot be null.");
133+
Assert.state(stepExecutionIncrementer != null, "StepExecutionIncrementer cannot be null.");
134134
}
135135

136136
/**

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/AbstractJobRepositoryFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -207,7 +207,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
207207

208208
@Override
209209
public void afterPropertiesSet() throws Exception {
210-
Assert.notNull(transactionManager, "TransactionManager must not be null.");
210+
Assert.state(transactionManager != null, "TransactionManager must not be null.");
211211

212212
initializeProxy();
213213
}

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -170,7 +170,7 @@ public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incremente
170170
@Override
171171
public void afterPropertiesSet() throws Exception {
172172

173-
Assert.notNull(dataSource, "DataSource must not be null.");
173+
Assert.state(dataSource != null, "DataSource must not be null.");
174174

175175
if (jdbcOperations == null) {
176176
jdbcOperations = new JdbcTemplate(dataSource);
@@ -197,12 +197,12 @@ public void afterPropertiesSet() throws Exception {
197197
serializer = defaultSerializer;
198198
}
199199

200-
Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), () -> "'" + databaseType
200+
Assert.state(incrementerFactory.isSupportedIncrementerType(databaseType), () -> "'" + databaseType
201201
+ "' is an unsupported database type. The supported database types are "
202202
+ StringUtils.arrayToCommaDelimitedString(incrementerFactory.getSupportedIncrementerTypes()));
203203

204204
if(lobType != null) {
205-
Assert.isTrue(isValidTypes(lobType), "lobType must be a value from the java.sql.Types class");
205+
Assert.state(isValidTypes(lobType), "lobType must be a value from the java.sql.Types class");
206206
}
207207

208208
super.afterPropertiesSet();

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SimpleChunkProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -85,7 +85,7 @@ public void setItemWriter(ItemWriter<? super O> itemWriter) {
8585
*/
8686
@Override
8787
public void afterPropertiesSet() throws Exception {
88-
Assert.notNull(itemWriter, "ItemWriter must be set");
88+
Assert.state(itemWriter != null, "ItemWriter must be set");
8989
}
9090

9191
/**

spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/CallableTaskletAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,7 @@ public void setCallable(Callable<RepeatStatus> callable) {
5050
*/
5151
@Override
5252
public void afterPropertiesSet() throws Exception {
53-
Assert.notNull(callable, "A Callable is required");
53+
Assert.state(callable != null, "A Callable is required");
5454
}
5555

5656
/**

spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
import org.springframework.core.task.TaskExecutor;
3838
import org.springframework.lang.Nullable;
3939
import org.springframework.util.Assert;
40+
import org.springframework.util.StringUtils;
4041

4142
/**
4243
* {@link Tasklet} that executes a system command.
@@ -175,10 +176,10 @@ public void setWorkingDirectory(String dir) {
175176

176177
@Override
177178
public void afterPropertiesSet() throws Exception {
178-
Assert.hasLength(command, "'command' property value is required");
179-
Assert.notNull(systemProcessExitCodeMapper, "SystemProcessExitCodeMapper must be set");
180-
Assert.isTrue(timeout > 0, "timeout value must be greater than zero");
181-
Assert.notNull(taskExecutor, "taskExecutor is required");
179+
Assert.state(StringUtils.hasLength(command), "'command' property value is required");
180+
Assert.state(systemProcessExitCodeMapper != null, "SystemProcessExitCodeMapper must be set");
181+
Assert.state(timeout > 0, "timeout value must be greater than zero");
182+
Assert.state(taskExecutor != null, "taskExecutor is required");
182183
stoppable = jobExplorer != null;
183184
}
184185

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessorTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,9 +40,9 @@ public class JobRegistryBeanPostProcessorTests {
4040
public void testInitializationFails() throws Exception {
4141
try {
4242
processor.afterPropertiesSet();
43-
fail("Expected IllegalArgumentException");
43+
fail("Expected IllegalStateException");
4444
}
45-
catch (IllegalArgumentException e) {
45+
catch (IllegalStateException e) {
4646
// expected
4747
assertTrue(e.getMessage().contains("JobRegistry"));
4848
}

spring-batch-core/src/test/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBeanTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2008 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,7 +79,7 @@ public void testMissingDataSource() throws Exception {
7979
factory.afterPropertiesSet();
8080
fail();
8181
}
82-
catch (IllegalArgumentException ex) {
82+
catch (IllegalStateException ex) {
8383
// expected
8484
String message = ex.getMessage();
8585
assertTrue("Wrong message: " + message, message.indexOf("DataSource") >= 0);

0 commit comments

Comments
 (0)