BATCH-2248: Fixes for Ldif reader tests

Conflicts:
	spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/ldif/LdifReaderTests.java
	spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/ldif/MappingLdifReaderTests.java
	spring-batch-core-tests/src/test/resources/applicationContext-test1.xml
	spring-batch-core-tests/src/test/resources/applicationContext-test2.xml
This commit is contained in:
Michael Minella
2014-06-30 16:49:53 -05:00
committed by Michael Minella
parent 8e49ec6f1d
commit d175f2d05a
6 changed files with 133 additions and 131 deletions

View File

@@ -20,13 +20,13 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
@@ -39,8 +39,6 @@ import java.io.File;
import java.io.FileReader;
import java.net.MalformedURLException;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/applicationContext-test1.xml"})
public class LdifReaderTests {
@@ -53,18 +51,16 @@ public class LdifReaderTests {
private JobLauncher jobLauncher;
@Autowired
private Job validJob;
@Qualifier("job1")
private Job job1;
@Autowired
private Job invalidJob;
@Qualifier("job2")
private Job job2;
public LdifReaderTests() {
try {
expected = new ClassPathResource("/expectedOutput.ldif");
actual = new UrlResource("file:target/test-outputs/output.ldif");
} catch (MalformedURLException e) {
log.error("Unexpected error", e);
}
public LdifReaderTests() throws MalformedURLException {
expected = new ClassPathResource("/expectedOutput.ldif");
actual = new UrlResource("file:target/test-outputs/output.ldif");
}
@Before
@@ -74,37 +70,37 @@ public class LdifReaderTests {
@Test
public void testValidRun() throws Exception {
JobExecution jobExecution = jobLauncher.run(validJob, new JobParameters());
JobExecution jobExecution = jobLauncher.run(job1, new JobParameters());
//Ensure job completed successfully.
Assert.isTrue(jobExecution.getExitStatus().equals(ExitStatus.COMPLETED), "Step Execution did not complete normally: " + jobExecution.getExitStatus());
//Check output.
Assert.isTrue(actual.exists(), "Actual does not exist.");
assertFileEquals(expected.getFile(), actual.getFile());
compareFiles(expected.getFile(), actual.getFile());
}
@Test
public void testResourceNotExists() throws Exception {
JobExecution jobExecution = jobLauncher.run(invalidJob, new JobParameters());
JobExecution jobExecution = jobLauncher.run(job2, new JobParameters());
assertEquals("The job status is not FAILED.", jobExecution.getStatus(), BatchStatus.FAILED);
Assert.isTrue(jobExecution.getStepExecutions().iterator().next().getExitStatus().getExitDescription().contains("Failed to initialize the reader"), "The job failed for the wrong reason.");
Assert.isTrue(jobExecution.getExitStatus().getExitCode().equals("FAILED"), "The job exit status is not FAILED.");
Assert.isTrue(jobExecution.getAllFailureExceptions().get(0).getMessage().contains("Failed to initialize the reader"), "The job failed for the wrong reason.");
}
public static void assertFileEquals(File expected, File actual) throws Exception {
private void compareFiles(File expected, File actual) throws Exception {
BufferedReader expectedReader = new BufferedReader(new FileReader(expected));
BufferedReader actualReader = new BufferedReader(new FileReader(actual));
try {
int lineNum = 1;
for (String expectedLine = null; (expectedLine = expectedReader.readLine()) != null; lineNum++) {
String actualLine = actualReader.readLine();
assertEquals("Line number " + lineNum + " does not match.", expectedLine, actualLine);
junit.framework.Assert.assertEquals("Line number " + lineNum + " does not match.", expectedLine, actualLine);
}
String actualLine = actualReader.readLine();
assertEquals("More lines than expected. There should not be a line number " + lineNum + ".", null,
actualLine);
junit.framework.Assert.assertEquals("More lines than expected. There should not be a line number " + lineNum + ".", null,
actualLine);
}
finally {
expectedReader.close();

View File

@@ -20,13 +20,13 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
@@ -36,12 +36,10 @@ import org.springframework.util.Assert;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/applicationContext-test2.xml"})
public class MappingLdifReaderTests {
@@ -54,18 +52,16 @@ public class MappingLdifReaderTests {
private JobLauncher launcher;
@Autowired
private Job validJob;
@Qualifier("job1")
private Job job1;
@Autowired
private Job invalidJob;
@Qualifier("job2")
private Job job2;
public MappingLdifReaderTests() {
try {
expected = new ClassPathResource("/expectedOutput.ldif");
actual = new UrlResource("file:target/test-outputs/output.ldif");
} catch (MalformedURLException e) {
log.error("Unexpected error", e);
}
public MappingLdifReaderTests() throws MalformedURLException {
expected = new ClassPathResource("/expectedOutput.ldif");
actual = new UrlResource("file:target/test-outputs/output.ldif");
}
@Before
@@ -75,40 +71,49 @@ public class MappingLdifReaderTests {
@Test
public void testValidRun() throws Exception {
JobExecution jobExecution = launcher.run(validJob, new JobParameters());
JobExecution jobExecution = launcher.run(job1, new JobParameters());
//Ensure job completed successfully.
Assert.isTrue(jobExecution.getExitStatus().equals(ExitStatus.COMPLETED), "Step Execution did not complete normally: " + jobExecution.getExitStatus());
//Check output.
assertTrue("Actual does not exist.", actual.exists());
assertFileEquals(expected.getFile(), actual.getFile());
Assert.isTrue(actual.exists(), "Actual does not exist.");
Assert.isTrue(compareFiles(expected.getFile(), actual.getFile()));
}
@Test
public void testResourceNotExists() throws Exception {
JobExecution jobExecution = launcher.run(invalidJob, new JobParameters());
JobExecution jobExecution = launcher.run(job2, new JobParameters());
assertEquals("The job exit status is not FAILED.", jobExecution.getStatus(), BatchStatus.FAILED);
assertTrue("The job failed for the wrong reason.", jobExecution.getStepExecutions().iterator().next().getExitStatus().getExitDescription().contains("Failed to initialize the reader"));
Assert.isTrue(jobExecution.getExitStatus().getExitCode().equals("FAILED"), "The job exit status is not FAILED.");
Assert.isTrue(jobExecution.getAllFailureExceptions().get(0).getMessage().contains("Failed to initialize the reader"), "The job failed for the wrong reason.");
}
public static void assertFileEquals(File expected, File actual) throws Exception {
BufferedReader expectedReader = new BufferedReader(new FileReader(expected));
BufferedReader actualReader = new BufferedReader(new FileReader(actual));
try {
int lineNum = 1;
for (String expectedLine = null; (expectedLine = expectedReader.readLine()) != null; lineNum++) {
String actualLine = actualReader.readLine();
assertEquals("Line number " + lineNum + " does not match.", expectedLine, actualLine);
private boolean compareFiles(File expected, File actual) throws Exception {
boolean equal = true;
FileInputStream expectedStream = new FileInputStream(expected);
FileInputStream actualStream = new FileInputStream(actual);
//Construct BufferedReader from InputStreamReader
BufferedReader expectedReader = new BufferedReader(new InputStreamReader(expectedStream));
BufferedReader actualReader = new BufferedReader(new InputStreamReader(actualStream));
String line = null;
while ((line = expectedReader.readLine()) != null) {
if(!line.equals(actualReader.readLine())) {
equal = false;
break;
}
String actualLine = actualReader.readLine();
assertEquals("More lines than expected. There should not be a line number " + lineNum + ".", null,
actualLine);
} finally {
expectedReader.close();
actualReader.close();
}
if(actualReader.readLine() != null) {
equal = false;
}
expectedReader.close();
return equal;
}
}
}

View File

@@ -1,41 +1,40 @@
<?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:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
<batch:job id="validJob">
<batch:step id="validJob.step1">
<batch:tasklet>
<batch:chunk reader="itemReader1" writer="itemWriter" commit-interval="2" skip-limit="1">
<batch:skippable-exception-classes>
<batch:include class="org.springframework.ldap.ldif.InvalidAttributeFormatException"/>
</batch:skippable-exception-classes>
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<job id="job1" xmlns="http://www.springframework.org/schema/batch">
<step id="step1">
<tasklet>
<chunk reader="itemReader1" writer="itemWriter" commit-interval="2" skip-limit="1">
<skippable-exception-classes>
<include class="org.springframework.ldap.ldif.InvalidAttributeFormatException"/>
</skippable-exception-classes>
</chunk>
</tasklet>
</step>
</job>
<batch:job id="invalidJob">
<batch:step id="invalidJob.step1">
<batch:tasklet>
<batch:chunk reader="itemReader2" writer="itemWriter" commit-interval="2" />
</batch:tasklet>
</batch:step>
</batch:job>
<job id="job2" xmlns="http://www.springframework.org/schema/batch">
<step id="step2">
<tasklet>
<chunk reader="itemReader2" writer="itemWriter" commit-interval="2" />
</tasklet>
</step>
</job>
<bean id="itemReader1" class="org.springframework.batch.item.ldif.LdifReader" scope="step">
<property name="resource" value="test.ldif" />
<bean id="itemReader1" class="org.springframework.batch.item.ldif.LdifReader">
<property name="resource" value="classpath:/test.ldif" />
<property name="recordsToSkip" value="1" />
</bean>
<bean id="itemReader2" class="org.springframework.batch.item.ldif.LdifReader" scope="step">
<property name="resource" value="missing.ldif" />
<bean id="itemReader2" class="org.springframework.batch.item.ldif.LdifReader">
<property name="resource" value="file:src/test/resources/missing.ldif" />
<property name="recordsToSkip" value="1" />
</bean>
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:target/test-outputs/output.ldif" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />

View File

@@ -1,38 +1,37 @@
<?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:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
<batch:job id="validJob">
<batch:step id="validJob.step1">
<batch:tasklet>
<batch:chunk reader="itemReader1" writer="itemWriter" commit-interval="2" skip-limit="1">
<batch:skippable-exception-classes>
<batch:include class="org.springframework.ldap.ldif.InvalidAttributeFormatException"/>
</batch:skippable-exception-classes>
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<job id="job1" xmlns="http://www.springframework.org/schema/batch">
<step id="step1">
<tasklet transaction-manager="transactionManager">
<chunk reader="itemReader1" writer="itemWriter" commit-interval="2" skip-limit="1">
<skippable-exception-classes>
<include class="org.springframework.ldap.ldif.InvalidAttributeFormatException"/>
</skippable-exception-classes>
</chunk>
</tasklet>
</step>
</job>
<batch:job id="invalidJob">
<batch:step id="invalidJob.step1">
<batch:tasklet>
<batch:chunk reader="itemReader2" writer="itemWriter" commit-interval="2" />
</batch:tasklet>
</batch:step>
</batch:job>
<job id="job2" xmlns="http://www.springframework.org/schema/batch">
<step id="step2">
<tasklet transaction-manager="transactionManager">
<chunk reader="itemReader2" writer="itemWriter" commit-interval="2" />
</tasklet>
</step>
</job>
<bean id="itemReader1" class="org.springframework.batch.item.ldif.MappingLdifReader">
<property name="resource" value="test.ldif" />
<property name="resource" value="file:src/test/resources/test.ldif" />
<property name="recordsToSkip" value="1" />
<property name="recordMapper" ref="recordMapper" />
</bean>
<bean id="itemReader2" class="org.springframework.batch.item.ldif.MappingLdifReader">
<property name="resource" value="missing.ldif" />
<property name="resource" value="file:src/test/resources/missing.ldif" />
<property name="recordsToSkip" value="1" />
<property name="recordMapper" ref="recordMapper" />
</bean>

View File

@@ -23,12 +23,11 @@ import java.util.ArrayList;
import java.util.List;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.repeat.RepeatCallback;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatStatus;
@@ -38,9 +37,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jms.connection.SessionProxy;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.SessionCallback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.BeforeTransaction;
@@ -66,6 +63,8 @@ public class SynchronousTests implements ApplicationContextAware {
private ApplicationContext applicationContext;
private List<String> list = new ArrayList<String>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
@@ -94,8 +93,6 @@ public class SynchronousTests implements ApplicationContextAware {
assertEquals(0, count);
}
List<String> list = new ArrayList<String>();
@Transactional
@Test
public void testCommit() throws Exception {
@@ -106,12 +103,15 @@ public class SynchronousTests implements ApplicationContextAware {
@Override
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
String text = (String) jmsTemplate.receiveAndConvert("queue");
System.out.println("text = " + text);
list.add(text);
jdbcTemplate.update("INSERT into T_BARS (id,name,foo_date) values (?,?,null)", list.size(), text);
return RepeatStatus.continueIf(text != null);
}
});
System.err.println(jdbcTemplate.queryForList("select * from T_BARS"));
int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class);
assertEquals(2, count);
@@ -159,7 +159,8 @@ public class SynchronousTests implements ApplicationContextAware {
assertTrue("Foo not on queue", msgs.contains("foo"));
}
@Transactional @Test
@Transactional
@Test
public void testPartialRollback() throws Exception {
// The JmsTemplate is used elsewhere outside a transaction, so
@@ -179,6 +180,7 @@ public class SynchronousTests implements ApplicationContextAware {
@Override
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
String text = (String) txJmsTemplate.receiveAndConvert("queue");
System.out.println("Receiving in transaction: " + text);
list.add(text);
jdbcTemplate.update("INSERT into T_BARS (id,name,foo_date) values (?,?,null)", list.size(), text);
return RepeatStatus.continueIf(text != null);
@@ -187,32 +189,36 @@ public class SynchronousTests implements ApplicationContextAware {
// Simulate a message system failure before the main transaction
// commits...
txJmsTemplate.execute(new SessionCallback<Void>() {
@Override
public Void doInJms(Session session) throws JMSException {
try {
assertTrue("Not a SessionProxy - wrong spring version?", session instanceof SessionProxy);
((SessionProxy) session).getTargetSession().rollback();
}
catch (JMSException e) {
throw e;
}
catch (Exception e) {
// swallow it
e.printStackTrace();
}
return null;
}
});
// txJmsTemplate.execute(new SessionCallback<Void>() {
// @Override
// public Void doInJms(Session session) throws JMSException {
// try {
// System.out.println("Session = " + session + " pass test? " + (session instanceof SessionProxy));
// assertTrue("Not a SessionProxy - wrong spring version?", session instanceof SessionProxy);
// ((SessionProxy) session).getTargetSession().rollback();
// }
// catch (JMSException e) {
// throw e;
// }
// catch (Exception e) {
// // swallow it
// e.printStackTrace();
// }
// return null;
// }
// });
return null;
}
});
System.err.println(jdbcTemplate.queryForList("select * from T_BARS"));
String text = "";
List<String> msgs = new ArrayList<String>();
while (text != null) {
text = (String) txJmsTemplate.receiveAndConvert("queue");
System.out.println("text = " + text);
msgs.add(text);
}

View File

@@ -1,11 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:/data-source-context.xml" />