Skip to content

Commit e13b1d8

Browse files
authored
Merge pull request #11 from spacious-team/develop
Релиз 2024.1
2 parents 04c482f + e9529c0 commit e13b1d8

File tree

4 files changed

+76
-11
lines changed

4 files changed

+76
-11
lines changed

pom.xml

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
<groupId>org.spacious-team</groupId>
2626
<artifactId>table-wrapper-csv-impl</artifactId>
27-
<version>2023.1</version>
27+
<version>2024.1</version>
2828
<packaging>jar</packaging>
2929

3030
<name>Table Wrapper API CSV Implementation</name>
@@ -49,8 +49,8 @@
4949
<maven.compiler.release>11</maven.compiler.release>
5050
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5151
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
52-
<lombok.version>1.18.28</lombok.version>
53-
<checkerframework.version>3.32.0</checkerframework.version>
52+
<lombok.version>1.18.32</lombok.version>
53+
<checkerframework.version>3.42.0</checkerframework.version>
5454
</properties>
5555

5656
<repositories>
@@ -123,15 +123,15 @@
123123
<plugins>
124124
<plugin>
125125
<artifactId>maven-surefire-plugin</artifactId>
126-
<version>2.22.2</version> <!-- JUnit 5 requirement -->
126+
<version>3.2.5</version> <!-- JUnit 5 requirement -->
127127
</plugin>
128128
</plugins>
129129
</pluginManagement>
130130
<plugins>
131131
<plugin>
132132
<groupId>org.apache.maven.plugins</groupId>
133133
<artifactId>maven-compiler-plugin</artifactId>
134-
<version>3.10.1</version>
134+
<version>3.13.0</version>
135135
<configuration>
136136
<fork>true</fork> <!-- Must fork or else JVM arguments are ignored. -->
137137
<showDeprecation>true</showDeprecation>
@@ -174,7 +174,7 @@
174174
<plugin>
175175
<groupId>org.jacoco</groupId>
176176
<artifactId>jacoco-maven-plugin</artifactId>
177-
<version>0.8.8</version>
177+
<version>0.8.12</version>
178178
<executions>
179179
<execution>
180180
<id>prepare-agent</id>
@@ -194,7 +194,7 @@
194194
<plugin>
195195
<groupId>org.apache.maven.plugins</groupId>
196196
<artifactId>maven-source-plugin</artifactId>
197-
<version>3.2.1</version>
197+
<version>3.3.1</version>
198198
<executions>
199199
<execution>
200200
<id>attach-sources</id>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Table Wrapper CSV Impl
3+
* Copyright (C) 2024 Spacious Team <[email protected]>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as
7+
* published by the Free Software Foundation, either version 3 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package org.spacious_team.table_wrapper.csv;
20+
21+
import java.io.FilterInputStream;
22+
23+
class CloseIgnoringInputStream extends FilterInputStream {
24+
25+
public CloseIgnoringInputStream(java.io.InputStream in) {
26+
super(in);
27+
}
28+
29+
@Override
30+
public void close() {
31+
// do nothing
32+
}
33+
}

src/main/java/org/spacious_team/table_wrapper/csv/CsvReportPage.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.univocity.parsers.csv.CsvParser;
2222
import com.univocity.parsers.csv.CsvParserSettings;
23+
import org.checkerframework.checker.nullness.qual.NonNull;
2324
import org.checkerframework.checker.nullness.qual.Nullable;
2425
import org.spacious_team.table_wrapper.api.AbstractReportPage;
2526
import org.spacious_team.table_wrapper.api.TableCellAddress;
@@ -44,23 +45,38 @@ public class CsvReportPage extends AbstractReportPage<CsvTableRow> {
4445
* Field and line delimiter detected automatically. UTF-8 encoded file expected.
4546
*/
4647
public CsvReportPage(Path path) throws IOException {
47-
this(Files.newInputStream(path, StandardOpenOption.READ));
48+
try (InputStream inputStream = Files.newInputStream(path, StandardOpenOption.READ)) {
49+
this.rows = readRows(inputStream, UTF_8, getDefaultCsvParserSettings());
50+
}
4851
}
4952

5053
/**
51-
* Closes inputStream if success. UTF-8 encoded stream data expected.
54+
* Field and line delimiter detected automatically. UTF-8 encoded file expected.
55+
*
56+
* @implSpec Does not close inputStream
5257
*/
5358
public CsvReportPage(InputStream inputStream) throws IOException {
5459
this(inputStream, UTF_8, getDefaultCsvParserSettings());
5560
}
5661

5762
/**
58-
* Closes inputStream if success
63+
* @implSpec Does not close inputStream
5964
*/
6065
public CsvReportPage(InputStream inputStream, Charset charset, CsvParserSettings csvParserSettings) throws IOException {
66+
CloseIgnoringInputStream closeIgnoringInputStream = new CloseIgnoringInputStream(inputStream);
67+
this.rows = readRows(closeIgnoringInputStream, charset, csvParserSettings);
68+
}
69+
70+
/**
71+
* @implSpec Closes inputStream
72+
*/
73+
private static String[] @NonNull [] readRows(InputStream inputStream,
74+
Charset charset,
75+
CsvParserSettings csvParserSettings) throws IOException {
6176
try (Reader inputReader = new InputStreamReader(inputStream, charset)) {
6277
CsvParser parser = new CsvParser(csvParserSettings);
63-
rows = parser.parseAll(inputReader).toArray(new String[0][]);
78+
return parser.parseAll(inputReader)
79+
.toArray(new String[0][]);
6480
}
6581
}
6682

src/test/java/org/spacious_team/table_wrapper/csv/CsvReportPageTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import java.nio.file.Files;
3333
import java.nio.file.Path;
3434

35+
import static java.nio.charset.StandardCharsets.UTF_8;
3536
import static org.junit.jupiter.api.Assertions.assertEquals;
3637
import static org.junit.jupiter.api.Assertions.assertNull;
38+
import static org.mockito.Mockito.*;
3739

3840
class CsvReportPageTest {
3941

@@ -53,6 +55,20 @@ void createFromFile() throws IOException {
5355
}
5456
}
5557

58+
@Test
59+
void createFomInputStream_inputStreamNotClosed() throws IOException {
60+
ByteArrayInputStream is = spy(new ByteArrayInputStream(new byte[]{}));
61+
new CsvReportPage(is);
62+
verify(is, never()).close();
63+
}
64+
65+
@Test
66+
void createFomInputStreamAndSettings_inputStreamNotClosed() throws IOException {
67+
ByteArrayInputStream is = spy(new ByteArrayInputStream(new byte[]{}));
68+
new CsvReportPage(is, UTF_8, CsvReportPage.getDefaultCsvParserSettings());
69+
verify(is, never()).close();
70+
}
71+
5672
@ParameterizedTest
5773
@ValueSource(strings = {"UTF-8", "Windows-1251"})
5874
void testInputDataCharset(String charsetName) throws IOException {

0 commit comments

Comments
 (0)