Ver código fonte

add test case

zhou-hao 7 anos atrás
pai
commit
02a3984a9c

+ 22 - 14
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/TreeSupportEntity.java

@@ -72,19 +72,27 @@ public interface TreeSupportEntity<PK> extends GenericEntity<PK> {
         });
     }
 
-    /**
-     * 将树形结构转为列表结构,并填充对应的数据。<br>
-     * 如树结构数据: {name:'父节点',children:[{name:'子节点1'},{name:'子节点2'}]}<br>
-     * 解析后:[{id:'id1',name:'父节点',path:'<b>aoSt</b>'},{id:'id2',name:'子节点1',path:'<b>aoSt</b>-oS5a'},{id:'id3',name:'子节点2',path:'<b>aoSt</b>-uGpM'}]
-     *
-     * @param parent      树结构的根节点
-     * @param target      目标集合,转换后的数据将直接添加({@link List#add(Object)})到这个集合.
-     * @param <T>         继承{@link TreeSupportEntity}的类型
-     * @param idGenerator ID生成策略
-     * @param <PK>        主键类型
-     */
-    static <T extends TreeSupportEntity<PK>, PK> void expandTree2List(TreeSupportEntity<PK> parent, List<T> target, IDGenerator<PK> idGenerator) {
+    static <T extends TreeSupportEntity<PK>, PK> void expandTree2List(T parent, List<T> target, IDGenerator<PK> idGenerator) {
+        expandTree2List(parent,target,idGenerator,null);
+    }
+        /**
+         * 将树形结构转为列表结构,并填充对应的数据。<br>
+         * 如树结构数据: {name:'父节点',children:[{name:'子节点1'},{name:'子节点2'}]}<br>
+         * 解析后:[{id:'id1',name:'父节点',path:'<b>aoSt</b>'},{id:'id2',name:'子节点1',path:'<b>aoSt</b>-oS5a'},{id:'id3',name:'子节点2',path:'<b>aoSt</b>-uGpM'}]
+         *
+         * @param parent      树结构的根节点
+         * @param target      目标集合,转换后的数据将直接添加({@link List#add(Object)})到这个集合.
+         * @param <T>         继承{@link TreeSupportEntity}的类型
+         * @param idGenerator ID生成策略
+         * @param <PK>        主键类型
+         */
+    static <T extends TreeSupportEntity<PK>, PK> void expandTree2List(T parent, List<T> target, IDGenerator<PK> idGenerator, BiConsumer<T, List<T>> childConsumer) {
+
         List<T> children = parent.getChildren();
+        if(childConsumer!=null){
+            childConsumer.accept(parent,new ArrayList<>());
+        }
+        target.add(parent);
         if (parent.getPath() == null) {
             parent.setPath(RandomUtil.randomChar(4));
             if (parent.getPath() != null) {
@@ -115,8 +123,8 @@ public interface TreeSupportEntity<PK> extends GenericEntity<PK> {
                 child.setParentId(pid);
                 child.setPath(parent.getPath() + "-" + RandomUtil.randomChar(4));
                 child.setLevel(child.getPath().split("-").length);
-                target.add(child);
-                expandTree2List(child, target, idGenerator);
+
+                expandTree2List(child, target, idGenerator,childConsumer);
             }
         }
     }

+ 18 - 0
hsweb-commons/hsweb-commons-entity/src/test/java/org/hswebframework/web/commons/entity/MenuEntity.java

@@ -0,0 +1,18 @@
+package org.hswebframework.web.commons.entity;
+
+import lombok.*;
+
+import java.util.List;
+
+@Getter
+@Setter
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class MenuEntity extends SimpleTreeSortSupportEntity<Integer> {
+    private static final long serialVersionUID = 5548107788893085691L;
+
+    private String name;
+
+    private List<MenuEntity> children;
+}

+ 61 - 0
hsweb-commons/hsweb-commons-entity/src/test/java/org/hswebframework/web/commons/entity/TreeSupportEntityTests.java

@@ -0,0 +1,61 @@
+package org.hswebframework.web.commons.entity;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import org.hswebframework.web.id.IDGenerator;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Predicate;
+
+import static org.junit.Assert.*;
+
+public class TreeSupportEntityTests {
+
+    @Test
+    public void test() {
+        MenuEntity parent = MenuEntity.builder().build();
+        parent.setName("menu-1");
+        parent.setId(1);
+        parent.setParentId(-1);
+
+        MenuEntity m101 = MenuEntity.builder().build();
+        m101.setName("menu-101");
+        m101.setId(101);
+        m101.setParentId(1);
+
+        MenuEntity m102 = MenuEntity.builder().build();
+        m102.setName("menu-102");
+        m102.setId(102);
+        m102.setParentId(1);
+
+        MenuEntity m10201 = MenuEntity.builder().build();
+        m10201.setName("menu-10201");
+        m10201.setId(10201);
+        m10201.setParentId(102);
+
+        //list转为树形结构
+        List<MenuEntity> tree = TreeSupportEntity
+                .list2tree(Arrays.asList(parent, m101, m102, m10201), MenuEntity::setChildren, (Predicate<MenuEntity>) menu -> menu.getParentId().equals(-1));
+
+        Assert.assertEquals(tree.get(0).getChildren().get(0).getId(), Integer.valueOf(101));
+        Assert.assertEquals(tree.get(0).getChildren().get(1).getId(), Integer.valueOf(102));
+
+        Assert.assertEquals(tree.get(0).getChildren().get(1).getChildren().get(0).getId(), Integer.valueOf(10201));
+
+        System.out.println(JSON.toJSONString(tree, SerializerFeature.PrettyFormat));
+
+        List<MenuEntity> list = new ArrayList<>();
+
+        //将树形结构展平为list
+        TreeSupportEntity.expandTree2List(tree.get(0), list, () -> (int) Math.round(Math.random() * 1000000), MenuEntity::setChildren);
+
+        System.out.println(JSON.toJSONString(list, SerializerFeature.PrettyFormat));
+
+        Assert.assertEquals(list.size(), 4);
+
+    }
+}

+ 59 - 0
hsweb-commons/hsweb-commons-entity/src/test/java/org/hswebframework/web/commons/entity/factory/MapperEntityFactoryTests.java

@@ -0,0 +1,59 @@
+package org.hswebframework.web.commons.entity.factory;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+
+import static org.junit.Assert.*;
+
+public class MapperEntityFactoryTests {
+
+
+    @Test
+    public void testCreateEntity() {
+        MapperEntityFactory entityFactory = new MapperEntityFactory();
+
+
+        entityFactory.addMapping(TestEntity.class, entityFactory.initCache(NewTestEntity.class));
+
+        TestEntity entity = entityFactory.newInstance(TestEntity.class);
+
+        Assert.assertEquals(entity.getClass(), NewTestEntity.class);
+
+
+        entity = entityFactory.copyProperties(new HashMap<String, Object>() {
+            private static final long serialVersionUID = 6458422824954290386L;
+
+            {
+                put("name", "张三");
+                put("nickName", "小张");
+            }
+        }, entity);
+
+        Assert.assertEquals(entity.getName(), "张三");
+        Assert.assertEquals(((NewTestEntity) entity).getNickName(), "小张");
+
+
+        entityFactory.addCopier(new CustomPropertyCopier());
+
+        HashMap<String, Object> data = new HashMap<>();
+        data.put("name", "李四");
+        data.put("nickName", "小李");
+        entityFactory.copyProperties(data, entity);
+
+        Assert.assertEquals(entity.getName(), "李四");
+        Assert.assertEquals(((NewTestEntity) entity).getNickName(), "小李");
+
+    }
+
+    class CustomPropertyCopier implements PropertyCopier<HashMap, NewTestEntity> {
+
+        @Override
+        public NewTestEntity copyProperties(HashMap source, NewTestEntity target) {
+            target.setName((String) source.get("name"));
+            target.setNickName((String) source.get("nickName"));
+            return target;
+        }
+    }
+}

+ 11 - 0
hsweb-commons/hsweb-commons-entity/src/test/java/org/hswebframework/web/commons/entity/factory/NewTestEntity.java

@@ -0,0 +1,11 @@
+package org.hswebframework.web.commons.entity.factory;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class NewTestEntity extends TestEntity {
+    private static final long serialVersionUID = -8151514416436801617L;
+    private String nickName;
+}

+ 15 - 0
hsweb-commons/hsweb-commons-entity/src/test/java/org/hswebframework/web/commons/entity/factory/TestEntity.java

@@ -0,0 +1,15 @@
+package org.hswebframework.web.commons.entity.factory;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.hswebframework.web.commons.entity.SimpleGenericEntity;
+
+@Getter
+@Setter
+public class TestEntity extends SimpleGenericEntity<String> {
+    private static final long serialVersionUID = 2468328156748007412L;
+
+    private String name;
+
+
+}