修改BaseTest,修复全局变量引用log报空指针异常

main
youHong.ai 2022-11-23 14:29:56 +08:00
parent 15dd223a9b
commit 8a8cf33c4a
2 changed files with 147 additions and 9 deletions

View File

@ -1,8 +1,11 @@
package basetest;
import aiyh.utils.Util;
import aiyh.utils.excention.CustomerException;
import cn.hutool.core.lang.Console;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -25,15 +28,14 @@ public class BaseTest {
private ApplicationContext ctx;
private Properties properties;
@Before
public void before() {
this.getContext();
GCONST.setServerName(this.properties.getProperty("serverName"));
GCONST.setRootPath(this.properties.getProperty("rootPath"));
GCONST.setSystemFilePath(this.properties.getProperty("systemFilePath"));
GCONST.setLogPath(this.properties.getProperty("logPath"));
private static Properties propertiesStatic;
@BeforeClass
public static void beforeStatic(){
BaseTest.getContextStatic();
GCONST.setServerName(propertiesStatic.getProperty("serverName"));
GCONST.setRootPath(propertiesStatic.getProperty("rootPath"));
GCONST.setSystemFilePath(propertiesStatic.getProperty("systemFilePath"));
GCONST.setLogPath(propertiesStatic.getProperty("logPath"));
}
private void getContext() {
@ -74,9 +76,57 @@ public class BaseTest {
}
}
@Before
public void before() {
this.getContext();
GCONST.setServerName(this.properties.getProperty("serverName"));
GCONST.setRootPath(this.properties.getProperty("rootPath"));
GCONST.setSystemFilePath(this.properties.getProperty("systemFilePath"));
GCONST.setLogPath(this.properties.getProperty("logPath"));
}
private static void getContextStatic() {
Properties propertiesXml = new Properties();
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
BaseTestConfig baseTestConfig = (BaseTestConfig) ctx.getBean("basetest");
propertiesXml.setProperty("serverName", baseTestConfig.getServerName());
propertiesXml.setProperty("rootPath", baseTestConfig.getRootPath());
propertiesXml.setProperty("systemFilePath", baseTestConfig.getSystemFilePath());
propertiesXml.setProperty("logPath", baseTestConfig.getLogPath());
} catch (Exception e) {
}
Properties properties = new Properties();
// 使用ClassLoader加载properties配置文件生成对应的输入流
InputStream in = BaseTest.class.getClassLoader().getResourceAsStream("application.properties");
// 使用properties对象加载输入流
try {
properties.load(in);
if(in != null){
try{
in.close();
}catch (IOException ex){
}
}
Enumeration<?> enumeration = propertiesXml.propertyNames();
while (enumeration.hasMoreElements()){
String key = (String) enumeration.nextElement();
String value = propertiesXml.getProperty(key);
properties.setProperty(key,value);
}
propertiesStatic = properties;
} catch (IOException ex) {
throw new CustomerException("未发现application.properties",ex);
}
}
@Test
public void with() {
Logger logger = Util.getLogger();
logger.info("aafasdf");
String sql = "select COMPANYNAME,LICENSE,EXPIREDATE,CVERSION from license ";
RecordSet rs = new RecordSet();
rs.executeQuery(sql);

View File

@ -1,11 +1,24 @@
package youhong.ai.pcn;
import aiyh.utils.Util;
import com.alibaba.fastjson.JSON;
import ebu7common.youhong.ai.sftp.SftpConnectUtil;
import basetest.BaseTest;
import org.apache.log4j.Logger;
import org.junit.Test;
import weaver.general.GCONST;
import weaver.youhong.ai.pcn.hrorganization.sftp.FetchDataUtil;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <h1></h1>
*
@ -16,6 +29,8 @@ import weaver.youhong.ai.pcn.hrorganization.sftp.FetchDataUtil;
public class TestOrganization extends BaseTest {
private Logger log = Util.getLogger();
@Test
public void testSftp(){
@ -28,4 +43,77 @@ public class TestOrganization extends BaseTest {
fetchDataUtil.downloadFile(sftpConnectUtil,
"HRIS_PositionExport20221120", GCONST.getSysFilePath() + "HRIS_PositionExport20221120.csv");
}
@Test
public void testStaticLog(){
log.info("哈哈哈好的方式");
}
@Test
public void testReadCsv(){
String srcPath = GCONST.getSysFilePath() + "HRIS_PositionExport20221120" + ".csv";
// String charset = "utf-8";
// try (CSVReader csvReader = new CSVReaderBuilder(new BufferedReader(new InputStreamReader(new FileInputStream(new File(srcPath)), charset))).build()) {
// Iterator<String[]> iterator = csvReader.iterator();
// while (iterator.hasNext()) {
// Arrays.stream(iterator.next()).forEach(System.out::print);
// System.out.println();
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
BufferedReader reader = null;
String line = null;
try {
reader = new BufferedReader(new FileReader(srcPath));
} catch (FileNotFoundException e) {
System.out.println("[读取CSV文件插入数据时读取文件异常]");
e.printStackTrace();
}
String[] fieldsArr = null;
int lineNum = 0;
int insertResult = 0;
// TableInfo tableInfo = new TableInfo();
// tableInfo.setTableName(tableName);
try {
List<List<String>> listField = new ArrayList<>();
while ((line = reader.readLine()) != null) {
if (lineNum == 0) {
//表头信息
fieldsArr = line.split(",");
} else {
//数据信息
String str;
line += ",";
Pattern pCells = Pattern
.compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");
Matcher mCells = pCells.matcher(line);
List<String> cells = new LinkedList();//每行记录一个list
//读取每个单元格
while (mCells.find()) {
str = mCells.group();
str = str.replaceAll(
"(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
str = str.replaceAll("(?sm)(\"(\"))", "$2");
cells.add(str);
}
//从第2行起的数据信息list
listField.add(cells);
}
lineNum++;
}
System.out.println(Arrays.toString(fieldsArr));
for (List<String> list : listField) {
System.out.println(list);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}