|
|
@@ -0,0 +1,145 @@
|
|
|
+package com.yango.javaailangchain4j.service;
|
|
|
+
|
|
|
+import freemarker.core.TemplateClassResolver;
|
|
|
+import freemarker.template.Configuration;
|
|
|
+import freemarker.template.ObjectWrapper;
|
|
|
+import freemarker.template.TemplateException;
|
|
|
+import freemarker.template.TemplateExceptionHandler;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
+import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+class TemplateProcessingServiceTest {
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private Configuration freemarkerConfig;
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ private TemplateProcessingService templateProcessingService;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ MockitoAnnotations.openMocks(this);
|
|
|
+
|
|
|
+ // 设置更多 Configuration 属性,避免执行时报错
|
|
|
+ when(freemarkerConfig.getIncompatibleImprovements())
|
|
|
+ .thenReturn(Configuration.VERSION_2_3_32);
|
|
|
+ // 自动检测命名约定
|
|
|
+ when(freemarkerConfig.getNamingConvention())
|
|
|
+ .thenReturn(Configuration.AUTO_DETECT_NAMING_CONVENTION);
|
|
|
+
|
|
|
+ when(freemarkerConfig.getLocale()).thenReturn(java.util.Locale.getDefault());
|
|
|
+ when(freemarkerConfig.getTemplateExceptionHandler())
|
|
|
+ .thenReturn(TemplateExceptionHandler.RETHROW_HANDLER);
|
|
|
+
|
|
|
+ // 设置必要的配置避免执行时报错
|
|
|
+ when(freemarkerConfig.getObjectWrapper())
|
|
|
+ .thenReturn(ObjectWrapper.DEFAULT_WRAPPER);
|
|
|
+ when(freemarkerConfig.getDefaultEncoding())
|
|
|
+ .thenReturn("UTF-8");
|
|
|
+ when(freemarkerConfig.getWrapUncheckedExceptions())
|
|
|
+ .thenReturn(true);
|
|
|
+ when(freemarkerConfig.getFallbackOnNullLoopVariable())
|
|
|
+ .thenReturn(false);
|
|
|
+ when(freemarkerConfig.getNewBuiltinClassResolver())
|
|
|
+ .thenReturn(TemplateClassResolver.SAFER_RESOLVER);
|
|
|
+ when(freemarkerConfig.getNumberFormat())
|
|
|
+ .thenReturn("0.##");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProcessTemplateWithCustomTemplateName() throws IOException, TemplateException {
|
|
|
+ // 准备测试数据
|
|
|
+ String templateString = "Hello ${name}!";
|
|
|
+ Map<String, Object> templateData = new HashMap<>();
|
|
|
+ templateData.put("name", "World");
|
|
|
+ String templateName = "testTemplate";
|
|
|
+
|
|
|
+ // 执行测试
|
|
|
+ String result = templateProcessingService.processTemplate(templateString, templateData, templateName);
|
|
|
+
|
|
|
+ // 验证结果
|
|
|
+ assertNotNull(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProcessTemplateWithDefaultTemplateName() throws IOException, TemplateException {
|
|
|
+ // 准备测试数据
|
|
|
+ String templateString = "Hello ${name}!";
|
|
|
+ Map<String, Object> templateData = new HashMap<>();
|
|
|
+ templateData.put("name", "World");
|
|
|
+
|
|
|
+ // 执行测试
|
|
|
+ String result = templateProcessingService.processTemplate(templateString, templateData);
|
|
|
+
|
|
|
+ // 验证结果
|
|
|
+ assertNotNull(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProcessTemplateWithMultipleVariables() throws IOException, TemplateException {
|
|
|
+ // 准备测试数据
|
|
|
+ String templateString = "学生${studentName}在${courseName}课程中获得了${score}分";
|
|
|
+ Map<String, Object> templateData = new HashMap<>();
|
|
|
+ templateData.put("studentName", "张三");
|
|
|
+ templateData.put("courseName", "数学");
|
|
|
+ templateData.put("score", 95);
|
|
|
+
|
|
|
+ // 执行测试
|
|
|
+ String result = templateProcessingService.processTemplate(templateString, templateData);
|
|
|
+
|
|
|
+ // 验证结果包含预期内容
|
|
|
+ assertNotNull(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProcessTemplateWithEmptyTemplate() throws IOException, TemplateException {
|
|
|
+ // 准备测试数据
|
|
|
+ String templateString = "";
|
|
|
+ Map<String, Object> templateData = new HashMap<>();
|
|
|
+
|
|
|
+ // 执行测试
|
|
|
+ String result = templateProcessingService.processTemplate(templateString, templateData);
|
|
|
+
|
|
|
+ // 验证结果
|
|
|
+ assertEquals("", result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProcessTemplateWithNullData() throws IOException, TemplateException {
|
|
|
+ // 准备测试数据
|
|
|
+ String templateString = "Static text";
|
|
|
+
|
|
|
+ // 执行测试
|
|
|
+ String result = templateProcessingService.processTemplate(templateString, null);
|
|
|
+
|
|
|
+ // 验证结果
|
|
|
+ assertEquals("Static text", result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProcessTemplateWithNewlines() throws IOException, TemplateException {
|
|
|
+ // 准备测试数据
|
|
|
+ String templateString = "Line 1\nLine 2\n${variable}";
|
|
|
+ Map<String, Object> templateData = new HashMap<>();
|
|
|
+ templateData.put("variable", "Line 3");
|
|
|
+
|
|
|
+ // 执行测试
|
|
|
+ String result = templateProcessingService.processTemplate(templateString, templateData);
|
|
|
+
|
|
|
+ // 验证结果包含换行符
|
|
|
+ assertNotNull(result);
|
|
|
+ assertTrue(result.contains("\n"));
|
|
|
+ }
|
|
|
+}
|