Announcement Announcement Module
Collapse
No announcement yet.
FlatFileItemWriter & File Locking Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • FlatFileItemWriter & File Locking

    I'm stuck on something and can't get passed it. I'm trying to use a JUnit test class to send through different input files with various test conditions. The job has two steps. Step 1 reads an input file, and writes valid records to a temp file that's used as input for the Step 2. I can run these JUnit tests in isolation just fine with expected results, but when I run them as two Tests in the same JUnit Test Case, the second Test always fails because it cannot delete the temp file that was created in the first JUnit Test. I've noticed other things like my ExecutionContext is dirty between the two runs.

    My JUnit test looks like.

    public void testValidTransactionJob() throws Exception
    {
    builder.addString( "test", "testValidTransactionJob" );
    builder.addString( "inputFile", "file:JUnit/resources/input/transaction-valid-job.csv" );
    builder.addString( "errorFile", "file:JUnit/resources/output/errorFile.csv" );
    builder.addString( "outputFile", "file:JUnit/resources/output/output.csv" );
    JobExecution jobExecution = runJob();
    assertEquals( BatchStatus.COMPLETED, jobExecution.getStatus() );
    }
    @Test
    public void testValidTransactionJob2() throws Exception
    {
    builder.addString( "test", "testValidTransactionJob2" );
    builder.addString( "inputFile", "file:JUnit/resources/input/transaction-valid-job.csv" );
    builder.addString( "errorFile", "file:JUnit/resources/output/errorFile.csv" );
    builder.addString( "outputFile", "file:JUnit/resources/output/output.csv" );
    JobExecution jobExecution = runJob();
    assertEquals( BatchStatus.COMPLETED, jobExecution.getStatus() );
    }

    The runJob method
    public JobExecution runJob() throws Exception
    {
    return launcher.run( job, builder.toJobParameters() );
    }


    Am I trying to do something that's just not reasonable? Should each input file have its own JUnit tests? I was hoping to have just on JUnit test case with a whole series of different input files for testing one Job.

    Here are some things I've tried.

    * I've found things like setShouldDeleteIfExists, I messed with my scope of the writer in the configuration.
    * I think part of the issues is that I don't have a new ExecutionContext each time. If I could figure this out, then I might not have my issues with the file contention.
    * We have a custom composite writer that we open and write in the write method, but we never close it, is this an issue? I've added the setShouldDeleteIfExists(true) in the setTransactionWriter method but still doesn't work.

  • #2
    Originally posted by dghighfill View Post
    We have a custom composite writer that we open and write in the write method, but we never close it, is this an issue?
    I suspect this is the problem. File locks are often OS-specific as well, but in general it is good practice to close any file you open.

    Comment

    Working...
    X