首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Maven account-service事例

2013-07-09 
Maven account-service例子1.在Eclipse中新建Maven Module,名称为account-service,父项目选择account,新建

Maven account-service例子

1.在Eclipse中新建Maven Module,名称为account-service,父项目选择account,新建好后,account的POM文件内容为:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.iteye.xujava</groupId><artifactId>account</artifactId><version>1.0.0</version><packaging>pom</packaging><name>Account</name><modules><module>account-email</module><module>account-persist</module><module>account-captcha</module>    <module>account-service</module>  </modules><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><springframework.version>2.5.6</springframework.version><junit.version>4.9</junit.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency></dependencies></dependencyManagement><build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><configuration><encoding>UTF-8</encoding></configuration></plugin></plugins></pluginManagement></build></project>

account-service的POM内容为:

<?xml version="1.0"?><projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>com.iteye.xujava</groupId><artifactId>account</artifactId><version>1.0.0</version></parent><artifactId>account-service</artifactId><name>account-service</name><properties><greenmail.version>1.3.1b</greenmail.version></properties><dependencies><dependency><groupId>${project.groupId}</groupId><artifactId>account-email</artifactId><version>${project.version}</version></dependency><dependency><groupId>${project.groupId}</groupId><artifactId>account-persist</artifactId><version>${project.version}</version></dependency><dependency><groupId>${project.groupId}</groupId><artifactId>account-captcha</artifactId><version>${project.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>com.icegreen</groupId><artifactId>greenmail</artifactId><version>${greenmail.version}</version><scope>test</scope></dependency></dependencies><build><testResources><testResource><directory>src/test/resources</directory><filtering>true</filtering></testResource></testResources></build></project>

?

2.在src/main/java目录的包com.iteye.xujava.account.service下新建下面4个文件:

package com.iteye.xujava.account.service;public class SignUpRequest {private String id;private String email;private String name;private String password;private String confirmPassword;private String captchaKey;private String captchaValue;private String activateServiceUrl;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getConfirmPassword() {return confirmPassword;}public void setConfirmPassword(String confirmPassword) {this.confirmPassword = confirmPassword;}public String getCaptchaKey() {return captchaKey;}public void setCaptchaKey(String captchaKey) {this.captchaKey = captchaKey;}public String getCaptchaValue() {return captchaValue;}public void setCaptchaValue(String captchaValue) {this.captchaValue = captchaValue;}public String getActivateServiceUrl() {return activateServiceUrl;}public void setActivateServiceUrl(String activateServiceUrl) {this.activateServiceUrl = activateServiceUrl;}}

?

package com.iteye.xujava.account.service;public class AccountServiceException extends Exception {private static final long serialVersionUID = 949282286716874926L;public AccountServiceException(String message) {super(message);}public AccountServiceException(String message, Throwable throwable) {super(message, throwable);}}

?

package com.iteye.xujava.account.service;public interface AccountService {String generateCaptchaKey() throws AccountServiceException;byte[] generateCaptchaImage(String captchaKey) throws AccountServiceException;void signUp(SignUpRequest signUpRequest) throws AccountServiceException;void activate(String activationNumber) throws AccountServiceException;void login(String id, String password) throws AccountServiceException;}

?

package com.iteye.xujava.account.service;import java.util.HashMap;import java.util.Map;import com.iteye.xujava.account.captcha.AccountCaptchaException;import com.iteye.xujava.account.captcha.AccountCaptchaService;import com.iteye.xujava.account.captcha.RandomGenerator;import com.iteye.xujava.account.email.AccountEmailException;import com.iteye.xujava.account.email.AccountEmailService;import com.iteye.xujava.account.persist.Account;import com.iteye.xujava.account.persist.AccountPersistException;import com.iteye.xujava.account.persist.AccountPersistService;public class AccountServiceImpl implements AccountService {private Map<String, String> activationMap = new HashMap<String, String>();private AccountPersistService accountPersistService;private AccountEmailService accountEmailService;private AccountCaptchaService accountCaptchaService;public AccountPersistService getAccountPersistService() {return accountPersistService;}public void setAccountPersistService(AccountPersistService accountPersistService) {this.accountPersistService = accountPersistService;}public AccountEmailService getAccountEmailService() {return accountEmailService;}public void setAccountEmailService(AccountEmailService accountEmailService) {this.accountEmailService = accountEmailService;}public AccountCaptchaService getAccountCaptchaService() {return accountCaptchaService;}public void setAccountCaptchaService(AccountCaptchaService accountCaptchaService) {this.accountCaptchaService = accountCaptchaService;}public byte[] generateCaptchaImage(String captchaKey) throws AccountServiceException {try {return accountCaptchaService.generateCaptchaImage(captchaKey);} catch (AccountCaptchaException e) {throw new AccountServiceException("Unable to generate Captcha Image.", e);}}public String generateCaptchaKey() throws AccountServiceException {try {return accountCaptchaService.generateCaptchaKey();} catch (AccountCaptchaException e) {throw new AccountServiceException("Unable to generate Captcha key.", e);}}public void signUp(SignUpRequest signUpRequest) throws AccountServiceException {try {if (!signUpRequest.getPassword().equals(signUpRequest.getConfirmPassword())) {throw new AccountServiceException("2 passwords do not match.");}if (!accountCaptchaService.validateCaptcha(signUpRequest.getCaptchaKey(), signUpRequest.getCaptchaValue())) {throw new AccountServiceException("Incorrect Captcha.");}Account account = new Account();account.setId(signUpRequest.getId());account.setEmail(signUpRequest.getEmail());account.setName(signUpRequest.getName());account.setPassword(signUpRequest.getPassword());account.setActivated(false);accountPersistService.createAccount(account);String activationId = RandomGenerator.getRandomString();activationMap.put(activationId, account.getId());String link = signUpRequest.getActivateServiceUrl().endsWith("/") ? signUpRequest.getActivateServiceUrl() + activationId : signUpRequest.getActivateServiceUrl() + "?key=" + activationId;accountEmailService.sendMail(account.getEmail(), "Please Activate Your Account", link);} catch (AccountCaptchaException e) {throw new AccountServiceException("Unable to validate captcha.", e);} catch (AccountPersistException e) {throw new AccountServiceException("Unable to create account.", e);} catch (AccountEmailException e) {throw new AccountServiceException("Unable to send actiavtion mail.", e);}}public void activate(String activationId) throws AccountServiceException {String accountId = activationMap.get(activationId);if (accountId == null) {throw new AccountServiceException("Invalid account activation ID.");}try {Account account = accountPersistService.readAccount(accountId);account.setActivated(true);accountPersistService.updateAccount(account);} catch (AccountPersistException e) {throw new AccountServiceException("Unable to activate account.");}}public void login(String id, String password) throws AccountServiceException {try {Account account = accountPersistService.readAccount(id);if (account == null) {throw new AccountServiceException("Account does not exist.");}if (!account.isActivated()) {throw new AccountServiceException("Account is disabled.");}if (!account.getPassword().equals(password)) {throw new AccountServiceException("Incorrect password.");}} catch (AccountPersistException e) {throw new AccountServiceException("Unable to log in.", e);}}}

?

3.在src/test/java目录的包com.iteye.xujava.account.service下新建测试文件

package com.iteye.xujava.account.service;import static junit.framework.Assert.*;import java.io.File;import java.util.ArrayList;import java.util.List;import javax.mail.Message;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.icegreen.greenmail.util.GreenMail;import com.icegreen.greenmail.util.GreenMailUtil;import com.icegreen.greenmail.util.ServerSetup;import com.iteye.xujava.account.captcha.AccountCaptchaService;public class AccountServiceTest {private GreenMail greenMail;private AccountService accountService;@Beforepublic void prepare() throws Exception {String[] springConfigFiles = { "account-email.xml", "account-persist.xml", "account-captcha.xml", "account-service.xml" };ApplicationContext ctx = new ClassPathXmlApplicationContext(springConfigFiles);AccountCaptchaService accountCaptchaService = (AccountCaptchaService) ctx.getBean("accountCaptchaService");List<String> preDefinedTexts = new ArrayList<String>();preDefinedTexts.add("12345");preDefinedTexts.add("abcde");accountCaptchaService.setPreDefinedTexts(preDefinedTexts);accountService = (AccountService) ctx.getBean("accountService");greenMail = new GreenMail(ServerSetup.SMTP);greenMail.setUser("test@xujava.iteye.com", "123456");greenMail.start();File persistDataFile = new File("target/test-classes/persist-data.xml");if (persistDataFile.exists()) {persistDataFile.delete();}}@Testpublic void testAccountService() throws Exception {// 1. Get captchaString captchaKey = accountService.generateCaptchaKey();accountService.generateCaptchaImage(captchaKey);String captchaValue = "12345";// 2. Submit sign up RequestSignUpRequest signUpRequest = new SignUpRequest();signUpRequest.setCaptchaKey(captchaKey);signUpRequest.setCaptchaValue(captchaValue);signUpRequest.setId("xuj");signUpRequest.setEmail("test2@xujava.iteye.com");signUpRequest.setName("xuj");signUpRequest.setPassword("admin");signUpRequest.setConfirmPassword("admin");signUpRequest.setActivateServiceUrl("http://localhost:8080/account/activate");accountService.signUp(signUpRequest);// 3. Read activation linkgreenMail.waitForIncomingEmail(2000, 1);Message[] msgs = greenMail.getReceivedMessages();assertEquals(1, msgs.length);assertEquals("Please Activate Your Account", msgs[0].getSubject());String activationLink = GreenMailUtil.getBody(msgs[0]).trim();// 3a. Try login but not activatedtry {accountService.login("xuj", "admin");fail("Disabled account shouldn't be able to log in.");} catch (AccountServiceException e) {}// 4. Activate accountString activationCode = activationLink.substring(activationLink.lastIndexOf("=") + 1);accountService.activate(activationCode);// 5. Login with correct id and passwordaccountService.login("xuj", "admin");// 5a. Login with incorrect passwordtry {accountService.login("xuj", "admin1");fail("Password is incorrect, shouldn't be able to login.");} catch (AccountServiceException e) {}}@Afterpublic void stopMailServer() throws Exception {greenMail.stop();}}

?

?

4.在src/main/resources目录下新建account-service.xml和service.properties

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="accountService" ref="accountPersistService" /><property name="accountEmailService" ref="accountEmailService" /><property name="accountCaptchaService" ref="accountCaptchaService" /></bean></beans>

?

email.protocol=smtpemail.host=127.0.0.1email.port=25email.username=test@xujava.iteye.comemail.password=123456email.auth=trueemail.systemEmail=test@xujava.iteye.compersist.file=E:/mavenspace/account/account-service/target/test-classes/persist-data.xml

??

5.运行mvn clean test

????????? mvn clean install

?

?

?

热点排行