From 87f040e2349e0911053ac5ec0838f85e45f241ca Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Thu, 28 Nov 2019 15:08:34 +0100 Subject: [PATCH] Fix default value of comment prefix in FlatFileItemReaderBuilder Before this commit, the default value of comment prefix in FlatFileItemReaderBuilder was not consistent with the one in FlatFileItemReader. This commit changes the default value of comment prefix to # in the builder to be consistent with the reader. Resolves BATCH-2862 --- .../batch/item/file/FlatFileItemReader.java | 7 +++++-- .../file/builder/FlatFileItemReaderBuilder.java | 15 +++++++++------ .../builder/FlatFileItemReaderBuilderTests.java | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java index b5ab389ce..8bfd0df2a 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java @@ -40,6 +40,7 @@ import org.springframework.util.StringUtils; * about the problematic line and its line number. * * @author Robert Kasanicky + * @author Mahmoud Ben Hassine */ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemReader implements ResourceAwareItemReaderItemStream, InitializingBean { @@ -49,6 +50,8 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea // default encoding for input files public static final String DEFAULT_CHARSET = Charset.defaultCharset().name(); + public static final String[] DEFAULT_COMMENT_PREFIXES = new String[] { "#" }; + private RecordSeparatorPolicy recordSeparatorPolicy = new SimpleRecordSeparatorPolicy(); private Resource resource; @@ -57,7 +60,7 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea private int lineCount = 0; - private String[] comments = new String[] { "#" }; + private String[] comments = DEFAULT_COMMENT_PREFIXES; private boolean noInput = false; @@ -133,7 +136,7 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea /** * Setter for comment prefixes. Can be used to ignore header lines as well by using e.g. the first couple of column - * names as a prefix. + * names as a prefix. Defaults to {@link #DEFAULT_COMMENT_PREFIXES}. * * @param comments an array of comment line prefixes. */ diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java index f75adc8d0..b451135fa 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2018 the original author or authors. + * Copyright 2016-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,7 +70,8 @@ public class FlatFileItemReaderBuilder { private Resource resource; - private List comments = new ArrayList<>(); + private List comments = + new ArrayList<>(Arrays.asList(FlatFileItemReader.DEFAULT_COMMENT_PREFIXES)); private int linesToSkip = 0; @@ -165,6 +166,7 @@ public class FlatFileItemReaderBuilder { /** * Add a string to the list of Strings that indicate commented lines. + * Defaults to {@link FlatFileItemReader#DEFAULT_COMMENT_PREFIXES}. * * @param comment the string to define a commented line. * @return The current instance of the builder. @@ -176,15 +178,16 @@ public class FlatFileItemReaderBuilder { } /** - * An array of Strings that indicate lines that are comments (and therefore skipped by - * the reader. + * Set an array of Strings that indicate lines that are comments (and therefore skipped by + * the reader). This method overrides the default comment prefixes which are + * {@link FlatFileItemReader#DEFAULT_COMMENT_PREFIXES}. * * @param comments an array of strings to identify comments. * @return The current instance of the builder. * @see FlatFileItemReader#setComments(String[]) */ - public FlatFileItemReaderBuilder comments(String[] comments) { - this.comments.addAll(Arrays.asList(comments)); + public FlatFileItemReaderBuilder comments(String... comments) { + this.comments = Arrays.asList(comments); return this; } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilderTests.java index 1af45f068..6d4bb4b64 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilderTests.java @@ -295,7 +295,7 @@ public class FlatFileItemReaderBuilderTests { public void testDefaultComments() throws Exception { FlatFileItemReader reader = new FlatFileItemReaderBuilder() .name("fooReader") - .resource(getResource("1,2,3\n4,5,6")) + .resource(getResource("1,2,3\n4,5,6\n#this is a default comment")) .delimited() .names(new String[] {"first", "second", "third"}) .targetType(Foo.class)