Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Repository
- @Transactional
- public class UserDAOImpl implements UserDAO {
- ....
- public void editUser(User user) {
- System.out.println("tttt: " + user);
- if ((user.getUserid() == BigInteger.ONE) && !user.getUsername().equalsIgnoreCase("admin")) {
- em.detach(user);
- throw new IllegalArgumentException ("Cannot change username property for the user admin");
- }
- if ((user.getUserid() == BigInteger.ONE) || !user.getIsActive().toString().equalsIgnoreCase("Yes")) {
- em.detach(user);
- throw new IllegalArgumentException ("Cannot change isactive property for the user admin");
- }
- if ((user.getUserid() == BigInteger.ONE) || !user.getRole().toString().equalsIgnoreCase("Administrator")) {
- em.detach(user);
- throw new IllegalArgumentException ("Cannot change role property for the user admin");
- }
- em.merge(user);
- }
- ....
- }
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath:test-context.xml",
- "classpath:/META-INF/spring/applicationContext.xml"})
- @Transactional
- @TransactionConfiguration(defaultRollback = true)
- public class UserDaoTest {
- ....
- @Test
- public void testEditAdminIsactive() {
- User user = userDao.findByUsername("admin");
- user.setIsActive(YNflag.valueOf("No"));
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Cannot change isactive property for the user admin");
- userDao.editUser(user);
- }
- @Test
- public void testEditAdminRole() {
- User user = userDao.findByUsername("admin");
- user.setRole(UserRole.valueOf("Student"));
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Cannot change role property for the user admin");
- userDao.editUser(user);
- }
- @Test
- public void testEditAdminUsername() {
- User user = userDao.findByUsername("admin");
- user.setUsername("admin1");
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Cannot change username property for the user admin");
- userDao.editUser(user);
- }
- ....
- }
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">
- <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="jpaVendorAdapter">
- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
- </property>
- <property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml"/>
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
- <property name="entityManagerFactory" ref="entityManagerFactory"/>
- </bean>
- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="entityManagerFactory"/>
- </bean>
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/fsms?useSSL=false"/>
- <property name="username" value="fsms"/>
- <property name="password" value="fsms"/>
- </bean>
- </beans>
- <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
- <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <class>com.rsa.projects.webtools.sfsms.model.User</class>
- <properties>
- <property name="hibernate.hbm2ddl.auto" value="update"/>
- <property name="hibernate.show_sql" value="true"/>
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
- </properties>
- </persistence-unit>
- </persistence>
- tttt: {Userid: 1, Username: admin, Fullname: Administrator, Isactive: No, Role: Administrator, Address: , Password: fsms, PhoneNumber: 911234567890}
- tttt: {Userid: 1, Username: admin, Fullname: Administrator, Isactive: Yes, Role: Student, Address: , Password: fsms, PhoneNumber: 911234567890}
- tttt: {Userid: 1, Username: admin1, Fullname: Administrator, Isactive: Yes, Role: Administrator, Address: , Password: fsms, PhoneNumber: 911234567890}
- testEditAdminRole(com.rsa.projects.webtools.sfsms.test.UserDaoTest) Time elapsed: 0.043 sec <<< FAILURE!
- Expected: (an instance of java.lang.IllegalArgumentException and exception with message a string containing "Cannot change role property for the user admin")
- 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