This commit is contained in:
shiziyuan9527 2020-09-17 21:27:36 +08:00
commit 2bad9f925d
5 changed files with 11 additions and 26 deletions

View File

@ -15,7 +15,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<shiro.version>1.5.1</shiro.version> <shiro.version>1.6.0</shiro.version>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<jmeter.version>5.2.1</jmeter.version> <jmeter.version>5.2.1</jmeter.version>
<nacos.version>1.1.3</nacos.version> <nacos.version>1.1.3</nacos.version>

View File

@ -9,10 +9,7 @@ import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpHeader;
import java.io.BufferedReader; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -21,10 +18,8 @@ import java.util.Optional;
public abstract class ApiImportAbstractParser implements ApiImportParser { public abstract class ApiImportAbstractParser implements ApiImportParser {
protected String getApiTestStr(InputStream source) { protected String getApiTestStr(InputStream source) {
BufferedReader bufferedReader;
StringBuilder testStr = null; StringBuilder testStr = null;
try { try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(source, StandardCharsets.UTF_8))) {
bufferedReader = new BufferedReader(new InputStreamReader(source, StandardCharsets.UTF_8));
testStr = new StringBuilder(); testStr = new StringBuilder();
String inputStr; String inputStr;
while ((inputStr = bufferedReader.readLine()) != null) { while ((inputStr = bufferedReader.readLine()) != null) {

View File

@ -17,7 +17,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.*; import java.util.*;
public abstract class EasyExcelListener<T> extends AnalysisEventListener<T> { public abstract class EasyExcelListener<T> extends AnalysisEventListener<T> implements AutoCloseable {
protected List<ExcelErrData<T>> errList = new ArrayList<>(); protected List<ExcelErrData<T>> errList = new ArrayList<>();
@ -153,6 +153,8 @@ public abstract class EasyExcelListener<T> extends AnalysisEventListener<T> {
return errList; return errList;
} }
@Override
public void close() { public void close() {
this.easyExcelI18nTranslator.resetExcelProperty(); this.easyExcelI18nTranslator.resetExcelProperty();
} }

View File

@ -5,7 +5,6 @@ import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.exception.ExcelException; import io.metersphere.exception.ExcelException;
import org.apache.poi.ss.usermodel.IndexedColors;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
@ -13,7 +12,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.List; import java.util.List;
public class EasyExcelExporter { public class EasyExcelExporter implements AutoCloseable {
EasyExcelI18nTranslator easyExcelI18nTranslator; EasyExcelI18nTranslator easyExcelI18nTranslator;
@ -47,6 +46,7 @@ public class EasyExcelExporter {
} }
} }
@Override
public void close() { public void close() {
easyExcelI18nTranslator.resetExcelProperty(); easyExcelI18nTranslator.resetExcelProperty();
} }

View File

@ -273,16 +273,12 @@ public class TestCaseService {
Set<String> userIds = userRoleMapper.selectByExample(userRoleExample).stream().map(UserRole::getUserId).collect(Collectors.toSet()); Set<String> userIds = userRoleMapper.selectByExample(userRoleExample).stream().map(UserRole::getUserId).collect(Collectors.toSet());
EasyExcelListener easyExcelListener = null; try (EasyExcelListener easyExcelListener = new TestCaseDataListener(this, projectId, testCaseNames, userIds)) {
try {
easyExcelListener = new TestCaseDataListener(this, projectId, testCaseNames, userIds);
EasyExcelFactory.read(multipartFile.getInputStream(), TestCaseExcelData.class, easyExcelListener).sheet().doRead(); EasyExcelFactory.read(multipartFile.getInputStream(), TestCaseExcelData.class, easyExcelListener).sheet().doRead();
errList = easyExcelListener.getErrList(); errList = easyExcelListener.getErrList();
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
MSException.throwException(e.getMessage()); MSException.throwException(e.getMessage());
} finally {
easyExcelListener.close();
} }
} }
@ -316,15 +312,11 @@ public class TestCaseService {
} }
public void testCaseTemplateExport(HttpServletResponse response) { public void testCaseTemplateExport(HttpServletResponse response) {
EasyExcelExporter easyExcelExporter = null; try (EasyExcelExporter easyExcelExporter = new EasyExcelExporter(TestCaseExcelData.class)) {
try {
easyExcelExporter = new EasyExcelExporter(TestCaseExcelData.class);
easyExcelExporter.export(response, generateExportTemplate(), easyExcelExporter.export(response, generateExportTemplate(),
Translator.get("test_case_import_template_name"), Translator.get("test_case_import_template_sheet")); Translator.get("test_case_import_template_name"), Translator.get("test_case_import_template_sheet"));
} catch (Exception e) { } catch (Exception e) {
MSException.throwException(e); MSException.throwException(e);
} finally {
easyExcelExporter.close();
} }
} }
@ -398,15 +390,11 @@ public class TestCaseService {
} }
public void testCaseExport(HttpServletResponse response, TestCaseBatchRequest request) { public void testCaseExport(HttpServletResponse response, TestCaseBatchRequest request) {
EasyExcelExporter easyExcelExporter = null; try (EasyExcelExporter easyExcelExporter = new EasyExcelExporter(TestCaseExcelData.class)) {
try {
easyExcelExporter = new EasyExcelExporter(TestCaseExcelData.class);
easyExcelExporter.export(response, generateTestCaseExcel(request), easyExcelExporter.export(response, generateTestCaseExcel(request),
Translator.get("test_case_import_template_name"), Translator.get("test_case_import_template_sheet")); Translator.get("test_case_import_template_name"), Translator.get("test_case_import_template_sheet"));
} catch (Exception e) { } catch (Exception e) {
MSException.throwException(e); MSException.throwException(e);
} finally {
easyExcelExporter.close();
} }
} }