1616 */
1717package org .apache .commons .io .input ;
1818
19-
2019import static org .junit .Assert .assertEquals ;
2120
2221import java .io .BufferedReader ;
23- import java .io .File ;
24- import java .io .FileInputStream ;
2522import java .io .IOException ;
26- import java .io .InputStreamReader ;
2723import java .net .URISyntaxException ;
24+ import java .nio .charset .Charset ;
25+ import java .nio .file .*;
2826import java .util .Arrays ;
2927import java .util .Collection ;
3028import java .util .Stack ;
3129
30+ import com .google .common .jimfs .Configuration ;
31+ import com .google .common .jimfs .Jimfs ;
3232import org .junit .After ;
33+ import org .junit .Before ;
3334import org .junit .Test ;
3435import org .junit .runner .RunWith ;
3536import org .junit .runners .Parameterized ;
4041 */
4142@ RunWith (Parameterized .class )
4243public class ReversedLinesFileReaderTestParamFile {
43-
44- @ Parameters (name = "{0}, charset={1}" )
45- public static Collection <Object []> blockSizes () {
44+ @ Parameters (name = "{0}, encoding={1}, blockSize={2}, useNonDefaultFileSystem={3}" )
45+ public static Collection <Object []> parameters () {
4646 return Arrays .asList (new Object [][]{
47- {"test-file-20byteslength.bin" , "ISO_8859_1" , null },
48- {"test-file-iso8859-1-shortlines-win-linebr.bin" , "ISO_8859_1" , null },
49- {"test-file-iso8859-1.bin" , "ISO_8859_1" , null },
50- {"test-file-shiftjis.bin" , "Shift_JIS" , null },
51- {"test-file-utf16be.bin" , "UTF-16BE" , null },
52- {"test-file-utf16le.bin" , "UTF-16LE" , null },
53- {"test-file-utf8-cr-only.bin" , "UTF-8" , null },
54- {"test-file-utf8-win-linebr.bin" , "UTF-8" , null },
55- {"test-file-utf8-win-linebr.bin" , "UTF-8" , 1 },
56- {"test-file-utf8-win-linebr.bin" , "UTF-8" , 2 },
57- {"test-file-utf8-win-linebr.bin" , "UTF-8" , 3 },
58- {"test-file-utf8-win-linebr.bin" , "UTF-8" , 4 },
59- {"test-file-utf8.bin" , "UTF-8" , null },
60- {"test-file-windows-31j.bin" , "windows-31j" , null },
61- {"test-file-gbk.bin" , "gbk" , null },
62- {"test-file-x-windows-949.bin" , "x-windows-949" , null },
63- {"test-file-x-windows-950.bin" , "x-windows-950" , null },
47+ {"test-file-20byteslength.bin" , "ISO_8859_1" , null , false },
48+ {"test-file-iso8859-1-shortlines-win-linebr.bin" , "ISO_8859_1" , null , false },
49+ {"test-file-iso8859-1.bin" , "ISO_8859_1" , null , false },
50+ {"test-file-shiftjis.bin" , "Shift_JIS" , null , false },
51+ {"test-file-utf16be.bin" , "UTF-16BE" , null , false },
52+ {"test-file-utf16le.bin" , "UTF-16LE" , null , false },
53+ {"test-file-utf8-cr-only.bin" , "UTF-8" , null , false },
54+ {"test-file-utf8-win-linebr.bin" , "UTF-8" , null , false },
55+ {"test-file-utf8-win-linebr.bin" , "UTF-8" , 1 , false },
56+ {"test-file-utf8-win-linebr.bin" , "UTF-8" , 2 , false },
57+ {"test-file-utf8-win-linebr.bin" , "UTF-8" , 3 , false },
58+ {"test-file-utf8-win-linebr.bin" , "UTF-8" , 4 , false },
59+ {"test-file-utf8.bin" , "UTF-8" , null , false },
60+ {"test-file-utf8.bin" , "UTF-8" , null , true },
61+ {"test-file-windows-31j.bin" , "windows-31j" , null , false },
62+ {"test-file-gbk.bin" , "gbk" , null , false },
63+ {"test-file-x-windows-949.bin" , "x-windows-949" , null , false },
64+ {"test-file-x-windows-950.bin" , "x-windows-950" , null , false },
6465 });
6566 }
6667
68+ private Path file ;
69+ private FileSystem fileSystem ;
6770 private ReversedLinesFileReader reversedLinesFileReader ;
6871 private BufferedReader bufferedReader ;
6972
7073 private final String fileName ;
71- private final String encoding ;
72- private final int buffSize ;
74+ private final Charset encoding ;
75+ private final Integer blockSize ;
76+ private final boolean useNonDefaultFileSystem ;
7377
74- public ReversedLinesFileReaderTestParamFile (final String fileName , final String encoding , final Integer buffsize ) {
78+ public ReversedLinesFileReaderTestParamFile (final String fileName , final String encoding , final Integer blockSize , final boolean useNonDefaultFileSystem ) {
7579 this .fileName = fileName ;
76- this .encoding = encoding ;
77- this .buffSize = buffsize == null ? 4096 : buffsize ;
80+ this .encoding = Charset .forName (encoding );
81+ this .blockSize = blockSize ;
82+ this .useNonDefaultFileSystem = useNonDefaultFileSystem ;
83+ }
84+
85+ @ Before
86+ public void prepareFile () throws URISyntaxException , IOException {
87+ file = Paths .get (getClass ().getResource ("/" + fileName ).toURI ());
88+ if (useNonDefaultFileSystem ) {
89+ fileSystem = Jimfs .newFileSystem (Configuration .unix ());
90+ file = Files .copy (file , fileSystem .getPath ("/" + fileName ));
91+ }
7892 }
7993
8094 @ Test
81- public void testDataIntegrityWithBufferedReader () throws URISyntaxException , IOException {
82- final File testFileIso = new File (this .getClass ().getResource ("/" + fileName ).toURI ());
83- reversedLinesFileReader = new ReversedLinesFileReader (testFileIso , buffSize , encoding );
95+ public void testDataIntegrityWithBufferedReader () throws IOException {
96+ reversedLinesFileReader = blockSize == null
97+ ? new ReversedLinesFileReader (file , encoding )
98+ : new ReversedLinesFileReader (file , blockSize , encoding );
8499
85100 final Stack <String > lineStack = new Stack <>();
86101
87- bufferedReader = new BufferedReader ( new InputStreamReader ( new FileInputStream ( testFileIso ) , encoding ) );
88- String line = null ;
102+ bufferedReader = Files . newBufferedReader ( file , encoding );
103+ String line ;
89104
90105 // read all lines in normal order
91106 while ((line = bufferedReader .readLine ()) != null ) {
@@ -97,11 +112,10 @@ public void testDataIntegrityWithBufferedReader() throws URISyntaxException, IOE
97112 final String lineFromBufferedReader = lineStack .pop ();
98113 assertEquals (lineFromBufferedReader , line );
99114 }
100-
101115 }
102116
103117 @ After
104- public void closeReader () {
118+ public void releaseResources () {
105119 try {
106120 bufferedReader .close ();
107121 } catch (final Exception e ) {
@@ -112,7 +126,10 @@ public void closeReader() {
112126 } catch (final Exception e ) {
113127 // ignore
114128 }
129+ try {
130+ fileSystem .close ();
131+ } catch (final Exception e ) {
132+ // ignore
133+ }
115134 }
116-
117-
118135}
0 commit comments