Guest User

Untitled

a guest
Mar 26th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 2.162 sec
  2. <<< FAILURE!
  3. test1(com.core.newbie.action.UserActionTest) Time elapsed: 0.866 sec <<<
  4. ERROR!
  5. java.lang.IllegalStateException: Failed to load ApplicationContext
  6. test2(com.core.newbie.action.UserActionTest) Time elapsed: 0.48 sec <<<
  7. ERROR!
  8. java.lang.IllegalStateException: Failed to load ApplicationContext
  9.  
  10. package com.core.newbie.model;
  11.  
  12. import javax.persistence.*;
  13. import java.util.Set;
  14.  
  15. public class User {
  16.  
  17. private Integer id;
  18.  
  19. private String userName;
  20.  
  21. private String password;
  22.  
  23. private Integer age;
  24.  
  25. @ManyToMany
  26. @JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"),
  27. inverseJoinColumns = @JoinColumn(name = "role_id"))
  28. private Set<Role> roles;
  29.  
  30. public Integer getId() {
  31. return id;
  32. }
  33.  
  34. public void setId(final Integer id) {
  35. this.id = id;
  36. }
  37.  
  38. public String getUserName() {
  39. return userName;
  40. }
  41.  
  42. public void setUserName(final String userName) {
  43. this.userName = userName == null ? null : userName.trim();
  44. }
  45.  
  46. public String getPassword() {
  47. return password;
  48. }
  49.  
  50. public void setPassword(final String password) {
  51. this.password = password == null ? null : password.trim();
  52. }
  53.  
  54. public Integer getAge() {
  55. return age;
  56. }
  57.  
  58. public void setAge(final Integer age) {
  59. this.age = age;
  60. }
  61.  
  62. public Set<Role> getRoles() {
  63. return roles;
  64. }
  65.  
  66. public void setRoles(Set<Role> roles) {
  67. this.roles = roles;
  68. }
  69. }
  70.  
  71. package com.core.newbie.dao;
  72.  
  73. import com.core.newbie.model.User;
  74. import org.springframework.data.jpa.repository.JpaRepository;
  75.  
  76. public interface UserDao extends JpaRepository<User, Long> {
  77. User findByUsername(String username);
  78. }
  79.  
  80. package com.core.newbie.service;
  81.  
  82. import com.core.newbie.model.User;
  83.  
  84. public interface UserService {
  85. User getUserById(int userId);
  86.  
  87. User findByUsername(String username);
  88.  
  89. void insetUser(User user);
  90. }
  91.  
  92. package com.core.newbie.service;
  93.  
  94. import com.core.newbie.dao.RoleDao;
  95. import com.core.newbie.dao.UserDao;
  96. import com.core.newbie.model.Role;
  97. import com.core.newbie.model.User;
  98. import org.springframework.beans.factory.annotation.Autowired;
  99. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  100. import org.springframework.stereotype.Service;
  101.  
  102. import java.util.HashSet;
  103. import java.util.Set;
  104.  
  105. @Service
  106. public abstract class UserServiceImpl implements UserService {
  107.  
  108. @Autowired
  109. private UserDao userDao;
  110.  
  111. @Autowired
  112. private RoleDao roleDao;
  113.  
  114. @Autowired
  115. private BCryptPasswordEncoder bCryptPasswordEncoder;
  116.  
  117. @Override
  118. public void insetUser(User user) {
  119. user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
  120. Set<Role> roles = new HashSet<>();
  121. roles.add(roleDao.getOne(1L));
  122. user.setRoles(roles);
  123. userDao.insetUser(user);
  124. }
  125.  
  126. @Override
  127. public User findByUsername(String username) {
  128. return userDao.findByUsername(username);
  129. }
  130. }
  131.  
  132. package com.core.newbie.service;
  133.  
  134. import com.core.newbie.dao.UserDao;
  135. import com.core.newbie.model.Role;
  136. import com.core.newbie.model.User;
  137. import org.springframework.beans.factory.annotation.Autowired;
  138. import org.springframework.security.core.GrantedAuthority;
  139. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  140. import org.springframework.security.core.userdetails.UserDetails;
  141. import org.springframework.security.core.userdetails.UserDetailsService;
  142. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  143. import org.springframework.transaction.annotation.Transactional;
  144.  
  145. import java.util.HashSet;
  146. import java.util.Set;
  147.  
  148. public class UserDetailsServiceImpl implements UserDetailsService {
  149.  
  150. @Autowired
  151. private UserDao userDao;
  152.  
  153. @Override
  154. @Transactional(readOnly = true)
  155. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  156. User user = userDao.findByUsername(username);
  157.  
  158. Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
  159.  
  160. for (Role role : user.getRoles()) {
  161. grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
  162. }
  163. return new
  164. org.springframework.security.core.userdetails.User(user.getUsername(),
  165. user.getPassword(), grantedAuthorities);
  166. }
  167. }
  168.  
  169. UserActionTest.java:
  170.  
  171. package com.core.newbie.action;
  172.  
  173. import com.alibaba.fastjson.JSON;
  174. import com.core.newbie.model.User;
  175. import com.core.newbie.service.UserService;
  176. import org.apache.log4j.Logger;
  177. import org.junit.Test;
  178. import org.junit.runner.RunWith;
  179. import org.springframework.beans.factory.annotation.Autowired;
  180. import org.springframework.test.context.ContextConfiguration;
  181. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  182.  
  183. @RunWith(SpringJUnit4ClassRunner.class)
  184. @ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
  185. public class UserActionTest {
  186. private static final Logger logger = Logger.getLogger(UserActionTest.class);
  187. private UserService userService;
  188.  
  189.  
  190. @Autowired
  191. public void setUserService(final UserService userService) {
  192. this.userService = userService;
  193. }
  194.  
  195. public UserService getUserService() {
  196. return userService;
  197. }
  198.  
  199. @Test
  200. public void test1() {
  201. final User user = userService.getUserById(1);
  202. logger.info(JSON.toJSON(user));
  203. }
  204.  
  205. @Test
  206. public void test2() {
  207. final User user = new User();
  208. user.setId(112);
  209. user.setAge(14);
  210. user.setPassword("12");
  211. user.setUserName("derrick-2");
  212. userService.insetUser(user);
  213. logger.info(JSON.toJSON(user));
  214. }
  215. }
Add Comment
Please, Sign In to add comment