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"?> <project xsi: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; @Before public 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(); } } @Test public void testAccountService() throws Exception { // 1. Get captcha String captchaKey = accountService.generateCaptchaKey(); accountService.generateCaptchaImage(captchaKey); String captchaValue = "12345"; // 2. Submit sign up Request SignUpRequest 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 link greenMail.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 activated try { accountService.login("xuj", "admin"); fail("Disabled account shouldn't be able to log in."); } catch (AccountServiceException e) { } // 4. Activate account String activationCode = activationLink.substring(activationLink.lastIndexOf("=") + 1); accountService.activate(activationCode); // 5. Login with correct id and password accountService.login("xuj", "admin"); // 5a. Login with incorrect password try { accountService.login("xuj", "admin1"); fail("Password is incorrect, shouldn't be able to login."); } catch (AccountServiceException e) { } } @After public 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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="accountService" class="com.iteye.xujava.account.service.AccountServiceImpl"> <property name="accountPersistService" ref="accountPersistService" /> <property name="accountEmailService" ref="accountEmailService" /> <property name="accountCaptchaService" ref="accountCaptchaService" /> </bean> </beans>
email.protocol=smtp email.host=127.0.0.1 email.port=25 email.username=test@xujava.iteye.com email.password=123456 email.auth=true email.systemEmail=test@xujava.iteye.com persist.file=E:/mavenspace/account/account-service/target/test-classes/persist-data.xml
5.运行mvn clean test
mvn clean install
相关推荐
7. Maven插件:Maven可以通过插件扩展功能,例如maven-war-plugin用于打包Web应用,maven-surefire-plugin用于执行测试。 通过博文链接(https://xujava.iteye.com/blog/1888949)可以获取更详细的步骤和代码示例,...
apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-...
exec-maven-plugin是Maven生态系统中的一个插件,它允许用户在Maven构建过程中执行外部命令或脚本。这使得Maven项目可以集成更多的自定义操作,比如运行特定的脚本、调用系统命令等。本文将详细介绍exec-maven-...
apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-...
apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-...
**eclipse-maven3-plugin** 是一个专门为 Eclipse IDE 设计的插件,它允许开发者在 Eclipse 开发环境中无缝集成 Maven 构建工具。Maven 是一个项目管理和综合工具,广泛用于 Java 应用程序的构建、依赖管理和项目...
maven3-plugin-3.0.1-sources.jar
maven-aether-provider-3.2.1-sources.jar maven-antrun-plugin-1.3.jar maven-archiver-2.2.jar maven-artifact-3.2.1-sources.jar maven-assembly-plugin-2.2-beta-5.jar maven-bundle-plugin-1.0.0.jar maven-...
apache-maven-3.0.5 apache-maven-3.1.1 apache-maven-3.2.5 apache-maven-3.3.9 apache-maven-3.5.4 apache-maven-3.6.3 apache-maven-3.8.5 每个版本包含4个文件: apache-maven-3.8.5-bin.tar.gz apache-maven-...
打开并输入:path= D:/Development/eclipse-JavaEE/eclipse/plugins/maven(请参照上面对应你的 maven 插件) 4. 重启 eclipse,OK,完成了,启动后你打开Window ---> Preferences 会发现一个多了一个选项Maven...
ECLIPSE MAVEN3插件文件(eclipse-maven3-plugin工具)
maven-deploy-plugin-2.8.2.jar
apache-maven-3.0.4-bin.tar.gz apache-maven-3.0.4-bin.zip apache-maven-3.0.5-bin.tar.gz apache-maven-3.0.5-bin.zip apache-maven-3.1.0-bin.tar.gz apache-maven-3.1.0-bin.zip apache-maven-3.1.1-bin.tar.gz...
`maven-compiler-plugin-3.8.0-source-release` 是 Maven 生态系统中不可或缺的一部分,它提供了可靠的源代码编译功能,使得开发者能够专注于编写代码,而无需关心构建过程的细节。通过理解 Maven 插件的工作原理和...
maven3-plugin-3.0.0-1-sources.jar
每个阶段都可以通过特定的插件来执行,例如,`maven-compiler-plugin`用于编译源代码,`maven-surefire-plugin`负责运行单元测试。Maven通过使用Project Object Model (POM)文件来描述项目信息,包括依赖、构建配置...
"apache-maven-3.9.0-bin.tar" 是Apache Maven 3.9.0版本的Linux二进制发行版,以tar归档格式提供。这个版本包含了运行Maven所需的所有文件,包括可执行脚本、库文件和文档。用户在Linux环境下,可以将此文件移动到...
maven-jar-plugin-3.1.1.jar
maven-antrun-plugin-3.0.0.jar