Files
plusone-commons/src/test/java/xyz/zhouxy/plusone/commons/util/TreeBuilderTests.java

235 lines
8.7 KiB
Java
Raw Normal View History

2024-10-12 00:57:35 +08:00
/*
* Copyright 2023-2025 the original author or authors.
2024-10-12 00:57:35 +08:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2023-06-04 00:00:00 +08:00
package xyz.zhouxy.plusone.commons.util;
2024-12-24 11:35:02 +08:00
import static org.junit.jupiter.api.Assertions.assertEquals;
2024-10-09 18:46:18 +08:00
import java.io.Serializable;
2024-12-24 11:35:02 +08:00
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
2023-06-04 00:00:00 +08:00
import java.util.List;
2024-12-24 11:35:02 +08:00
import java.util.Map;
2023-06-04 00:00:00 +08:00
import java.util.Optional;
2024-10-09 18:46:18 +08:00
import java.util.stream.Collectors;
2023-06-04 00:00:00 +08:00
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2024-12-24 11:35:02 +08:00
import com.google.common.collect.ImmutableList;
2023-06-04 00:00:00 +08:00
import com.google.common.collect.Lists;
import com.google.gson.Gson;
2023-06-04 00:00:00 +08:00
2024-10-09 18:46:18 +08:00
import cn.hutool.core.util.ObjectUtil;
2024-12-24 11:35:02 +08:00
import lombok.EqualsAndHashCode;
2023-06-04 00:00:00 +08:00
import lombok.ToString;
class TreeBuilderTests {
private static final Logger log = LoggerFactory.getLogger(TreeBuilderTests.class);
2024-12-24 11:35:02 +08:00
private final MenuItem A = MenuItem.of("A", "首页", "/home", 1);
private final MenuList B = MenuList.of("B", "系统管理", 3);
private final MenuItem B001 = /**/MenuItem.of("B", "B001", "功能管理", "/sys/function-mgmt", 4);
private final MenuItem B002 = /**/MenuItem.of("B", "B002", "角色管理", "/sys/role-mgmt", 3);
private final MenuItem B003 = /**/MenuItem.of("B", "B003", "账号管理", "/sys/account-mgmt", 2);
private final MenuItem B004 = /**/MenuItem.of("B", "B004", "系统参数管理", "/sys/param-mgmt", 1);
private final MenuList C = MenuList.of("C", "一级菜单C", 2);
private final MenuList C1 = /**/MenuList.of("C", "C1", "二级菜单C1", 3);
private final MenuItem C1001 = /**//**/MenuItem.of("C1", "C1001", "三级菜单C1001", "/c/c1/c1001", 1);
private final MenuItem C1002 = /**//**/MenuItem.of("C1", "C1002", "三级菜单C1002", "/c/c1/c1002", 2);
private final MenuItem C2 = /**/MenuItem.of("C", "C2", "二级菜单C2", "/c/c2", 1);
private final MenuItem C3 = /**/MenuItem.of("C", "C3", "二级菜单C3", "/c/c3", 2);
private final List<Menu> menus = ImmutableList.of(B, C1002, A, B004, C3, B001, B003, C1, C1001, B002, C, C2);
private final TreeBuilder<Menu, MenuList, String> treeBuilder = new TreeBuilder<>(
Menu::getMenuCode,
2024-04-16 21:06:02 +08:00
menu -> Optional.ofNullable(menu.parentMenuCode),
MenuList::addChild,
Comparator.comparing(Menu::getOrderNum));
2023-06-04 00:00:00 +08:00
@Test
2024-12-24 11:35:02 +08:00
void testBuildTreeAndSortedByOrderNum() {
List<Menu> clonedMenus = menus.stream().map(ObjectUtil::clone).collect(Collectors.toList());
2024-10-09 18:46:18 +08:00
List<Menu> menuTreeSortedByOrderNum = treeBuilder.buildTree(clonedMenus);
2024-04-16 21:06:02 +08:00
log.info("menuTreeSortedByOrderNum: {}", new Gson().toJson(menuTreeSortedByOrderNum));
assertEquals(
clonedMenus.stream()
2024-12-24 11:35:02 +08:00
.filter(menu -> menu.getParentMenuCode() == null)
.sorted(Comparator.comparing(Menu::getOrderNum))
2024-12-24 11:35:02 +08:00
.collect(Collectors.toList()),
menuTreeSortedByOrderNum);
2024-12-24 11:35:02 +08:00
Map<String, Menu> menuMap = new HashMap<>();
for (Menu element : clonedMenus) {
menuMap.put(element.getMenuCode(), element);
}
assertEquals(
Arrays.stream(new Menu[] { B001, B002, B003, B004 })
.sorted(Comparator.comparing(Menu::getOrderNum))
2024-12-24 11:35:02 +08:00
.collect(Collectors.toList()),
MenuList.class.cast(menuMap.get("B")).children);
2024-12-24 11:35:02 +08:00
assertEquals(
Arrays.stream(new Menu[] { C1, C2, C3 })
.sorted(Comparator.comparing(Menu::getOrderNum))
2024-12-24 11:35:02 +08:00
.collect(Collectors.toList()),
MenuList.class.cast(menuMap.get("C")).children);
2024-12-24 11:35:02 +08:00
assertEquals(
Arrays.stream(new Menu[] { C1001, C1002 })
.sorted(Comparator.comparing(Menu::getOrderNum))
2024-12-24 11:35:02 +08:00
.collect(Collectors.toList()),
MenuList.class.cast(menuMap.get("C1")).children);
2024-12-24 11:35:02 +08:00
}
@Test
void testBuildTreeAndSortedByMenuCode() {
List<Menu> clonedMenus;
clonedMenus = menus.stream().map(ObjectUtil::clone).collect(Collectors.toList());
List<Menu> menuTreeSortedByMenuCode = treeBuilder
.buildTree(clonedMenus, Comparator.comparing(Menu::getMenuCode));
2024-04-16 21:06:02 +08:00
log.info("menuTreeSortedByMenuCode: {}", new Gson().toJson(menuTreeSortedByMenuCode));
2023-06-04 00:00:00 +08:00
assertEquals(
clonedMenus.stream()
2024-12-24 11:35:02 +08:00
.filter(menu -> menu.getParentMenuCode() == null)
.sorted(Comparator.comparing(Menu::getMenuCode))
2024-12-24 11:35:02 +08:00
.collect(Collectors.toList()),
menuTreeSortedByMenuCode);
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
Map<String, Menu> menuMap = new HashMap<>();
for (Menu element : clonedMenus) {
menuMap.put(element.getMenuCode(), element);
}
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
assertEquals(ImmutableList.of(B001, B002, B003, B004),
MenuList.class.cast(menuMap.get("B")).children);
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
assertEquals(ImmutableList.of(C1, C2, C3),
MenuList.class.cast(menuMap.get("C")).children);
2024-04-16 21:06:02 +08:00
2024-12-24 11:35:02 +08:00
assertEquals(ImmutableList.of(C1001, C1002),
MenuList.class.cast(menuMap.get("C1")).children);
2024-04-16 21:06:02 +08:00
}
2024-10-09 18:46:18 +08:00
2024-12-24 11:35:02 +08:00
@ToString
@EqualsAndHashCode
private static abstract class Menu implements Serializable { // NOSONAR
protected final String parentMenuCode;
protected final String menuCode;
protected final String title;
protected final int orderNum;
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
public Menu(String parentMenuCode, String menuCode, String title, int orderNum) {
this.parentMenuCode = parentMenuCode;
this.menuCode = menuCode;
this.title = title;
this.orderNum = orderNum;
}
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
public String getMenuCode() {
return menuCode;
}
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
public String getParentMenuCode() {
return parentMenuCode;
}
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
public String getTitle() {
return title;
}
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
public int getOrderNum() {
return orderNum;
}
private static final long serialVersionUID = 20240917181424L;
2023-06-04 00:00:00 +08:00
}
2024-10-09 18:46:18 +08:00
2024-12-24 11:35:02 +08:00
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
private static final class MenuItem extends Menu {
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
private final String url;
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
private MenuItem(String parentMenuCode, String menuCode, String title, String url, int orderNum) {
super(parentMenuCode, menuCode, title, orderNum);
this.url = url;
}
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
static MenuItem of(String parentMenuCode, String menuCode, String title, String url, int orderNum) {
return new MenuItem(parentMenuCode, menuCode, title, url, orderNum);
}
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
static MenuItem of(String menuCode, String title, String url, int orderNum) {
return new MenuItem(null, menuCode, title, url, orderNum);
}
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
public String getUrl() {
return url;
}
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
private static final long serialVersionUID = 20240917181910L;
2023-06-04 00:00:00 +08:00
}
2024-12-24 11:35:02 +08:00
@ToString(callSuper = true)
private static final class MenuList extends Menu {
private List<Menu> children;
2023-06-04 00:00:00 +08:00
2024-12-24 11:35:02 +08:00
private MenuList(String parentMenuCode, String menuCode, String title, int orderNum) {
super(parentMenuCode, menuCode, title, orderNum);
}
static MenuList of(String parentMenuCode, String menuCode, String title, int orderNum) {
return new MenuList(parentMenuCode, menuCode, title, orderNum);
}
static MenuList of(String menuCode, String title, int orderNum) {
return new MenuList(null, menuCode, title, orderNum);
2023-06-04 00:00:00 +08:00
}
2024-10-09 18:46:18 +08:00
2024-12-24 11:35:02 +08:00
@SuppressWarnings("unused")
static MenuList of(String menuCode, String title, Iterable<Menu> children, int orderNum) {
return of(null, menuCode, title, children, orderNum);
}
static MenuList of(String parentMenuCode, String menuCode, String title, Iterable<Menu> children,
int orderNum) {
final MenuList instance = of(parentMenuCode, menuCode, title, orderNum);
children.forEach(instance::addChild);
return instance;
}
public void addChild(Menu child) {
if (this.children == null) {
this.children = Lists.newArrayList();
}
this.children.add(child);
}
private static final long serialVersionUID = 20240917181917L;
}
2023-06-04 00:00:00 +08:00
}