From e74f0320115fee5f594dddc7177b347c6783c296 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Thu, 23 Jan 2014 09:26:05 +0800 Subject: [PATCH] 1.23 commit --- .../service/infrastructure/UserService.java | 222 ++++++++---------- .../master/test/service/Test_UserService.java | 31 +-- src/test/resources/service-test-context.xml | 28 +-- 3 files changed, 134 insertions(+), 147 deletions(-) diff --git a/src/main/java/org/bench4q/master/service/infrastructure/UserService.java b/src/main/java/org/bench4q/master/service/infrastructure/UserService.java index 5f01caa9..703a7e4c 100644 --- a/src/main/java/org/bench4q/master/service/infrastructure/UserService.java +++ b/src/main/java/org/bench4q/master/service/infrastructure/UserService.java @@ -1,118 +1,104 @@ -package org.bench4q.master.service.infrastructure; - -import org.bench4q.master.entity.User; -import org.bench4q.master.helper.HashHelper; -import org.bench4q.master.helper.SessionHelper; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.criterion.Restrictions; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class UserService { - private SessionHelper sessionHelper; - private HashHelper hashHelper; - public static byte NORAML_AUTHENTICATION = 0; - public static byte SUPER_AUTHENTICATION = 1; - - private SessionHelper getSessionHelper() { - return sessionHelper; - } - - @Autowired - public void setSessionHelper(SessionHelper sessionHelper) { - this.sessionHelper = sessionHelper; - } - - private HashHelper getHashHelper() { - return hashHelper; - } - - @Autowired - private void setHashHelper(HashHelper hashHelper) { - this.hashHelper = hashHelper; - } - - public boolean register(String userName, String password) { - Session session = this.getSessionHelper().openSession(); - Transaction transaction = session.beginTransaction(); - try { - User user = (User) session.createCriteria(User.class) - .add(Restrictions.eq("userName", userName)).uniqueResult(); - if (user != null) { - return false; - } - user = new User(); - user.setUserName(userName); - user.setPassword(this.hashPassword(password)); - user.setScope(UserService.NORAML_AUTHENTICATION); - session.merge(user); - transaction.commit(); - return true; - } catch (Exception e) { - e.printStackTrace(); - transaction.rollback(); - return false; - } finally { - if (session != null) { - session.close(); - } - } - } - - public boolean validateUser(String userName, String password) { - Session session = this.getSessionHelper().openSession(); - try { - User user = (User) session.createCriteria(User.class) - .add(Restrictions.eq("userName", userName)).uniqueResult(); - if (user == null) { - return false; - } - if (user.getPassword().equals(this.hashPassword(password))) { - return true; - } - return false; - } catch (Exception e) { - e.printStackTrace(); - return false; - } finally { - if (session != null) { - session.close(); - } - } - } - - public String hashPassword(String password) { - return this.getHashHelper().sha1Hash(password); - } - - public User getUserByName(String userName) { - Session session = this.getSessionHelper().openSession(); - try { - return (User) session.createCriteria(User.class) - .add(Restrictions.eq("userName", userName)).uniqueResult(); - } catch (Exception e) { - e.printStackTrace(); - return null; - } finally { - if (session != null) { - session.close(); - } - } - } - - public User getUserById(int userId) { - Session session = this.getSessionHelper().openSession(); - try { - return (User) session.get(User.class, userId); - } catch (Exception e) { - e.printStackTrace(); - return null; - } finally { - if (session != null) { - session.close(); - } - } - } -} +package org.bench4q.master.service.infrastructure; + +import org.bench4q.master.entity.User; +import org.bench4q.master.factory.UserFactory; +import org.bench4q.master.helper.HashHelper; +import org.bench4q.master.helper.SessionHelper; +import org.bench4q.master.repository.UserRepository; +import org.hibernate.Session; +import org.hibernate.criterion.Restrictions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class UserService { + private SessionHelper sessionHelper; + private UserFactory userFactory; + private UserRepository userRepository; + private HashHelper hashHelper; + public static byte NORAML_AUTHENTICATION = 0; + public static byte SUPER_AUTHENTICATION = 1; + + private SessionHelper getSessionHelper() { + return sessionHelper; + } + + @Autowired + public void setSessionHelper(SessionHelper sessionHelper) { + this.sessionHelper = sessionHelper; + } + + private UserFactory getUserFactory() { + return userFactory; + } + + @Autowired + private void setUserFactory(UserFactory userFactory) { + this.userFactory = userFactory; + } + + private UserRepository getUserRepository() { + return userRepository; + } + + @Autowired + private void setUserRepository(UserRepository userRepository) { + this.userRepository = userRepository; + } + + private HashHelper getHashHelper() { + return hashHelper; + } + + @Autowired + private void setHashHelper(HashHelper hashHelper) { + this.hashHelper = hashHelper; + } + + public boolean register(String userName, String password) { + return this.getUserRepository().attatch( + this.getUserFactory().createUser(userName, password)); + } + + public boolean delete(String userName) { + return this.getUserRepository().detach(userName); + } + + public boolean validateUser(String userName, String password) { + return this.getUserRepository().isExist( + this.getUserFactory().createUser(userName, password)); + } + + public String hashPassword(String password) { + return this.getHashHelper().sha1Hash(password); + } + + public User getUserByName(String userName) { + Session session = this.getSessionHelper().openSession(); + try { + return (User) session.createCriteria(User.class) + .add(Restrictions.eq("userName", userName)).uniqueResult(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + if (session != null) { + session.close(); + } + } + } + + public User getUserById(int userId) { + Session session = this.getSessionHelper().openSession(); + try { + return (User) session.get(User.class, userId); + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + if (session != null) { + session.close(); + } + } + } +} diff --git a/src/test/java/org/bench4q/master/test/service/Test_UserService.java b/src/test/java/org/bench4q/master/test/service/Test_UserService.java index 479b6cf5..d8f06ddf 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_UserService.java +++ b/src/test/java/org/bench4q/master/test/service/Test_UserService.java @@ -3,6 +3,7 @@ package org.bench4q.master.test.service; import static org.junit.Assert.*; import org.bench4q.master.service.infrastructure.UserService; +import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -28,19 +29,19 @@ public class Test_UserService { assertNotNull(this.getUserService()); } - // @Test - // public void testQueryTestPlans() { - // List testPlanDBs = this.getUserService().querytestPlans(5); - // assertNotNull(testPlanDBs); - // assertTrue(testPlanDBs.size() > 0); - // } - // - // @Test - // public void testQueryTestPlan() { - // TestPlanDB testPlan = this.getUserService().queryTestPlan(5, - // UUID.fromString("ad10a4be-e6e2-4ffc-9282-3ba5c1a1316b")); - // assertNotNull(testPlan); - // assertEquals(testPlan.getTestPlanRunId().toString(), - // "ad10a4be-e6e2-4ffc-9282-3ba5c1a1316b"); - // } + @Test + public void testRegister() { + assertTrue(this.getUserService().register("test1", "test1")); + } + + @Test + public void testIsExist() { + this.getUserService().register("test1", "test1"); + assertTrue(this.getUserService().validateUser("test1", "test1")); + } + + @After + public void cleanUp() { + this.getUserService().delete("test1"); + } } diff --git a/src/test/resources/service-test-context.xml b/src/test/resources/service-test-context.xml index 7deea251..948a7422 100644 --- a/src/test/resources/service-test-context.xml +++ b/src/test/resources/service-test-context.xml @@ -1,14 +1,14 @@ - - - - - - - + + + + + + +