Fixes LdifReader related tests

The LdifReader tests were not passing when they were brough over from
Spring LDAP due to the fact that they depended on old conventions for
Spring Batch.  This wasn't found until someone logged a bug because not
all tests are executed with every build.  These tests should now pass.
This commit is contained in:
Michael Minella
2014-09-17 13:30:46 -05:00
parent 119573f4ae
commit cc15f4a464
6 changed files with 92 additions and 84 deletions

View File

@@ -20,12 +20,14 @@ 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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.test.context.ContextConfiguration;
@@ -34,10 +36,11 @@ 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;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/applicationContext-test1.xml"})
public class LdifReaderTests {
@@ -50,11 +53,14 @@ public class LdifReaderTests {
private JobLauncher jobLauncher;
@Autowired
private Job job;
private Job validJob;
@Autowired
private Job invalidJob;
public LdifReaderTests() {
try {
expected = new UrlResource("file:src/test/resources/expectedOutput.ldif");
expected = new ClassPathResource("/expectedOutput.ldif");
actual = new UrlResource("file:target/test-outputs/output.ldif");
} catch (MalformedURLException e) {
log.error("Unexpected error", e);
@@ -68,48 +74,41 @@ public class LdifReaderTests {
@Test
public void testValidRun() throws Exception {
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
JobExecution jobExecution = jobLauncher.run(validJob, 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()));
assertFileEquals(expected.getFile(), actual.getFile());
}
@Test
public void testResourceNotExists() throws Exception {
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
JobExecution jobExecution = jobLauncher.run(invalidJob, 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.");
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.");
}
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;
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);
}
String actualLine = actualReader.readLine();
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

@@ -20,12 +20,14 @@ 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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.test.context.ContextConfiguration;
@@ -34,10 +36,12 @@ 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;
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 {
@@ -50,11 +54,14 @@ public class MappingLdifReaderTests {
private JobLauncher launcher;
@Autowired
private Job job;
private Job validJob;
@Autowired
private Job invalidJob;
public MappingLdifReaderTests() {
try {
expected = new UrlResource("file:src/test/resources/expectedOutput.ldif");
expected = new ClassPathResource("/expectedOutput.ldif");
actual = new UrlResource("file:target/test-outputs/output.ldif");
} catch (MalformedURLException e) {
log.error("Unexpected error", e);
@@ -68,49 +75,40 @@ public class MappingLdifReaderTests {
@Test
public void testValidRun() throws Exception {
JobExecution jobExecution = launcher.run(job, new JobParameters());
JobExecution jobExecution = launcher.run(validJob, 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()));
assertTrue("Actual does not exist.", actual.exists());
assertFileEquals(expected.getFile(), actual.getFile());
}
@Test
public void testResourceNotExists() throws Exception {
JobExecution jobExecution = launcher.run(job, new JobParameters());
JobExecution jobExecution = launcher.run(invalidJob, 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.");
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"));
}
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;
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);
}
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

@@ -5,32 +5,37 @@
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: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>org.springframework.ldap.ldif.InvalidAttributeFormatException</batch:skippable-exception-classes>
<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: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>
<bean id="itemReader1" class="org.springframework.ldap.ldif.batch.LdifReader">
<property name="resource" value="file:src/test/resources/test.ldif" />
<bean id="itemReader1" class="org.springframework.batch.item.ldif.LdifReader" scope="step">
<property name="resource" value="test.ldif" />
<property name="recordsToSkip" value="1" />
</bean>
<bean id="itemReader2" class="org.springframework.ldap.ldif.batch.LdifReader">
<property name="resource" value="file:src/test/resources/missing.ldif" />
<bean id="itemReader2" class="org.springframework.batch.item.ldif.LdifReader" scope="step">
<property name="resource" value="missing.ldif" />
<property name="recordsToSkip" value="1" />
</bean>
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<property name="resource" value="file:target/test-outputs/output.ldif" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />

View File

@@ -5,9 +5,9 @@
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: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"/>
@@ -15,21 +15,24 @@
</batch:chunk>
</batch:tasklet>
</batch:step>
<batch:step id="step2">
<batch:tasklet transaction-manager="transactionManager">
</batch: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>
<bean id="itemReader1" class="org.springframework.batch.item.ldif.MappingLdifReader">
<property name="resource" value="file:src/test/resources/test.ldif" />
<property name="resource" value="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="file:src/test/resources/missing.ldif" />
<property name="resource" value="missing.ldif" />
<property name="recordsToSkip" value="1" />
<property name="recordMapper" ref="recordMapper" />
</bean>

View File

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

View File

@@ -115,6 +115,8 @@ public class LdifReader extends AbstractItemCountingItemStreamItemReader<LdapAtt
if (resource == null)
throw new IllegalStateException("A resource has not been set.");
System.out.println("Resource = " + resource.toString());
if (!resource.exists()) {
if (strict) {
throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode): "+resource);