Merge remote-tracking branch 'origin/main'

This commit is contained in:
weiweiw 2024-08-24 08:55:36 +08:00
commit f5d16c850f
9 changed files with 41 additions and 26 deletions

View File

@ -2,6 +2,7 @@ package com.bonus.common.core.utils;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@ -95,7 +96,15 @@ public class FileUtils {
* @throws IOException 如果写入文件时发生错误 * @throws IOException 如果写入文件时发生错误
*/ */
public static void writeYml(File file, Map<String, Object> data) throws IOException { public static void writeYml(File file, Map<String, Object> data) throws IOException {
Yaml yaml = new Yaml(); // 设置 YAML 的格式化选项
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); // 使用块格式而不是流格式
options.setIndent(2); // 设置缩进
options.setPrettyFlow(true); // 保持格式的美观
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); // 纯文本格式
// 初始化 YAML 实例
Yaml yaml = new Yaml(options);
try (FileOutputStream fos = new FileOutputStream(file)) { try (FileOutputStream fos = new FileOutputStream(file)) {
yaml.dump(data, new OutputStreamWriter(fos, StandardCharsets.UTF_8)); yaml.dump(data, new OutputStreamWriter(fos, StandardCharsets.UTF_8));
} }

View File

@ -6,6 +6,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoField; import java.time.temporal.ChronoField;
import java.util.Date; import java.util.Date;
@ -151,10 +152,13 @@ public class DateUtilsTest {
Date endOfWeek = DateUtils.getEndOfWeek(); Date endOfWeek = DateUtils.getEndOfWeek();
assertNotNull(endOfWeek); assertNotNull(endOfWeek);
// 检查是否为星期日 // 检查是否为星期日并且时间为 23:59:59
LocalDate localDate = LocalDate.now(); LocalDate localDate = LocalDate.now();
LocalDate expectedEnd = localDate.with(java.time.DayOfWeek.SUNDAY); LocalDateTime expectedEnd = localDate.with(java.time.DayOfWeek.SUNDAY).atTime(23, 59, 59);
assertEquals(DateUtils.toDate(expectedEnd.atTime(23, 59, 59)), endOfWeek);
// 忽略毫秒部分进行比较
LocalDateTime actualEnd = endOfWeek.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().withNano(0);
assertEquals(expectedEnd, actualEnd);
} }
@Test @Test
@ -181,14 +185,14 @@ public class DateUtilsTest {
@Test @Test
public void testGetMonthOrDay() { public void testGetMonthOrDay() {
String monthOrDay = DateUtils.getMonthOrDay(32, "/"); String monthOrDay = DateUtils.getMonthOrDay(31, "/");
assertNotNull(monthOrDay); assertNotNull(monthOrDay);
assertTrue(monthOrDay.matches("\\d{2}/\\d{2}")); assertTrue(monthOrDay.matches("\\d{2}/\\d{2}"));
} }
@Test @Test
public void testConvertToISO8601() { public void testConvertToISO8601() {
String dateStr = "2024-08-32 12:00:00"; String dateStr = "2024-08-31 12:00:00";
String iso8601 = DateUtils.convertToISO8601(dateStr, "yyyy-MM-dd HH:mm:ss"); String iso8601 = DateUtils.convertToISO8601(dateStr, "yyyy-MM-dd HH:mm:ss");
assertNotNull(iso8601); assertNotNull(iso8601);
assertTrue(iso8601.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}")); assertTrue(iso8601.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}"));

View File

@ -4,7 +4,6 @@ import org.junit.Test;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import java.io.File; import java.io.File;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -21,6 +20,7 @@ public class FileUtilsTest {
File txtFile = new File("src/test/java/com/bonus/common/core/utils/test.txt"); File txtFile = new File("src/test/java/com/bonus/common/core/utils/test.txt");
File svgFile = new File("src/test/java/com/bonus/common/core/utils/test.svg"); File svgFile = new File("src/test/java/com/bonus/common/core/utils/test.svg");
File cimeFile = new File("src/test/java/com/bonus/common/core/utils/test.cime"); File cimeFile = new File("src/test/java/com/bonus/common/core/utils/test.cime");
@Test @Test
public void testReadProperties() throws Exception { public void testReadProperties() throws Exception {
Properties properties = FileUtils.readProperties(propertiesFile); Properties properties = FileUtils.readProperties(propertiesFile);
@ -30,8 +30,8 @@ public class FileUtilsTest {
@Test @Test
public void testWriteProperties() throws Exception { public void testWriteProperties() throws Exception {
Properties properties = new Properties(); Properties properties = FileUtils.readProperties(propertiesFile);
properties.setProperty("key", "value"); properties.setProperty("anotherKey1", "anotherValue1");
FileUtils.writeProperties(propertiesFile, properties); FileUtils.writeProperties(propertiesFile, properties);
Properties loadedProperties = FileUtils.readProperties(propertiesFile); Properties loadedProperties = FileUtils.readProperties(propertiesFile);
assertEquals("value", loadedProperties.getProperty("key")); assertEquals("value", loadedProperties.getProperty("key"));
@ -46,9 +46,10 @@ public class FileUtilsTest {
@Test @Test
public void testWriteYml() throws Exception { public void testWriteYml() throws Exception {
Map<String, Object> ymlData = Collections.singletonMap("key", "value"); Map<String, Object> ymlData = FileUtils.readYml(ymlFile);
//Map<String, Object> ymlData = Collections.singletonMap("key", "value");
ymlData.put("anotherKey1", "anotherValue1");
FileUtils.writeYml(ymlFile, ymlData); FileUtils.writeYml(ymlFile, ymlData);
Map<String, Object> loadedYmlData = FileUtils.readYml(ymlFile); Map<String, Object> loadedYmlData = FileUtils.readYml(ymlFile);
assertEquals("value", loadedYmlData.get("key")); assertEquals("value", loadedYmlData.get("key"));
} }
@ -80,9 +81,9 @@ public class FileUtilsTest {
@Test @Test
public void testWriteJson() throws Exception { public void testWriteJson() throws Exception {
Map<String, Object> jsonData = Collections.singletonMap("key", "value"); Map<String, Object> jsonData = FileUtils.readJson(jsonFile, Map.class);
jsonData.put("anotherKey1", "anotherValue1");
FileUtils.writeJson(jsonFile, jsonData); FileUtils.writeJson(jsonFile, jsonData);
Map<String, Object> loadedJsonData = FileUtils.readJson(jsonFile, Map.class); Map<String, Object> loadedJsonData = FileUtils.readJson(jsonFile, Map.class);
assertEquals("value", loadedJsonData.get("key")); assertEquals("value", loadedJsonData.get("key"));
} }
@ -95,7 +96,7 @@ public class FileUtilsTest {
@Test @Test
public void testWriteTxt() throws Exception { public void testWriteTxt() throws Exception {
String newContent = "This is updated text content."; String newContent = "This is a test text file.";
FileUtils.writeTxt(txtFile, newContent); FileUtils.writeTxt(txtFile, newContent);
String updatedContent = FileUtils.readTxt(txtFile); String updatedContent = FileUtils.readTxt(txtFile);
@ -110,7 +111,7 @@ public class FileUtilsTest {
@Test @Test
public void testWriteSvg() throws Exception { public void testWriteSvg() throws Exception {
String newContent = "<svg>This is updated SVG content.</svg>"; String newContent = "<svg>This is a test SVG file.</svg>";
FileUtils.writeSvg(svgFile, newContent); FileUtils.writeSvg(svgFile, newContent);
String updatedContent = FileUtils.readSvg(svgFile); String updatedContent = FileUtils.readSvg(svgFile);
@ -125,7 +126,7 @@ public class FileUtilsTest {
@Test @Test
public void testWriteCime() throws Exception { public void testWriteCime() throws Exception {
String newContent = "This is updated CIME content."; String newContent = "This is a test CIME file.";
FileUtils.writeCime(cimeFile, newContent); FileUtils.writeCime(cimeFile, newContent);
String updatedContent = FileUtils.readCime(cimeFile); String updatedContent = FileUtils.readCime(cimeFile);

View File

@ -1 +1 @@
This is updated CIME content. This is a test CIME file.

View File

@ -1,4 +1 @@
{ {"key":"value","anotherKey":"anotherValue","anotherKey1":"anotherValue1"}
"key": "value",
"anotherKey": "anotherValue"
}

View File

@ -1,2 +1,4 @@
#Sat Aug 24 08:51:32 CST 2024
anotherKey=anotherValue
key=value key=value
anotherKey=anotherValue anotherKey1=anotherValue1

View File

@ -1 +1 @@
<svg>This is updated SVG content.</svg> <svg>This is a test SVG file.</svg>

Before

Width:  |  Height:  |  Size: 39 B

After

Width:  |  Height:  |  Size: 35 B

View File

@ -1,4 +1,5 @@
<root> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root newAttribute="newValue">
<key>value</key> <key>value</key>
<anotherKey>anotherValue</anotherKey> <anotherKey>anotherValue</anotherKey>
</root> </root>

View File

@ -1,2 +1,3 @@
key: value key: value
anotherKey: anotherValue anotherKey: anotherValue
anotherKey1: anotherValue1