BATCH-1834: extend step xsd to allow custom namespaces as tasklets

This commit is contained in:
Costin Leau
2012-07-04 18:03:08 +03:00
committed by Dave Syer
parent 1fecf92614
commit f767dc8824
8 changed files with 181 additions and 31 deletions

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.batch.core.configuration.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.lang.reflect.Field;
import org.junit.Before;
import org.junit.Test;
@@ -29,11 +28,16 @@ import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.flow.FlowJob;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.test.namespace.config.DummyNamespaceHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ReflectionUtils;
import static org.junit.Assert.*;
/**
@@ -52,6 +56,15 @@ public class TaskletParserBeanPropertiesTests {
@Qualifier("job2")
private FlowJob job2;
@Autowired
@Qualifier("job3")
private Job job3;
@Autowired
@Qualifier("job4")
private Job job4;
@Autowired
@Qualifier("tasklet")
private TestTasklet tasklet;
@@ -86,4 +99,32 @@ public class TaskletParserBeanPropertiesTests {
assertEquals("foo", tasklet.getName());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
}
@Test
public void testTasklet3() throws Exception {
assertNotNull(job3);
JobExecution jobExecution = jobRepository.createJobExecution(job3.getName(), new JobParameters());
job3.execute(jobExecution);
assertEquals(FlowJob.class, job3.getClass());
Step step = ((FlowJob) job3).getStep("step3");
Field field = ReflectionUtils.findField(TaskletStep.class, "tasklet");
ReflectionUtils.makeAccessible(field);
TestTasklet tasklet = (TestTasklet) ReflectionUtils.getField(field, step);
assertEquals("foobar", tasklet.getName());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
@Test
public void testCustomNestedTasklet() throws Exception {
assertNotNull(job4);
JobExecution jobExecution = jobRepository.createJobExecution(job4.getName(), new JobParameters());
job4.execute(jobExecution);
assertEquals(FlowJob.class, job4.getClass());
Step step = ((FlowJob) job4).getStep("step4");
Field field = ReflectionUtils.findField(TaskletStep.class, "tasklet");
ReflectionUtils.makeAccessible(field);
TestTasklet tasklet = (TestTasklet) ReflectionUtils.getField(field, step);
assertEquals(DummyNamespaceHandler.LABEL, tasklet.getName());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2006-2012 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.batch.test.namespace.config;
import java.util.Random;
import org.springframework.batch.core.configuration.xml.TestTasklet;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.NamespaceHandler;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
* Empty implementation used for testing only.
*
* @author Costin Leau
*/
public class DummyNamespaceHandler implements NamespaceHandler {
public static String LABEL = new Random().toString();
public BeanDefinitionHolder decorate(Node source, BeanDefinitionHolder definition, ParserContext parserContext) {
return null;
}
public void init() {
}
public BeanDefinition parse(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(TestTasklet.class);
builder.addPropertyValue("name", LABEL);
return builder.getBeanDefinition();
}
}

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- test schema used for checking namespace discovery -->
<xsd:schema xmlns="http://www.springframework.org/schema/batch/test"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/schema/batch/test"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<!-- simple element -->
<xsd:element name="test" type="xsd:string"/>
</xsd:schema>