Java 类org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl 实例源码

项目:expense-tracker    文件:GroupServiceImplTests.java   
@Test
public void groupServiceTests() {
    userDetailService = ctx.getBean(JdbcDaoImpl.class);
    UserDetails userDetails = userDetailService.loadUserByUsername("Admin");
    Authentication authToken = new UsernamePasswordAuthenticationToken(
            userDetails.getUsername(), userDetails.getPassword(),
            userDetails.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(authToken);
    rCtx = new MockRequestContext();
    MockExternalContext ec = new MockExternalContext();
    ec.setCurrentUser("Admin");
    ((MockRequestContext) rCtx).setExternalContext(ec);

    // This test runs with two groups already setup in the embedded test db

    // get all groups
    Collection<GroupBean> groups = groupService.getGroups();
    Assert.assertNotNull("Expected a non null group list", groups);
    Assert.assertTrue("Expected exactly two groups", groups.size() == 2);

    // Add a group
    GroupBean gb = new GroupBean();
    gb.setActive(true);
    gb.setGroupDescription("Test Description");
    gb.setGroupName("Test group name");
    gb.setUsers(null);
    GroupBean retGb = groupService.addGroup(gb);
    Assert.assertNotNull("Failed to create group", retGb);
    groups = groupService.getGroups();
    Assert.assertTrue("Expected exactly three groups", groups.size() == 3);
    //
    validationService.checkGroupCreationResult(retGb, rCtx);
    Message[] msgs = rCtx.getMessageContext().getAllMessages();
    Assert.assertTrue("Expected one message", msgs.length == 1);
    Message msg = msgs[0];
    Assert.assertTrue("Expected a success message",
            "Your group was created succesfuly.".equals(msg.getText()));
    // clear this message
    rCtx.getMessageContext().clearMessages();
    // now try adding group again with same name
    retGb = groupService.addGroup(gb);
    Assert.assertNull("Should have failed to create group", retGb);
    validationService.checkGroupCreationResult(retGb, rCtx);
    msgs = rCtx.getMessageContext().getAllMessages();
    Assert.assertTrue("Expected one message", msgs.length == 1);
    msg = msgs[0];
    Assert.assertTrue("Expected a failure message",
            "Failed to create group.".equals(msg.getText()));
}