发表于: 2025-06-15 20:41:38

0 26


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)

UserServiceTest

.

import org.example.model.User;
import org.example.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceTest {

private UserService userService;

@Before
   public void init() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
userService = ctx.getBean(UserService.class);
}

@Test
   public void testAddNodesWithAllFields() {
// 1. 添加根节点 (level=0)
       User root = new User();
root.setPid(0L); // 父节点ID,根节点为0
       root.setLevel(0L); // 层级:0-一级节点
       root.setName("根节点"); // 节点名称
       root.setState(String.valueOf(1)); // 状态:1-启用
       root.setCreateAt(System.currentTimeMillis()); // 创建时间
       root.setUpdateAt(System.currentTimeMillis()); // 更新时间
       root.setAuthor("admin"); // 创建人
       root.setSort(1L); // 排序号

       Long rootId = userService.create(root);
System.out.println("添加根节点成功,ID=" + rootId);

// 2. 添加二级节点 (level=1)
       User second = new User();
second.setPid(rootId); // 父节点为刚创建的根节点
       second.setLevel(1L); // 层级:1-二级节点
       second.setName("二级节点"); // 节点名称
       second.setState(String.valueOf(1)); // 状态:1-启用
       second.setCreateAt(System.currentTimeMillis());
second.setUpdateAt(System.currentTimeMillis());
second.setAuthor("manager"); // 创建人
       second.setSort(1L); // 排序号

       Long secondId = userService.create(second);
System.out.println("添加二级节点成功,ID=" + secondId);

// 3. 添加三级节点 (level=2)
       User third = new User();
third.setPid(secondId); // 父节点为刚创建的二级节点
       third.setLevel(2L); // 层级:2-三级节点
       third.setName("三级节点"); // 节点名称
       third.setState(String.valueOf(1)); // 状态:1-启用
       third.setCreateAt(System.currentTimeMillis());
third.setUpdateAt(System.currentTimeMillis());
third.setAuthor("user"); // 创建人
       third.setSort(1L); // 排序号

       // 三级节点特有字段
       third.setIntroduce("这是三级节点的详细描述信息"); // 描述
       third.setImage("/images/product001.jpg"); // 图片路径
       third.setUrl("http://example.com/product001"); // 链接
       third.setPicture("/images/detail001.jpg"); // 详情图片

       Long thirdId = userService.create(third);
System.out.println("添加三级节点成功,ID=" + thirdId) ;
}

@Test
   public void testQueryNode() {
String nodename = ("新名称"); // 测试查询ID=1的节点
       User node = userService.getByName(nodename);

if (node == null) {
System.out.println("没有找到该节点.");
return;
}

System.out.println("ID的节点信息为: " + node);

// 如果是三级节点,额外显示特有字段
       if (node.getLevel() == 2) {
System.out.println("特有字段 -> 描述: " + node.getIntroduce()
+ ", 图片: " + node.getImage());
}
}
@Test
   public void testDeleteNode() {
Long nodeId = 3L; // 测试删除ID=3的节点
       if (userService.deleteById(nodeId)) {
System.out.println("删除节点成功");
} else {
System.out.println("删除失败(节点不存在或有子节点)");
}
}

@Test
   public void testUpdateNode() {
User node = new User();
node.setId(1L); // 要修改的节点ID
       node.setName("新名称");
node.setSort(2L);
node.setAuthor("新作者");
node.setUpdateAt(System.currentTimeMillis());

if (userService.update(node)) {
System.out.println("更新成功");
} else {
System.out.println("更新失败");
}
}
}


明天计划的事情:(一定要写非常细致的内容)
遇到的问题:(遇到什么困难,怎么解决的)
收获:(通过今天的学习,学到了什么知识)


返回列表 返回列表
评论

    分享到