ecology_maven/customization/test/NewUtilTest.java

412 lines
14 KiB
Java

package customization.test;
import aiyh.utils.Util;
import aiyh.utils.fileUtil.ProperUtil;
import com.alibaba.fastjson.JSON;
import com.api.aiyh_pcn.patentWall.service.PatentWallService;
import com.api.aiyh_pcn.patentWall.vo.PatentVO;
import com.api.aiyh_pcn.patentWall.vo.SearchInputVO;
import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import weaver.common.util.string.StringUtil;
import weaver.general.GCONST;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author EBU7-dev1-ayh
* @create 2021/11/29 0029 10:16
*/
public class NewUtilTest {
@Before
public void before() throws Exception {
GCONST.setServerName("ecology");
GCONST.setRootPath("H:\\e-cology-dev\\web\\");
}
@AfterClass
public static void after() {
System.out.println("单元测试结束!");
}
@Test
public void testKey() {
String key = "aiyh.patent.config[11].public";
String pattern = "^(aiyh.patent\\.)(?<key>(\\w+))$";
String pattern1 = "^(aiyh.patent\\.)(?<key>(?<objKey>(\\w+))\\.(\\S)+(?!\\[))";
String pattern2 = "^(aiyh.patent\\.)(?<key>(\\w+))(\\[([0-9])+])$";
String pattern3 = "^(aiyh.patent\\.)(?<key>(\\w+))(\\[(?<index>([0-9])+)])\\.(?<objKey>(\\S)+)$";
Pattern compile = Pattern.compile(pattern);
Matcher matcher = compile.matcher(key);
while (matcher.find()) {
System.out.println("单一值");
System.out.println(matcher.group("key"));
}
compile = Pattern.compile(pattern2);
matcher = compile.matcher(key);
while (matcher.find()) {
System.out.println("数组");
System.out.println(matcher.group("key"));
}
compile = Pattern.compile(pattern3);
matcher = compile.matcher(key);
while (matcher.find()) {
System.out.println("对象数组");
System.out.println(matcher.group("key"));
System.out.println(matcher.group("objKey"));
System.out.println(matcher.group("index"));
}
compile = Pattern.compile(pattern1);
matcher = compile.matcher(key);
while (matcher.find()) {
System.out.println("多个");
System.out.println(matcher.group("key"));
System.out.println(matcher.group("objKey"));
}
}
@Test
public void testGetPro() {
String key = "aiyh.patent.config[0]";
String prefix = "aiyh.patent";
String value = "第一个元素";
getPro(prefix, key, value);
}
public void getPro(String prefix, String key, Object value) {
Map<String, Object> result = new HashMap<>();
// result = keyHandler(prefix, key, value, result);
String key2 = "aiyh.patent.config[1]";
// result = keyHandler(prefix, key2, "第二个元素", result);
System.out.println(JSON.toJSONString(result));
}
@Test
public void redPro() {
String fileName = "test";
String prefix = "aiyh.patentWall";
Map<String, Object> result = Util.readProperties2Map(fileName, prefix);
System.out.println(JSON.toJSONString(result));
}
public Map<String, Object> getProperties2Map(String fileName, String prefix) {
String propertyPath = GCONST.getPropertyPath();
if (StringUtil.isNullOrEmpty(fileName)) {
return null;
}
if (fileName.contains(".properties")) {
fileName.replace(".properties", "");
}
String path = propertyPath + "prop2map" + File.separator + fileName + ".properties";
ProperUtil prop = new ProperUtil();
Map<String, Object> map = new HashMap<>();
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(path));
prop.load(inputStream);
// Enumeration<?> enumeration = prop.propertyNames();
// 顺序读取
Enumeration<?> enumeration = prop.keys();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
String value = prop.getProperty(key);
System.out.println(key);
keyHandler(prefix, key, value, map);
;
}
} catch (IOException e) {
throw new RuntimeException("找不到文件:" + path);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return map;
}
public Map<String, Object> keyHandler(String prePrefix, String key, Object value, Map<String, Object> preResult) {
String objRegex = "^(" + prePrefix + "\\.)(?<key>(\\w+))$";
Pattern compile = Pattern.compile(objRegex);
Matcher matcher = compile.matcher(key);
if (matcher.find()) {
// 只匹配前缀.key=value模式的
String resultKey = matcher.group("key");
preResult.put(resultKey, prop2MapPutValue(value));
}
String moreKey = "^(" + prePrefix + "\\.)(?<key>(?<objKey>(\\w+))\\.(\\S)+(?!\\[))";
compile = Pattern.compile(moreKey);
matcher = compile.matcher(key);
if (matcher.find()) {
// 匹配前缀.key1.key2=value模式的
String objKey = matcher.group("objKey");
String prefixStr = prePrefix + "." + objKey;
Map<String, Object> valueMap;
if (preResult.containsKey(objKey)) {
valueMap = (Map<String, Object>) preResult.get(objKey);
keyHandler(prefixStr, key, value, valueMap);
return null;
}
valueMap = new HashMap<>();
keyHandler(prefixStr, key, value, valueMap);
preResult.put(objKey, valueMap);
return null;
}
String strList = "^(" + prePrefix + "\\.)(?<key>(\\w+))(\\[(?<index>([0-9])+)])$";
compile = Pattern.compile(strList);
matcher = compile.matcher(key);
if (matcher.find()) {
// 匹配前缀.key[0]=value模式的
String objKey = matcher.group("key");
int index = Integer.parseInt(matcher.group("index"));
if (preResult.containsKey(objKey)) {
// 存在值
List<Object> valueList = (List<Object>) preResult.get(objKey);
if (index >= valueList.size()) {
valueList.add(prop2MapPutValue(value));
} else {
valueList.set(index, prop2MapPutValue(value));
}
return null;
}
List<Object> valueList = new ArrayList<>();
valueList.add(prop2MapPutValue(value));
preResult.put(objKey, valueList);
return null;
}
String objArray = "^(" + prePrefix + "\\.)(?<arrKey>(\\w+))(\\[(?<index>([0-9])+)])\\.(?<objKey>(\\S)+)$";
// String objArray = "^("+prePrefix+"\\.)(?<arrKey>(\\w+))(\\[(?<index>([0-9])+)])(\\.(?<objKey>(\\S)+))+";
compile = Pattern.compile(objArray);
matcher = compile.matcher(key);
if (matcher.find()) {
// 匹配前缀.key[0].name=value的模式
String arrKey = matcher.group("arrKey");
String objKey = matcher.group("objKey");
int index = Integer.parseInt(matcher.group("index"));
List<Map<String, Object>> mapList;
if (preResult.containsKey(arrKey)) {
// 存在
mapList = (List<Map<String, Object>>) preResult.get(arrKey);
// mapList
Map<String, Object> valueMap;
if (index >= mapList.size()) {
valueMap = new HashMap<>();
valueMap.put(objKey, prop2MapPutValue(value));
mapList.add(valueMap);
return null;
}
valueMap = mapList.get(index);
String arrMoreKey = "(?<key>(\\w+))\\.(\\S)+(?!\\[)";
Pattern arrMoreKeyCompile = Pattern.compile(arrMoreKey);
Matcher arrMoreKeyMatcher = arrMoreKeyCompile.matcher(objKey);
if (arrMoreKeyMatcher.find()) {
String arrMoreObjKey = arrMoreKeyMatcher.group("key");
Map<String, Object> arrMoreValue;
if (valueMap.containsKey(arrMoreObjKey)) {
arrMoreValue = (Map<String, Object>) valueMap.get(arrMoreObjKey);
keyHandler(arrMoreObjKey, objKey, value, arrMoreValue);
return null;
}
arrMoreValue = new HashMap<>();
keyHandler(arrMoreObjKey, objKey, value, arrMoreValue);
valueMap.put(arrMoreObjKey, arrMoreValue);
return null;
}
arrMoreKey = "(?<key>(\\w+))(\\[(?<index>([0-9])+)])$";
arrMoreKeyCompile = Pattern.compile(arrMoreKey);
arrMoreKeyMatcher = arrMoreKeyCompile.matcher(objKey);
if (arrMoreKeyMatcher.find()) {
String arrMoreArrKey = arrMoreKeyMatcher.group("key");
int arrMoreIndex = Integer.parseInt(arrMoreKeyMatcher.group("index"));
List<Object> arrMoreListValue;
if (valueMap.containsKey(arrMoreArrKey)) {
// 存在值
arrMoreListValue = (List<Object>) valueMap.get(arrMoreArrKey);
if (arrMoreIndex >= arrMoreListValue.size()) {
arrMoreListValue.add(prop2MapPutValue(value));
} else {
arrMoreListValue.set(arrMoreIndex, prop2MapPutValue(value));
}
return null;
}
arrMoreListValue = new ArrayList<>();
arrMoreListValue.add(prop2MapPutValue(value));
valueMap.put(arrMoreArrKey, arrMoreListValue);
return null;
}
// 直接添加
valueMap.put(objKey, prop2MapPutValue(value));
return null;
}
// 不存在
mapList = new ArrayList<>();
Map<String, Object> valueMap = new HashMap<>();
valueMap.put(objKey, prop2MapPutValue(value));
mapList.add(valueMap);
preResult.put(arrKey, mapList);
}
return null;
}
public Object prop2MapPutValue(Object value) {
if (value == null) {
return null;
}
String valueStr = String.valueOf(value).trim();
if (valueStr.startsWith("\"") && valueStr.endsWith("\"")) {
return valueStr.substring(1, valueStr.length() - 1);
}
if (valueStr.contains(",")) {
return valueStr.split(",");
}
return value;
}
@Test
public void testSubstr() {
System.out.println("\"这个是文本框\"".trim().substring(1, "\"这个是文本框\"".trim().length() - 1));
}
@Test
public void testSplit() {
String str = "12,343,89\\,99";
String str1 = "abcd'efg'hig?'klmn";
String[] data = str1.split("(?<!\\?)'");
System.out.println(Arrays.toString(data));
String[] split = str.split("(?<!\\\\),");
System.out.println(Arrays.toString(split));
for (int i = 0; i < split.length; i++) {
split[i] = split[i].replaceAll("\\\\,", ",");
}
for (String s : split) {
System.out.println(s);
}
}
@Test
public void testLanguage() {
String selectName = "~`~`7 其它 不知道 管他呢,阿斯顿发的`~`8 other`~`9 其它`~`~";
String showName = selectName;
if (selectName.startsWith("~`~`") && selectName.endsWith("`~`~")) {
String pattern = "(`~`7 )(?<lable>(\\w*|\\W*|[\\u4e00-\\u9fa5]*))(`~`)";
Pattern compile = Pattern.compile(pattern);
Matcher matcher = compile.matcher(selectName);
if (matcher.find()) {
showName = matcher.group("lable");
}
}
System.out.println(showName);
}
@Test
public void testPatentServer() {
PatentWallService patentWallService = new PatentWallService();
List<SearchInputVO> inputList = patentWallService.getSearchList("patentWall", 8);
System.out.println(JSON.toJSONString(inputList));
List<PatentVO> patentWallList = patentWallService.getList(null, "patentWall");
System.out.println(JSON.toJSONString(patentWallList));
}
@Test
public void testRegx() {
String pattern = "\\$\\{(?<field>(\\s|\\S)+?)}";
Pattern compile = Pattern.compile(pattern);
Matcher matcher = compile.matcher("${id}");
if (matcher.find()) {
System.out.println(matcher.group("field"));
}
}
@Test
public void testSplic() {
List<Integer> list = new ArrayList<>();
list.add(0);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
System.out.println(list);
System.out.println(list.subList(0, 2));
list.removeAll(list.subList(0, 2));
System.out.println(list);
}
@Test
public void test() {
String bizContent = "{\"contractExtension\":\".pdf\",\"contractTitle\":\"PCN Template - Non-Disclosure Agreement (Corporate).pdf\",\"uploadType\":1,\"customerNo\":\"ecef845ba95a409393cb66271a41b0a6\"}";
System.out.println(cn.hutool.core.util.URLUtil.encodeAll(bizContent, Charset.forName("UTF-8")));
}
@Test
public void testReg() {
String var15 = "<html> \n" +
" <head> \n" +
" <title></title> \n" +
" <link href=\"/cloudstore/resource/pc/ckeditor-4.6.2/weaver/contents.css\" rel=\"stylesheet\" type=\"text/css\" /> \n" +
" <link href=\"/cloudstore/resource/pc/ckeditor-4.6.2/plugins/copyformatting/styles/copyformatting.css\" rel=\"stylesheet\" type=\"text/css\" /> \n" +
" <link href=\"/cloudstore/resource/pc/ckeditor-4.6.2/plugins/tableselection/styles/tableselection.css\" rel=\"stylesheet\" type=\"text/css\" /> \n" +
" <link href=\"/cloudstore/resource/pc/ckeditor-4.6.2/plugins/image2/styles/image2.css\" rel=\"stylesheet\" type=\"text/css\" /> \n" +
" <link href=\"/cloudstore/resource/pc/ckeditor-4.6.2/plugins/widget/styles/widget.css\" rel=\"stylesheet\" type=\"text/css\" /> \n" +
" </head> \n" +
" <body style=\"height: 747px;\"> \n" +
" <p>< img alt=\"\" src=\"/weaver/weaver.file.FileDownload?fileid=acdde79d8027d30e809be35a885a28eb184a3d7609b33666049ac12a48c5c8c3c10908eae8d1f6d650bfffce6db986fb5069726005eaf18c1\" /></p > \n" +
" </body> \n" +
" </html> \n" +
" ";
ArrayList var17 = Util.matchAll(var15, "/weaver/weaver.file.FileDownload\\?fileid=([a-z0-9]+)", 1, 1);
System.out.println(var17.size());
System.out.println(var17);
}
@Test
public void testMessage() {
MessageType messageType = MessageType.newInstance(1146); //消息来源(见文档第四点补充)
Set<String> userIdList = new HashSet<>(); //接收人id
userIdList.add("22");
userIdList.add("23");
userIdList.add("24");
String title = "标题"; //标题
String context = "内容"; //内容
String linkUrl = ""; //PC端链接 纯文本就传空字符串
String linkMobileUrl = ""; //移动端链接 纯文本就传空字符串
try {
MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, linkUrl, linkMobileUrl);
messageBean.setCreater(1);//创建人id
//message.setBizState("0");需要修改消息状态时传入,表示消息最初状态为待处理
// messageBean.setTargetId("121|22"); //消息来源code +“|”+业务id 需要修改消息状态时传入,这个字段是自定义的,和修改消息状态的时候传入相同的值,可做更新。
Util_Message.store(messageBean);
} catch (IOException e) {
e.printStackTrace();
}
}
}