工具类

This commit is contained in:
jiang 2024-08-27 11:27:02 +08:00
parent 8ae40fdb5c
commit 09eca8b799
3 changed files with 435 additions and 230 deletions

View File

@ -22,7 +22,7 @@ import java.util.Properties;
* 文件操作工具类提供对常见文件格式的读写操作
* 支持的文件格式包括PropertiesYMLXMLJSONCIMESVG
*/
public class FileUtils {
public class ConfigFileUtils {
/**
* 读取文件内容为字符串

View File

@ -1,26 +1,36 @@
package com.bonus.common.core.utils;
import com.bonus.common.core.constant.Constants;
import com.bonus.common.core.text.StrFormatter;
import org.springframework.util.AntPathMatcher;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.springframework.util.AntPathMatcher;
import com.bonus.common.core.constant.Constants;
import com.bonus.common.core.text.StrFormatter;
import java.util.UUID;
/**
* 字符串工具类
*
* @author bonus
*/
public class StringUtils extends org.apache.commons.lang3.StringUtils
{
/** 空字符串 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
private static final String EMPTY_STRING = "";
/**
* 空字符串
*/
private static final String NULLSTR = "";
/** 下划线 */
/**
* 下划线
*/
private static final char SEPARATOR = '_';
/** 星号 */
/**
* 星号
*/
private static final char ASTERISK = '*';
/**
@ -29,8 +39,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param value defaultValue 要判断的value
* @return value 返回值
*/
public static <T> T nvl(T value, T defaultValue)
{
public static <T> T nvl(T value, T defaultValue) {
return value != null ? value : defaultValue;
}
@ -40,8 +49,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param coll 要判断的Collection
* @return true为空 false非空
*/
public static boolean isEmpty(Collection<?> coll)
{
public static boolean isEmpty(Collection<?> coll) {
return isNull(coll) || coll.isEmpty();
}
@ -51,8 +59,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param coll 要判断的Collection
* @return true非空 false
*/
public static boolean isNotEmpty(Collection<?> coll)
{
public static boolean isNotEmpty(Collection<?> coll) {
return !isEmpty(coll);
}
@ -60,10 +67,9 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* * 判断一个对象数组是否为空
*
* @param objects 要判断的对象数组
** @return true为空 false非空
* * @return true为空 false非空
*/
public static boolean isEmpty(Object[] objects)
{
public static boolean isEmpty(Object[] objects) {
return isNull(objects) || (objects.length == 0);
}
@ -73,8 +79,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param objects 要判断的对象数组
* @return true非空 false
*/
public static boolean isNotEmpty(Object[] objects)
{
public static boolean isNotEmpty(Object[] objects) {
return !isEmpty(objects);
}
@ -84,8 +89,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param map 要判断的Map
* @return true为空 false非空
*/
public static boolean isEmpty(Map<?, ?> map)
{
public static boolean isEmpty(Map<?, ?> map) {
return isNull(map) || map.isEmpty();
}
@ -95,8 +99,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param map 要判断的Map
* @return true非空 false
*/
public static boolean isNotEmpty(Map<?, ?> map)
{
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
@ -106,8 +109,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param str String
* @return true为空 false非空
*/
public static boolean isEmpty(String str)
{
public static boolean isEmpty(String str) {
return isNull(str) || NULLSTR.equals(str.trim());
}
@ -117,8 +119,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param str String
* @return true非空串 false空串
*/
public static boolean isNotEmpty(String str)
{
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
@ -128,8 +129,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param object Object
* @return true为空 false非空
*/
public static boolean isNull(Object object)
{
public static boolean isNull(Object object) {
return object == null;
}
@ -139,67 +139,75 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param object Object
* @return true非空 false
*/
public static boolean isNotNull(Object object)
{
public static boolean isNotNull(Object object) {
return !isNull(object);
}
/**
* 判断字符串是否为空白
*
* @param str 字符串
* @return 是否为空白
*/
public static boolean isBlank(String str) {
return str == null || str.trim().isEmpty();
}
/**
* 判断字符串是否不为空白
*
* @param str 字符串
* @return 是否不为空白
*/
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
/**
* * 判断一个对象是否是数组类型Java基本型别的数组
*
* @param object 对象
* @return true是数组 false不是数组
*/
public static boolean isArray(Object object)
{
public static boolean isArray(Object object) {
return isNotNull(object) && object.getClass().isArray();
}
/**
* 去空格
*/
public static String trim(String str)
{
public static String trim(String str) {
return (str == null ? "" : str.trim());
}
/**
* 替换指定字符串的指定区间内字符为"*"
*
* @param str 字符串
* @param str 字符串
* @param startInclude 开始位置包含
* @param endExclude 结束位置不包含
* @param endExclude 结束位置不包含
* @return 替换后的字符串
*/
public static String hide(CharSequence str, int startInclude, int endExclude)
{
if (isEmpty(str))
{
public static String hide(CharSequence str, int startInclude, int endExclude) {
if (isEmpty(str)) {
return NULLSTR;
}
final int strLength = str.length();
if (startInclude > strLength)
{
if (startInclude > strLength) {
return NULLSTR;
}
if (endExclude > strLength)
{
if (endExclude > strLength) {
endExclude = strLength;
}
if (startInclude > endExclude)
{
if (startInclude > endExclude) {
// 如果起始位置大于结束位置不替换
return NULLSTR;
}
final char[] chars = new char[strLength];
for (int i = 0; i < strLength; i++)
{
if (i >= startInclude && i < endExclude)
{
for (int i = 0; i < strLength; i++) {
if (i >= startInclude && i < endExclude) {
chars[i] = ASTERISK;
}
else
{
} else {
chars[i] = str.charAt(i);
}
}
@ -209,28 +217,23 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
/**
* 截取字符串
*
* @param str 字符串
* @param str 字符串
* @param start 开始
* @return 结果
*/
public static String substring(final String str, int start)
{
if (str == null)
{
public static String substring(final String str, int start) {
if (str == null) {
return NULLSTR;
}
if (start < 0)
{
if (start < 0) {
start = str.length() + start;
}
if (start < 0)
{
if (start < 0) {
start = 0;
}
if (start > str.length())
{
if (start > str.length()) {
return NULLSTR;
}
@ -240,43 +243,35 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
/**
* 截取字符串
*
* @param str 字符串
* @param str 字符串
* @param start 开始
* @param end 结束
* @param end 结束
* @return 结果
*/
public static String substring(final String str, int start, int end)
{
if (str == null)
{
public static String substring(final String str, int start, int end) {
if (str == null) {
return NULLSTR;
}
if (end < 0)
{
if (end < 0) {
end = str.length() + end;
}
if (start < 0)
{
if (start < 0) {
start = str.length() + start;
}
if (end > str.length())
{
if (end > str.length()) {
end = str.length();
}
if (start > end)
{
if (start > end) {
return NULLSTR;
}
if (start < 0)
{
if (start < 0) {
start = 0;
}
if (end < 0)
{
if (end < 0) {
end = 0;
}
@ -289,18 +284,14 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param str 要判断的value
* @return 结果
*/
public static boolean hasText(String str)
{
public static boolean hasText(String str) {
return (str != null && !str.isEmpty() && containsText(str));
}
private static boolean containsText(CharSequence str)
{
private static boolean containsText(CharSequence str) {
int strLen = str.length();
for (int i = 0; i < strLen; i++)
{
if (!Character.isWhitespace(str.charAt(i)))
{
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return true;
}
}
@ -317,13 +308,11 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* 转义\ format("this is \\\\{} for {}", "a", "b") - this is \a for b
*
* @param template 文本模板被替换的部分用 {} 表示
* @param params 参数值
* @param params 参数值
* @return 格式化后的文本
*/
public static String format(String template, Object... params)
{
if (isEmpty(params) || isEmpty(template))
{
public static String format(String template, Object... params) {
if (isEmpty(params) || isEmpty(template)) {
return template;
}
return StrFormatter.format(template, params);
@ -335,8 +324,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param link 链接
* @return 结果
*/
public static boolean ishttp(String link)
{
public static boolean ishttp(String link) {
return StringUtils.startsWithAny(link, Constants.HTTP, Constants.HTTPS);
}
@ -344,21 +332,15 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* 判断给定的collection列表中是否包含数组array 判断给定的数组array中是否包含给定的元素value
*
* @param collection 给定的集合
* @param array 给定的数组
* @param array 给定的数组
* @return boolean 结果
*/
public static boolean containsAny(Collection<String> collection, String... array)
{
if (isEmpty(collection) || isEmpty(array))
{
public static boolean containsAny(Collection<String> collection, String... array) {
if (isEmpty(collection) || isEmpty(array)) {
return false;
}
else
{
for (String str : array)
{
if (collection.contains(str))
{
} else {
for (String str : array) {
if (collection.contains(str)) {
return true;
}
}
@ -369,10 +351,8 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
/**
* 驼峰转下划线命名
*/
public static String toUnderScoreCase(String str)
{
if (str == null)
{
public static String toUnderScoreCase(String str) {
if (str == null) {
return null;
}
StringBuilder sb = new StringBuilder();
@ -382,31 +362,23 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
boolean curreCharIsUpperCase = true;
// 下一字符是否大写
boolean nexteCharIsUpperCase = true;
for (int i = 0; i < str.length(); i++)
{
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (i > 0)
{
if (i > 0) {
preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
}
else
{
} else {
preCharIsUpperCase = false;
}
curreCharIsUpperCase = Character.isUpperCase(c);
if (i < (str.length() - 1))
{
if (i < (str.length() - 1)) {
nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
}
if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase)
{
if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
sb.append(SEPARATOR);
}
else if (i != 0 && !preCharIsUpperCase && curreCharIsUpperCase)
{
} else if (i != 0 && !preCharIsUpperCase && curreCharIsUpperCase) {
sb.append(SEPARATOR);
}
sb.append(Character.toLowerCase(c));
@ -418,18 +390,14 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
/**
* 是否包含字符串
*
* @param str 验证字符串
* @param str 验证字符串
* @param strs 字符串组
* @return 包含返回true
*/
public static boolean inStringIgnoreCase(String str, String... strs)
{
if (str != null && strs != null)
{
for (String s : strs)
{
if (str.equalsIgnoreCase(trim(s)))
{
public static boolean inStringIgnoreCase(String str, String... strs) {
if (str != null && strs != null) {
for (String s : strs) {
if (str.equalsIgnoreCase(trim(s))) {
return true;
}
}
@ -443,29 +411,23 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param name 转换前的下划线大写方式命名的字符串
* @return 转换后的驼峰式命名的字符串
*/
public static String convertToCamelCase(String name)
{
public static String convertToCamelCase(String name) {
final String underscoreChar = "_";
StringBuilder result = new StringBuilder();
// 快速检查
if (name == null || name.isEmpty())
{
if (name == null || name.isEmpty()) {
// 没必要转换
return "";
}
else if (!name.contains(underscoreChar))
{
} else if (!name.contains(underscoreChar)) {
// 不含下划线仅将首字母大写
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
// 用下划线将原始字符串分割
String[] camels = name.split("_");
for (String camel : camels)
{
for (String camel : camels) {
// 跳过原始字符串中开头结尾的下换线或双重下划线
if (camel.isEmpty())
{
if (camel.isEmpty()) {
continue;
}
// 首字母大写
@ -479,34 +441,25 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* 驼峰式命名法
* 例如user_name- userName
*/
public static String toCamelCase(String s)
{
if (s == null)
{
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
if (s.indexOf(SEPARATOR) == -1)
{
if (s.indexOf(SEPARATOR) == -1) {
return s;
}
s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
boolean upperCase = false;
for (int i = 0; i < s.length(); i++)
{
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == SEPARATOR)
{
if (c == SEPARATOR) {
upperCase = true;
}
else if (upperCase)
{
} else if (upperCase) {
sb.append(Character.toUpperCase(c));
upperCase = false;
}
else
{
} else {
sb.append(c);
}
}
@ -516,20 +469,16 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
/**
* 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
*
* @param str 指定字符串
* @param str 指定字符串
* @param strs 需要检查的字符串数组
* @return 是否匹配
*/
public static boolean matches(String str, List<String> strs)
{
if (isEmpty(str) || isEmpty(strs))
{
public static boolean matches(String str, List<String> strs) {
if (isEmpty(str) || isEmpty(strs)) {
return false;
}
for (String pattern : strs)
{
if (isMatch(pattern, str))
{
for (String pattern : strs) {
if (isMatch(pattern, str)) {
return true;
}
}
@ -543,67 +492,323 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* ** 表示任意层路径;
*
* @param pattern 匹配规则
* @param url 需要匹配的url
* @return 匹配返回true,否则返回false
* @param url 需要匹配的url
* @return 匹配返回true, 否则返回false
*/
public static boolean isMatch(String pattern, String url)
{
public static boolean isMatch(String pattern, String url) {
AntPathMatcher matcher = new AntPathMatcher();
return matcher.match(pattern, url);
}
@SuppressWarnings("unchecked")
public static <T> T cast(Object obj)
{
public static <T> T cast(Object obj) {
return (T) obj;
}
/**
* 数字左边补齐0使之达到指定长度注意如果数字转换为字符串后长度大于size则只保留 最后size个字符
*
* @param num 数字对象
* @param num 数字对象
* @param size 字符串指定长度
* @return 返回数字的字符串格式该字符串为指定长度
*/
public static final String padl(final Number num, final int size)
{
public static final String padl(final Number num, final int size) {
return padl(num.toString(), size, '0');
}
/**
* 字符串左补齐如果原始字符串s长度大于size则只保留最后size个字符
*
* @param s 原始字符串
* @param s 原始字符串
* @param size 字符串指定长度
* @param c 用于补齐的字符
* @param c 用于补齐的字符
* @return 返回指定长度的字符串由原字符串左补齐或截取得到
*/
public static final String padl(final String s, final int size, final char c)
{
public static final String padl(final String s, final int size, final char c) {
final StringBuilder sb = new StringBuilder(size);
if (s != null)
{
if (s != null) {
final int len = s.length();
if (s.length() <= size)
{
for (int i = size - len; i > 0; i--)
{
if (s.length() <= size) {
for (int i = size - len; i > 0; i--) {
sb.append(c);
}
sb.append(s);
}
else
{
} else {
return s.substring(len - size, len);
}
}
else
{
for (int i = size; i > 0; i--)
{
} else {
for (int i = size; i > 0; i--) {
sb.append(c);
}
}
return sb.toString();
}
/**
* 将字符串转换为大写
*
* @param str 字符串
* @return 大写字符串
*/
public static String toUpperCase(String str) {
return str == null ? null : str.toUpperCase();
}
/**
* 将字符串转换为小写
*
* @param str 字符串
* @return 小写字符串
*/
public static String toLowerCase(String str) {
return str == null ? null : str.toLowerCase();
}
/**
* 检查字符串是否为数字
*
* @param str 字符串
* @return 是否为数字
*/
public static boolean isNumeric(String str) {
if (isEmpty(str)) {
return false;
}
return str.chars().allMatch(Character::isDigit);
}
/**
* 检查字符串是否为字母
*
* @param str 字符串
* @return 是否为字母
*/
public static boolean isAlphabetic(String str) {
if (isEmpty(str)) {
return false;
}
return str.chars().allMatch(Character::isLetter);
}
/**
* 检查字符串是否为字母或数字
*
* @param str 字符串
* @return 是否为字母或数字
*/
public static boolean isAlphanumeric(String str) {
if (isEmpty(str)) {
return false;
}
return str.chars().allMatch(Character::isLetterOrDigit);
}
/**
* 判断字符串是否包含特定子字符串
*
* @param str 字符串
* @param substr 子字符串
* @return 是否包含子字符串
*/
public static boolean contains(String str, String substr) {
if (str == null || substr == null) {
return false;
}
return str.contains(substr);
}
/**
* 字符串反转
*
* @param str 字符串
* @return 反转后的字符串
*/
public static String reverse(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
/**
* 替换字符串中的所有子字符串
*
* @param str 字符串
* @param target 目标子字符串
* @param replacement 替换子字符串
* @return 替换后的字符串
*/
public static String replace(String str, String target, String replacement) {
if (str == null || target == null || replacement == null) {
return str;
}
return str.replace(target, replacement);
}
/**
* 去除字符串中的所有空白字符
*
* @param str 字符串
* @return 去除空白后的字符串
*/
public static String removeWhitespace(String str) {
if (str == null) {
return null;
}
return str.replaceAll("\\s", EMPTY_STRING);
}
/**
* 将字符串分割为数组
*
* @param str 字符串
* @param delimiter 分隔符
* @return 分割后的字符串数组
*/
public static String[] split(String str, String delimiter) {
if (str == null || delimiter == null) {
return new String[0];
}
return str.split(delimiter);
}
/**
* 将字符串数组合并为一个字符串
*
* @param array 字符串数组
* @param delimiter 分隔符
* @return 合并后的字符串
*/
public static String join(String[] array, String delimiter) {
if (array == null || delimiter == null) {
return null;
}
return String.join(delimiter, array);
}
/**
* 字符串首字母大写
*
* @param str 字符串
* @return 首字母大写的字符串
*/
public static String capitalize(String str) {
if (isEmpty(str)) {
return str;
}
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
/**
* 字符串首字母小写
*
* @param str 字符串
* @return 首字母小写的字符串
*/
public static String uncapitalize(String str) {
if (isEmpty(str)) {
return str;
}
return str.substring(0, 1).toLowerCase() + str.substring(1);
}
/**
* 将字符串进行 Base64 编码
*
* @param str 字符串
* @return 编码后的字符串
*/
public static String encodeBase64(String str) {
if (str == null) {
return null;
}
return java.util.Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8));
}
/**
* Base64 编码的字符串解码
*
* @param base64Str Base64 编码字符串
* @return 解码后的字符串
*/
public static String decodeBase64(String base64Str) {
if (base64Str == null) {
return null;
}
byte[] decodedBytes = java.util.Base64.getDecoder().decode(base64Str);
return new String(decodedBytes, StandardCharsets.UTF_8);
}
/**
* 字符串是否以指定后缀结尾
*
* @param str 字符串
* @param suffix 后缀
* @return 是否以指定后缀结尾
*/
public static boolean endsWith(String str, String suffix) {
if (str == null || suffix == null) {
return false;
}
return str.endsWith(suffix);
}
/**
* 字符串是否以指定前缀开头
*
* @param str 字符串
* @param prefix 前缀
* @return 是否以指定前缀开头
*/
public static boolean startsWith(String str, String prefix) {
if (str == null || prefix == null) {
return false;
}
return str.startsWith(prefix);
}
/**
* 移除前缀
*
* @param str 字符串
* @param prefix 前缀
* @return 移除前缀
*/
public static String removePrefix(String str, String prefix) {
return (isBlank(str) || isBlank(prefix) || !str.startsWith(prefix)) ? str : str.substring(prefix.length());
}
/**
* 移除后缀
*
* @param str 字符串
* @param suffix 后缀
* @return 移除后缀
*/
public static String removeSuffix(String str, String suffix) {
return (isBlank(str) || isBlank(suffix) || !str.endsWith(suffix)) ? str : str.substring(0, str.length() - suffix.length());
}
/**
* HTML 转义
*
* @param html html 字符串
* @return 转义字符
*/
public static String escapeHtml(String html) {
return StringUtils.replaceEach(html,
new String[]{"&", "\"", "<", ">", "'"},
new String[]{"&amp;", "&quot;", "&lt;", "&gt;", "&#39;"});
}
/**
* uuid生成
*
* @return uuid
*/
public static String randomUUID() {
return UUID.randomUUID().toString().replace("-", "");
}
}

View File

@ -23,131 +23,131 @@ public class FileUtilsTest {
@Test
public void testReadProperties() throws Exception {
Properties properties = FileUtils.readProperties(propertiesFile);
Properties properties = ConfigFileUtils.readProperties(propertiesFile);
assertEquals("value", properties.getProperty("key"));
assertEquals("anotherValue", properties.getProperty("anotherKey"));
}
@Test
public void testWriteProperties() throws Exception {
Properties properties = FileUtils.readProperties(propertiesFile);
Properties properties = ConfigFileUtils.readProperties(propertiesFile);
properties.setProperty("anotherKey1", "anotherValue1");
FileUtils.writeProperties(propertiesFile, properties);
Properties loadedProperties = FileUtils.readProperties(propertiesFile);
ConfigFileUtils.writeProperties(propertiesFile, properties);
Properties loadedProperties = ConfigFileUtils.readProperties(propertiesFile);
assertEquals("value", loadedProperties.getProperty("key"));
}
@Test
public void testReadYml() throws Exception {
Map<String, Object> ymlData = FileUtils.readYml(ymlFile);
Map<String, Object> ymlData = ConfigFileUtils.readYml(ymlFile);
assertEquals("value", ymlData.get("key"));
assertEquals("anotherValue", ymlData.get("anotherKey"));
}
@Test
public void testWriteYml() throws Exception {
Map<String, Object> ymlData = FileUtils.readYml(ymlFile);
Map<String, Object> ymlData = ConfigFileUtils.readYml(ymlFile);
//Map<String, Object> ymlData = Collections.singletonMap("key", "value");
ymlData.put("anotherKey1", "anotherValue1");
FileUtils.writeYml(ymlFile, ymlData);
Map<String, Object> loadedYmlData = FileUtils.readYml(ymlFile);
ConfigFileUtils.writeYml(ymlFile, ymlData);
Map<String, Object> loadedYmlData = ConfigFileUtils.readYml(ymlFile);
assertEquals("value", loadedYmlData.get("key"));
}
@Test
public void testReadXml() throws Exception {
Document document = FileUtils.readXml(xmlFile);
Document document = ConfigFileUtils.readXml(xmlFile);
assertNotNull(document.getElementsByTagName("key").item(0));
assertEquals("value", document.getElementsByTagName("key").item(0).getTextContent());
}
@Test
public void testWriteXml() throws Exception {
Document document = FileUtils.readXml(xmlFile);
Document document = ConfigFileUtils.readXml(xmlFile);
document.getDocumentElement().setAttribute("newAttribute", "newValue");
FileUtils.writeXml(xmlFile, document);
ConfigFileUtils.writeXml(xmlFile, document);
Document updatedDocument = FileUtils.readXml(xmlFile);
Document updatedDocument = ConfigFileUtils.readXml(xmlFile);
assertEquals("newValue", updatedDocument.getDocumentElement().getAttribute("newAttribute"));
}
@Test
public void testReadJson() throws Exception {
Map<String, Object> jsonData = FileUtils.readJson(jsonFile, Map.class);
Map<String, Object> jsonData = ConfigFileUtils.readJson(jsonFile, Map.class);
assertEquals("value", jsonData.get("key"));
assertEquals("anotherValue", jsonData.get("anotherKey"));
}
@Test
public void testWriteJson() throws Exception {
Map<String, Object> jsonData = FileUtils.readJson(jsonFile, Map.class);
Map<String, Object> jsonData = ConfigFileUtils.readJson(jsonFile, Map.class);
jsonData.put("anotherKey1", "anotherValue1");
FileUtils.writeJson(jsonFile, jsonData);
Map<String, Object> loadedJsonData = FileUtils.readJson(jsonFile, Map.class);
ConfigFileUtils.writeJson(jsonFile, jsonData);
Map<String, Object> loadedJsonData = ConfigFileUtils.readJson(jsonFile, Map.class);
assertEquals("value", loadedJsonData.get("key"));
}
@Test
public void testReadTxt() throws Exception {
String content = FileUtils.readTxt(txtFile);
String content = ConfigFileUtils.readTxt(txtFile);
assertEquals("This is a test text file.", content);
}
@Test
public void testWriteTxt() throws Exception {
String newContent = "This is a test text file.";
FileUtils.writeTxt(txtFile, newContent);
ConfigFileUtils.writeTxt(txtFile, newContent);
String updatedContent = FileUtils.readTxt(txtFile);
String updatedContent = ConfigFileUtils.readTxt(txtFile);
assertEquals(newContent, updatedContent);
}
@Test
public void testReadSvg() throws Exception {
String content = FileUtils.readSvg(svgFile);
String content = ConfigFileUtils.readSvg(svgFile);
assertEquals("<svg>This is a test SVG file.</svg>", content);
}
@Test
public void testWriteSvg() throws Exception {
String newContent = "<svg>This is a test SVG file.</svg>";
FileUtils.writeSvg(svgFile, newContent);
ConfigFileUtils.writeSvg(svgFile, newContent);
String updatedContent = FileUtils.readSvg(svgFile);
String updatedContent = ConfigFileUtils.readSvg(svgFile);
assertEquals(newContent, updatedContent);
}
@Test
public void testReadCime() throws Exception {
String content = FileUtils.readCime(cimeFile);
String content = ConfigFileUtils.readCime(cimeFile);
assertEquals("This is a test CIME file.", content);
}
@Test
public void testWriteCime() throws Exception {
String newContent = "This is a test CIME file.";
FileUtils.writeCime(cimeFile, newContent);
ConfigFileUtils.writeCime(cimeFile, newContent);
String updatedContent = FileUtils.readCime(cimeFile);
String updatedContent = ConfigFileUtils.readCime(cimeFile);
assertEquals(newContent, updatedContent);
}
@Test
public void testReadPropertyByKey() throws Exception {
String value = FileUtils.readPropertyByKey(propertiesFile, "key");
String value = ConfigFileUtils.readPropertyByKey(propertiesFile, "key");
assertEquals("value", value);
}
@Test
public void testReadYmlByKey() throws Exception {
Object value = FileUtils.readYmlByKey(ymlFile, "key");
Object value = ConfigFileUtils.readYmlByKey(ymlFile, "key");
assertEquals("value", value);
}
@Test
public void testReadJsonByKey() throws Exception {
Object value = FileUtils.readJsonByKey(jsonFile, "key");
Object value = ConfigFileUtils.readJsonByKey(jsonFile, "key");
assertEquals("value", value);
}
}