mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-31 22:18:30 +00:00
init project
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class MainTest {
|
||||
|
||||
@Test
|
||||
public void testTemplate() {
|
||||
System.out.println(StringUtils.MULTIPLE_HYPHENS);
|
||||
System.out.println("XX测试:");
|
||||
System.out.println("**********");
|
||||
System.out.println(StringUtils.MULTIPLE_HYPHENS);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class AssertionUtilsTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void classLocation() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util;
|
||||
|
||||
import com.zfoo.protocol.util.ClassUtils;
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class ClassUtilTest {
|
||||
|
||||
// ClassUtilTest
|
||||
@Test
|
||||
public void classLocation() {
|
||||
String str = ClassUtils.classLocation(Integer.class);
|
||||
Assert.assertEquals("jrt:/java.base/java/lang/Integer.class", str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllClasses() throws Exception {
|
||||
System.out.println(StringUtils.MULTIPLE_HYPHENS);
|
||||
System.out.println("某个包下的所有类查找测试:");
|
||||
Set<Class<?>> set = ClassUtils.getAllClasses("com.zfoo");
|
||||
for (Class<?> clazz : set) {
|
||||
System.out.println(clazz.getName());
|
||||
}
|
||||
System.out.println(StringUtils.MULTIPLE_HYPHENS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getClassPath() {
|
||||
System.out.println(ClassUtils.getClassAbsPath(User.class));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getClassFromClassPath() throws IOException {
|
||||
System.out.println(new String(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("com.zfoo.util.ClassUtilsTest"))));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DomUtilsTest {
|
||||
|
||||
private static final String XML_WITH_HEAD = "" +
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n" +
|
||||
"\n" +
|
||||
"<protocols version=\"1.0\" author=\"jaysunxiao\">\n" +
|
||||
"\n" +
|
||||
" <protocol id=\"0\" location=\"com.zfoo.test.CM_Int\"/>\n" +
|
||||
" <protocol id=\"1\" location=\"com.zfoo.test.SM_Int\"/>\n" +
|
||||
" <protocol id=\"2\" location=\"com.zfoo.test.CM_Float\"/>\n" +
|
||||
"\n" +
|
||||
"</protocols>";
|
||||
|
||||
private static final String XML_OF_STANDARD_TEXT =
|
||||
"<protocols version=\"1.0\" author=\"jaysunxiao\">\n" +
|
||||
"\n" +
|
||||
" <protocol id=\"0\" location=\"com.zfoo.test.CM_Int\"/>\n" +
|
||||
" <protocol id=\"1\" location=\"com.zfoo.test.SM_Int\"/>\n" +
|
||||
" <protocol id=\"2\" location=\"com.zfoo.test.CM_Float\"/>\n" +
|
||||
"\n" +
|
||||
"</protocols>";
|
||||
|
||||
@Test
|
||||
public void testXmlWithHead() {
|
||||
Protos protos = DomUtils.string2Object(XML_WITH_HEAD, Protos.class);
|
||||
Assert.assertEquals("jaysunxiao", protos.getAuthor());
|
||||
Assert.assertEquals("1.0", protos.getVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXmlOfStandardText() {
|
||||
Protos protos = DomUtils.string2Object(XML_OF_STANDARD_TEXT, Protos.class);
|
||||
Assert.assertEquals("jaysunxiao", protos.getAuthor());
|
||||
Assert.assertEquals("1.0", protos.getVersion());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@JsonPropertyOrder({"version", "author", "list"})
|
||||
@JacksonXmlRootElement(localName = "protocols")
|
||||
class Protos {
|
||||
@JacksonXmlProperty(isAttribute = true, localName = "version")
|
||||
private String version;
|
||||
@JacksonXmlProperty(isAttribute = true, localName = "author")
|
||||
private String author;
|
||||
@JacksonXmlProperty(localName = "protocol")
|
||||
@JacksonXmlElementWrapper(useWrapping = false)
|
||||
private List<Proto> protos;
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public List<Proto> getProtos() {
|
||||
return protos;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonPropertyOrder({"id", "location"})
|
||||
class Proto {
|
||||
@JacksonXmlProperty(isAttribute = true, localName = "id")
|
||||
private int id;
|
||||
@JacksonXmlProperty(isAttribute = true, localName = "location")
|
||||
private String location;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
|
||||
enum Num {
|
||||
ONE,
|
||||
TWO,
|
||||
THREE;
|
||||
}
|
||||
|
||||
public class EnumUtilsTest {
|
||||
|
||||
@Test
|
||||
public void isInEnumsTest() {
|
||||
Assert.assertTrue(EnumUtils.isInEnums("ONE", new Num[]{Num.ONE, Num.TWO, Num.THREE}));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util;
|
||||
|
||||
import com.zfoo.protocol.util.FileUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class FileUtilTest {
|
||||
|
||||
@Test
|
||||
public void absPathTest() {
|
||||
var absPath = FileUtils.getProAbsPath();
|
||||
System.out.println(absPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createFile() throws IOException {
|
||||
FileUtils.createFile(FileUtils.getProAbsPath() + File.separator + "hello", "hhh");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteFile() {
|
||||
FileUtils.deleteFile(new File(FileUtils.getProAbsPath() + File.separator + "hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeFile() {
|
||||
FileUtils.writeStringToFile(new File(FileUtils.getProAbsPath() + File.separator + "test.txt"), "hello world!");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void readFile() {
|
||||
String str = FileUtils.readFileToString(new File(FileUtils.getProAbsPath() + File.separator + "test.txt"));
|
||||
System.out.println(str);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getProjectPath() {
|
||||
System.out.println(FileUtils.getProAbsPath());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void searchFile() {
|
||||
FileUtils.searchFileInProject(new File(FileUtils.getProAbsPath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllFiles() {
|
||||
List<File> list = FileUtils.getAllReadableFiles(new File(FileUtils.getProAbsPath()));
|
||||
for (File file : list) {
|
||||
System.out.println(file.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void searchFileInProject() {
|
||||
System.out.println(FileUtils.searchFileInProject("User"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util;
|
||||
|
||||
import com.zfoo.protocol.model.Triple;
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
|
||||
|
||||
public class JsonUtilTest {
|
||||
|
||||
public static String id = "\"id\":\"1000\"";
|
||||
public static String name = "\"name\":\"jaysunxiao\"";
|
||||
public static String sex = "\"sex\":\"man\"";
|
||||
public static String age = "\"age\":22";
|
||||
public static String list = "\"list\":[1,2,3]";
|
||||
public static String map = "\"map\":{\"1\":1,\"2\":2,\"3\":3}";
|
||||
|
||||
public static String userJson = "{" + id + "," + name + "," + sex + ","
|
||||
+ age + "," + list + "," + map + "}";
|
||||
|
||||
@Test
|
||||
public void string2Object() {
|
||||
User user = JsonUtils.string2Object(userJson, User.class);
|
||||
Assert.assertEquals(user.getId(), "1000");
|
||||
Assert.assertEquals(user.getName(), "jaysunxiao");
|
||||
Assert.assertEquals(user.getSex(), "man");
|
||||
Assert.assertEquals(user.getList().size(), 3);
|
||||
Assert.assertEquals(user.getMap().size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void object2String() {
|
||||
User user = new User();
|
||||
user.setId("1000");
|
||||
user.setName("jaysunxiao");
|
||||
user.setSex("man");
|
||||
user.setAge(22);
|
||||
//数组,链表,list
|
||||
List<Integer> list = new ArrayList<>();
|
||||
list.add(1);
|
||||
list.add(2);
|
||||
list.add(3);
|
||||
user.setList(list);
|
||||
//map
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
map.put(1, 1);
|
||||
map.put(2, 2);
|
||||
map.put(3, 3);
|
||||
user.setMap(map);
|
||||
|
||||
Assert.assertEquals(JsonUtils.object2String(user), userJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string2List() {
|
||||
String str = "[1,2,3]";
|
||||
List<Integer> list = JsonUtils.string2List(str, Integer.class);
|
||||
|
||||
Assert.assertEquals(list.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string2Set() {
|
||||
String str = "[1,2,3]";
|
||||
Set<Integer> set = JsonUtils.string2Set(str, Integer.class);
|
||||
|
||||
Assert.assertEquals(set.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string2Map() {
|
||||
String str = "{\"1\":1,\"2\":2,\"3\":3}";
|
||||
Map<Integer, Integer> map = JsonUtils.string2Map(str, Integer.class, Integer.class);
|
||||
|
||||
Assert.assertEquals(map.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string2Array() {
|
||||
String str = "[1,2,3]";
|
||||
Integer[] list = JsonUtils.string2Array(str, Integer.class);
|
||||
|
||||
Assert.assertEquals(list.length, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeTest() {
|
||||
Assert.assertEquals(JsonUtils.getNode(userJson, "id").asText(), "1000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tripleTest() {
|
||||
var triple = new Triple<String, String, String>("a", "b", "c");
|
||||
var tripleStr = JsonUtils.object2String(triple);
|
||||
var temp = JsonUtils.string2Object(tripleStr, Triple.class);
|
||||
}
|
||||
}
|
||||
|
||||
//@JsonIgnoreProperties({"name", "age"})//可以将它看做是@JsonIgnore的批量操作
|
||||
class User {
|
||||
private String id;
|
||||
//@JsonIgnore//作用在字段或方法上,用来完全忽略被注解的字段和方法对应的属性
|
||||
//@JsonProperty//注意这里必须得有该注解,因为没有提供对应的getId和setId函数,而是其他的getter和setter,防止遗漏该属性
|
||||
private String name;
|
||||
private String sex;
|
||||
private int age;
|
||||
private List<Integer> list;
|
||||
private Map<Integer, Integer> map;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id='" + id + '\'' +
|
||||
", name='" + name + '\'' +
|
||||
", sex='" + sex + '\'' +
|
||||
", age=" + age +
|
||||
", list=" + list +
|
||||
", map=" + map +
|
||||
'}';
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSex() {
|
||||
return sex;
|
||||
}
|
||||
|
||||
public void setSex(String sex) {
|
||||
this.sex = sex;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public List<Integer> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<Integer> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public Map<Integer, Integer> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setMap(Map<Integer, Integer> map) {
|
||||
this.map = map;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util;
|
||||
|
||||
import com.zfoo.util.net.NetUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class NetUtilsTest {
|
||||
|
||||
@Test
|
||||
public void localhostTest() {
|
||||
var localHostStr = NetUtils.getLocalhostStr();
|
||||
var localHost = NetUtils.getLocalhost();
|
||||
Assert.assertEquals(localHostStr, localHost.getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ipv4Test() {
|
||||
var ipLong = NetUtils.ipv4ToLong(NetUtils.LOCAL_LOOPBACK_IP);
|
||||
var ipStr = NetUtils.longToIpv4(ipLong);
|
||||
Assert.assertEquals(ipStr, NetUtils.LOCAL_LOOPBACK_IP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isInnerIpTest() {
|
||||
Assert.assertTrue(NetUtils.isInnerIP(NetUtils.LOCAL_LOOPBACK_IP));
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void isNetCatTest() throws IOException {
|
||||
NetUtils.netCat(NetUtils.LOCAL_LOOPBACK_IP, 9000, new byte[]{1, 2, 3});
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void isUsableLocalPortTest() {
|
||||
System.out.println(NetUtils.isAvailablePort(2181));
|
||||
System.out.println(NetUtils.getAvailablePort(2181));
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void getAllNetworkInterfaceTest() {
|
||||
var set = NetUtils.getAllNetworkInterface();
|
||||
set.stream().forEach(it -> System.out.println(it));
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void getAllAddressTest() {
|
||||
var set = NetUtils.getAllAddress();
|
||||
set.stream().forEach(it -> System.out.println(it));
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void localIpv4sTest() {
|
||||
var set = NetUtils.localIpv4s();
|
||||
set.stream().forEach(it -> System.out.println(it));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util;
|
||||
|
||||
import com.zfoo.protocol.util.ReflectionUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
|
||||
@interface Id {
|
||||
}
|
||||
|
||||
|
||||
@Ignore
|
||||
public class ReflectUtilTest {
|
||||
|
||||
@Test
|
||||
public void testGetFieldsByAnnotation() {
|
||||
Field[] fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(User.class, Id.class);
|
||||
System.out.println(fields.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilterFieldsInClass() {
|
||||
ReflectionUtils.filterFieldsInClass(User.class, new Predicate<Field>() {
|
||||
@Override
|
||||
public boolean test(Field field) {
|
||||
return field != null;
|
||||
}
|
||||
}, new Consumer<Field>() {
|
||||
@Override
|
||||
public void accept(Field field) {
|
||||
System.out.println(field.getName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util;
|
||||
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class StringUtilTest {
|
||||
|
||||
@Test
|
||||
public void formatTest() {
|
||||
String str = StringUtils.format("this is {} for {}", "a", "b");
|
||||
Assert.assertEquals("this is a for b", str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty() {
|
||||
Assert.assertFalse(StringUtils.isEmpty(" "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void capitalize() {
|
||||
String str = "hello world!";
|
||||
Assert.assertEquals(StringUtils.capitalize(str), "Hello world!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unCapitalize() {
|
||||
String str = "Hello world!";
|
||||
Assert.assertEquals(StringUtils.uncapitalize(str), "hello world!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util.captcha;
|
||||
|
||||
import com.zfoo.util.captcha.model.CaptchaFontEnum;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class CaptchaTest {
|
||||
|
||||
@Test
|
||||
public void pngTest() throws Exception {
|
||||
|
||||
for (var font : CaptchaFontEnum.values()) {
|
||||
PngCaptcha pngCaptcha = new PngCaptcha();
|
||||
pngCaptcha.setLength(4);
|
||||
pngCaptcha.setCaptchaFont(font);
|
||||
pngCaptcha.buildCaptcha();
|
||||
System.out.println(pngCaptcha.captcha());
|
||||
pngCaptcha.drawImage(new FileOutputStream(new File(font.name() + ".png")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void gifTest() throws Exception {
|
||||
for (var font : CaptchaFontEnum.values()) {
|
||||
GifCaptcha gifCaptcha = new GifCaptcha();
|
||||
gifCaptcha.setLength(5);
|
||||
gifCaptcha.setCaptchaFont(font);
|
||||
gifCaptcha.buildCaptcha();
|
||||
System.out.println(gifCaptcha.captcha());
|
||||
gifCaptcha.drawImage(new FileOutputStream(new File(font.name() + ".gif")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void arithmeticTest() throws Exception {
|
||||
for (var font : CaptchaFontEnum.values()) {
|
||||
ArithmeticCaptcha specCaptcha = new ArithmeticCaptcha();
|
||||
specCaptcha.setLength(3);
|
||||
specCaptcha.setCaptchaFont(font);
|
||||
specCaptcha.buildCaptcha();
|
||||
System.out.println(specCaptcha.getArithmeticString() + " " + specCaptcha.captcha());
|
||||
specCaptcha.drawImage(new FileOutputStream(new File(font.name() + ".png")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void base64Test() throws Exception {
|
||||
for (var i = 0; i < Long.MAX_VALUE; i++) {
|
||||
GifCaptcha gifCaptcha = new GifCaptcha();
|
||||
gifCaptcha.buildCaptcha();
|
||||
gifCaptcha.toBase64();
|
||||
|
||||
PngCaptcha pngCaptcha = new PngCaptcha();
|
||||
pngCaptcha.buildCaptcha();
|
||||
pngCaptcha.toBase64();
|
||||
|
||||
ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha();
|
||||
arithmeticCaptcha.buildCaptcha();
|
||||
arithmeticCaptcha.toBase64();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util.collection;
|
||||
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class CollectionUtilsTest {
|
||||
|
||||
@Test
|
||||
public void mapRemoveTest() {
|
||||
Map<String, String> map = new ConcurrentHashMap<>();
|
||||
map.put("a", "a");
|
||||
Assert.assertEquals(map.remove("a"), "a");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void collateTest() {
|
||||
var a = new ArrayList<>(List.of(1, 2, 3, 9));
|
||||
var b = new ArrayList<>(List.of(2, 4, 6, 7, 8));
|
||||
var c = CollectionUtils.collate(a, b);
|
||||
var d = List.of(1, 2, 2, 3, 4, 6, 7, 8, 9);
|
||||
Assert.assertArrayEquals(c.toArray(), d.toArray());
|
||||
|
||||
a = new ArrayList<>(List.of(1, 2, 3));
|
||||
b = new ArrayList<>();
|
||||
c = CollectionUtils.collate(a, b);
|
||||
Assert.assertArrayEquals(c.toArray(), a.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listJoinListTest() {
|
||||
var a = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
var b = List.of(11, 22, 33, 44, 55);
|
||||
var c = List.of(1, 2, 3, 4, 5, 6);
|
||||
|
||||
var d = CollectionUtils.listJoinList(true, new Pair<>(2, a), new Pair<>(2, b), new Pair<>(2, c));
|
||||
Assert.assertArrayEquals(d.toArray(), List.of(1, 2, 11, 22, 3, 4, 5, 6, 33, 44, 7, 8, 55, 9).toArray());
|
||||
|
||||
var e = CollectionUtils.listJoinList(false, new Pair<>(2, a), new Pair<>(2, b), new Pair<>(2, c));
|
||||
Assert.assertArrayEquals(e.toArray(), List.of(1, 2, 11, 22, 1, 2, 3, 4, 33, 44, 3, 4, 5, 6, 55, 5, 6, 7, 8, 9).toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subListLastTest() {
|
||||
var list = List.of(1, 2, 3, 4, 5);
|
||||
Assert.assertArrayEquals(CollectionUtils.subListLast(list, 1).toArray(), List.of(5).toArray());
|
||||
Assert.assertArrayEquals(CollectionUtils.subListLast(list, 4).toArray(), List.of(2, 3, 4, 5).toArray());
|
||||
Assert.assertArrayEquals(CollectionUtils.subListLast(list, 5).toArray(), list.toArray());
|
||||
Assert.assertArrayEquals(CollectionUtils.subListLast(list, 6).toArray(), list.toArray());
|
||||
Assert.assertArrayEquals(CollectionUtils.subListLast(list, 100).toArray(), list.toArray());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util.collection;
|
||||
|
||||
import com.zfoo.protocol.collection.tree.GeneralTree;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class GeneralTreeTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
var generalTree = new GeneralTree<String>();
|
||||
generalTree.addNode("a", "hi");
|
||||
generalTree.addNode("a.b", "hello");
|
||||
generalTree.addNode("a.b.c", "world1");
|
||||
generalTree.addNode("a.b.d", "world2");
|
||||
generalTree.addNode("a.b.e", "world3");
|
||||
|
||||
System.out.println(generalTree.getNodeByPath("a.b").getData());
|
||||
System.out.println(generalTree.getNodeByPath("a.b").getChildren());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util.math;
|
||||
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class ConsistentHashTest {
|
||||
|
||||
//待添加入Hash环的服务器列表
|
||||
private static List<Pair<String, String>> servers = List.of(new Pair<>("192.168.0.0:111", "192.168.0.0:111")
|
||||
, new Pair<>("192.168.0.1:111", "192.168.0.1:111"), new Pair<>("192.168.0.2:111", "192.168.0.2:111"));
|
||||
|
||||
private static List<Pair<String, String>> nums = List.of(new Pair<>("1", "1"), new Pair<>("2", "2"), new Pair<>("3", "3"));
|
||||
|
||||
private static List<Pair<String, String>> chars = List.of(new Pair<>("a", "a"), new Pair<>("b", "b"), new Pair<>("c", "c"));
|
||||
|
||||
|
||||
@Test
|
||||
public void consistentHashTest() {
|
||||
test(servers);
|
||||
System.out.println(StringUtils.MULTIPLE_HYPHENS);
|
||||
test(nums);
|
||||
System.out.println(StringUtils.MULTIPLE_HYPHENS);
|
||||
test(chars);
|
||||
System.out.println(StringUtils.MULTIPLE_HYPHENS);
|
||||
}
|
||||
|
||||
public void test(List<Pair<String, String>> list) {
|
||||
var realNodeMap = new HashMap<String, Integer>();
|
||||
var hitNodeMap = new HashMap<Integer, String>();
|
||||
|
||||
list.forEach(it -> realNodeMap.put(it.getKey(), 0));
|
||||
|
||||
var consistentHash = new ConsistentHash<>(list, 300);
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
var key = String.valueOf(i);
|
||||
var realNode = consistentHash.getRealNode(key).getKey();
|
||||
|
||||
int nums = realNodeMap.get(realNode);
|
||||
realNodeMap.put(realNode, ++nums);
|
||||
hitNodeMap.put(i, realNode);
|
||||
}
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
for (int j = 0; j < 100000; j++) {
|
||||
var key = String.valueOf(j);
|
||||
var realNode = consistentHash.getRealNode(key).getKey();
|
||||
Assert.assertEquals(realNode, hitNodeMap.get(j));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(JsonUtils.object2String(realNodeMap));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testTreeMap() {
|
||||
var treeMap = new TreeMap<Integer, String>();
|
||||
treeMap.put(1, "a");
|
||||
treeMap.put(100, "b");
|
||||
treeMap.put(200, "c");
|
||||
|
||||
System.out.println(treeMap.ceilingEntry(8));
|
||||
System.out.println(treeMap.ceilingEntry(450));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util.math.dfa;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class WordTreeTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
WordTree tree = new WordTree();
|
||||
tree.addWord("大");
|
||||
tree.addWord("大土豆");
|
||||
tree.addWord("土豆");
|
||||
tree.addWord("刚出锅");
|
||||
tree.addWord("出锅");
|
||||
tree.addWord("fuck");
|
||||
|
||||
// 正文
|
||||
String text = "text asdff asdf afucksdf ";
|
||||
List<String> matchAll = tree.matchAll(text, -1, true, false);
|
||||
System.out.println(matchAll);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util.math.lexer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class LexicalAnalysisTest {
|
||||
private static final String str = " void main() {\n" +
|
||||
" if (a == 8) {\n" +
|
||||
" c = c +2;\n" +
|
||||
" b = 1.2;\n" +
|
||||
" c = c + 3;\n" +
|
||||
" d = \"hello world\";\n" +
|
||||
" }\n" +
|
||||
" }";
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
var lexical = new LexicalAnalysis();
|
||||
lexical.analyze(str.toCharArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util.security;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class AesUtilsTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String passWord = "hello world";
|
||||
String encodePassWorld = AesUtils.getEncryptString(passWord);
|
||||
Assert.assertEquals(passWord, AesUtils.getDecryptString(encodePassWorld));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util.security;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class IdUtilsTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
var atomicInteger = new AtomicInteger(Integer.MAX_VALUE);
|
||||
atomicInteger.getAndIncrement();
|
||||
Assert.assertEquals(atomicInteger.get(), Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util.security;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class MD5UtilsTest {
|
||||
|
||||
@Test
|
||||
public void md5Test() {
|
||||
var str = "qwerasdfzxcv1234;:'";
|
||||
Assert.assertEquals(MD5Utils.bytesToMD5(str.getBytes()).toLowerCase(), DigestUtils.md5DigestAsHex(str.getBytes()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.util.security;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class ZipTest {
|
||||
|
||||
// Zip算法压缩测试
|
||||
@Test
|
||||
public void test() {
|
||||
String str = "ZIP,是一个文件的压缩的算法,原名Deflate(真空),发明者为菲利普·卡兹(Phil Katz))," +
|
||||
"他于1989年1月公布了该格式的资料。ZIP通常使用后缀名“.zip”,它的MIME格式为 application/zip 。" +
|
||||
"目前,ZIP格式属于几种主流的压缩格式之一,其竞争者包括RAR格式以及开放源码的7-Zip格式。" +
|
||||
"从性能上比较,RAR格式较ZIP格式压缩率较高,但是它的压缩时间远远高于Zip。" +
|
||||
"而7-Zip(7z)由于提供了免费的压缩工具而逐渐在更多的领域得到应用。";
|
||||
|
||||
byte[] bytes = str.getBytes();
|
||||
|
||||
// 压缩前数组长度
|
||||
Assert.assertEquals(bytes.length, 555);
|
||||
|
||||
bytes = ZipUtils.zip(bytes);
|
||||
// 压缩后数组长度
|
||||
Assert.assertEquals(bytes.length, 438);
|
||||
|
||||
bytes = ZipUtils.unZip(bytes);
|
||||
|
||||
Assert.assertEquals(new String(bytes), str);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user