Skip to content

Commit

Permalink
Throw IllegalStateException from afterPropertiesSet
Browse files Browse the repository at this point in the history
Consistently use Assert.state in the afterPropertiesSet()
methods to throw IllegalStateException instead of
IllegalArgumentException when some properties are missing
and/or invalid.

Resolves #2244
  • Loading branch information
danilopiazza authored and fmbenhassine committed Nov 23, 2022
1 parent b9d6e26 commit fc0ec01
Show file tree
Hide file tree
Showing 96 changed files with 188 additions and 178 deletions.
Expand Up @@ -278,7 +278,7 @@ private void doUnregister(String jobName) {

@Override
public void afterPropertiesSet() {
Assert.notNull(jobRegistry, "Job registry could not be null.");
Assert.state(jobRegistry != null, "Job registry could not be null.");
}

}
Expand Up @@ -98,7 +98,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(jobRegistry, "JobRegistry must not be null");
Assert.state(jobRegistry != null, "JobRegistry must not be null");
}

/**
Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Convenience factory for {@link SimpleFlow} instances for use in the XML namespace. It
Expand Down Expand Up @@ -94,7 +95,7 @@ public void setStateTransitions(List<StateTransition> stateTransitions) {
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.hasText(name, "The flow must have a name");
Assert.state(StringUtils.hasText(name), "The flow must have a name");

if (flowType == null) {
flowType = SimpleFlow.class;
Expand Down
Expand Up @@ -153,7 +153,7 @@ public void setConversionService(@NonNull ConfigurableConversionService conversi
@Override
public void afterPropertiesSet() throws Exception {

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

if (jdbcOperations == null) {
jdbcOperations = new JdbcTemplate(dataSource);
Expand Down
Expand Up @@ -128,7 +128,7 @@ public void setJobParametersValidator(JobParametersValidator jobParametersValida
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(jobRepository, "JobRepository must be set");
Assert.state(jobRepository != null, "JobRepository must be set");
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2018 the original author or authors.
* Copyright 2011-2022 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.
Expand Down Expand Up @@ -60,8 +60,8 @@ public void setValidators(List<JobParametersValidator> validators) {

@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(validators, "The 'validators' may not be null");
Assert.notEmpty(validators, "The 'validators' may not be empty");
Assert.state(validators != null, "The 'validators' may not be null");
Assert.state(!validators.isEmpty(), "The 'validators' may not be empty");
}

}
Expand Up @@ -110,10 +110,10 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(jobLauncher, "JobLauncher must be provided");
Assert.notNull(jobRegistry, "JobLocator must be provided");
Assert.notNull(jobExplorer, "JobExplorer must be provided");
Assert.notNull(jobRepository, "JobRepository must be provided");
Assert.state(jobLauncher != null, "JobLauncher must be provided");
Assert.state(jobRegistry != null, "JobLocator must be provided");
Assert.state(jobExplorer != null, "JobExplorer must be provided");
Assert.state(jobRepository != null, "JobRepository must be provided");
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2022 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.
Expand Down Expand Up @@ -196,7 +196,7 @@ public void setMetaDataMap(Map<String, String> metaDataMap) {

@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(delegate, "Delegate must not be null");
Assert.state(delegate != null, "Delegate must not be null");
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 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.
Expand All @@ -25,6 +25,7 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

/**
* This class can be used to automatically promote items from the {@link Step}
Expand Down Expand Up @@ -77,10 +78,10 @@ public ExitStatus afterStep(StepExecution stepExecution) {

@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(this.keys, "The 'keys' property must be provided");
Assert.notEmpty(this.keys, "The 'keys' property must not be empty");
Assert.notNull(this.statuses, "The 'statuses' property must be provided");
Assert.notEmpty(this.statuses, "The 'statuses' property must not be empty");
Assert.state(this.keys != null, "The 'keys' property must be provided");
Assert.state(!ObjectUtils.isEmpty(this.keys), "The 'keys' property must not be empty");
Assert.state(this.statuses != null, "The 'statuses' property must be provided");
Assert.state(!ObjectUtils.isEmpty(this.statuses), "The 'statuses' property must not be empty");
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 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.
Expand Down Expand Up @@ -78,8 +78,8 @@ public void setStepExecutionSplitter(StepExecutionSplitter stepExecutionSplitter
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(stepExecutionSplitter, "StepExecutionSplitter must be provided");
Assert.notNull(partitionHandler, "PartitionHandler must be provided");
Assert.state(stepExecutionSplitter != null, "StepExecutionSplitter must be provided");
Assert.state(partitionHandler != null, "PartitionHandler must be provided");
super.afterPropertiesSet();
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2022 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.
Expand Down Expand Up @@ -79,7 +79,7 @@ public void setClobTypeToUse(int clobTypeToUse) {

@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(jdbcTemplate, "JdbcOperations is required");
Assert.state(jdbcTemplate != null, "JdbcOperations is required");
}

}
Expand Up @@ -138,7 +138,7 @@ public void setConversionService(@NonNull ConfigurableConversionService conversi
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Assert.notNull(jobExecutionIncrementer, "The jobExecutionIncrementer must not be null.");
Assert.state(jobExecutionIncrementer != null, "The jobExecutionIncrementer must not be null.");
}

@Override
Expand Down
Expand Up @@ -311,7 +311,7 @@ public void setJobInstanceIncrementer(DataFieldMaxValueIncrementer jobInstanceIn
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Assert.notNull(jobInstanceIncrementer, "jobInstanceIncrementer is required");
Assert.state(jobInstanceIncrementer != null, "jobInstanceIncrementer is required");
}

/**
Expand Down
Expand Up @@ -129,7 +129,7 @@ public void setStepExecutionIncrementer(DataFieldMaxValueIncrementer stepExecuti
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Assert.notNull(stepExecutionIncrementer, "StepExecutionIncrementer cannot be null.");
Assert.state(stepExecutionIncrementer != null, "StepExecutionIncrementer cannot be null.");
}

/**
Expand Down
Expand Up @@ -178,7 +178,7 @@ public void setTransactionAttributeSource(TransactionAttributeSource transaction

@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(transactionManager, "TransactionManager must not be null.");
Assert.state(transactionManager != null, "TransactionManager must not be null.");
if (this.transactionAttributeSource == null) {
Properties transactionAttributes = new Properties();
transactionAttributes.setProperty("create*",
Expand Down
Expand Up @@ -199,7 +199,7 @@ public void setConversionService(@NonNull ConfigurableConversionService conversi
@Override
public void afterPropertiesSet() throws Exception {

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

if (jdbcOperations == null) {
jdbcOperations = new JdbcTemplate(dataSource);
Expand All @@ -226,12 +226,12 @@ public void afterPropertiesSet() throws Exception {
serializer = defaultSerializer;
}

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

if (clobType != null) {
Assert.isTrue(isValidTypes(clobType), "lobType must be a value from the java.sql.Types class");
Assert.state(isValidTypes(clobType), "lobType must be a value from the java.sql.Types class");
}

if (this.conversionService == null) {
Expand Down
Expand Up @@ -99,7 +99,7 @@ public void setMeterRegistry(MeterRegistry meterRegistry) {
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(itemWriter, "ItemWriter must be set");
Assert.state(itemWriter != null, "ItemWriter must be set");
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2019 the original author or authors.
* Copyright 2006-2022 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.
Expand Down Expand Up @@ -49,7 +49,7 @@ public void setCallable(Callable<RepeatStatus> callable) {
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(callable, "A Callable is required");
Assert.state(callable != null, "A Callable is required");
}

/**
Expand Down
Expand Up @@ -37,6 +37,8 @@
import org.springframework.core.task.TaskExecutor;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
* {@link Tasklet} that executes a system command.
Expand Down Expand Up @@ -190,14 +192,13 @@ public void setWorkingDirectory(String dir) {

@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(commandRunner, "CommandRunner must be set");
Assert.notNull(cmdArray, "'cmdArray' property value must not be null");
Assert.notEmpty(cmdArray, "'cmdArray' property value is required with at least 1 element");
Assert.noNullElements(cmdArray, "'cmdArray' property value must not contain be null elements");
Assert.hasLength(cmdArray[0], "'cmdArray' property value is required with at least 1 element");
Assert.notNull(systemProcessExitCodeMapper, "SystemProcessExitCodeMapper must be set");
Assert.isTrue(timeout > 0, "timeout value must be greater than zero");
Assert.notNull(taskExecutor, "taskExecutor is required");
Assert.state(commandRunner != null, "CommandRunner must be set");
Assert.state(cmdArray != null, "'cmdArray' property value must not be null");
Assert.state(!ObjectUtils.isEmpty(cmdArray), "'cmdArray' property value is required with at least 1 element");
Assert.state(StringUtils.hasText(cmdArray[0]), "'cmdArray' property value is required with at least 1 element");
Assert.state(systemProcessExitCodeMapper != null, "SystemProcessExitCodeMapper must be set");
Assert.state(timeout > 0, "timeout value must be greater than zero");
Assert.state(taskExecutor != null, "taskExecutor is required");
stoppable = jobExplorer != null;
}

Expand Down
Expand Up @@ -38,7 +38,7 @@ class JobRegistryBeanPostProcessorTests {

@Test
void testInitializationFails() {
Exception exception = assertThrows(IllegalArgumentException.class, processor::afterPropertiesSet);
Exception exception = assertThrows(IllegalStateException.class, processor::afterPropertiesSet);
assertTrue(exception.getMessage().contains("JobRegistry"));
}

Expand Down
Expand Up @@ -84,7 +84,7 @@ void testCustomJdbcOperations() throws Exception {
void testMissingDataSource() {

factory.setDataSource(null);
Exception exception = assertThrows(IllegalArgumentException.class, factory::afterPropertiesSet);
Exception exception = assertThrows(IllegalStateException.class, factory::afterPropertiesSet);
String message = exception.getMessage();
assertTrue(message.contains("DataSource"), "Wrong message: " + message);

Expand Down
Expand Up @@ -41,13 +41,13 @@ void setUp() {
@Test
void testValidatorsCanNotBeNull() {
compositeJobParametersValidator.setValidators(null);
assertThrows(IllegalArgumentException.class, compositeJobParametersValidator::afterPropertiesSet);
assertThrows(IllegalStateException.class, compositeJobParametersValidator::afterPropertiesSet);
}

@Test
void testValidatorsCanNotBeEmpty() {
compositeJobParametersValidator.setValidators(new ArrayList<>());
assertThrows(IllegalArgumentException.class, compositeJobParametersValidator::afterPropertiesSet);
assertThrows(IllegalStateException.class, compositeJobParametersValidator::afterPropertiesSet);
}

@Test
Expand Down
Expand Up @@ -120,7 +120,7 @@ void testToString() {
@Test
void testAfterPropertiesSet() {
job.setJobRepository(null);
Exception exception = assertThrows(IllegalArgumentException.class, () -> job.afterPropertiesSet());
Exception exception = assertThrows(IllegalStateException.class, () -> job.afterPropertiesSet());
assertTrue(exception.getMessage().contains("JobRepository"));
}

Expand Down
Expand Up @@ -141,7 +141,7 @@ public Properties getProperties(@Nullable JobParameters params) {
@Test
void testMandatoryProperties() {
jobOperator = new SimpleJobOperator();
assertThrows(IllegalArgumentException.class, jobOperator::afterPropertiesSet);
assertThrows(IllegalStateException.class, jobOperator::afterPropertiesSet);
}

/**
Expand Down
Expand Up @@ -247,7 +247,7 @@ void promoteEntriesKeyNotFoundStrict() throws Exception {
void keysMustBeSet() {
ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();
// didn't set the keys, same as listener.setKeys(null);
assertThrows(IllegalArgumentException.class, listener::afterPropertiesSet);
assertThrows(IllegalStateException.class, listener::afterPropertiesSet);
}

}
Expand Up @@ -241,7 +241,7 @@ void testCustomJdbcOperations() throws Exception {
void testMissingDataSource() {

factory.setDataSource(null);
Exception exception = assertThrows(IllegalArgumentException.class, factory::afterPropertiesSet);
Exception exception = assertThrows(IllegalStateException.class, factory::afterPropertiesSet);
String message = exception.getMessage();
assertTrue(message.contains("DataSource"), "Wrong message: " + message);

Expand All @@ -255,7 +255,7 @@ void testMissingTransactionManager() {
when(incrementerFactory.isSupportedIncrementerType("mockDb")).thenReturn(true);
when(incrementerFactory.getSupportedIncrementerTypes()).thenReturn(new String[0]);

Exception exception = assertThrows(IllegalArgumentException.class, () -> factory.afterPropertiesSet());
Exception exception = assertThrows(IllegalStateException.class, () -> factory.afterPropertiesSet());
String message = exception.getMessage();
assertTrue(message.contains("TransactionManager"), "Wrong message: " + message);

Expand All @@ -268,7 +268,7 @@ void testInvalidDatabaseType() {
when(incrementerFactory.isSupportedIncrementerType("foo")).thenReturn(false);
when(incrementerFactory.getSupportedIncrementerTypes()).thenReturn(new String[0]);

Exception exception = assertThrows(IllegalArgumentException.class, () -> factory.afterPropertiesSet());
Exception exception = assertThrows(IllegalStateException.class, () -> factory.afterPropertiesSet());
String message = exception.getMessage();
assertTrue(message.contains("foo"), "Wrong message: " + message);

Expand Down Expand Up @@ -363,7 +363,7 @@ public void testCustomTransactionAttributesSource() throws Exception {
@Test
void testInvalidCustomLobType() {
factory.setClobType(Integer.MAX_VALUE);
assertThrows(IllegalArgumentException.class, this::testCreateRepository);
assertThrows(IllegalStateException.class, this::testCreateRepository);
}

@Test
Expand Down
Expand Up @@ -40,7 +40,7 @@ public RepeatStatus call() throws Exception {

@Test
void testAfterPropertiesSet() {
assertThrows(IllegalArgumentException.class, adapter::afterPropertiesSet);
assertThrows(IllegalStateException.class, adapter::afterPropertiesSet);
}

}

0 comments on commit fc0ec01

Please sign in to comment.