Using Asserts with IllegalArgumentException/IllegalStateException instead of ConfigurationException.

This commit is contained in:
Mark Fisher
2008-09-29 14:24:56 +00:00
parent c78e7e67ad
commit 1f0cf0fcf2
8 changed files with 16 additions and 36 deletions

View File

@@ -25,8 +25,8 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -60,10 +60,8 @@ public abstract class AbstractChannelAdapterParser extends AbstractBeanDefinitio
private String createDirectChannel(Element element, ParserContext parserContext) {
String channelId = element.getAttribute("id");
if (!StringUtils.hasText(channelId)) {
throw new ConfigurationException("The channel-adapter's 'id' attribute is required when no 'channel' "
Assert.hasText(channelId, "The channel-adapter's 'id' attribute is required when no 'channel' "
+ "reference has been provided, because that 'id' would be used for the created channel.");
}
BeanDefinitionBuilder channelBuilder = BeanDefinitionBuilder.genericBeanDefinition(DirectChannel.class);
BeanDefinitionHolder holder = new BeanDefinitionHolder(channelBuilder.getBeanDefinition(), channelId);
BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());

View File

@@ -25,7 +25,6 @@ import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.endpoint.MessageEndpoint;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -85,9 +84,7 @@ public abstract class AbstractEndpointParser extends AbstractSingleBeanDefinitio
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
if (this.shouldCreateAdapter(element)) {
String ref = element.getAttribute(REF_ATTRIBUTE);
if (!StringUtils.hasText(ref)) {
throw new ConfigurationException("The '" + REF_ATTRIBUTE + "' attribute is required.");
}
Assert.hasText(ref, "The '" + REF_ATTRIBUTE + "' attribute is required.");
if (StringUtils.hasText(element.getAttribute(METHOD_ATTRIBUTE))) {
String method = element.getAttribute(METHOD_ATTRIBUTE);
String adapterBeanName = this.parseAdapter(ref, method, element, parserContext);
@@ -98,9 +95,7 @@ public abstract class AbstractEndpointParser extends AbstractSingleBeanDefinitio
}
}
String inputChannel = element.getAttribute(INPUT_CHANNEL_ATTRIBUTE);
if (!StringUtils.hasText(inputChannel)) {
throw new ConfigurationException("the '" + INPUT_CHANNEL_ATTRIBUTE + "' attribute is required");
}
Assert.hasText(inputChannel, "the '" + INPUT_CHANNEL_ATTRIBUTE + "' attribute is required");
Element pollerElement = DomUtils.getChildElementByTagName(element, POLLER_ELEMENT);
if (pollerElement != null) {
IntegrationNamespaceUtils.configureTrigger(pollerElement, builder);

View File

@@ -22,10 +22,8 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.endpoint.OutboundChannelAdapter;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
/**
@@ -43,9 +41,7 @@ public abstract class AbstractOutboundChannelAdapterParser extends AbstractChann
String consumerBeanName = this.parseAndRegisterConsumer(element, parserContext);
adapterBuilder.addConstructorArgReference(consumerBeanName);
if (pollerElement != null) {
if (!StringUtils.hasText(channelName)) {
throw new ConfigurationException("outbound channel adapter with a 'poller' requires a 'channel' to poll");
}
Assert.hasText(channelName, "outbound channel adapter with a 'poller' requires a 'channel' to poll");
IntegrationNamespaceUtils.configureTrigger(pollerElement, adapterBuilder);
Element txElement = DomUtils.getChildElementByTagName(pollerElement, "transactional");
if (txElement != null) {

View File

@@ -21,10 +21,9 @@ import org.w3c.dom.Element;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.scheduling.IntervalTrigger;
import org.springframework.util.StringUtils;
import org.springframework.util.Assert;
import org.springframework.util.xml.DomUtils;
/**
@@ -37,9 +36,7 @@ public abstract class AbstractPollingInboundChannelAdapterParser extends Abstrac
@Override
protected AbstractBeanDefinition doParse(Element element, ParserContext parserContext, String channelName) {
String source = this.parseSource(element, parserContext);
if (!StringUtils.hasText(source)) {
throw new ConfigurationException("failed to parse source");
}
Assert.hasText(source, "failed to parse source");
Element pollerElement = DomUtils.getChildElementByTagName(element, "poller");
BeanDefinitionBuilder adapterBuilder = BeanDefinitionBuilder.genericBeanDefinition(SourcePollingChannelAdapter.class);
adapterBuilder.addPropertyReference("source", source);

View File

@@ -24,11 +24,11 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.Conventions;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.scheduling.CronTrigger;
import org.springframework.integration.scheduling.IntervalTrigger;
import org.springframework.integration.scheduling.Trigger;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -143,10 +143,8 @@ public abstract class IntegrationNamespaceUtils {
Trigger trigger = null;
String interval = pollerElement.getAttribute("interval");
String cron = pollerElement.getAttribute("cron");
if (!(StringUtils.hasText(interval) ^ StringUtils.hasText(cron))) {
throw new ConfigurationException(
"A <poller> element must include either an 'interval' or a 'cron' expression (but not both).");
}
Assert.isTrue(StringUtils.hasText(interval) ^ StringUtils.hasText(cron),
"A <poller> element must include either an 'interval' or a 'cron' expression (but not both).");
if (StringUtils.hasText(interval)) {
Long period = Long.valueOf(interval);
IntervalTrigger intervalTrigger = new IntervalTrigger(period);

View File

@@ -28,10 +28,10 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.bus.DefaultMessageBus;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.bus.MessageBusAwareBeanPostProcessor;
import org.springframework.util.Assert;
/**
* Parser for the &lt;message-bus&gt; element of the integration namespace.
@@ -59,10 +59,8 @@ public class MessageBusParser extends AbstractSimpleBeanDefinitionParser {
@Override
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
throws BeanDefinitionStoreException {
if (parserContext.getRegistry().containsBeanDefinition(MESSAGE_BUS_BEAN_NAME)) {
throw new ConfigurationException("Only one instance of '" + MessageBus.class.getSimpleName()
+ "' is allowed per ApplicationContext.");
}
Assert.state(!parserContext.getRegistry().containsBeanDefinition(MESSAGE_BUS_BEAN_NAME),
"Only one instance of '" + MessageBus.class.getSimpleName() + "' is allowed per ApplicationContext.");
return MESSAGE_BUS_BEAN_NAME;
}

View File

@@ -21,8 +21,8 @@ import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.channel.interceptor.WireTap;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -35,9 +35,7 @@ public class WireTapParser implements BeanDefinitionRegisteringParser {
public String parse(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(WireTap.class);
String targetRef = element.getAttribute("channel");
if (!StringUtils.hasText(targetRef)) {
throw new ConfigurationException("the 'channel' attribute is required");
}
Assert.hasText(targetRef, "the 'channel' attribute is required");
builder.addConstructorArgReference(targetRef);
String selectorRef = element.getAttribute("selector");
if (StringUtils.hasText(selectorRef)) {

View File

@@ -72,7 +72,7 @@ public class MessageBusParserTests {
}
catch (BeanDefinitionStoreException e) {
exceptionThrown = true;
assertEquals(ConfigurationException.class, e.getCause().getClass());
assertEquals(IllegalStateException.class, e.getCause().getClass());
}
assertTrue(exceptionThrown);
}