Skip to content

Commit

Permalink
Merge pull request #11 from spacious-team/develop
Browse files Browse the repository at this point in the history
Релиз 2024.1
  • Loading branch information
vananiev authored Apr 21, 2024
2 parents 04c482f + e9529c0 commit e13b1d8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<groupId>org.spacious-team</groupId>
<artifactId>table-wrapper-csv-impl</artifactId>
<version>2023.1</version>
<version>2024.1</version>
<packaging>jar</packaging>

<name>Table Wrapper API CSV Implementation</name>
Expand All @@ -49,8 +49,8 @@
<maven.compiler.release>11</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<lombok.version>1.18.28</lombok.version>
<checkerframework.version>3.32.0</checkerframework.version>
<lombok.version>1.18.32</lombok.version>
<checkerframework.version>3.42.0</checkerframework.version>
</properties>

<repositories>
Expand Down Expand Up @@ -123,15 +123,15 @@
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version> <!-- JUnit 5 requirement -->
<version>3.2.5</version> <!-- JUnit 5 requirement -->
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<version>3.13.0</version>
<configuration>
<fork>true</fork> <!-- Must fork or else JVM arguments are ignored. -->
<showDeprecation>true</showDeprecation>
Expand Down Expand Up @@ -174,7 +174,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<version>0.8.12</version>
<executions>
<execution>
<id>prepare-agent</id>
Expand All @@ -194,7 +194,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Table Wrapper CSV Impl
* Copyright (C) 2024 Spacious Team <spacious-team@ya.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.spacious_team.table_wrapper.csv;

import java.io.FilterInputStream;

class CloseIgnoringInputStream extends FilterInputStream {

public CloseIgnoringInputStream(java.io.InputStream in) {
super(in);
}

@Override
public void close() {
// do nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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

/**
* Closes inputStream if success. UTF-8 encoded stream data expected.
* Field and line delimiter detected automatically. UTF-8 encoded file expected.
*
* @implSpec Does not close inputStream
*/
public CsvReportPage(InputStream inputStream) throws IOException {
this(inputStream, UTF_8, getDefaultCsvParserSettings());
}

/**
* Closes inputStream if success
* @implSpec Does not close inputStream
*/
public CsvReportPage(InputStream inputStream, Charset charset, CsvParserSettings csvParserSettings) throws IOException {
CloseIgnoringInputStream closeIgnoringInputStream = new CloseIgnoringInputStream(inputStream);
this.rows = readRows(closeIgnoringInputStream, charset, csvParserSettings);
}

/**
* @implSpec Closes inputStream
*/
private static String[] @NonNull [] readRows(InputStream inputStream,
Charset charset,
CsvParserSettings csvParserSettings) throws IOException {
try (Reader inputReader = new InputStreamReader(inputStream, charset)) {
CsvParser parser = new CsvParser(csvParserSettings);
rows = parser.parseAll(inputReader).toArray(new String[0][]);
return parser.parseAll(inputReader)
.toArray(new String[0][]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import java.nio.file.Files;
import java.nio.file.Path;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.*;

class CsvReportPageTest {

Expand All @@ -53,6 +55,20 @@ void createFromFile() throws IOException {
}
}

@Test
void createFomInputStream_inputStreamNotClosed() throws IOException {
ByteArrayInputStream is = spy(new ByteArrayInputStream(new byte[]{}));
new CsvReportPage(is);
verify(is, never()).close();
}

@Test
void createFomInputStreamAndSettings_inputStreamNotClosed() throws IOException {
ByteArrayInputStream is = spy(new ByteArrayInputStream(new byte[]{}));
new CsvReportPage(is, UTF_8, CsvReportPage.getDefaultCsvParserSettings());
verify(is, never()).close();
}

@ParameterizedTest
@ValueSource(strings = {"UTF-8", "Windows-1251"})
void testInputDataCharset(String charsetName) throws IOException {
Expand Down

0 comments on commit e13b1d8

Please sign in to comment.