BATCH-1456: add dummy 2.0 schema and a detector for the wrong schema declaration.

This commit is contained in:
dsyer
2009-12-04 18:36:05 +00:00
parent f3cb6304a2
commit b451c219bf
6 changed files with 78 additions and 2 deletions

View File

@@ -37,4 +37,5 @@ public class CoreNamespaceHandler extends NamespaceHandlerSupport {
this.registerBeanDefinitionParser("job-listener", new TopLevelJobListenerParser());
this.registerBeanDefinitionParser("step-listener", new TopLevelStepListenerParser());
}
}

View File

@@ -58,6 +58,14 @@ public class JobParser extends AbstractSingleBeanDefinitionParser {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
if (!namespaceMatchesVersion(element)
|| !namespaceMatchesVersion(element.getOwnerDocument().getDocumentElement())) {
parserContext.getReaderContext().error(
"You cannot use spring-batch-2.0.xsd with Spring Batch 2.1. Please upgrade your schema declarations "
+ "(or use the spring-batch.xsd alias if you are feeling lucky).", element);
return;
}
CoreNamespaceUtils.autoregisterBeansForNamespace(parserContext, parserContext.extractSource(element));
String jobName = element.getAttribute("id");
@@ -107,7 +115,7 @@ public class JobParser extends AbstractSingleBeanDefinitionParser {
}
Element description = DomUtils.getChildElementByTagName(element, "description");
if (description!=null) {
if (description != null) {
builder.getBeanDefinition().setDescription(description.getTextContent());
}
@@ -134,4 +142,21 @@ public class JobParser extends AbstractSingleBeanDefinitionParser {
}
/**
* Check that the schema location declared in the source file being parsed
* matches the Spring Batch version. (The old 2.0 schema is not 100%
* compatible with the new parser, so it is an error to explicitly define
* 2.0. It might be an error to declare spring-batch.xsd as an alias, but
* you are only going to find that out when one of the sub parses breaks.)
*
* @param element the element that is to be parsed next
* @return true if we find a schema declaration that matches
*/
private boolean namespaceMatchesVersion(Element element) {
String schemaLocation = element.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation");
return schemaLocation.matches("(?m).*spring-batch-2.1.xsd.*")
|| schemaLocation.matches("(?m).*spring-batch.xsd.*")
|| !schemaLocation.matches("(?m).*spring-batch.*");
}
}

View File

@@ -1,2 +1,3 @@
http\://www.springframework.org/schema/batch/spring-batch.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.1.xsd
http\://www.springframework.org/schema/batch/spring-batch-2.1.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.1.xsd
http\://www.springframework.org/schema/batch/spring-batch-2.1.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.1.xsd
http\://www.springframework.org/schema/batch/spring-batch-2.0.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.0-error.xsd

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/batch"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="http://www.springframework.org/schema/batch"
elementFormDefault="qualified" attributeFormDefault="unqualified"
version="2.0-error">
<xsd:annotation>
<xsd:documentation><![CDATA[
It is an error to load this file. That will happen if you use Spring Batch 2.1 and declare the namespace schema
location as http://www.springframework.org/schema/batch/spring-batch-2.0.xsd. If you see errors mentioning this
URL or this file, just change your schema declarations to 2.1, or to the alias spring-batch.xsd. This file is
included in the 2.1 jar file to make sure the error is fail fast: otherwise you might get the parser going out
to the internet looking for the old 2.0 XSD.
]]></xsd:documentation>
</xsd:annotation>
</xsd:schema>

View File

@@ -5,6 +5,7 @@ import static org.junit.Assert.fail;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -48,4 +49,20 @@ public class JobParserExceptionTests {
}
}
@Test
public void testWrongSchemaInRoot() {
try {
new ClassPathXmlApplicationContext(
"org/springframework/batch/core/configuration/xml/JobParserWrongSchemaInRootTests-context.xml");
fail("Error expected");
}
catch (BeanDefinitionParsingException e) {
String message = e.getMessage();
assertTrue("Wrong message: "+message, message.matches("(?s).*You cannot use spring-batch-2.0.xsd.*"));
} catch (BeanDefinitionStoreException e) {
// Probably the internet is not available and the schema validation failed.
// We don't want an automated build to fail if that happens.
}
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<beans:import resource="common-context.xml" />
<job id="job1">
<step id="s1">
<tasklet ref="dummyTasklet" />
</step>
</job>
</beans:beans>