BATCH-2248: Fixes for Ldif reader tests

This commit is contained in:
Michael Minella
2014-06-30 16:49:53 -05:00
committed by Michael Minella
parent 8b26504db1
commit 090e6f2039
7 changed files with 126 additions and 120 deletions

View File

@@ -26,6 +26,8 @@ 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;
import org.springframework.test.context.ContextConfiguration;
@@ -34,8 +36,7 @@ import org.springframework.util.Assert;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.net.MalformedURLException;
@RunWith(SpringJUnit4ClassRunner.class)
@@ -50,15 +51,16 @@ public class LdifReaderTests {
private JobLauncher jobLauncher;
@Autowired
private Job job;
@Qualifier("job1")
private Job job1;
public LdifReaderTests() {
try {
expected = new UrlResource("file:src/test/resources/expectedOutput.ldif");
actual = new UrlResource("file:target/test-outputs/output.ldif");
} catch (MalformedURLException e) {
log.error("Unexpected error", e);
}
@Autowired
@Qualifier("job2")
private Job job2;
public LdifReaderTests() throws MalformedURLException {
expected = new ClassPathResource("/expectedOutput.ldif");
actual = new UrlResource("file:target/test-outputs/output.ldif");
}
@Before
@@ -68,48 +70,41 @@ public class LdifReaderTests {
@Test
public void testValidRun() throws Exception {
JobExecution jobExecution = jobLauncher.run(job, 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.");
Assert.isTrue(compareFiles(expected.getFile(), actual.getFile()));
compareFiles(expected.getFile(), actual.getFile());
}
@Test
public void testResourceNotExists() throws Exception {
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
JobExecution jobExecution = jobLauncher.run(job2, new JobParameters());
Assert.isTrue(jobExecution.getExitStatus().getExitCode().equals("FAILED"), "The job exit status is not FAILED.");
Assert.isTrue(jobExecution.getExitStatus().getExitDescription().contains("Failed to initialize the reader"), "The job failed for the wrong reason.");
Assert.isTrue(jobExecution.getAllFailureExceptions().get(0).getMessage().contains("Failed to initialize the reader"), "The job failed for the wrong reason.");
}
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;
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();
junit.framework.Assert.assertEquals("Line number " + lineNum + " does not match.", expectedLine, actualLine);
}
String actualLine = actualReader.readLine();
junit.framework.Assert.assertEquals("More lines than expected. There should not be a line number " + lineNum + ".", null,
actualLine);
}
if(actualReader.readLine() != null) {
equal = false;
finally {
expectedReader.close();
actualReader.close();
}
expectedReader.close();
return equal;
}
}

View File

@@ -26,6 +26,8 @@ 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;
import org.springframework.test.context.ContextConfiguration;
@@ -50,15 +52,16 @@ public class MappingLdifReaderTests {
private JobLauncher launcher;
@Autowired
private Job job;
@Qualifier("job1")
private Job job1;
public MappingLdifReaderTests() {
try {
expected = new UrlResource("file:src/test/resources/expectedOutput.ldif");
actual = new UrlResource("file:target/test-outputs/output.ldif");
} catch (MalformedURLException e) {
log.error("Unexpected error", e);
}
@Autowired
@Qualifier("job2")
private Job job2;
public MappingLdifReaderTests() throws MalformedURLException {
expected = new ClassPathResource("/expectedOutput.ldif");
actual = new UrlResource("file:target/test-outputs/output.ldif");
}
@Before
@@ -68,7 +71,7 @@ public class MappingLdifReaderTests {
@Test
public void testValidRun() throws Exception {
JobExecution jobExecution = launcher.run(job, 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());
@@ -80,10 +83,10 @@ public class MappingLdifReaderTests {
@Test
public void testResourceNotExists() throws Exception {
JobExecution jobExecution = launcher.run(job, new JobParameters());
JobExecution jobExecution = launcher.run(job2, new JobParameters());
Assert.isTrue(jobExecution.getExitStatus().getExitCode().equals("FAILED"), "The job exit status is not FAILED.");
Assert.isTrue(jobExecution.getExitStatus().getExitDescription().contains("Failed to initialize the reader"), "The job failed for the wrong reason.");
Assert.isTrue(jobExecution.getAllFailureExceptions().get(0).getMessage().contains("Failed to initialize the reader"), "The job failed for the wrong reason.");
}

View File

@@ -1,31 +1,35 @@
<?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="job">
<batch:step id="step1" next="step2">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="itemReader1" writer="itemWriter" commit-interval="2" skip-limit="1">
<batch:skippable-exception-classes>org.springframework.ldap.ldif.InvalidAttributeFormatException</batch:skippable-exception-classes>
</batch:chunk>
</batch:tasklet>
</batch:step>
<batch:step id="step2">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="itemReader2" writer="itemWriter" commit-interval="2" />
</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>
<bean id="itemReader1" class="org.springframework.ldap.ldif.batch.LdifReader">
<property name="resource" value="file:src/test/resources/test.ldif" />
<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">
<property name="resource" value="classpath:/test.ldif" />
<property name="recordsToSkip" value="1" />
</bean>
<bean id="itemReader2" class="org.springframework.ldap.ldif.batch.LdifReader">
<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>

View File

@@ -1,26 +1,28 @@
<?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="job">
<batch:step id="step1" next="step2">
<batch:tasklet transaction-manager="transactionManager">
<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:step id="step2">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="itemReader2" writer="itemWriter" commit-interval="2" />
</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>
<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="file:src/test/resources/test.ldif" />

View File

@@ -72,3 +72,4 @@ objectclass: organizationalPerson
sn: Jensen
cn: Horatio Jensen
cn: Horatio N Jensen