INT-1957 Inner Poller Can Have default="true"

This commit is contained in:
Gary Russell
2011-07-01 17:57:05 -04:00
parent 4738581ad1
commit 3b00de4202
3 changed files with 182 additions and 3 deletions

View File

@@ -36,6 +36,7 @@ import org.w3c.dom.Element;
* @author Marius Bogoevici
* @author Alex Peters
* @author Oleg Zhurakousky
* @author Gary Russell
*/
public abstract class IntegrationNamespaceUtils {
@@ -148,9 +149,18 @@ public abstract class IntegrationNamespaceUtils {
public static void configurePollerMetadata(Element pollerElement, BeanDefinitionBuilder targetBuilder,
ParserContext parserContext) {
if (pollerElement.hasAttribute("ref")) {
if (pollerElement.getAttributes().getLength() != 1) {
parserContext.getReaderContext().error(
"A 'poller' element that provides a 'ref' must have no other attributes.", pollerElement);
int numberOfAttributes = pollerElement.getAttributes().getLength();
if (numberOfAttributes != 1) {
/*
* When importing the core namespace, e.g. into jdbc, we get a 'default="false"' attribute,
* even if not explicitly declared.
*/
if (!(numberOfAttributes == 2 &&
pollerElement.hasAttribute("default") &&
pollerElement.getAttribute("default").equals("false"))) {
parserContext.getReaderContext().error(
"A 'poller' element that provides a 'ref' must have no other attributes.", pollerElement);
}
}
if (pollerElement.getChildNodes().getLength() != 0) {
parserContext.getReaderContext().error(

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<int:poller id="outer" fixed-rate="5000"/>
<int:channel id="someChannel"/>
<int-jdbc:inbound-channel-adapter channel="someChannel" jdbc-operations="ops"
query="select 1">
<int:poller ref="outer"/>
</int-jdbc:inbound-channel-adapter>
<bean id="ops" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.jdbc.core.JdbcOperations"/>
</bean>
</beans>

View File

@@ -0,0 +1,147 @@
/*
* Copyright 2002-2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.jdbc.config;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
/**
* @author Gary Russell
* @since 2.0.5
*
*/
public class InnerPollerParserTests {
@Test
public void testRefGood() {
new ClassPathXmlApplicationContext("InnerPollerParserTests-context.xml", InnerPollerParserTests.class);
}
@Test
public void testRefExtraAttribute() {
try {
// Load context from a String to avoid IDEs reporting the invalid configuration
String badContext =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<beans xmlns=\"http://www.springframework.org/schema/beans\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:int=\"http://www.springframework.org/schema/integration\"" +
" xmlns:int-jdbc=\"http://www.springframework.org/schema/integration/jdbc\"" +
" xsi:schemaLocation=\"http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd" +
" http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-2.0.xsd" +
" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\">" +
"" +
" <int:poller id=\"outer\" fixed-rate=\"5000\"/>" +
"" +
" <int:channel id=\"someChannel\"/>" +
"" +
" <int-jdbc:inbound-channel-adapter channel=\"someChannel\" jdbc-operations=\"ops\"" +
" query=\"select 1\">" +
" <int:poller ref=\"outer\" fixed-rate=\"1000\"/>" + // <<<<< fixed-rate not allowed here
" </int-jdbc:inbound-channel-adapter>" +
"" +
" <bean id=\"ops\" class=\"org.mockito.Mockito\" factory-method=\"mock\">" +
" <constructor-arg value=\"org.springframework.jdbc.core.JdbcOperations\"/>" +
" </bean>" +
"</beans>";
Resource resource = new ByteArrayResource(badContext.getBytes());
new GenericXmlApplicationContext(resource);
fail("Expected Failure to load ApplicationContext");
} catch (BeanDefinitionParsingException bdpe) {
assertTrue(bdpe.getMessage().startsWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other attributes."));
}
}
@Test
public void testRefDefaultTrue() {
try {
// Load context from a String to avoid IDEs reporting the invalid configuration
String badContext =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<beans xmlns=\"http://www.springframework.org/schema/beans\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:int=\"http://www.springframework.org/schema/integration\"" +
" xmlns:int-jdbc=\"http://www.springframework.org/schema/integration/jdbc\"" +
" xsi:schemaLocation=\"http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd" +
" http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-2.0.xsd" +
" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\">" +
"" +
" <int:poller id=\"outer\" fixed-rate=\"5000\"/>" +
"" +
" <int:channel id=\"someChannel\"/>" +
"" +
" <int-jdbc:inbound-channel-adapter channel=\"someChannel\" jdbc-operations=\"ops\"" +
" query=\"select 1\">" +
" <int:poller ref=\"outer\" default=\"true\"/>" + // <<<<< default true not allowed here
" </int-jdbc:inbound-channel-adapter>" +
"" +
" <bean id=\"ops\" class=\"org.mockito.Mockito\" factory-method=\"mock\">" +
" <constructor-arg value=\"org.springframework.jdbc.core.JdbcOperations\"/>" +
" </bean>" +
"</beans>";
Resource resource = new ByteArrayResource(badContext.getBytes());
new GenericXmlApplicationContext(resource);
fail("Expected Failure to load ApplicationContext");
} catch (BeanDefinitionParsingException bdpe) {
assertTrue(bdpe.getMessage().startsWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other attributes."));
}
}
@Test
public void testRefExtraAttributeAndDefaultFalse() {
try {
// Load context from a String to avoid IDEs reporting the invalid configuration
String badContext =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<beans xmlns=\"http://www.springframework.org/schema/beans\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:int=\"http://www.springframework.org/schema/integration\"" +
" xmlns:int-jdbc=\"http://www.springframework.org/schema/integration/jdbc\"" +
" xsi:schemaLocation=\"http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd" +
" http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-2.0.xsd" +
" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\">" +
"" +
" <int:poller id=\"outer\" fixed-rate=\"5000\"/>" +
"" +
" <int:channel id=\"someChannel\"/>" +
"" +
" <int-jdbc:inbound-channel-adapter channel=\"someChannel\" jdbc-operations=\"ops\"" +
" query=\"select 1\">" +
" <int:poller ref=\"outer\" default=\"false\" fixed-rate=\"1000\"/>" + // <<<<< fixed-rate not allowed here
" </int-jdbc:inbound-channel-adapter>" +
"" +
" <bean id=\"ops\" class=\"org.mockito.Mockito\" factory-method=\"mock\">" +
" <constructor-arg value=\"org.springframework.jdbc.core.JdbcOperations\"/>" +
" </bean>" +
"</beans>";
Resource resource = new ByteArrayResource(badContext.getBytes());
new GenericXmlApplicationContext(resource);
fail("Expected Failure to load ApplicationContext");
} catch (BeanDefinitionParsingException bdpe) {
assertTrue(bdpe.getMessage().startsWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other attributes."));
}
}
}