@Test
  public void loadUserExist() {

    // 准备数据
    User user = new User();
    user.setLoginName("admin");
    user.setShaPassword(new ShaPasswordEncoder().encodePassword("admin", null));
    Role role1 = new Role();
    role1.setName("admin");
    Role role2 = new Role();
    role2.setName("user");
    user.getRoleList().add(role1);
    user.getRoleList().add(role2);

    // 录制脚本
    EasyMock.expect(mockAccountManager.findUserByLoginName("admin")).andReturn(user);
    control.replay();

    // 执行测试
    OperatorDetails operator =
        (OperatorDetails) userDetailsService.loadUserByUsername(user.getLoginName());

    // 校验结果
    assertEquals(user.getLoginName(), operator.getUsername());
    assertEquals(new ShaPasswordEncoder().encodePassword("admin", null), operator.getPassword());
    assertEquals(2, operator.getAuthorities().size());
    assertEquals(
        new GrantedAuthorityImpl("ROLE_admin"), operator.getAuthorities().iterator().next());
    assertNotNull(operator.getLoginTime());
  }
  @Test(expected = UsernameNotFoundException.class)
  public void loadUserByWrongUserName() {
    // 录制脚本
    EasyMock.expect(mockAccountManager.findUserByLoginName("foo")).andReturn(null);
    control.replay();

    assertNull(userDetailsService.loadUserByUsername("foo"));
  }
예제 #3
0
 @ModelAttribute("user")
 public User getAccount(@PathVariable("id") Long id) {
   return accountManager.getUser(id);
 }
예제 #4
0
 @RequestMapping(value = "save/{id}", method = RequestMethod.POST)
 public String save(@ModelAttribute("user") User user, RedirectAttributes redirectAttributes) {
   accountManager.saveUser(user);
   return "redirect:/common/user/";
 }