Commit aa193b2bfcfe1a4fcc550aa25636b18fc15d9eaa
0 parents
First version
Showing
25 changed files
with
1077 additions
and
0 deletions
Show diff stats
1 | +++ a/pom.xml | ||
@@ -0,0 +1,141 @@ | @@ -0,0 +1,141 @@ | ||
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 | ||
5 | + http://maven.apache.org/maven-v4_0_0.xsd"> | ||
6 | + | ||
7 | + <modelVersion>4.0.0</modelVersion> | ||
8 | + <groupId>fr.plil.sio</groupId> | ||
9 | + <artifactId>spring-mvc-sample</artifactId> | ||
10 | + <packaging>war</packaging> | ||
11 | + <version>0.2</version> | ||
12 | + | ||
13 | + <dependencies> | ||
14 | + | ||
15 | + <dependency> | ||
16 | + <groupId>org.springframework</groupId> | ||
17 | + <artifactId>spring-webmvc</artifactId> | ||
18 | + <version>${spring.version}</version> | ||
19 | + </dependency> | ||
20 | + | ||
21 | + <dependency> | ||
22 | + <groupId>org.springframework</groupId> | ||
23 | + <artifactId>spring-web</artifactId> | ||
24 | + <version>${spring.version}</version> | ||
25 | + </dependency> | ||
26 | + | ||
27 | + <dependency> | ||
28 | + <groupId>org.springframework</groupId> | ||
29 | + <artifactId>spring-context</artifactId> | ||
30 | + <version>${spring.version}</version> | ||
31 | + </dependency> | ||
32 | + | ||
33 | + <dependency> | ||
34 | + <groupId>javax.servlet</groupId> | ||
35 | + <artifactId>javax.servlet-api</artifactId> | ||
36 | + <version>${servlet.version}</version> | ||
37 | + <scope>provided</scope> | ||
38 | + </dependency> | ||
39 | + | ||
40 | + <dependency> | ||
41 | + <groupId>javax.servlet.jsp</groupId> | ||
42 | + <artifactId>jsp-api</artifactId> | ||
43 | + <version>${jsp.version}</version> | ||
44 | + <scope>provided</scope> | ||
45 | + </dependency> | ||
46 | + | ||
47 | + <dependency> | ||
48 | + <groupId>javax.el</groupId> | ||
49 | + <artifactId>el-api</artifactId> | ||
50 | + <version>${jsp.el.version}</version> | ||
51 | + <scope>provided</scope> | ||
52 | + </dependency> | ||
53 | + | ||
54 | + <dependency> | ||
55 | + <groupId>javax.servlet</groupId> | ||
56 | + <artifactId>jstl</artifactId> | ||
57 | + <version>${jsp.jstl.version}</version> | ||
58 | + </dependency> | ||
59 | + | ||
60 | + <dependency> | ||
61 | + <groupId>junit</groupId> | ||
62 | + <artifactId>junit</artifactId> | ||
63 | + <version>${junit.version}</version> | ||
64 | + <scope>test</scope> | ||
65 | + </dependency> | ||
66 | + | ||
67 | + <dependency> | ||
68 | + <groupId>org.springframework</groupId> | ||
69 | + <artifactId>spring-test</artifactId> | ||
70 | + <version>${spring.version}</version> | ||
71 | + <scope>test</scope> | ||
72 | + </dependency> | ||
73 | + | ||
74 | + <dependency> | ||
75 | + <groupId>org.slf4j</groupId> | ||
76 | + <artifactId>slf4j-api</artifactId> | ||
77 | + <version>${slf4j.version}</version> | ||
78 | + <scope>compile</scope> | ||
79 | + </dependency> | ||
80 | + | ||
81 | + <dependency> | ||
82 | + <groupId>ch.qos.logback</groupId> | ||
83 | + <artifactId>logback-classic</artifactId> | ||
84 | + <version>${logback.version}</version> | ||
85 | + </dependency> | ||
86 | + | ||
87 | + <dependency> | ||
88 | + <groupId>org.slf4j</groupId> | ||
89 | + <artifactId>log4j-over-slf4j</artifactId> | ||
90 | + <version>${slf4j.version}</version> | ||
91 | + </dependency> | ||
92 | + | ||
93 | + <dependency> | ||
94 | + <groupId>org.slf4j</groupId> | ||
95 | + <artifactId>jcl-over-slf4j</artifactId> | ||
96 | + <version>${slf4j.version}</version> | ||
97 | + </dependency> | ||
98 | + | ||
99 | + <dependency> | ||
100 | + <groupId>org.slf4j</groupId> | ||
101 | + <artifactId>jul-to-slf4j</artifactId> | ||
102 | + <version>${slf4j.version}</version> | ||
103 | + </dependency> | ||
104 | + | ||
105 | + </dependencies> | ||
106 | + | ||
107 | + <build> | ||
108 | + <plugins> | ||
109 | + <plugin> | ||
110 | + <groupId>org.apache.maven.plugins</groupId> | ||
111 | + <artifactId>maven-compiler-plugin</artifactId> | ||
112 | + <version>2.3.2</version> | ||
113 | + <configuration> | ||
114 | + <source>1.6</source> | ||
115 | + <target>1.6</target> | ||
116 | + </configuration> | ||
117 | + </plugin> | ||
118 | + <plugin> | ||
119 | + <artifactId>maven-war-plugin</artifactId> | ||
120 | + <configuration> | ||
121 | + <failOnMissingWebXml>false</failOnMissingWebXml> | ||
122 | + </configuration> | ||
123 | + <version>2.1.1</version> | ||
124 | + </plugin> | ||
125 | + </plugins> | ||
126 | + <finalName>spring-mvc-sample</finalName> | ||
127 | + </build> | ||
128 | + | ||
129 | + <properties> | ||
130 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
131 | + <spring.version>3.2.5.RELEASE</spring.version> | ||
132 | + <slf4j.version>1.7.5</slf4j.version> | ||
133 | + <junit.version>4.10</junit.version> | ||
134 | + <logback.version>1.0.13</logback.version> | ||
135 | + <servlet.version>3.1.0</servlet.version> | ||
136 | + <jsp.version>2.2</jsp.version> | ||
137 | + <jsp.el.version>2.2</jsp.el.version> | ||
138 | + <jsp.jstl.version>1.2</jsp.jstl.version> | ||
139 | + </properties> | ||
140 | + | ||
141 | +</project> | ||
0 | \ No newline at end of file | 142 | \ No newline at end of file |
src/main/java/fr/plil/sio/web/mvc/CheckUserInterceptor.java
0 โ 100644
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/CheckUserInterceptor.java | ||
@@ -0,0 +1,44 @@ | @@ -0,0 +1,44 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import java.io.IOException;import javax.annotation.Resource; | ||
4 | +; | ||
5 | +import javax.servlet.http.HttpServletRequest; | ||
6 | +import javax.servlet.http.HttpServletResponse; | ||
7 | +import org.slf4j.Logger; | ||
8 | +import org.slf4j.LoggerFactory; | ||
9 | +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||
10 | + | ||
11 | +public class CheckUserInterceptor extends HandlerInterceptorAdapter { | ||
12 | + | ||
13 | + private static final Logger logger = LoggerFactory.getLogger(CheckUserInterceptor.class); | ||
14 | + | ||
15 | + @Resource | ||
16 | + private UserSession userSession; | ||
17 | + | ||
18 | + @Override | ||
19 | + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, | ||
20 | + Object handler) throws IOException { | ||
21 | + | ||
22 | + logger.debug("path info: " + request.getPathInfo()); | ||
23 | + | ||
24 | + if (request.getPathInfo().equals("/login")) { | ||
25 | + logger.debug("access granted as path is /login"); | ||
26 | + return true; | ||
27 | + } | ||
28 | + | ||
29 | + String username = userSession.getUsername(); | ||
30 | + | ||
31 | + if (username != null) { | ||
32 | + logger.debug("authenticated"); | ||
33 | + return true; | ||
34 | + } else { | ||
35 | + logger.debug("not authenticated"); | ||
36 | + response.sendRedirect("login"); | ||
37 | + return false; | ||
38 | + } | ||
39 | + } | ||
40 | + | ||
41 | + void setUserSession(UserSession userSession) { | ||
42 | + this.userSession = userSession; | ||
43 | + } | ||
44 | +} |
src/main/java/fr/plil/sio/web/mvc/LoginController.java
0 โ 100644
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/LoginController.java | ||
@@ -0,0 +1,51 @@ | @@ -0,0 +1,51 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import javax.annotation.Resource; | ||
4 | +import org.springframework.stereotype.Controller; | ||
5 | +import org.springframework.validation.BindingResult; | ||
6 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
7 | +import org.springframework.web.bind.annotation.RequestMethod; | ||
8 | +import org.springframework.web.servlet.ModelAndView; | ||
9 | + | ||
10 | +@Controller | ||
11 | +public class LoginController { | ||
12 | + | ||
13 | + @Resource | ||
14 | + private UserDao userDao; | ||
15 | + | ||
16 | + @Resource | ||
17 | + private UserSession userSession; | ||
18 | + | ||
19 | + @RequestMapping(value = "/login", method = RequestMethod.GET) | ||
20 | + public ModelAndView getLoginForm() { | ||
21 | + return new ModelAndView("login", "user", new User()); | ||
22 | + } | ||
23 | + | ||
24 | + @RequestMapping(value = "/login", method = RequestMethod.POST) | ||
25 | + public String postLoginCheck(User user, BindingResult result) { | ||
26 | + | ||
27 | + User userFromDao = userDao.getFromUsername(user.getUsername()); | ||
28 | + | ||
29 | + if (userFromDao == null) { | ||
30 | + result.rejectValue("username","login.form.invalid"); | ||
31 | + return "login"; | ||
32 | + } | ||
33 | + | ||
34 | + if(!userFromDao.getPassword().equals(user.getPassword())) { | ||
35 | + result.rejectValue("username","login.form.invalid"); | ||
36 | + return "login"; | ||
37 | + } | ||
38 | + | ||
39 | + userSession.setUsername(userFromDao.getUsername()); | ||
40 | + | ||
41 | + return "redirect:/"; | ||
42 | + } | ||
43 | + | ||
44 | + public void setUserDao(UserDao userDao) { | ||
45 | + this.userDao = userDao; | ||
46 | + } | ||
47 | + | ||
48 | + public void setUserSession(UserSession userSession) { | ||
49 | + this.userSession = userSession; | ||
50 | + } | ||
51 | +} | ||
0 | \ No newline at end of file | 52 | \ No newline at end of file |
src/main/java/fr/plil/sio/web/mvc/LogoutController.java
0 โ 100644
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/LogoutController.java | ||
@@ -0,0 +1,23 @@ | @@ -0,0 +1,23 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import javax.annotation.Resource; | ||
4 | +import org.springframework.stereotype.Controller; | ||
5 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
6 | +import org.springframework.web.bind.annotation.RequestMethod; | ||
7 | + | ||
8 | +@Controller | ||
9 | +public class LogoutController { | ||
10 | + | ||
11 | + @Resource | ||
12 | + private UserSession userSession; | ||
13 | + | ||
14 | + @RequestMapping(value = {"/logout"}, method = RequestMethod.GET) | ||
15 | + public String getLogout() { | ||
16 | + userSession.setUsername(null); | ||
17 | + return "redirect:/"; | ||
18 | + } | ||
19 | + | ||
20 | + public void setUserSession(UserSession userSession) { | ||
21 | + this.userSession = userSession; | ||
22 | + } | ||
23 | +} |
src/main/java/fr/plil/sio/web/mvc/NewUserController.java
0 โ 100644
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/NewUserController.java | ||
@@ -0,0 +1,60 @@ | @@ -0,0 +1,60 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import javax.annotation.Resource; | ||
4 | +import org.springframework.stereotype.Controller; | ||
5 | +import org.springframework.validation.BindingResult; | ||
6 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
7 | +import org.springframework.web.bind.annotation.RequestMethod; | ||
8 | +import org.springframework.web.servlet.ModelAndView; | ||
9 | + | ||
10 | +@Controller | ||
11 | +public class NewUserController { | ||
12 | + | ||
13 | + @Resource | ||
14 | + private UserDao userDao; | ||
15 | + @Resource | ||
16 | + private UserSession userSession; | ||
17 | + @Resource | ||
18 | + private UserValidator userValidator; | ||
19 | + | ||
20 | + @RequestMapping(value = {"/newUser"}, method = RequestMethod.GET) | ||
21 | + public ModelAndView getNewUserForm() { | ||
22 | + return new ModelAndView("newUser", "user", new User()); | ||
23 | + } | ||
24 | + | ||
25 | + @RequestMapping(value = {"/newUser"}, method = RequestMethod.POST) | ||
26 | + public String postNewUser(User user, BindingResult result) { | ||
27 | + | ||
28 | + if (!userSession.getUsername().equals("admin")) { | ||
29 | + result.rejectValue("username", "new.user.only.admin"); | ||
30 | + } | ||
31 | + | ||
32 | + userValidator.validate(user, result); | ||
33 | + | ||
34 | + boolean present = (userDao.getFromUsername(user.getUsername()) != null); | ||
35 | + | ||
36 | + if (present) { | ||
37 | + result.rejectValue("username", "new.user.form.present"); | ||
38 | + } | ||
39 | + | ||
40 | + if(result.hasErrors()) { | ||
41 | + return "newUser"; | ||
42 | + } | ||
43 | + | ||
44 | + userDao.save(user); | ||
45 | + | ||
46 | + return "redirect:/"; | ||
47 | + } | ||
48 | + | ||
49 | + public void setUserDao(UserDao userDao) { | ||
50 | + this.userDao = userDao; | ||
51 | + } | ||
52 | + | ||
53 | + public void setUserSession(UserSession userSession) { | ||
54 | + this.userSession = userSession; | ||
55 | + } | ||
56 | + | ||
57 | + public void setUserValidator(UserValidator userValidator) { | ||
58 | + this.userValidator = userValidator; | ||
59 | + } | ||
60 | +} |
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/User.java | ||
@@ -0,0 +1,42 @@ | @@ -0,0 +1,42 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +public class User { | ||
4 | + | ||
5 | + private String username; | ||
6 | + private String password; | ||
7 | + | ||
8 | + public String getUsername() { | ||
9 | + return username; | ||
10 | + } | ||
11 | + | ||
12 | + public void setUsername(String username) { | ||
13 | + this.username = username; | ||
14 | + } | ||
15 | + | ||
16 | + public String getPassword() { | ||
17 | + return password; | ||
18 | + } | ||
19 | + | ||
20 | + public void setPassword(String password) { | ||
21 | + this.password = password; | ||
22 | + } | ||
23 | + | ||
24 | + @Override | ||
25 | + public boolean equals(Object obj) { | ||
26 | + if (obj == null) { | ||
27 | + return false; | ||
28 | + } | ||
29 | + if (getClass() != obj.getClass()) { | ||
30 | + return false; | ||
31 | + } | ||
32 | + final User other = (User) obj; | ||
33 | + return !((this.username == null) ? (other.username != null) : !this.username.equals(other.username)); | ||
34 | + } | ||
35 | + | ||
36 | + @Override | ||
37 | + public int hashCode() { | ||
38 | + int hash = 7; | ||
39 | + hash = 29 * hash + (this.username != null ? this.username.hashCode() : 0); | ||
40 | + return hash; | ||
41 | + } | ||
42 | +} |
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/UserDao.java | ||
@@ -0,0 +1,16 @@ | @@ -0,0 +1,16 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import java.util.Set; | ||
4 | + | ||
5 | +public interface UserDao { | ||
6 | + | ||
7 | + boolean save(User user); | ||
8 | + | ||
9 | + User getFromUsername(String username); | ||
10 | + | ||
11 | + Set<User> getAllUsers(); | ||
12 | + | ||
13 | + boolean update(User user); | ||
14 | + | ||
15 | + boolean delete(User user); | ||
16 | +} |
src/main/java/fr/plil/sio/web/mvc/UserDaoImpl.java
0 โ 100644
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/UserDaoImpl.java | ||
@@ -0,0 +1,48 @@ | @@ -0,0 +1,48 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import java.util.HashSet; | ||
4 | +import java.util.Set; | ||
5 | +import org.springframework.stereotype.Repository; | ||
6 | + | ||
7 | +@Repository(value = "userDao") | ||
8 | +public class UserDaoImpl implements UserDao { | ||
9 | + | ||
10 | + private Set<User> users = new HashSet<User>(); | ||
11 | + | ||
12 | + public UserDaoImpl() { | ||
13 | + User user = new User(); | ||
14 | + user.setUsername("admin"); | ||
15 | + user.setPassword("admin"); | ||
16 | + users.add(user); | ||
17 | + } | ||
18 | + | ||
19 | + @Override | ||
20 | + public boolean save(User user) { | ||
21 | + return users.add(user); | ||
22 | + } | ||
23 | + | ||
24 | + @Override | ||
25 | + public User getFromUsername(String username) { | ||
26 | + for (User user : users) { | ||
27 | + if (user.getUsername().equals(username)) { | ||
28 | + return user; | ||
29 | + } | ||
30 | + } | ||
31 | + return null; | ||
32 | + } | ||
33 | + | ||
34 | + @Override | ||
35 | + public Set<User> getAllUsers() { | ||
36 | + return users; | ||
37 | + } | ||
38 | + | ||
39 | + @Override | ||
40 | + public boolean update(User user) { | ||
41 | + throw new UnsupportedOperationException("Not supported."); | ||
42 | + } | ||
43 | + | ||
44 | + @Override | ||
45 | + public boolean delete(User user) { | ||
46 | + throw new UnsupportedOperationException("Not supported."); | ||
47 | + } | ||
48 | +} |
src/main/java/fr/plil/sio/web/mvc/UserSession.java
0 โ 100644
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/UserSession.java | ||
@@ -0,0 +1,17 @@ | @@ -0,0 +1,17 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import java.io.Serializable; | ||
4 | +import org.springframework.stereotype.Component; | ||
5 | + | ||
6 | +public class UserSession implements Serializable { | ||
7 | + | ||
8 | + private String username; | ||
9 | + | ||
10 | + public String getUsername() { | ||
11 | + return username; | ||
12 | + } | ||
13 | + | ||
14 | + public void setUsername(String username) { | ||
15 | + this.username = username; | ||
16 | + } | ||
17 | +} |
src/main/java/fr/plil/sio/web/mvc/UserValidator.java
0 โ 100644
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/UserValidator.java | ||
@@ -0,0 +1,28 @@ | @@ -0,0 +1,28 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import org.springframework.stereotype.Component; | ||
4 | +import org.springframework.validation.Errors; | ||
5 | +import org.springframework.validation.Validator; | ||
6 | + | ||
7 | +@Component | ||
8 | +public class UserValidator implements Validator { | ||
9 | + | ||
10 | + @Override | ||
11 | + public boolean supports(Class<?> clazz) { | ||
12 | + return User.class.equals(clazz); | ||
13 | + } | ||
14 | + | ||
15 | + @Override | ||
16 | + public void validate(Object target, Errors errors) { | ||
17 | + User user = (User)target; | ||
18 | + | ||
19 | + if (user.getUsername().length() < 3) { | ||
20 | + errors.rejectValue("username", "validator.user.username.minimal.size"); | ||
21 | + } | ||
22 | + | ||
23 | + if (user.getPassword().length() < 3) { | ||
24 | + errors.rejectValue("password", "validator.user.password.minimal.size"); | ||
25 | + } | ||
26 | + } | ||
27 | + | ||
28 | +} |
src/main/java/fr/plil/sio/web/mvc/ViewUsersController.java
0 โ 100644
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/ViewUsersController.java | ||
@@ -0,0 +1,41 @@ | @@ -0,0 +1,41 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import java.util.Set; | ||
4 | +import javax.annotation.Resource; | ||
5 | +import org.springframework.stereotype.Controller; | ||
6 | +import org.springframework.web.bind.annotation.ModelAttribute; | ||
7 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
8 | +import org.springframework.web.bind.annotation.RequestMethod; | ||
9 | + | ||
10 | +@Controller | ||
11 | +public class ViewUsersController { | ||
12 | + | ||
13 | + @Resource | ||
14 | + private UserDao userDao; | ||
15 | + | ||
16 | + @Resource | ||
17 | + private UserSession userSession; | ||
18 | + | ||
19 | + @ModelAttribute("users") | ||
20 | + public Set<User> populateUsers() { | ||
21 | + return userDao.getAllUsers(); | ||
22 | + } | ||
23 | + | ||
24 | + @ModelAttribute("userSession") | ||
25 | + public UserSession populateUser() { | ||
26 | + return userSession; | ||
27 | + } | ||
28 | + | ||
29 | + @RequestMapping(value={"/"},method=RequestMethod.GET) | ||
30 | + public String getViewUsers() { | ||
31 | + return "viewUsers"; | ||
32 | + } | ||
33 | + | ||
34 | + public void setUserDao(UserDao userDao) { | ||
35 | + this.userDao = userDao; | ||
36 | + } | ||
37 | + | ||
38 | + public void setUserSession(UserSession userSession) { | ||
39 | + this.userSession = userSession; | ||
40 | + } | ||
41 | +} |
src/main/java/fr/plil/sio/web/mvc/WebAppConfig.java
0 โ 100644
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/WebAppConfig.java | ||
@@ -0,0 +1,62 @@ | @@ -0,0 +1,62 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import org.springframework.context.MessageSource; | ||
4 | +import org.springframework.context.annotation.Bean; | ||
5 | +import org.springframework.context.annotation.ComponentScan; | ||
6 | +import org.springframework.context.annotation.Configuration; | ||
7 | +import org.springframework.context.annotation.Scope; | ||
8 | +import org.springframework.context.annotation.ScopedProxyMode; | ||
9 | +import org.springframework.context.support.ResourceBundleMessageSource; | ||
10 | +import org.springframework.web.servlet.ViewResolver; | ||
11 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
12 | +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
13 | +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; | ||
14 | +import org.springframework.web.servlet.view.InternalResourceViewResolver; | ||
15 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; | ||
16 | + | ||
17 | +@EnableWebMvc | ||
18 | +@ComponentScan(basePackages = {"fr.plil.sio.web.mvc"}, scopedProxy = ScopedProxyMode.TARGET_CLASS) | ||
19 | +@Configuration | ||
20 | +public class WebAppConfig extends WebMvcConfigurationSupport { | ||
21 | + | ||
22 | + @Bean | ||
23 | + @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) | ||
24 | + public UserSession userSession() { | ||
25 | + return new UserSession(); | ||
26 | + } | ||
27 | + | ||
28 | + @Bean | ||
29 | + public CheckUserInterceptor checkUserInterceptor() { | ||
30 | + return new CheckUserInterceptor(); | ||
31 | + } | ||
32 | + | ||
33 | + @Override | ||
34 | + public void addInterceptors(InterceptorRegistry registry) { | ||
35 | + CheckUserInterceptor interceptor = checkUserInterceptor(); | ||
36 | + interceptor.setUserSession(userSession()); | ||
37 | + registry.addInterceptor(interceptor); | ||
38 | + } | ||
39 | + | ||
40 | + @Bean | ||
41 | + public MessageSource messageSource() { | ||
42 | + ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); | ||
43 | + messageSource.setBasename("messages"); | ||
44 | + return messageSource; | ||
45 | + } | ||
46 | + | ||
47 | + @Bean | ||
48 | + public ViewResolver viewResolver() { | ||
49 | + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); | ||
50 | + resolver.setPrefix("/WEB-INF/jsp/"); | ||
51 | + resolver.setSuffix(".jsp"); | ||
52 | + return resolver; | ||
53 | + } | ||
54 | + | ||
55 | + @Bean | ||
56 | + public RequestMappingHandlerMapping requestMappingHandlerMapping() { | ||
57 | + RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping(); | ||
58 | + handlerMapping.setUseSuffixPatternMatch(false); | ||
59 | + handlerMapping.setUseTrailingSlashMatch(false); | ||
60 | + return handlerMapping; | ||
61 | + } | ||
62 | +} |
src/main/java/fr/plil/sio/web/mvc/WebAppInitializer.java
0 โ 100644
1 | +++ a/src/main/java/fr/plil/sio/web/mvc/WebAppInitializer.java | ||
@@ -0,0 +1,28 @@ | @@ -0,0 +1,28 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import javax.servlet.ServletContext; | ||
4 | +import javax.servlet.ServletException; | ||
5 | +import javax.servlet.ServletRegistration; | ||
6 | +import org.springframework.web.WebApplicationInitializer; | ||
7 | +import org.springframework.web.servlet.DispatcherServlet; | ||
8 | +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | ||
9 | +import org.springframework.web.context.ContextLoaderListener; | ||
10 | +import org.springframework.web.context.WebApplicationContext; | ||
11 | + | ||
12 | +public class WebAppInitializer implements WebApplicationInitializer { | ||
13 | + | ||
14 | + @Override | ||
15 | + public void onStartup(ServletContext servletContext) throws ServletException { | ||
16 | + WebApplicationContext context = getContext(); | ||
17 | + servletContext.addListener(new ContextLoaderListener(context)); | ||
18 | + ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context)); | ||
19 | + dispatcher.setLoadOnStartup(1); | ||
20 | + dispatcher.addMapping("/*"); | ||
21 | + } | ||
22 | + | ||
23 | + private AnnotationConfigWebApplicationContext getContext() { | ||
24 | + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); | ||
25 | + context.register(WebAppConfig.class); | ||
26 | + return context; | ||
27 | + } | ||
28 | +} |
1 | +++ a/src/main/resources/messages.properties | ||
@@ -0,0 +1,21 @@ | @@ -0,0 +1,21 @@ | ||
1 | +domain.user.username=Username | ||
2 | +domain.user.password=Password | ||
3 | + | ||
4 | +validator.user.username.minimal.size=Username must be at least 3 characters | ||
5 | +validator.user.password.minimal.size=Password must be at least 3 characters | ||
6 | + | ||
7 | +login.page.title=Login Page | ||
8 | +login.main.header=Please login... | ||
9 | +login.form.submit=login | ||
10 | +login.form.invalid=Invalid username and/or password | ||
11 | + | ||
12 | +view.users.page.title=User List | ||
13 | +view.users.main.header=List of users | ||
14 | +view.users.greetings=Hello | ||
15 | +view.users.main.logout=Logout | ||
16 | + | ||
17 | +new.user.page.title=New user | ||
18 | +new.user.main.header=Add a new user | ||
19 | +new.user.form.submit=Add the user | ||
20 | +new.user.only.admin=Only administrator user can do it | ||
21 | +new.user.form.present=user already present | ||
0 | \ No newline at end of file | 22 | \ No newline at end of file |
1 | +++ a/src/main/webapp/WEB-INF/jsp/login.jsp | ||
@@ -0,0 +1,33 @@ | @@ -0,0 +1,33 @@ | ||
1 | +<%@ page contentType="text/html" pageEncoding="UTF-8" %> | ||
2 | +<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> | ||
3 | +<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> | ||
4 | + | ||
5 | +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | ||
6 | + "http://www.w3.org/TR/html4/strict.dtd"> | ||
7 | + | ||
8 | +<html> | ||
9 | + <head> | ||
10 | + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
11 | + <title><spring:message code="login.page.title"/></title> | ||
12 | + </head> | ||
13 | + <body> | ||
14 | + <h1><spring:message code="login.main.header"/></h1> | ||
15 | + <form:form commandName="user" method="POST"> | ||
16 | + <table id="login" class="box"> | ||
17 | + <tr> | ||
18 | + <td><form:errors path="*"/></td> | ||
19 | + </tr> | ||
20 | + <tr> | ||
21 | + <td><spring:message code="domain.user.username"/></td> | ||
22 | + <td><form:input path="username"/></td> | ||
23 | + </tr> | ||
24 | + <tr> | ||
25 | + <td><spring:message code="domain.user.password"/></td> | ||
26 | + <td><form:password path="password"/></td></tr> | ||
27 | + <tr> | ||
28 | + <td><input type="submit" value="<spring:message code="login.form.submit"/>"/></td> | ||
29 | + </tr> | ||
30 | + </table> | ||
31 | + </form:form> | ||
32 | + </body> | ||
33 | +</html> | ||
0 | \ No newline at end of file | 34 | \ No newline at end of file |
1 | +++ a/src/main/webapp/WEB-INF/jsp/newUser.jsp | ||
@@ -0,0 +1,36 @@ | @@ -0,0 +1,36 @@ | ||
1 | +<%@ page contentType="text/html" pageEncoding="UTF-8" %> | ||
2 | +<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> | ||
3 | +<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> | ||
4 | + | ||
5 | +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | ||
6 | + "http://www.w3.org/TR/html4/strict.dtd"> | ||
7 | + | ||
8 | +<html> | ||
9 | + <head> | ||
10 | + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
11 | + <title><spring:message code="new.user.page.title"/></title> | ||
12 | + </head> | ||
13 | + <body> | ||
14 | + <h1><spring:message code="new.user.main.header"/></h1> | ||
15 | + <form:form commandName="user" method="POST"> | ||
16 | + <table> | ||
17 | + <tr> | ||
18 | + <td><form:errors path="username"/></td> | ||
19 | + </tr> | ||
20 | + <tr> | ||
21 | + <td><spring:message code="domain.user.username"/></td> | ||
22 | + <td><form:input path="username"/></td> | ||
23 | + </tr> | ||
24 | + <tr> | ||
25 | + <td><form:errors path="password"/></td> | ||
26 | + </tr> | ||
27 | + <tr> | ||
28 | + <td><spring:message code="domain.user.password"/></td> | ||
29 | + <td><form:password path="password"/></td></tr> | ||
30 | + <tr> | ||
31 | + <td><input type="submit" value="<spring:message code="new.user.form.submit"/>"/></td> | ||
32 | + </tr> | ||
33 | + </table> | ||
34 | + </form:form> | ||
35 | + </body> | ||
36 | +</html> |
1 | +++ a/src/main/webapp/WEB-INF/jsp/viewUsers.jsp | ||
@@ -0,0 +1,39 @@ | @@ -0,0 +1,39 @@ | ||
1 | +<%@ page contentType="text/html" pageEncoding="UTF-8" %> | ||
2 | +<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> | ||
3 | +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> | ||
4 | + | ||
5 | +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | ||
6 | + "http://www.w3.org/TR/html4/strict.dtd"> | ||
7 | + | ||
8 | +<html> | ||
9 | + <head> | ||
10 | + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
11 | + <title><spring:message code="view.users.page.title"/></title> | ||
12 | + </head> | ||
13 | + <body> | ||
14 | + <h1><spring:message code="view.users.main.header"/></h1> | ||
15 | + | ||
16 | + <h4><spring:message code="view.users.greetings"/> ${userSession.username} !</h4> | ||
17 | + | ||
18 | + <table> | ||
19 | + <thead> | ||
20 | + <tr> | ||
21 | + <td><spring:message code="domain.user.username"/></td> | ||
22 | + </tr> | ||
23 | + </thead> | ||
24 | + <tbody> | ||
25 | + <c:forEach items="${users}" var="user"> | ||
26 | + <tr> | ||
27 | + <td>${user.username}</td> | ||
28 | + </tr> | ||
29 | + </c:forEach> | ||
30 | + </tbody> | ||
31 | + </table> | ||
32 | + | ||
33 | + <ul> | ||
34 | + <li><a href="newUser"><spring:message code="new.user.main.header"/></a></li> | ||
35 | + <li><a href="logout"><spring:message code="view.users.main.logout"/></a></li> | ||
36 | + </ul> | ||
37 | + | ||
38 | +</body> | ||
39 | +</html> |
src/test/java/fr/plil/sio/web/mvc/CheckUserInterceptorTest.java
0 โ 100644
1 | +++ a/src/test/java/fr/plil/sio/web/mvc/CheckUserInterceptorTest.java | ||
@@ -0,0 +1,45 @@ | @@ -0,0 +1,45 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import java.io.IOException; | ||
4 | +import static org.junit.Assert.*; | ||
5 | +import org.junit.Before; | ||
6 | +import org.junit.Test; | ||
7 | +import org.springframework.mock.web.MockHttpServletRequest; | ||
8 | +import org.springframework.mock.web.MockHttpServletResponse; | ||
9 | + | ||
10 | +public class CheckUserInterceptorTest { | ||
11 | + | ||
12 | + private CheckUserInterceptor interceptor; | ||
13 | + private UserSession userSession; | ||
14 | + private MockHttpServletRequest request; | ||
15 | + private MockHttpServletResponse response; | ||
16 | + | ||
17 | + @Before | ||
18 | + public void createInstances() { | ||
19 | + interceptor = new CheckUserInterceptor(); | ||
20 | + userSession = new UserSession(); | ||
21 | + interceptor.setUserSession(userSession); | ||
22 | + request = new MockHttpServletRequest(); | ||
23 | + response = new MockHttpServletResponse(); | ||
24 | + } | ||
25 | + | ||
26 | + @Test | ||
27 | + public void checkPreHandleServletPathIsLogin() throws IOException { | ||
28 | + request.setPathInfo("/login"); | ||
29 | + assertTrue(interceptor.preHandle(request, response, null)); | ||
30 | + } | ||
31 | + | ||
32 | + @Test | ||
33 | + public void checkPreHandleUsernameInSession() throws IOException { | ||
34 | + userSession.setUsername("admin"); | ||
35 | + request.setPathInfo("/blabla"); | ||
36 | + assertTrue(interceptor.preHandle(request, response, null)); | ||
37 | + } | ||
38 | + | ||
39 | + @Test | ||
40 | + public void checkPreHandleUsernameNotInSession() throws IOException { | ||
41 | + request.setPathInfo("/blabla"); | ||
42 | + assertFalse(interceptor.preHandle(request, response, null)); | ||
43 | + assertEquals(response.getRedirectedUrl(),"login"); | ||
44 | + } | ||
45 | +} |
src/test/java/fr/plil/sio/web/mvc/LoginControllerTest.java
0 โ 100644
1 | +++ a/src/test/java/fr/plil/sio/web/mvc/LoginControllerTest.java | ||
@@ -0,0 +1,55 @@ | @@ -0,0 +1,55 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import static org.junit.Assert.*; | ||
4 | +import org.junit.Before; | ||
5 | +import org.junit.Test; | ||
6 | +import org.springframework.validation.BeanPropertyBindingResult; | ||
7 | +import org.springframework.validation.BindingResult; | ||
8 | +import org.springframework.web.servlet.ModelAndView; | ||
9 | + | ||
10 | +public class LoginControllerTest { | ||
11 | + | ||
12 | + private LoginController loginController; | ||
13 | + private BindingResult results; | ||
14 | + private User user; | ||
15 | + private UserDao userDao; | ||
16 | + private UserSession userSession; | ||
17 | + | ||
18 | + @Before | ||
19 | + public void createInstances() { | ||
20 | + loginController = new LoginController(); | ||
21 | + user = new User(); | ||
22 | + results = new BeanPropertyBindingResult(user, "user"); | ||
23 | + userDao = new UserDaoImpl(); | ||
24 | + loginController.setUserDao(userDao); | ||
25 | + userSession = new UserSession(); | ||
26 | + loginController.setUserSession(userSession); | ||
27 | + } | ||
28 | + | ||
29 | + @Test | ||
30 | + public void testGetLoginForm() { | ||
31 | + ModelAndView mav = loginController.getLoginForm(); | ||
32 | + assertEquals("login", mav.getViewName()); | ||
33 | + assertEquals(1, mav.getModelMap().size()); | ||
34 | + assertTrue(mav.getModel().containsKey("user")); | ||
35 | + assertTrue(mav.getModel().get("user") instanceof User); | ||
36 | + } | ||
37 | + | ||
38 | + @Test | ||
39 | + public void testPostLoginCheckSucceed() { | ||
40 | + user.setUsername("admin"); | ||
41 | + user.setPassword("admin"); | ||
42 | + String view = loginController.postLoginCheck(user, results); | ||
43 | + assertFalse(results.hasErrors()); | ||
44 | + assertEquals("redirect:/",view); | ||
45 | + } | ||
46 | + | ||
47 | + @Test | ||
48 | + public void testPostLoginCheckFailed() { | ||
49 | + user.setUsername("abc"); | ||
50 | + user.setPassword("abc"); | ||
51 | + String view = loginController.postLoginCheck(user, results); | ||
52 | + assertTrue(results.hasErrors()); | ||
53 | + assertEquals("login",view); | ||
54 | + } | ||
55 | +} |
src/test/java/fr/plil/sio/web/mvc/LogoutControllerTest.java
0 โ 100644
1 | +++ a/src/test/java/fr/plil/sio/web/mvc/LogoutControllerTest.java | ||
@@ -0,0 +1,18 @@ | @@ -0,0 +1,18 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import static org.junit.Assert.*; | ||
4 | +import org.junit.Test; | ||
5 | + | ||
6 | +public class LogoutControllerTest { | ||
7 | + | ||
8 | + @Test | ||
9 | + public void testGetLogout() { | ||
10 | + LogoutController logoutController = new LogoutController(); | ||
11 | + UserSession userSession = new UserSession(); | ||
12 | + userSession.setUsername("blabla"); | ||
13 | + logoutController.setUserSession(userSession); | ||
14 | + String view = logoutController.getLogout(); | ||
15 | + assertNull(userSession.getUsername()); | ||
16 | + assertEquals("redirect:/",view); | ||
17 | + } | ||
18 | +} |
src/test/java/fr/plil/sio/web/mvc/NewUserControllerTest.java
0 โ 100644
1 | +++ a/src/test/java/fr/plil/sio/web/mvc/NewUserControllerTest.java | ||
@@ -0,0 +1,78 @@ | @@ -0,0 +1,78 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import static org.junit.Assert.*; | ||
4 | +import org.junit.Before; | ||
5 | +import org.junit.Test; | ||
6 | +import org.springframework.validation.BeanPropertyBindingResult; | ||
7 | +import org.springframework.validation.BindingResult; | ||
8 | +import org.springframework.web.servlet.ModelAndView; | ||
9 | + | ||
10 | +public class NewUserControllerTest { | ||
11 | + | ||
12 | + private NewUserController newUserController; | ||
13 | + private BindingResult results; | ||
14 | + private User user; | ||
15 | + private UserDao userDao; | ||
16 | + private UserSession userSession; | ||
17 | + private UserValidator userValidator; | ||
18 | + | ||
19 | + @Before | ||
20 | + public void createInstances() { | ||
21 | + newUserController = new NewUserController(); | ||
22 | + user = new User(); | ||
23 | + results = new BeanPropertyBindingResult(user, "user"); | ||
24 | + userDao = new UserDaoImpl(); | ||
25 | + newUserController.setUserDao(userDao); | ||
26 | + userSession = new UserSession(); | ||
27 | + userSession.setUsername("admin"); | ||
28 | + newUserController.setUserSession(userSession); | ||
29 | + userValidator = new UserValidator(); | ||
30 | + newUserController.setUserValidator(userValidator); | ||
31 | + } | ||
32 | + | ||
33 | + @Test | ||
34 | + public void testGetNewUserForm() { | ||
35 | + ModelAndView mav = newUserController.getNewUserForm(); | ||
36 | + assertEquals("newUser", mav.getViewName()); | ||
37 | + assertEquals(1, mav.getModelMap().size()); | ||
38 | + assertTrue(mav.getModel().containsKey("user")); | ||
39 | + assertTrue(mav.getModel().get("user") instanceof User); | ||
40 | + } | ||
41 | + | ||
42 | + @Test | ||
43 | + public void testPostNewUserSucceed() { | ||
44 | + user.setUsername("abc"); | ||
45 | + user.setPassword("abc"); | ||
46 | + String view = newUserController.postNewUser(user, results); | ||
47 | + assertFalse(results.hasErrors()); | ||
48 | + assertEquals("redirect:/",view); | ||
49 | + } | ||
50 | + | ||
51 | + @Test | ||
52 | + public void testPostNewUserFailedNotAdmin() { | ||
53 | + user.setUsername("abc"); | ||
54 | + user.setPassword("abc"); | ||
55 | + userSession.setUsername("blabla"); | ||
56 | + String view = newUserController.postNewUser(user, results); | ||
57 | + assertTrue(results.hasErrors()); | ||
58 | + assertEquals("newUser",view); | ||
59 | + } | ||
60 | + | ||
61 | + @Test | ||
62 | + public void testPostNewUserFailedValidate() { | ||
63 | + user.setUsername("a"); | ||
64 | + user.setPassword("abc"); | ||
65 | + String view = newUserController.postNewUser(user, results); | ||
66 | + assertTrue(results.hasErrors()); | ||
67 | + assertEquals("newUser",view); | ||
68 | + } | ||
69 | + | ||
70 | + @Test | ||
71 | + public void testPostNewUserFailedAlreadyPresent() { | ||
72 | + user.setUsername("admin"); | ||
73 | + user.setPassword("blabla"); | ||
74 | + String view = newUserController.postNewUser(user, results); | ||
75 | + assertTrue(results.hasErrors()); | ||
76 | + assertEquals("newUser",view); | ||
77 | + } | ||
78 | +} |
src/test/java/fr/plil/sio/web/mvc/UserDaoTest.java
0 โ 100644
1 | +++ a/src/test/java/fr/plil/sio/web/mvc/UserDaoTest.java | ||
@@ -0,0 +1,63 @@ | @@ -0,0 +1,63 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import static org.junit.Assert.*; | ||
4 | +import org.junit.Before; | ||
5 | +import org.junit.Test; | ||
6 | + | ||
7 | +public class UserDaoTest { | ||
8 | + | ||
9 | + private UserDao userDao; | ||
10 | + | ||
11 | + @Before | ||
12 | + public void createInstances() { | ||
13 | + userDao = new UserDaoImpl(); | ||
14 | + } | ||
15 | + | ||
16 | + @Test | ||
17 | + public void testAdminPresent() { | ||
18 | + assertEquals(1, userDao.getAllUsers().size()); | ||
19 | + assertNotNull(userDao.getFromUsername("admin")); | ||
20 | + assertEquals("admin", userDao.getFromUsername("admin").getUsername()); | ||
21 | + } | ||
22 | + | ||
23 | + @Test | ||
24 | + public void testSaveSucceed() { | ||
25 | + User user = new User(); | ||
26 | + user.setUsername("abc"); | ||
27 | + user.setPassword("abc"); | ||
28 | + assertTrue(userDao.save(user)); | ||
29 | + } | ||
30 | + | ||
31 | + @Test | ||
32 | + public void testSaveFailed() { | ||
33 | + User user = new User(); | ||
34 | + user.setUsername("admin"); | ||
35 | + user.setPassword("abc"); | ||
36 | + assertFalse(userDao.save(user)); | ||
37 | + } | ||
38 | + | ||
39 | + @Test | ||
40 | + public void testGetFromUsernameFound() { | ||
41 | + User user = new User(); | ||
42 | + user.setUsername("abc"); | ||
43 | + user.setPassword("abc"); | ||
44 | + userDao.save(user); | ||
45 | + assertNotNull(userDao.getFromUsername("admin")); | ||
46 | + assertEquals("admin",userDao.getFromUsername("admin").getUsername()); | ||
47 | + } | ||
48 | + | ||
49 | + @Test | ||
50 | + public void testGetFromUsernameNotFound() { | ||
51 | + assertNull(userDao.getFromUsername("abc")); | ||
52 | + } | ||
53 | + | ||
54 | + @Test | ||
55 | + public void testGetAllUsers() { | ||
56 | + assertEquals(1,userDao.getAllUsers().size()); | ||
57 | + User user = new User(); | ||
58 | + user.setUsername("abc"); | ||
59 | + user.setPassword("abc"); | ||
60 | + assertTrue(userDao.save(user)); | ||
61 | + assertEquals(2,userDao.getAllUsers().size()); | ||
62 | + } | ||
63 | +} | ||
0 | \ No newline at end of file | 64 | \ No newline at end of file |
src/test/java/fr/plil/sio/web/mvc/UserValidatorTest.java
0 โ 100644
1 | +++ a/src/test/java/fr/plil/sio/web/mvc/UserValidatorTest.java | ||
@@ -0,0 +1,48 @@ | @@ -0,0 +1,48 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import static org.junit.Assert.*; | ||
4 | +import org.junit.Before; | ||
5 | +import org.junit.Test; | ||
6 | +import org.springframework.validation.BeanPropertyBindingResult; | ||
7 | +import org.springframework.validation.Errors; | ||
8 | + | ||
9 | +public class UserValidatorTest { | ||
10 | + | ||
11 | + private User user; | ||
12 | + private UserValidator validator; | ||
13 | + private Errors results; | ||
14 | + | ||
15 | + @Before | ||
16 | + public void createInstances() { | ||
17 | + validator = new UserValidator(); | ||
18 | + user = new User(); | ||
19 | + user.setUsername("abc"); | ||
20 | + user.setPassword("abc"); | ||
21 | + results = new BeanPropertyBindingResult(user, "user"); | ||
22 | + } | ||
23 | + | ||
24 | + @Test | ||
25 | + public void testSupports() { | ||
26 | + assertTrue(validator.supports(User.class)); | ||
27 | + } | ||
28 | + | ||
29 | + @Test | ||
30 | + public void testValidateCorrect() { | ||
31 | + validator.validate(user,results); | ||
32 | + assertFalse(results.hasErrors()); | ||
33 | + } | ||
34 | + | ||
35 | + @Test | ||
36 | + public void testValidatePasswordTooShort() { | ||
37 | + user.setPassword("ab"); | ||
38 | + validator.validate(user,results); | ||
39 | + assertTrue(results.hasErrors()); | ||
40 | + } | ||
41 | + | ||
42 | + @Test | ||
43 | + public void testValidateUsernameTooShort() { | ||
44 | + user.setUsername("ab"); | ||
45 | + validator.validate(user,results); | ||
46 | + assertTrue(results.hasErrors()); | ||
47 | + } | ||
48 | +} |
src/test/java/fr/plil/sio/web/mvc/ViewUsersControllerTest.java
0 โ 100644
1 | +++ a/src/test/java/fr/plil/sio/web/mvc/ViewUsersControllerTest.java | ||
@@ -0,0 +1,37 @@ | @@ -0,0 +1,37 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import static org.junit.Assert.*; | ||
4 | +import org.junit.Before; | ||
5 | +import org.junit.Test; | ||
6 | + | ||
7 | +public class ViewUsersControllerTest { | ||
8 | + | ||
9 | + private ViewUsersController viewUsersController; | ||
10 | + private UserDao userDao; | ||
11 | + private UserSession userSession; | ||
12 | + | ||
13 | + @Before | ||
14 | + public void createInstances() { | ||
15 | + viewUsersController = new ViewUsersController(); | ||
16 | + userDao = new UserDaoImpl(); | ||
17 | + viewUsersController.setUserDao(userDao); | ||
18 | + userSession = new UserSession(); | ||
19 | + userSession.setUsername("admin"); | ||
20 | + viewUsersController.setUserSession(userSession); | ||
21 | + } | ||
22 | + | ||
23 | + @Test | ||
24 | + public void testPopulateUsers() { | ||
25 | + assertEquals(1, viewUsersController.populateUsers().size()); | ||
26 | + } | ||
27 | + | ||
28 | + @Test | ||
29 | + public void testPopulateUser() { | ||
30 | + assertEquals("admin", viewUsersController.populateUser().getUsername()); | ||
31 | + } | ||
32 | + | ||
33 | + @Test | ||
34 | + public void testGetViewUsers() { | ||
35 | + assertEquals("viewUsers",viewUsersController.getViewUsers()); | ||
36 | + } | ||
37 | +} |