Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
f5d16c850f
|
|
@ -2,6 +2,7 @@ package com.bonus.common.core.utils;
|
|||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.w3c.dom.Document;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
|
|
@ -95,7 +96,15 @@ public class FileUtils {
|
|||
* @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)) {
|
||||
yaml.dump(data, new OutputStreamWriter(fos, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import java.text.ParseException;
|
|||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.util.Date;
|
||||
|
||||
|
|
@ -151,10 +152,13 @@ public class DateUtilsTest {
|
|||
Date endOfWeek = DateUtils.getEndOfWeek();
|
||||
assertNotNull(endOfWeek);
|
||||
|
||||
// 检查是否为星期日
|
||||
// 检查是否为星期日,并且时间为 23:59:59
|
||||
LocalDate localDate = LocalDate.now();
|
||||
LocalDate expectedEnd = localDate.with(java.time.DayOfWeek.SUNDAY);
|
||||
assertEquals(DateUtils.toDate(expectedEnd.atTime(23, 59, 59)), endOfWeek);
|
||||
LocalDateTime expectedEnd = localDate.with(java.time.DayOfWeek.SUNDAY).atTime(23, 59, 59);
|
||||
|
||||
// 忽略毫秒部分进行比较
|
||||
LocalDateTime actualEnd = endOfWeek.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().withNano(0);
|
||||
assertEquals(expectedEnd, actualEnd);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -181,14 +185,14 @@ public class DateUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testGetMonthOrDay() {
|
||||
String monthOrDay = DateUtils.getMonthOrDay(32, "/");
|
||||
String monthOrDay = DateUtils.getMonthOrDay(31, "/");
|
||||
assertNotNull(monthOrDay);
|
||||
assertTrue(monthOrDay.matches("\\d{2}/\\d{2}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
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");
|
||||
assertNotNull(iso8601);
|
||||
assertTrue(iso8601.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}"));
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import org.junit.Test;
|
|||
import org.w3c.dom.Document;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
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 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");
|
||||
|
||||
@Test
|
||||
public void testReadProperties() throws Exception {
|
||||
Properties properties = FileUtils.readProperties(propertiesFile);
|
||||
|
|
@ -30,8 +30,8 @@ public class FileUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testWriteProperties() throws Exception {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("key", "value");
|
||||
Properties properties = FileUtils.readProperties(propertiesFile);
|
||||
properties.setProperty("anotherKey1", "anotherValue1");
|
||||
FileUtils.writeProperties(propertiesFile, properties);
|
||||
Properties loadedProperties = FileUtils.readProperties(propertiesFile);
|
||||
assertEquals("value", loadedProperties.getProperty("key"));
|
||||
|
|
@ -46,9 +46,10 @@ public class FileUtilsTest {
|
|||
|
||||
@Test
|
||||
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);
|
||||
|
||||
Map<String, Object> loadedYmlData = FileUtils.readYml(ymlFile);
|
||||
assertEquals("value", loadedYmlData.get("key"));
|
||||
}
|
||||
|
|
@ -80,9 +81,9 @@ public class FileUtilsTest {
|
|||
|
||||
@Test
|
||||
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);
|
||||
|
||||
Map<String, Object> loadedJsonData = FileUtils.readJson(jsonFile, Map.class);
|
||||
assertEquals("value", loadedJsonData.get("key"));
|
||||
}
|
||||
|
|
@ -95,7 +96,7 @@ public class FileUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testWriteTxt() throws Exception {
|
||||
String newContent = "This is updated text content.";
|
||||
String newContent = "This is a test text file.";
|
||||
FileUtils.writeTxt(txtFile, newContent);
|
||||
|
||||
String updatedContent = FileUtils.readTxt(txtFile);
|
||||
|
|
@ -110,7 +111,7 @@ public class FileUtilsTest {
|
|||
|
||||
@Test
|
||||
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);
|
||||
|
||||
String updatedContent = FileUtils.readSvg(svgFile);
|
||||
|
|
@ -125,7 +126,7 @@ public class FileUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testWriteCime() throws Exception {
|
||||
String newContent = "This is updated CIME content.";
|
||||
String newContent = "This is a test CIME file.";
|
||||
FileUtils.writeCime(cimeFile, newContent);
|
||||
|
||||
String updatedContent = FileUtils.readCime(cimeFile);
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
This is updated CIME content.
|
||||
This is a test CIME file.
|
||||
|
|
@ -1,4 +1 @@
|
|||
{
|
||||
"key": "value",
|
||||
"anotherKey": "anotherValue"
|
||||
}
|
||||
{"key":"value","anotherKey":"anotherValue","anotherKey1":"anotherValue1"}
|
||||
|
|
@ -1,2 +1,4 @@
|
|||
key=value
|
||||
#Sat Aug 24 08:51:32 CST 2024
|
||||
anotherKey=anotherValue
|
||||
key=value
|
||||
anotherKey1=anotherValue1
|
||||
|
|
|
|||
|
|
@ -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 |
|
|
@ -1,4 +1,5 @@
|
|||
<root>
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<root newAttribute="newValue">
|
||||
<key>value</key>
|
||||
<anotherKey>anotherValue</anotherKey>
|
||||
</root>
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
key: value
|
||||
anotherKey: anotherValue
|
||||
anotherKey1: anotherValue1
|
||||
|
|
|
|||
Loading…
Reference in New Issue