Advertisement
Guest User

Untitled

a guest
Jul 11th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. @Repository
  2. @Transactional
  3. public class UserDAOImpl implements UserDAO {
  4. ....
  5. public void editUser(User user) {
  6. System.out.println("tttt: " + user);
  7. if ((user.getUserid() == BigInteger.ONE) && !user.getUsername().equalsIgnoreCase("admin")) {
  8. em.detach(user);
  9. throw new IllegalArgumentException ("Cannot change username property for the user admin");
  10. }
  11. if ((user.getUserid() == BigInteger.ONE) || !user.getIsActive().toString().equalsIgnoreCase("Yes")) {
  12. em.detach(user);
  13. throw new IllegalArgumentException ("Cannot change isactive property for the user admin");
  14. }
  15. if ((user.getUserid() == BigInteger.ONE) || !user.getRole().toString().equalsIgnoreCase("Administrator")) {
  16. em.detach(user);
  17. throw new IllegalArgumentException ("Cannot change role property for the user admin");
  18. }
  19. em.merge(user);
  20. }
  21. ....
  22. }
  23.  
  24. @RunWith(SpringJUnit4ClassRunner.class)
  25. @ContextConfiguration(locations = {"classpath:test-context.xml",
  26. "classpath:/META-INF/spring/applicationContext.xml"})
  27. @Transactional
  28. @TransactionConfiguration(defaultRollback = true)
  29. public class UserDaoTest {
  30. ....
  31. @Test
  32. public void testEditAdminIsactive() {
  33. User user = userDao.findByUsername("admin");
  34. user.setIsActive(YNflag.valueOf("No"));
  35. thrown.expect(IllegalArgumentException.class);
  36. thrown.expectMessage("Cannot change isactive property for the user admin");
  37. userDao.editUser(user);
  38. }
  39.  
  40. @Test
  41. public void testEditAdminRole() {
  42. User user = userDao.findByUsername("admin");
  43. user.setRole(UserRole.valueOf("Student"));
  44. thrown.expect(IllegalArgumentException.class);
  45. thrown.expectMessage("Cannot change role property for the user admin");
  46. userDao.editUser(user);
  47. }
  48.  
  49. @Test
  50. public void testEditAdminUsername() {
  51. User user = userDao.findByUsername("admin");
  52. user.setUsername("admin1");
  53. thrown.expect(IllegalArgumentException.class);
  54. thrown.expectMessage("Cannot change username property for the user admin");
  55. userDao.editUser(user);
  56. }
  57. ....
  58. }
  59.  
  60. <beans xmlns="http://www.springframework.org/schema/beans"
  61. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  62. xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  63. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  64. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">
  65.  
  66. <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  67. <property name="jpaVendorAdapter">
  68. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
  69. </property>
  70. <property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml"/>
  71. <property name="dataSource" ref="dataSource"/>
  72. </bean>
  73.  
  74. <bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
  75. <property name="entityManagerFactory" ref="entityManagerFactory"/>
  76. </bean>
  77.  
  78. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  79. <property name="entityManagerFactory" ref="entityManagerFactory"/>
  80. </bean>
  81.  
  82. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  83. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  84. <property name="url" value="jdbc:mysql://localhost:3306/fsms?useSSL=false"/>
  85. <property name="username" value="fsms"/>
  86. <property name="password" value="fsms"/>
  87. </bean>
  88. </beans>
  89.  
  90. <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
  91. <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
  92. <provider>org.hibernate.ejb.HibernatePersistence</provider>
  93. <class>com.rsa.projects.webtools.sfsms.model.User</class>
  94. <properties>
  95. <property name="hibernate.hbm2ddl.auto" value="update"/>
  96. <property name="hibernate.show_sql" value="true"/>
  97. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
  98. </properties>
  99. </persistence-unit>
  100. </persistence>
  101.  
  102. tttt: {Userid: 1, Username: admin, Fullname: Administrator, Isactive: No, Role: Administrator, Address: , Password: fsms, PhoneNumber: 911234567890}
  103. tttt: {Userid: 1, Username: admin, Fullname: Administrator, Isactive: Yes, Role: Student, Address: , Password: fsms, PhoneNumber: 911234567890}
  104. tttt: {Userid: 1, Username: admin1, Fullname: Administrator, Isactive: Yes, Role: Administrator, Address: , Password: fsms, PhoneNumber: 911234567890}
  105.  
  106. testEditAdminRole(com.rsa.projects.webtools.sfsms.test.UserDaoTest) Time elapsed: 0.043 sec <<< FAILURE!
  107. Expected: (an instance of java.lang.IllegalArgumentException and exception with message a string containing "Cannot change role property for the user admin")
  108. but: exception with message a string containing "Cannot change role property for the user admin" message was "Cannot change isactive property for the user admin"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement