Skip to content

Commit

Permalink
bump opencsv from 4.6 to 5.9 (via #2547)
Browse files Browse the repository at this point in the history
  • Loading branch information
kusaeva authored May 8, 2024
1 parent e3fc1d2 commit 3fc7bc2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@
*/
package io.qameta.allure;

import com.opencsv.bean.BeanField;
import com.opencsv.bean.ColumnPositionMappingStrategy;
import com.opencsv.bean.CsvBindByName;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.opencsv.bean.HeaderColumnNameMappingStrategyBuilder;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import io.qameta.allure.core.Configuration;
import io.qameta.allure.core.LaunchResults;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.io.IOException;
import java.io.Writer;
Expand Down Expand Up @@ -60,7 +56,8 @@ public void aggregate(final Configuration configuration,

try (Writer writer = Files.newBufferedWriter(csv)) {
final StatefulBeanToCsvBuilder<T> builder = new StatefulBeanToCsvBuilder<>(writer);
final CustomMappingStrategy<T> mappingStrategy = new CustomMappingStrategy<>();
final HeaderColumnNameMappingStrategy<T> mappingStrategy =
new HeaderColumnNameMappingStrategyBuilder<T>().build();
mappingStrategy.setType(type);
final StatefulBeanToCsv<T> beanWriter = builder.withMappingStrategy(mappingStrategy).build();
try {
Expand All @@ -72,39 +69,4 @@ public void aggregate(final Configuration configuration,
}

protected abstract List<T> getData(List<LaunchResults> launchesResults);

@SuppressWarnings("all")
private static final class CustomMappingStrategy<T> extends ColumnPositionMappingStrategy<T> {

@Override
public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException {

super.setColumnMapping(new String[FieldUtils.getAllFields(bean.getClass()).length]);
final int numColumns = findMaxFieldIndex();
if (!isAnnotationDriven() || numColumns == -1) {
return super.generateHeader(bean);
}

String[] header = new String[numColumns + 1];

BeanField<T> beanField;
for (int i = 0; i <= numColumns; i++) {
beanField = findField(i);
String columnHeaderName = extractHeaderName(beanField);
header[i] = columnHeaderName;
}
return header;
}

private String extractHeaderName(final BeanField<T> beanField) {
if (beanField == null || beanField.getField() == null
|| beanField.getField().getDeclaredAnnotationsByType(CsvBindByName.class).length == 0) {
return StringUtils.EMPTY;
}

final CsvBindByName bindByNameAnnotation = beanField.getField()
.getDeclaredAnnotationsByType(CsvBindByName.class)[0];
return bindByNameAnnotation.column();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
*/
package io.qameta.allure;

import com.opencsv.bean.BeanField;
import com.opencsv.bean.ColumnPositionMappingStrategy;
import com.opencsv.bean.CsvBindByName;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.opencsv.bean.HeaderColumnNameMappingStrategyBuilder;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import io.qameta.allure.core.Configuration;
import io.qameta.allure.core.LaunchResults;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -60,7 +57,8 @@ public void aggregate(final Configuration configuration,
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (Writer writer = new OutputStreamWriter(bos, StandardCharsets.UTF_8)) {
final StatefulBeanToCsvBuilder<T> builder = new StatefulBeanToCsvBuilder<>(writer);
final CustomMappingStrategy<T> mappingStrategy = new CustomMappingStrategy<>();
final HeaderColumnNameMappingStrategy<T> mappingStrategy =
new HeaderColumnNameMappingStrategyBuilder<T>().build();
mappingStrategy.setType(type);
final StatefulBeanToCsv<T> beanWriter = builder.withMappingStrategy(mappingStrategy).build();
try {
Expand All @@ -78,39 +76,4 @@ public void aggregate(final Configuration configuration,
}

protected abstract List<T> getData(List<LaunchResults> launchesResults);

@SuppressWarnings("all")
private static final class CustomMappingStrategy<T> extends ColumnPositionMappingStrategy<T> {

@Override
public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException {

super.setColumnMapping(new String[FieldUtils.getAllFields(bean.getClass()).length]);
final int numColumns = findMaxFieldIndex();
if (!isAnnotationDriven() || numColumns == -1) {
return super.generateHeader(bean);
}

String[] header = new String[numColumns + 1];

BeanField<T> beanField;
for (int i = 0; i <= numColumns; i++) {
beanField = findField(i);
String columnHeaderName = extractHeaderName(beanField);
header[i] = columnHeaderName;
}
return header;
}

private String extractHeaderName(final BeanField<T> beanField) {
if (beanField == null || beanField.getField() == null
|| beanField.getField().getDeclaredAnnotationsByType(CsvBindByName.class).length == 0) {
return StringUtils.EMPTY;
}

final CsvBindByName bindByNameAnnotation = beanField.getField()
.getDeclaredAnnotationsByType(CsvBindByName.class)[0];
return bindByNameAnnotation.column();
}
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ subprojects {
dependency("com.beust:jcommander:1.82")
dependency("com.github.spotbugs:spotbugs-annotations:4.8.4")
dependency("com.github.spotbugs:spotbugs:4.8.4")
dependency("com.opencsv:opencsv:4.6")
dependency("com.opencsv:opencsv:5.9")
dependency("com.puppycrawl.tools:checkstyle:10.16.0")
dependency("com.squareup.retrofit2:converter-jackson:${comSquareupRetrofit2Version}")
dependency("com.squareup.retrofit2:retrofit:${comSquareupRetrofit2Version}")
Expand Down

0 comments on commit 3fc7bc2

Please sign in to comment.