From cc15f4a464c6bc7e61055ee1ff91aa6adf3b136b Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Wed, 17 Sep 2014 13:30:46 -0500 Subject: [PATCH] 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. --- .../batch/core/test/ldif/LdifReaderTests.java | 61 ++++++++--------- .../test/ldif/MappingLdifReaderTests.java | 68 +++++++++---------- .../resources/applicationContext-test1.xml | 27 +++++--- .../resources/applicationContext-test2.xml | 17 +++-- .../src/test/resources/expectedOutput.ldif | 1 + .../batch/item/ldif/LdifReader.java | 2 + 6 files changed, 92 insertions(+), 84 deletions(-) diff --git a/spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/ldif/LdifReaderTests.java b/spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/ldif/LdifReaderTests.java index 807d3cc70..0b718b375 100644 --- a/spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/ldif/LdifReaderTests.java +++ b/spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/ldif/LdifReaderTests.java @@ -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; } } \ No newline at end of file diff --git a/spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/ldif/MappingLdifReaderTests.java b/spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/ldif/MappingLdifReaderTests.java index 06aa0186a..3d6ebff48 100644 --- a/spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/ldif/MappingLdifReaderTests.java +++ b/spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/ldif/MappingLdifReaderTests.java @@ -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; } -} \ No newline at end of file +} diff --git a/spring-batch-core-tests/src/test/resources/applicationContext-test1.xml b/spring-batch-core-tests/src/test/resources/applicationContext-test1.xml index 55d93790c..9e39eb897 100644 --- a/spring-batch-core-tests/src/test/resources/applicationContext-test1.xml +++ b/spring-batch-core-tests/src/test/resources/applicationContext-test1.xml @@ -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"> - - - + + + - org.springframework.ldap.ldif.InvalidAttributeFormatException + + + - - + + + + + - - + + - - + + - + diff --git a/spring-batch-core-tests/src/test/resources/applicationContext-test2.xml b/spring-batch-core-tests/src/test/resources/applicationContext-test2.xml index 4faebde1d..07aa507a6 100644 --- a/spring-batch-core-tests/src/test/resources/applicationContext-test2.xml +++ b/spring-batch-core-tests/src/test/resources/applicationContext-test2.xml @@ -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"> - - - + + + @@ -15,21 +15,24 @@ - - + + + + + - + - + diff --git a/spring-batch-core-tests/src/test/resources/expectedOutput.ldif b/spring-batch-core-tests/src/test/resources/expectedOutput.ldif index 4d345c052..82ac4e039 100644 --- a/spring-batch-core-tests/src/test/resources/expectedOutput.ldif +++ b/spring-batch-core-tests/src/test/resources/expectedOutput.ldif @@ -72,3 +72,4 @@ objectclass: organizationalPerson sn: Jensen cn: Horatio Jensen cn: Horatio N Jensen + diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ldif/LdifReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ldif/LdifReader.java index c54a58399..de39ecf41 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ldif/LdifReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ldif/LdifReader.java @@ -115,6 +115,8 @@ public class LdifReader extends AbstractItemCountingItemStreamItemReader