Browse Source

增加单元测试类

master
VIVIMAN 3 years ago
parent
commit
559fa5a120
  1. 6
      pom.xml
  2. 6
      src/main/java/com/insigma/utils/FileUtil.java
  3. 1
      src/main/java/com/insigma/utils/LinuxCommandUtil.java
  4. 48
      src/main/java/com/insigma/utils/MessageSourceHandler.java
  5. 20
      src/main/java/com/insigma/utils/WinCommandUtil.java
  6. 8
      src/main/java/com/insigma/utils/ZipUtil.java
  7. 16
      src/test/java/com/insigma/utils/DbUtilTest.java
  8. 55
      src/test/java/com/insigma/utils/FileUtilTest.java
  9. 23
      src/test/java/com/insigma/utils/WinCommandUtilTest.java
  10. 38
      src/test/java/com/insigma/utils/ZipUtilTest.java
  11. 79
      src/test/java/com/insigma/utils/my.ini

6
pom.xml

@ -181,6 +181,12 @@
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

6
src/main/java/com/insigma/utils/FileUtil.java

@ -68,8 +68,10 @@ public class FileUtil {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + File.separatorChar + tempList[i]);// 先删除文件夹里面的文件
File folderPath = new File(path + File.separatorChar + tempList[i]); // 再删除空文件夹
// 先删除文件夹里面的文件
delAllFile(path + File.separatorChar + tempList[i]);
// 再删除空文件夹
File folderPath = new File(path + File.separatorChar + tempList[i]);
folderPath.delete();
}
}

1
src/main/java/com/insigma/utils/LinuxCommandUtil.java

@ -36,6 +36,7 @@ public class LinuxCommandUtil implements Runnable {
exitVal = process.waitFor();
} catch (IOException | InterruptedException e) {
log.error("执行命令发生异常:{}", e.getMessage());
e.printStackTrace();
}

48
src/main/java/com/insigma/utils/MessageSourceHandler.java

@ -1,48 +0,0 @@
package com.insigma.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.RequestContextUtils;
import javax.servlet.http.HttpServletRequest;
/**
* @author Vivim
*/
@Component
@Slf4j
public class MessageSourceHandler {
// https://blog.csdn.net/P397226804/article/details/103960592
@Autowired
private HttpServletRequest request;
@Autowired
private MessageSource messageSource;
/**
* 如果是根据Request请求的语言来决定国际化
*
* @param messageKey
* @return
*/
public String getMessage(String messageKey) {
String message = messageSource.getMessage(messageKey, null, RequestContextUtils.getLocale(request));
return message;
}
/**
* 如果是根据应用部署的服务器系统来决定国际化
*
* @param messageKey
* @return
*/
public String getMessageServer(String messageKey) {
String message = messageSource.getMessage(messageKey, null, LocaleContextHolder.getLocale());
return message;
}
}

20
src/main/java/com/insigma/utils/WinCommandUtil.java

@ -4,7 +4,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.io.*;
import java.util.prefs.Preferences;
/**
* CommandImpUtil
@ -17,7 +16,7 @@ public class WinCommandUtil implements Runnable {
private String command;
private final String CMD = "cmd";
private final String EXE = "cmd.exe /c ";
private final String EXE = "cmd /c ";
public WinCommandUtil(String command) {
if(StringUtils.startsWithIgnoreCase(command, CMD)){
this.command = command;
@ -40,11 +39,11 @@ public class WinCommandUtil implements Runnable {
exitVal = process.waitFor();
} catch (IOException | InterruptedException e) {
log.error("执行命令发生异常:{}", e.getMessage());
e.printStackTrace();
}
if (exitVal != 0) {
log.error("执行命令发生异常:{}", exitVal);
throw new RuntimeException("cmd任务执行失败");
}
}
@ -75,20 +74,7 @@ public class WinCommandUtil implements Runnable {
}
public static class RegisterUtil {
// TODO 无法绕开系统权限
String[] keys = {"version", "initial", "creator"};
String[] values = {"1.3", "ini.mp3", "caokai1818@sina.com"};
//把相应的值储存到变量中去
public void writeValue() {
// HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表值.
Preferences pre = Preferences.systemRoot().node("/javaplayer");
for (int i = 0; i < keys.length; i++) {
pre.put(keys[i], values[i]);
}
}
public static void main(String[] args) {
RegisterUtil reg = new RegisterUtil();
reg.writeValue();
}
}
}

8
src/main/java/com/insigma/utils/ZipUtil.java

@ -14,6 +14,8 @@ public class ZipUtil {
private static final String SX = "/";
private static final String XX = "\\";
public static void zip7Z(String dirName, String zipFileName) {
File file = new File(dirName);
if(file.exists()){
if (XX.equals(File.separator)) {
String cmd7z = getRootPath() + "7-Zip/7z.exe";
try {
@ -38,7 +40,10 @@ public class ZipUtil {
}
}
}
}
public static void unZip7Z(String filepath, String destinationDir) {
File file = new File(filepath);
if(file.exists()){
if (XX.equals(File.separator)) {
String cmd7z = getRootPath() + "7-Zip/7z.exe";
try {
@ -62,8 +67,9 @@ public class ZipUtil {
}
}
}
}
public static String getRootPath() {
private static String getRootPath() {
return AppCfg.HZB + "/tomcat8/webapps/qggwy/WEB-INF/classes/static/softTools/";
}
}

16
src/test/java/com/insigma/utils/DbUtilTest.java

@ -0,0 +1,16 @@
package com.insigma.utils;
import cn.hutool.db.Session;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.sql.SQLException;
public class DbUtilTest {
@Test
public void getSessionTest() throws SQLException {
Session session = DbUtil.getSession("jdbc:mysql://127.0.0.1:35017/hy_qggwy", "root", "admin");
String queryString = session.queryString("select count(1) from a01 limit 1");
assertNotEquals("0", queryString);
}
}

55
src/test/java/com/insigma/utils/FileUtilTest.java

@ -0,0 +1,55 @@
package com.insigma.utils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.io.*;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
public class FileUtilTest {
@Test
public void replaceLine() throws IOException {
String path = "D:\\EPWork\\idea\\java_tool\\src\\test\\java\\com\\insigma\\utils\\my.ini";
String key = "read_rnd_buffer_size";
String val = "read_rnd_buffer_size = 64M";
FileUtil.replaceLine(path, key, val);
String temp = getValByKey(path, key);
assertEquals(val, temp);
log.info("完成测试!");
}
/***
* 读取某行数据
* @param path
* @param key
* @return
* @throws IOException
*/
private String getValByKey(String path, String key) throws IOException {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String temp;
// 保存该行前面的内容
while ((temp = br.readLine()) != null) {
if (temp.trim().startsWith(key)) {
return temp;
}
}
return null;
}
@Test
public void delAllFile() {
String path = "D:\\hzb2021\\mysql\\data-jyb";
FileUtil.delAllFile(path);
File file = new File(path);
assertFalse(file.exists());
log.info("完成测试!");
}
}

23
src/test/java/com/insigma/utils/WinCommandUtilTest.java

@ -0,0 +1,23 @@
package com.insigma.utils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.io.File;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
public class WinCommandUtilTest {
@Test
public void run() {
// 注意:该部分需要注意使用的分割线 下划线-
String dir = "e:\\ccc";
new WinCommandUtil("mkdir " + dir).run();
File file = new File(dir);
assertTrue(file.exists());
log.info("测试完成!");
}
}

38
src/test/java/com/insigma/utils/ZipUtilTest.java

@ -0,0 +1,38 @@
package com.insigma.utils;
import com.insigma.config.AppCfg;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.io.File;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
public class ZipUtilTest {
static {
AppCfg.HZB = "D:\\hzb2021";
}
@Test
public void zip7Z() {
String dir = "E:\\aaa";
String zip = "E:\\aaa.zip";
ZipUtil.zip7Z(dir, zip);
File file = new File(zip);
assertTrue(file.exists());
log.info("测试完成!");
}
@Test
public void unZip7Z() {
String dir = "E:\\bbb";
String zip = "E:\\aaa.zip";
ZipUtil.unZip7Z(zip, dir);
File file = new File(dir);
assertTrue(file.exists());
log.info("测试完成!");
}
}

79
src/test/java/com/insigma/utils/my.ini

@ -0,0 +1,79 @@
#high
[client]
port = 35017
socket = MySQL
[mysqld]
port = 35017
socket = MySQL
max_allowed_packet = 64M
key_buffer_size = 32M
table_open_cache = 2048
sort_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 64M
myisam_sort_buffer_size = 64M
net_buffer_length = 1M
thread_cache_size = 16
thread_stack = 16M
query_cache_size = 256M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
log_bin_trust_function_creators=1
server-id = 1
character-set-server=utf8
default-storage-engine=INNODB
innodb_buffer_pool_size=3000M
innodb_additional_mem_pool_size=64M
innodb_flush_log_at_trx_commit =0
innodb_log_buffer_size=32M
innodb_log_file_size=128M
sync_binlog = 1000
innodb_autoextend_increment=32M
innodb_stats_on_metadata=OFF
innodb_read_io_threads=4
innodb_write_io_threads = 4
innodb_file_per_table = ON
innodb_file_per_table=1
tmp_table_size = 256M
max_connections = 500
max_connect_errors = 30
binlog_cache_size = 4M
max_heap_table_size = 128M
join_buffer_size = 32M
sort_buffer_size = 12M
[mysqldump]
quick
max_allowed_packet = 64M
key_buffer_size=128M
tmp_table_size =128M
[myisamchk]
read_buffer = 2M
write_buffer = 2M
[mysql]
no-auto-rehash
default-character-set=utf8
[mysqlhotcopy]
interactive-timeout
max_allowed_packet = 64M
thread_cache_size = 16
thread_concurrency = 8
#table_cache = 4096 5.1.3 table_open_cache
#memlock
tmp_table_size = 128M
transaction_isolation = REPEATABLE-READ
#external-locking
#skip-networking
#log_slave_updates
#log
#log_warnings
#log_slow_queries
#long_query_time = 6
#log_long_format
Loading…
Cancel
Save