From ee75b2e28f13fa56b4fc595aaf82208b1b3ef7c1 Mon Sep 17 00:00:00 2001 From: timfruit Date: Wed, 17 Mar 2021 23:03:11 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AF=B9SysMenuServiceImpl#CreateMenu?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../permission/SysMenuServiceTest.java | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java new file mode 100644 index 000000000..668ce9b79 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java @@ -0,0 +1,109 @@ +package cn.iocoder.dashboard.modules.system.service.permission; + +import cn.iocoder.dashboard.BaseDbUnitTest; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuCreateReqVO; +import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysMenuDO; +import cn.iocoder.dashboard.modules.system.dal.mysql.permission.SysMenuMapper; +import cn.iocoder.dashboard.modules.system.enums.permission.MenuTypeEnum; +import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysMenuProducer; +import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysPermissionProducer; +import cn.iocoder.dashboard.modules.system.service.permission.impl.SysMenuServiceImpl; +import cn.iocoder.dashboard.util.AssertUtils; +import cn.iocoder.dashboard.util.RandomUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; + +import javax.annotation.Resource; + +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.MENU_PARENT_NOT_DIR_OR_MENU; +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.MENU_PARENT_NOT_EXISTS; +import static cn.iocoder.dashboard.util.RandomUtils.randomLongId; +import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; +import static org.mockito.Mockito.verify; + +@Import(SysMenuServiceImpl.class) +public class SysMenuServiceTest extends BaseDbUnitTest { + + + @Resource + private SysMenuServiceImpl sysMenuService; + + @MockBean + private SysPermissionService sysPermissionService; + + @MockBean + private SysMenuProducer sysMenuProducer; + + @Resource + private SysMenuMapper menuMapper; + + + @Test + public void testCreateMenu_success() { + //构造父目录 + SysMenuDO menuDO = createParentMenuDO(MenuTypeEnum.MENU); + menuMapper.insert(menuDO); + Long parentId = menuDO.getId(); + + //调用 + SysMenuCreateReqVO vo = createReqVO(parentId); + Long menuId = sysMenuService.createMenu(vo); + + //断言 + Assertions.assertNotNull(menuId); + // 校验记录的属性是否正确 + SysMenuDO ret = menuMapper.selectById(menuId); + AssertUtils.assertPojoEquals(vo, ret); + // 校验调用 + verify(sysMenuProducer).sendMenuRefreshMessage(); + } + + @Test + public void testCreateMenu_checkParentExist() { + Long parentId = RandomUtils.randomLongId(); + + // 调用 + SysMenuCreateReqVO vo = createReqVO(parentId); + + // 调用, 并断言异常 + AssertUtils.assertServiceException(() -> sysMenuService.createMenu(vo), MENU_PARENT_NOT_EXISTS); + } + + @Test + public void testCreateMenu_checkParentIsDirOrMenu() { + //构造父目录 + SysMenuDO menuDO = createParentMenuDO(MenuTypeEnum.BUTTON); + menuMapper.insert(menuDO); + Long parentId = menuDO.getId(); + + // 调用 + SysMenuCreateReqVO vo = createReqVO(parentId); + + // 调用, 并断言异常 + AssertUtils.assertServiceException(() -> sysMenuService.createMenu(vo), MENU_PARENT_NOT_DIR_OR_MENU); + } + + + private SysMenuCreateReqVO createReqVO(Long parentId) { + SysMenuCreateReqVO vo = randomPojo(SysMenuCreateReqVO.class, o -> { + o.setParentId(parentId); + o.setName("testSonName"); + o.setType(MenuTypeEnum.MENU.getType()); + o.setStatus(RandomUtils.randomCommonStatus()); + }); + return vo; + } + + private SysMenuDO createParentMenuDO(MenuTypeEnum typeEnum) { + SysMenuDO menuDO = randomPojo(SysMenuDO.class, o -> { + o.setName("testParentName"); + o.setType(typeEnum.getType()); + o.setStatus(RandomUtils.randomCommonStatus()); + }); + return menuDO; + } + +} From 310a436c124869617e8de77e43b6a73b1c73a597 Mon Sep 17 00:00:00 2001 From: timfruit Date: Sat, 20 Mar 2021 13:05:40 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=AE=8C=E6=88=90menu=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../permission/impl/SysMenuServiceImpl.java | 4 - .../permission/SysMenuServiceTest.java | 407 ++++++++++++++++-- .../dashboard/util/AopTargetUtils.java | 63 +++ 3 files changed, 434 insertions(+), 40 deletions(-) create mode 100644 src/test/java/cn/iocoder/dashboard/util/AopTargetUtils.java diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java index 9ed27d085..43d5fc132 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java @@ -168,10 +168,6 @@ public class SysMenuServiceImpl implements SysMenuService { */ @Transactional(rollbackFor = Exception.class) public void deleteMenu(Long menuId) { - // 校验更新的菜单是否存在 - if (menuMapper.selectById(menuId) == null) { - throw ServiceExceptionUtil.exception(MENU_NOT_EXISTS); - } // 校验是否还有子菜单 if (menuMapper.selectCountByParentId(menuId) > 0) { throw ServiceExceptionUtil.exception(MENU_EXISTS_CHILDREN); diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java index 668ce9b79..e6c861272 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java @@ -1,33 +1,38 @@ package cn.iocoder.dashboard.modules.system.service.permission; +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.lang.Assert; import cn.iocoder.dashboard.BaseDbUnitTest; import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuListReqVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuUpdateReqVO; import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysMenuDO; import cn.iocoder.dashboard.modules.system.dal.mysql.permission.SysMenuMapper; import cn.iocoder.dashboard.modules.system.enums.permission.MenuTypeEnum; import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysMenuProducer; -import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysPermissionProducer; import cn.iocoder.dashboard.modules.system.service.permission.impl.SysMenuServiceImpl; +import cn.iocoder.dashboard.util.AopTargetUtils; import cn.iocoder.dashboard.util.AssertUtils; import cn.iocoder.dashboard.util.RandomUtils; +import cn.iocoder.dashboard.util.object.ObjectUtils; +import com.google.common.collect.Multimap; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import javax.annotation.Resource; -import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.MENU_PARENT_NOT_DIR_OR_MENU; -import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.MENU_PARENT_NOT_EXISTS; -import static cn.iocoder.dashboard.util.RandomUtils.randomLongId; +import java.util.*; + +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.verify; @Import(SysMenuServiceImpl.class) public class SysMenuServiceTest extends BaseDbUnitTest { - @Resource private SysMenuServiceImpl sysMenuService; @@ -40,16 +45,47 @@ public class SysMenuServiceTest extends BaseDbUnitTest { @Resource private SysMenuMapper menuMapper; + @Test + public void testInitLocalCache_success() throws Exception { + SysMenuDO menuDO1 = createMenuDO(MenuTypeEnum.MENU, "xxxx", 0L); + menuMapper.insert(menuDO1); + SysMenuDO menuDO2 = createMenuDO(MenuTypeEnum.MENU, "xxxx", 0L); + menuMapper.insert(menuDO2); + + //调用 + sysMenuService.initLocalCache(); + + // 获取代理对象 + SysMenuServiceImpl target = (SysMenuServiceImpl) AopTargetUtils.getTarget(sysMenuService); + + Map menuCache = (Map) BeanUtil.getFieldValue(target, "menuCache"); + Assert.isTrue(menuCache.size()==2); + AssertUtils.assertPojoEquals(menuDO1,menuCache.get(menuDO1.getId())); + AssertUtils.assertPojoEquals(menuDO2,menuCache.get(menuDO2.getId())); + + Multimap permissionMenuCache = (Multimap) BeanUtil.getFieldValue(target, "permissionMenuCache"); + Assert.isTrue(permissionMenuCache.size()==2); + AssertUtils.assertPojoEquals(menuDO1,permissionMenuCache.get(menuDO1.getPermission())); + AssertUtils.assertPojoEquals(menuDO2,permissionMenuCache.get(menuDO2.getPermission())); + + Date maxUpdateTime = (Date) BeanUtil.getFieldValue(target, "maxUpdateTime"); + assertEquals(ObjectUtils.max(menuDO1.getUpdateTime(), menuDO2.getUpdateTime()), maxUpdateTime); + } @Test public void testCreateMenu_success() { //构造父目录 - SysMenuDO menuDO = createParentMenuDO(MenuTypeEnum.MENU); + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); menuMapper.insert(menuDO); Long parentId = menuDO.getId(); //调用 - SysMenuCreateReqVO vo = createReqVO(parentId); + SysMenuCreateReqVO vo = randomPojo(SysMenuCreateReqVO.class, o -> { + o.setParentId(parentId); + o.setName("testSonName"); + o.setType(MenuTypeEnum.MENU.getType()); + o.setStatus(RandomUtils.randomCommonStatus()); + }); Long menuId = sysMenuService.createMenu(vo); //断言 @@ -66,44 +102,343 @@ public class SysMenuServiceTest extends BaseDbUnitTest { Long parentId = RandomUtils.randomLongId(); // 调用 - SysMenuCreateReqVO vo = createReqVO(parentId); - - // 调用, 并断言异常 - AssertUtils.assertServiceException(() -> sysMenuService.createMenu(vo), MENU_PARENT_NOT_EXISTS); - } - - @Test - public void testCreateMenu_checkParentIsDirOrMenu() { - //构造父目录 - SysMenuDO menuDO = createParentMenuDO(MenuTypeEnum.BUTTON); - menuMapper.insert(menuDO); - Long parentId = menuDO.getId(); - - // 调用 - SysMenuCreateReqVO vo = createReqVO(parentId); - - // 调用, 并断言异常 - AssertUtils.assertServiceException(() -> sysMenuService.createMenu(vo), MENU_PARENT_NOT_DIR_OR_MENU); - } - - - private SysMenuCreateReqVO createReqVO(Long parentId) { SysMenuCreateReqVO vo = randomPojo(SysMenuCreateReqVO.class, o -> { o.setParentId(parentId); o.setName("testSonName"); o.setType(MenuTypeEnum.MENU.getType()); o.setStatus(RandomUtils.randomCommonStatus()); }); - return vo; + + // 调用, 并断言异常 + AssertUtils.assertServiceException(() -> sysMenuService.createMenu(vo), MENU_PARENT_NOT_EXISTS); } - private SysMenuDO createParentMenuDO(MenuTypeEnum typeEnum) { - SysMenuDO menuDO = randomPojo(SysMenuDO.class, o -> { - o.setName("testParentName"); - o.setType(typeEnum.getType()); + @Test + public void testCreateMenu_checkParentTypeError() { + //构造父目录 + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L); + menuMapper.insert(menuDO); + Long parentId = menuDO.getId(); + + // 调用 + SysMenuCreateReqVO vo = randomPojo(SysMenuCreateReqVO.class, o -> { + o.setParentId(parentId); + o.setName("testSonName"); + o.setType(MenuTypeEnum.MENU.getType()); o.setStatus(RandomUtils.randomCommonStatus()); }); - return menuDO; + + // 调用, 并断言异常 + AssertUtils.assertServiceException(() -> sysMenuService.createMenu(vo), MENU_PARENT_NOT_DIR_OR_MENU); } + @Test + public void testUpdateMenu_success() { + //构造父子目录 + SysMenuDO sonMenuDO = initParentAndSonMenuDO(); + Long sonId = sonMenuDO.getId(); + Long parentId = sonMenuDO.getParentId(); + + //调用 + SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { + o.setId(sonId); + o.setParentId(parentId); + o.setType(MenuTypeEnum.MENU.getType()); + o.setStatus(RandomUtils.randomCommonStatus()); + o.setName("pppppp"); //修改名字 + }); + sysMenuService.updateMenu(vo); + + //断言 + // 校验记录的属性是否正确 + SysMenuDO ret = menuMapper.selectById(sonId); + AssertUtils.assertPojoEquals(vo, ret); + // 校验调用 + verify(sysMenuProducer).sendMenuRefreshMessage(); + } + + @Test + public void testUpdateMenu_sonIdNotExist() { + Long sonId = 99999L; + Long parentId = 10000L; + + //调用 + SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { + o.setId(sonId); + o.setParentId(parentId); + o.setType(MenuTypeEnum.MENU.getType()); + o.setStatus(RandomUtils.randomCommonStatus()); + }); + //断言 + AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NOT_EXISTS); + } + + @Test + public void testUpdateMenu_canNotSetSelfToBeParent() { + //构造父子目录 + SysMenuDO sonMenuDO = initParentAndSonMenuDO(); + Long sonId = sonMenuDO.getId(); + + long updateParentId = sonId; + + //调用 + SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { + o.setId(sonId); + o.setParentId(updateParentId); + o.setType(MenuTypeEnum.MENU.getType()); + o.setStatus(RandomUtils.randomCommonStatus()); + }); + //断言 + AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_PARENT_ERROR); + } + + @Test + public void testUpdateMenu_parentNotExist() { + //构造父子目录 + SysMenuDO sonMenuDO = initParentAndSonMenuDO(); + Long sonId = sonMenuDO.getId(); + + long updateParentId = -999999; + + //调用 + SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { + o.setId(sonId); + o.setParentId(updateParentId); + o.setType(MenuTypeEnum.MENU.getType()); + o.setStatus(RandomUtils.randomCommonStatus()); + }); + //断言 + AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_PARENT_NOT_EXISTS); + } + + @Test + public void testUpdateMenu_parentTypeError() { + SysMenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "son", 999L); + menuMapper.insert(sonMenuDO); + Long sonId = sonMenuDO.getId(); + + //button类型 + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L); + menuMapper.insert(menuDO); + Long parentId = menuDO.getId(); + + //调用 + SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { + o.setId(sonId); + o.setParentId(parentId); + o.setType(MenuTypeEnum.MENU.getType()); + o.setStatus(RandomUtils.randomCommonStatus()); + }); + //断言 + AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_PARENT_NOT_DIR_OR_MENU); + } + + @Test + public void testUpdateMenu_sonMenuNameDuplicate() { + SysMenuDO parent = createMenuDO(MenuTypeEnum.MENU, "parent", 999L); + menuMapper.insert(parent); + Long parentId = parent.getId(); + + + SysMenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "son", parentId); + menuMapper.insert(sonMenuDO); + + SysMenuDO sonMenuDO2 = createMenuDO(MenuTypeEnum.MENU, "son2", parentId); + menuMapper.insert(sonMenuDO2); + + Long sonId = sonMenuDO.getId(); + String updateName = sonMenuDO2.getName(); + + //调用 + SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { + o.setId(sonId); + o.setParentId(parentId); + o.setType(MenuTypeEnum.MENU.getType()); + o.setStatus(RandomUtils.randomCommonStatus()); + o.setName(updateName); + }); + //断言 + AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NAME_DUPLICATE); + } + + @Test + public void testDeleteMenu_success() { + SysMenuDO sonMenuDO = initParentAndSonMenuDO(); + + Long sonId = sonMenuDO.getId(); + //调用 + sysMenuService.deleteMenu(sonId); + + //断言 + SysMenuDO menuDO = menuMapper.selectById(sonId); + Assert.isNull(menuDO); + verify(sysPermissionService).processMenuDeleted(sonId); + verify(sysMenuProducer).sendMenuRefreshMessage(); + } + + @Test + public void testDeleteMenu_menuNotExist() { + Long sonId = 99999L; + + AssertUtils.assertServiceException(() -> sysMenuService.deleteMenu(sonId), MENU_NOT_EXISTS); + } + + @Test + public void testDeleteMenu_existChildren() { + SysMenuDO sonMenu = initParentAndSonMenuDO(); + Long parentId = sonMenu.getParentId(); + + AssertUtils.assertServiceException(() -> sysMenuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN); + } + + @Test + public void testGetMenus_success() { + Map idMenuMap = new HashMap<>(); + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); + menuMapper.insert(menuDO); + idMenuMap.put(menuDO.getId(), menuDO); + + SysMenuDO sonMenu = createMenuDO(MenuTypeEnum.MENU, "son", menuDO.getId()); + menuMapper.insert(sonMenu); + idMenuMap.put(sonMenu.getId(), sonMenu); + + //调用 + List menuDOS = sysMenuService.getMenus(); + + //断言 + Assert.isTrue(menuDOS.size() == idMenuMap.size()); + for (SysMenuDO menu : menuDOS) { + AssertUtils.assertPojoEquals(idMenuMap.get(menu.getId()), menu); + } + } + + + @Test + public void testGetMenusReqVo_success() { + Map idMenuMap = new HashMap<>(); + SysMenuDO menu = createMenuDO(MenuTypeEnum.MENU, "name2", 0L, 1); + menuMapper.insert(menu); + idMenuMap.put(menu.getId(), menu); + + menu = createMenuDO(MenuTypeEnum.MENU, "11name111", 0L, 1); + menuMapper.insert(menu); + idMenuMap.put(menu.getId(), menu); + + menu = createMenuDO(MenuTypeEnum.MENU, "name", 0L, 1); + menuMapper.insert(menu); + idMenuMap.put(menu.getId(), menu); + + + menu = createMenuDO(MenuTypeEnum.MENU, "xxxxxx", 0L, 1); + menuMapper.insert(menu); + menu = createMenuDO(MenuTypeEnum.MENU, "name", 0L, 2); + menuMapper.insert(menu); + + + //调用 + SysMenuListReqVO reqVO = new SysMenuListReqVO(); + reqVO.setStatus(1); + reqVO.setName("name"); + List menuDOS = sysMenuService.getMenus(reqVO); + + //断言 + Assert.isTrue(menuDOS.size() == idMenuMap.size()); + for (SysMenuDO m : menuDOS) { + AssertUtils.assertPojoEquals(idMenuMap.get(m.getId()), m); + } + } + + @Test + public void testListMenusFromCache_success() throws Exception { + Map mockCacheMap = new HashMap<>(); + //获取代理对象 + SysMenuServiceImpl target = (SysMenuServiceImpl) AopTargetUtils.getTarget(sysMenuService); + BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); + + Map idMenuMap = new HashMap<>(); + SysMenuDO menuDO = createMenuDO(1L, MenuTypeEnum.MENU, "name", 0L, 1); + mockCacheMap.put(menuDO.getId(), menuDO); + idMenuMap.put(menuDO.getId(), menuDO); + + menuDO = createMenuDO(2L, MenuTypeEnum.MENU, "name", 0L, 1); + mockCacheMap.put(menuDO.getId(), menuDO); + idMenuMap.put(menuDO.getId(), menuDO); + + + menuDO = createMenuDO(3L, MenuTypeEnum.BUTTON, "name", 0L, 1); + mockCacheMap.put(menuDO.getId(), menuDO); + menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); + mockCacheMap.put(menuDO.getId(), menuDO); + + List menuDOS = sysMenuService.listMenusFromCache(Arrays.asList(MenuTypeEnum.MENU.getType()), Arrays.asList(1)); + Assert.isTrue(menuDOS.size() == idMenuMap.size()); + for (SysMenuDO m : menuDOS) { + AssertUtils.assertPojoEquals(idMenuMap.get(m.getId()), m); + } + } + + + @Test + public void testListMenusFromCache2_success() throws Exception { + Map mockCacheMap = new HashMap<>(); + //获取代理对象 + SysMenuServiceImpl target = (SysMenuServiceImpl) AopTargetUtils.getTarget(sysMenuService); + BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); + + Map idMenuMap = new HashMap<>(); + SysMenuDO menuDO = createMenuDO(1L, MenuTypeEnum.MENU, "name", 0L, 1); + mockCacheMap.put(menuDO.getId(), menuDO); + idMenuMap.put(menuDO.getId(), menuDO); + + menuDO = createMenuDO(2L, MenuTypeEnum.MENU, "name", 0L, 1); + mockCacheMap.put(menuDO.getId(), menuDO); + menuDO = createMenuDO(3L, MenuTypeEnum.BUTTON, "name", 0L, 1); + mockCacheMap.put(menuDO.getId(), menuDO); + menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); + mockCacheMap.put(menuDO.getId(), menuDO); + + List menuDOS = sysMenuService.listMenusFromCache(Arrays.asList(1L), + Arrays.asList(MenuTypeEnum.MENU.getType()), Arrays.asList(1)); + Assert.isTrue(menuDOS.size() == idMenuMap.size()); + for (SysMenuDO m : menuDOS) { + AssertUtils.assertPojoEquals(idMenuMap.get(m.getId()), m); + } + } + + /** + * 构造父子目录,返回子目录 + * + * @return + */ + private SysMenuDO initParentAndSonMenuDO() { + //构造父子目录 + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); + menuMapper.insert(menuDO); + Long parentId = menuDO.getId(); + + SysMenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "testSonName", parentId); + menuMapper.insert(sonMenuDO); + return sonMenuDO; + } + + private SysMenuDO createMenuDO(MenuTypeEnum typeEnum, String menuName, Long parentId) { + return createMenuDO(typeEnum, menuName, parentId, RandomUtils.randomCommonStatus()); + } + + private SysMenuDO createMenuDO(MenuTypeEnum typeEnum, String menuName, Long parentId, Integer status) { + return createMenuDO(null, typeEnum, menuName, parentId, status); + } + + private SysMenuDO createMenuDO(Long id, MenuTypeEnum typeEnum, String menuName, Long parentId, Integer status) { + SysMenuDO sonMenuDO = RandomUtils.randomPojo(SysMenuDO.class, o -> { + o.setId(id); + o.setParentId(parentId); + o.setType(typeEnum.getType()); + o.setStatus(status); + o.setName(menuName); + }); + return sonMenuDO; + } + + } diff --git a/src/test/java/cn/iocoder/dashboard/util/AopTargetUtils.java b/src/test/java/cn/iocoder/dashboard/util/AopTargetUtils.java new file mode 100644 index 000000000..376e47069 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/util/AopTargetUtils.java @@ -0,0 +1,63 @@ +package cn.iocoder.dashboard.util; + +import org.springframework.aop.framework.AdvisedSupport; +import org.springframework.aop.framework.AopProxy; +import org.springframework.aop.support.AopUtils; + +import java.lang.reflect.Field; + +/** + * http://www.bubuko.com/infodetail-3471885.html + */ +public class AopTargetUtils { + + + /** + * 获取 目标对象 + * + * @param proxy 代理对象 + * @return + * @throws Exception + */ + public static Object getTarget(Object proxy) throws Exception { + + if (!AopUtils.isAopProxy(proxy)) { + return proxy; //不是代理对象 + } + + if (AopUtils.isJdkDynamicProxy(proxy)) { + return getJdkDynamicProxyTargetObject(proxy); + } else { //cglib + return getCglibProxyTargetObject(proxy); + } + + } + + + private static Object getCglibProxyTargetObject(Object proxy) throws Exception { + Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0"); + h.setAccessible(true); + Object dynamicAdvisedInterceptor = h.get(proxy); + + Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised"); + advised.setAccessible(true); + + Object target = ((AdvisedSupport) advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget(); + + return target; + } + + private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception { + Field h = proxy.getClass().getSuperclass().getDeclaredField("h"); + h.setAccessible(true); + AopProxy aopProxy = (AopProxy) h.get(proxy); + + Field advised = aopProxy.getClass().getDeclaredField("advised"); + advised.setAccessible(true); + + Object target = ((AdvisedSupport) advised.get(aopProxy)).getTargetSource().getTarget(); + + return target; + } + +} \ No newline at end of file From d2e773e84946aa445cdbd95a107e834ee633f184 Mon Sep 17 00:00:00 2001 From: timfruit Date: Sun, 21 Mar 2021 22:55:15 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=89=BF=E8=89=BF?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../permission/impl/SysMenuServiceImpl.java | 7 +- .../permission/SysMenuServiceTest.java | 226 ++++++------------ .../dashboard/util/AopTargetUtils.java | 30 +-- 3 files changed, 87 insertions(+), 176 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java index 43d5fc132..ec4ea5a9b 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java @@ -15,6 +15,7 @@ import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysMenuProduce import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService; import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; import cn.iocoder.dashboard.util.collection.CollectionUtils; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Multimap; @@ -246,7 +247,8 @@ public class SysMenuServiceImpl implements SysMenuService { * @param parentId 父菜单编号 * @param childId 当前菜单编号 */ - private void checkParentResource(Long parentId, Long childId) { + @VisibleForTesting + public void checkParentResource(Long parentId, Long childId) { if (parentId == null || MenuIdEnum.ROOT.getId().equals(parentId)) { return; } @@ -275,7 +277,8 @@ public class SysMenuServiceImpl implements SysMenuService { * @param parentId 父菜单编号 * @param id 菜单编号 */ - private void checkResource(Long parentId, String name, Long id) { + @VisibleForTesting + public void checkResource(Long parentId, String name, Long id) { SysMenuDO menu = menuMapper.selectByParentIdAndName(parentId, name); if (menu == null) { return; diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java index e6c861272..35a88e741 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java @@ -26,7 +26,9 @@ import javax.annotation.Resource; import java.util.*; import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; -import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; +import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; +import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; +import static cn.iocoder.dashboard.util.RandomUtils.*; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.verify; @@ -59,14 +61,14 @@ public class SysMenuServiceTest extends BaseDbUnitTest { SysMenuServiceImpl target = (SysMenuServiceImpl) AopTargetUtils.getTarget(sysMenuService); Map menuCache = (Map) BeanUtil.getFieldValue(target, "menuCache"); - Assert.isTrue(menuCache.size()==2); - AssertUtils.assertPojoEquals(menuDO1,menuCache.get(menuDO1.getId())); - AssertUtils.assertPojoEquals(menuDO2,menuCache.get(menuDO2.getId())); + Assert.isTrue(menuCache.size() == 2); + assertPojoEquals(menuDO1, menuCache.get(menuDO1.getId())); + assertPojoEquals(menuDO2, menuCache.get(menuDO2.getId())); Multimap permissionMenuCache = (Multimap) BeanUtil.getFieldValue(target, "permissionMenuCache"); - Assert.isTrue(permissionMenuCache.size()==2); - AssertUtils.assertPojoEquals(menuDO1,permissionMenuCache.get(menuDO1.getPermission())); - AssertUtils.assertPojoEquals(menuDO2,permissionMenuCache.get(menuDO2.getPermission())); + Assert.isTrue(permissionMenuCache.size() == 2); + assertPojoEquals(menuDO1, permissionMenuCache.get(menuDO1.getPermission())); + assertPojoEquals(menuDO2, permissionMenuCache.get(menuDO2.getPermission())); Date maxUpdateTime = (Date) BeanUtil.getFieldValue(target, "maxUpdateTime"); assertEquals(ObjectUtils.max(menuDO1.getUpdateTime(), menuDO2.getUpdateTime()), maxUpdateTime); @@ -92,46 +94,11 @@ public class SysMenuServiceTest extends BaseDbUnitTest { Assertions.assertNotNull(menuId); // 校验记录的属性是否正确 SysMenuDO ret = menuMapper.selectById(menuId); - AssertUtils.assertPojoEquals(vo, ret); + assertPojoEquals(vo, ret); // 校验调用 verify(sysMenuProducer).sendMenuRefreshMessage(); } - @Test - public void testCreateMenu_checkParentExist() { - Long parentId = RandomUtils.randomLongId(); - - // 调用 - SysMenuCreateReqVO vo = randomPojo(SysMenuCreateReqVO.class, o -> { - o.setParentId(parentId); - o.setName("testSonName"); - o.setType(MenuTypeEnum.MENU.getType()); - o.setStatus(RandomUtils.randomCommonStatus()); - }); - - // 调用, 并断言异常 - AssertUtils.assertServiceException(() -> sysMenuService.createMenu(vo), MENU_PARENT_NOT_EXISTS); - } - - @Test - public void testCreateMenu_checkParentTypeError() { - //构造父目录 - SysMenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L); - menuMapper.insert(menuDO); - Long parentId = menuDO.getId(); - - // 调用 - SysMenuCreateReqVO vo = randomPojo(SysMenuCreateReqVO.class, o -> { - o.setParentId(parentId); - o.setName("testSonName"); - o.setType(MenuTypeEnum.MENU.getType()); - o.setStatus(RandomUtils.randomCommonStatus()); - }); - - // 调用, 并断言异常 - AssertUtils.assertServiceException(() -> sysMenuService.createMenu(vo), MENU_PARENT_NOT_DIR_OR_MENU); - } - @Test public void testUpdateMenu_success() { //构造父子目录 @@ -152,7 +119,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest { //断言 // 校验记录的属性是否正确 SysMenuDO ret = menuMapper.selectById(sonId); - AssertUtils.assertPojoEquals(vo, ret); + assertPojoEquals(vo, ret); // 校验调用 verify(sysMenuProducer).sendMenuRefreshMessage(); } @@ -170,102 +137,14 @@ public class SysMenuServiceTest extends BaseDbUnitTest { o.setStatus(RandomUtils.randomCommonStatus()); }); //断言 - AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NOT_EXISTS); - } - - @Test - public void testUpdateMenu_canNotSetSelfToBeParent() { - //构造父子目录 - SysMenuDO sonMenuDO = initParentAndSonMenuDO(); - Long sonId = sonMenuDO.getId(); - - long updateParentId = sonId; - - //调用 - SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { - o.setId(sonId); - o.setParentId(updateParentId); - o.setType(MenuTypeEnum.MENU.getType()); - o.setStatus(RandomUtils.randomCommonStatus()); - }); - //断言 - AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_PARENT_ERROR); - } - - @Test - public void testUpdateMenu_parentNotExist() { - //构造父子目录 - SysMenuDO sonMenuDO = initParentAndSonMenuDO(); - Long sonId = sonMenuDO.getId(); - - long updateParentId = -999999; - - //调用 - SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { - o.setId(sonId); - o.setParentId(updateParentId); - o.setType(MenuTypeEnum.MENU.getType()); - o.setStatus(RandomUtils.randomCommonStatus()); - }); - //断言 - AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_PARENT_NOT_EXISTS); - } - - @Test - public void testUpdateMenu_parentTypeError() { - SysMenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "son", 999L); - menuMapper.insert(sonMenuDO); - Long sonId = sonMenuDO.getId(); - - //button类型 - SysMenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L); - menuMapper.insert(menuDO); - Long parentId = menuDO.getId(); - - //调用 - SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { - o.setId(sonId); - o.setParentId(parentId); - o.setType(MenuTypeEnum.MENU.getType()); - o.setStatus(RandomUtils.randomCommonStatus()); - }); - //断言 - AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_PARENT_NOT_DIR_OR_MENU); - } - - @Test - public void testUpdateMenu_sonMenuNameDuplicate() { - SysMenuDO parent = createMenuDO(MenuTypeEnum.MENU, "parent", 999L); - menuMapper.insert(parent); - Long parentId = parent.getId(); - - - SysMenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "son", parentId); - menuMapper.insert(sonMenuDO); - - SysMenuDO sonMenuDO2 = createMenuDO(MenuTypeEnum.MENU, "son2", parentId); - menuMapper.insert(sonMenuDO2); - - Long sonId = sonMenuDO.getId(); - String updateName = sonMenuDO2.getName(); - - //调用 - SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { - o.setId(sonId); - o.setParentId(parentId); - o.setType(MenuTypeEnum.MENU.getType()); - o.setStatus(RandomUtils.randomCommonStatus()); - o.setName(updateName); - }); - //断言 - AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NAME_DUPLICATE); + assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NOT_EXISTS); } @Test public void testDeleteMenu_success() { SysMenuDO sonMenuDO = initParentAndSonMenuDO(); - Long sonId = sonMenuDO.getId(); + //调用 sysMenuService.deleteMenu(sonId); @@ -280,7 +159,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest { public void testDeleteMenu_menuNotExist() { Long sonId = 99999L; - AssertUtils.assertServiceException(() -> sysMenuService.deleteMenu(sonId), MENU_NOT_EXISTS); + assertServiceException(() -> sysMenuService.deleteMenu(sonId), MENU_NOT_EXISTS); } @Test @@ -288,7 +167,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest { SysMenuDO sonMenu = initParentAndSonMenuDO(); Long parentId = sonMenu.getParentId(); - AssertUtils.assertServiceException(() -> sysMenuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN); + assertServiceException(() -> sysMenuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN); } @Test @@ -307,15 +186,14 @@ public class SysMenuServiceTest extends BaseDbUnitTest { //断言 Assert.isTrue(menuDOS.size() == idMenuMap.size()); - for (SysMenuDO menu : menuDOS) { - AssertUtils.assertPojoEquals(idMenuMap.get(menu.getId()), menu); - } + menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); } @Test public void testGetMenusReqVo_success() { Map idMenuMap = new HashMap<>(); + //用于验证可以模糊搜索名称包含"name",状态为1的menu SysMenuDO menu = createMenuDO(MenuTypeEnum.MENU, "name2", 0L, 1); menuMapper.insert(menu); idMenuMap.put(menu.getId(), menu); @@ -328,13 +206,12 @@ public class SysMenuServiceTest extends BaseDbUnitTest { menuMapper.insert(menu); idMenuMap.put(menu.getId(), menu); - + //以下是不符合搜索条件的的menu menu = createMenuDO(MenuTypeEnum.MENU, "xxxxxx", 0L, 1); menuMapper.insert(menu); menu = createMenuDO(MenuTypeEnum.MENU, "name", 0L, 2); menuMapper.insert(menu); - //调用 SysMenuListReqVO reqVO = new SysMenuListReqVO(); reqVO.setStatus(1); @@ -343,9 +220,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest { //断言 Assert.isTrue(menuDOS.size() == idMenuMap.size()); - for (SysMenuDO m : menuDOS) { - AssertUtils.assertPojoEquals(idMenuMap.get(m.getId()), m); - } + menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); } @Test @@ -356,6 +231,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest { BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); Map idMenuMap = new HashMap<>(); + //用于验证搜索类型为MENU,状态为1的menu SysMenuDO menuDO = createMenuDO(1L, MenuTypeEnum.MENU, "name", 0L, 1); mockCacheMap.put(menuDO.getId(), menuDO); idMenuMap.put(menuDO.getId(), menuDO); @@ -364,7 +240,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest { mockCacheMap.put(menuDO.getId(), menuDO); idMenuMap.put(menuDO.getId(), menuDO); - + //以下是不符合搜索条件的menu menuDO = createMenuDO(3L, MenuTypeEnum.BUTTON, "name", 0L, 1); mockCacheMap.put(menuDO.getId(), menuDO); menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); @@ -372,9 +248,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest { List menuDOS = sysMenuService.listMenusFromCache(Arrays.asList(MenuTypeEnum.MENU.getType()), Arrays.asList(1)); Assert.isTrue(menuDOS.size() == idMenuMap.size()); - for (SysMenuDO m : menuDOS) { - AssertUtils.assertPojoEquals(idMenuMap.get(m.getId()), m); - } + menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); } @@ -386,10 +260,12 @@ public class SysMenuServiceTest extends BaseDbUnitTest { BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); Map idMenuMap = new HashMap<>(); + //验证搜索id为1, 类型为MENU, 状态为1 的menu SysMenuDO menuDO = createMenuDO(1L, MenuTypeEnum.MENU, "name", 0L, 1); mockCacheMap.put(menuDO.getId(), menuDO); idMenuMap.put(menuDO.getId(), menuDO); + //以下是不符合搜索条件的menu menuDO = createMenuDO(2L, MenuTypeEnum.MENU, "name", 0L, 1); mockCacheMap.put(menuDO.getId(), menuDO); menuDO = createMenuDO(3L, MenuTypeEnum.BUTTON, "name", 0L, 1); @@ -400,9 +276,57 @@ public class SysMenuServiceTest extends BaseDbUnitTest { List menuDOS = sysMenuService.listMenusFromCache(Arrays.asList(1L), Arrays.asList(MenuTypeEnum.MENU.getType()), Arrays.asList(1)); Assert.isTrue(menuDOS.size() == idMenuMap.size()); - for (SysMenuDO m : menuDOS) { - AssertUtils.assertPojoEquals(idMenuMap.get(m.getId()), m); - } + menuDOS.stream().forEach(menu -> assertPojoEquals(idMenuMap.get(menu.getId()), menu)); + } + + @Test + public void testCheckParentResource_success() { + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); + menuMapper.insert(menuDO); + Long parentId = menuDO.getId(); + + sysMenuService.checkParentResource(parentId, null); + } + + @Test + public void testCheckParentResource_canNotSetSelfToBeParent() { + assertServiceException(() -> sysMenuService.checkParentResource(1L, 1L), MENU_PARENT_ERROR); + } + + @Test + public void testCheckParentResource_parentNotExist() { + assertServiceException(() -> sysMenuService.checkParentResource(randomLongId(), null), MENU_PARENT_NOT_EXISTS); + } + + @Test + public void testCheckParentResource_parentTypeError() { + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L); + menuMapper.insert(menuDO); + Long parentId = menuDO.getId(); + + assertServiceException(() -> sysMenuService.checkParentResource(parentId, null), MENU_PARENT_NOT_DIR_OR_MENU); + } + + @Test + public void testCheckResource_success(){ + SysMenuDO sonMenu=initParentAndSonMenuDO(); + Long parentId=sonMenu.getParentId(); + + Long otherSonMenuId=randomLongId(); + String otherSonMenuName=randomString(); + + sysMenuService.checkResource(parentId,otherSonMenuName,otherSonMenuId); + } + + @Test + public void testCheckResource_sonMenuNameDuplicate(){ + SysMenuDO sonMenu=initParentAndSonMenuDO(); + Long parentId=sonMenu.getParentId(); + + Long otherSonMenuId=randomLongId(); + String otherSonMenuName=sonMenu.getName(); //相同名称 + + assertServiceException(()->sysMenuService.checkResource(parentId,otherSonMenuName,otherSonMenuId), MENU_NAME_DUPLICATE); } /** diff --git a/src/test/java/cn/iocoder/dashboard/util/AopTargetUtils.java b/src/test/java/cn/iocoder/dashboard/util/AopTargetUtils.java index 376e47069..79c5d2723 100644 --- a/src/test/java/cn/iocoder/dashboard/util/AopTargetUtils.java +++ b/src/test/java/cn/iocoder/dashboard/util/AopTargetUtils.java @@ -1,5 +1,6 @@ package cn.iocoder.dashboard.util; +import cn.hutool.core.bean.BeanUtil; import org.springframework.aop.framework.AdvisedSupport; import org.springframework.aop.framework.AopProxy; import org.springframework.aop.support.AopUtils; @@ -11,7 +12,6 @@ import java.lang.reflect.Field; */ public class AopTargetUtils { - /** * 获取 目标对象 * @@ -20,43 +20,27 @@ public class AopTargetUtils { * @throws Exception */ public static Object getTarget(Object proxy) throws Exception { - if (!AopUtils.isAopProxy(proxy)) { return proxy; //不是代理对象 } - if (AopUtils.isJdkDynamicProxy(proxy)) { return getJdkDynamicProxyTargetObject(proxy); } else { //cglib return getCglibProxyTargetObject(proxy); } - } - private static Object getCglibProxyTargetObject(Object proxy) throws Exception { - Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0"); - h.setAccessible(true); - Object dynamicAdvisedInterceptor = h.get(proxy); - - Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised"); - advised.setAccessible(true); - - Object target = ((AdvisedSupport) advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget(); - + Object dynamicAdvisedInterceptor = BeanUtil.getFieldValue(proxy, "CGLIB$CALLBACK_0"); + AdvisedSupport advisedSupport = (AdvisedSupport) BeanUtil.getFieldValue(dynamicAdvisedInterceptor, "advised"); + Object target = advisedSupport.getTargetSource().getTarget(); return target; } private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception { - Field h = proxy.getClass().getSuperclass().getDeclaredField("h"); - h.setAccessible(true); - AopProxy aopProxy = (AopProxy) h.get(proxy); - - Field advised = aopProxy.getClass().getDeclaredField("advised"); - advised.setAccessible(true); - - Object target = ((AdvisedSupport) advised.get(aopProxy)).getTargetSource().getTarget(); - + AopProxy aopProxy = (AopProxy) BeanUtil.getFieldValue(proxy, "h"); + AdvisedSupport advisedSupport = (AdvisedSupport) BeanUtil.getFieldValue(aopProxy, "advised"); + Object target = advisedSupport.getTargetSource().getTarget(); return target; }