rave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ja...@apache.org
Subject svn commit: r1158226 - in /incubator/rave/trunk/rave-portal/src: main/java/org/apache/rave/portal/web/validator/ test/java/org/apache/rave/portal/web/validator/
Date Tue, 16 Aug 2011 12:18:23 GMT
Author: jasha
Date: Tue Aug 16 12:18:22 2011
New Revision: 1158226

URL: http://svn.apache.org/viewvc?rev=1158226&view=rev
Log:
RAVE-192 added junit tests for NewAccountValidator
Modified some checks to prevent NPE's
supports() method should return the type of Object that it validates
validation errors should not be logged to error level (not application critical)
applied code formatting

Added:
    incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/
    incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/NewAccountValidatorTest.java
Modified:
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java?rev=1158226&r1=1158225&r2=1158226&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
Tue Aug 16 12:18:22 2011
@@ -19,9 +19,7 @@
 //TODO is this the right package name convention?
 package org.apache.rave.portal.web.validator;
 
-import java.util.ArrayList;
-import java.util.List;
-
+import org.apache.commons.lang.StringUtils;
 import org.apache.rave.portal.model.NewUser;
 import org.apache.rave.portal.service.UserService;
 import org.slf4j.Logger;
@@ -33,76 +31,75 @@ import org.springframework.validation.Va
 
 public class NewAccountValidator implements Validator {
 
-	 protected final Logger logger=LoggerFactory.getLogger(getClass());
-	 
-	 private UserService userService;
-
-	 @Autowired
-	 public NewAccountValidator(UserService userService) {
-		 this.userService = userService;
-	 }
-	 
-	 public boolean supports(Class aClass){
-		  return NewAccountValidator.class.equals(aClass);
-	 }
-	 
-	 public void validate(Object obj, Errors errors){
-		  logger.debug("Validator called");
-		  NewUser newUser = (NewUser) obj;
-		  List<ObjectError> errorList = new ArrayList<ObjectError>();
-		  
-		  //check if the username is null
-		  if(newUser.getUsername() == "") {
-			  errors.rejectValue("username", "username.required");
-			  logger.error("Username required");
-		  }
-		  
-		  //check if username length is less than 2
-		  else if(newUser.getUsername().length() < 2) {
-			  errors.rejectValue("username", "username.invalid.length");
-			  logger.error("Username must be atleast 2 characters long");
-		  }
-		  
-		  //check if username is already in use
-		  
-		  else if(userService.getUserByUsername(newUser.getUsername()) != null) {
-			  errors.rejectValue("username", "username.exits");
-			  logger.error("Username already exists");
-		  }
-		  
-		  
-		  //check if the password is null
-		  if(newUser.getPassword() == "") {
-			  errors.rejectValue("password", "password.required");
-			  logger.error("Password required");
-		  }
-		  
-		  //check if the password length is less than 4
-		  else if(newUser.getPassword().length() < 4) {
-			  errors.rejectValue("password", "password.invalid.lenght");
-			  logger.error("Password must be atleast 4 characters long");
-		  }
-		  
-		  //check if the confirm password is null
-		  if(newUser.getConfirmPassword() == "") {
-			  errors.rejectValue("confirmPassword", "confirmPassword.required");
-			  logger.error("Confirm Password required");
-		  }
-		  
-		  //check if the confirm password matches the previous entered password
-		  if(newUser.getConfirmPassword().length() != newUser.getPassword().length() || newUser.getConfirmPassword().compareTo(newUser.getPassword())
!= 0) {
-			  errors.rejectValue("confirmPassword", "confirmPassword.mismatch");
-			  logger.error("Password mismatch");
-		  }
-
-		  if(errors.hasErrors()){
-			  errorList = errors.getAllErrors();
-			  for (ObjectError error : errorList) {
-				  logger.error("Validation error: " + error.toString());
-			  }
-		  }
-		  else {
-			  logger.debug("Validation successful");
-		  }
-	 }
+    protected final Logger logger = LoggerFactory.getLogger(getClass());
+
+    private UserService userService;
+
+    @Autowired
+    public NewAccountValidator(UserService userService) {
+        this.userService = userService;
+    }
+
+    public boolean supports(Class aClass) {
+        return NewUser.class.isAssignableFrom(aClass);
+    }
+
+    public void validate(Object obj, Errors errors) {
+        logger.debug("Validator called");
+        NewUser newUser = (NewUser) obj;
+
+        //check if the username is null or empty
+        if (StringUtils.isBlank(newUser.getUsername())) {
+            errors.rejectValue("username", "username.required");
+            logger.info("Username required");
+        }
+
+        //check if username length is less than 2
+        else if (newUser.getUsername().length() < 2) {
+            errors.rejectValue("username", "username.invalid.length");
+            logger.info("Username must be atleast 2 characters long");
+        }
+
+        //check if username is already in use
+
+        else if (userService.getUserByUsername(newUser.getUsername()) != null) {
+            errors.rejectValue("username", "username.exits");
+            logger.info("Username already exists");
+        }
+
+
+        //check if the password is null
+        if (StringUtils.isBlank(newUser.getPassword())) {
+            errors.rejectValue("password", "password.required");
+            logger.info("Password required");
+        }
+
+        //check if the password length is less than 4
+        else if (newUser.getPassword().length() < 4) {
+            errors.rejectValue("password", "password.invalid.lenght");
+            logger.info("Password must be atleast 4 characters long");
+        }
+
+        //check if the confirm password is null
+        if (StringUtils.isBlank(newUser.getConfirmPassword())) {
+            errors.rejectValue("confirmPassword", "confirmPassword.required");
+            logger.info("Confirm Password required");
+        }
+
+        //check if the confirm password matches the previous entered password
+        //first check for null
+        if (newUser.getConfirmPassword() != null &&
+                !(newUser.getConfirmPassword().equals(newUser.getPassword()))) {
+            errors.rejectValue("confirmPassword", "confirmPassword.mismatch");
+            logger.info("Password mismatch");
+        }
+
+        if (errors.hasErrors()) {
+            for (ObjectError error : errors.getAllErrors()) {
+                logger.info("Validation error: " + error.toString());
+            }
+        } else {
+            logger.debug("Validation successful");
+        }
+    }
 }
\ No newline at end of file

Added: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/NewAccountValidatorTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/NewAccountValidatorTest.java?rev=1158226&view=auto
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/NewAccountValidatorTest.java
(added)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/NewAccountValidatorTest.java
Tue Aug 16 12:18:22 2011
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.rave.portal.web.validator;
+
+import org.apache.rave.portal.model.NewUser;
+import org.apache.rave.portal.model.User;
+import org.apache.rave.portal.service.UserService;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.validation.BindException;
+import org.springframework.validation.Errors;
+
+import static junit.framework.Assert.assertFalse;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+
+/**
+ * Test class for {@link NewAccountValidator}
+ */
+public class NewAccountValidatorTest {
+    private static final String VALID_NAME = "valid.name";
+    private static final String VALID_PASSWORD = "valid.password";
+    private static final String VALID_PAGELAYOUT = "valid.pagelayout";
+    private static final String FIELD_USERNAME = "username";
+    private static final String FIELD_PASSWORD = "password";
+    private static final String FIELD_CONFIRM_PASSWORD = "confirmPassword";
+    private static final String NEW_USER = "newUser";
+
+    private NewAccountValidator newAccountValidator;
+    private UserService mockUserService;
+
+    @Test
+    public void testSupports() throws Exception {
+        assertTrue(newAccountValidator.supports(NewUser.class));
+    }
+
+    @Test
+    public void testValidate() throws Exception {
+        NewUser newUser = new NewUser();
+        newUser.setUsername(VALID_NAME);
+        newUser.setPassword(VALID_PASSWORD);
+        newUser.setConfirmPassword(VALID_PASSWORD);
+        newUser.setPageLayout(VALID_PAGELAYOUT);
+        Errors errors = new BindException(newUser, NEW_USER);
+
+        expect(mockUserService.getUserByUsername("valid.name")).andReturn(null);
+        replay(mockUserService);
+
+        newAccountValidator.validate(newUser, errors);
+
+        assertFalse("No validation errors", errors.hasErrors());
+    }
+
+    @Test
+    public void testValidationFailsOnEmptyNewUser() throws Exception {
+        NewUser newUser = new NewUser();
+        Errors errors = new BindException(newUser, NEW_USER);
+        expect(mockUserService.getUserByUsername("")).andReturn(null);
+        replay(mockUserService);
+
+        newAccountValidator.validate(newUser, errors);
+
+        assertTrue("Validation errors", errors.hasErrors());
+        assertNotNull(errors.getFieldError(FIELD_USERNAME));
+        assertNotNull(errors.getFieldError(FIELD_PASSWORD));
+        assertNotNull(errors.getFieldError(FIELD_CONFIRM_PASSWORD));
+
+    }
+
+
+    @Test
+    public void testValidationFailsOnExistingUser() throws Exception {
+        NewUser newUser = new NewUser();
+        newUser.setUsername("ExistingUser");
+        newUser.setPassword(VALID_PASSWORD);
+        newUser.setConfirmPassword(VALID_PASSWORD);
+        newUser.setPageLayout(VALID_PAGELAYOUT);
+        Errors errors = new BindException(newUser, NEW_USER);
+
+        User user = createMock(User.class);
+        expect(mockUserService.getUserByUsername("ExistingUser")).andReturn(user);
+        replay(mockUserService);
+
+        newAccountValidator.validate(newUser, errors);
+
+        assertTrue("Validation errors", errors.hasErrors());
+        assertNotNull(errors.getFieldError(FIELD_USERNAME));
+    }
+
+
+    @Test
+    public void testValidationFailsOnShortUserName() throws Exception {
+        NewUser newUser = new NewUser();
+        newUser.setUsername("A");
+        newUser.setPassword(VALID_PASSWORD);
+        newUser.setConfirmPassword(VALID_PASSWORD);
+        newUser.setPageLayout(VALID_PAGELAYOUT);
+        Errors errors = new BindException(newUser, NEW_USER);
+        expect(mockUserService.getUserByUsername("A")).andReturn(null);
+        replay(mockUserService);
+
+        newAccountValidator.validate(newUser, errors);
+
+        assertTrue("Validation errors", errors.hasErrors());
+        assertNotNull(errors.getFieldError(FIELD_USERNAME));
+    }
+
+    @Test
+    public void testValidationFailsOnShortPassword() throws Exception {
+        NewUser newUser = new NewUser();
+        newUser.setUsername(VALID_NAME);
+        newUser.setPassword("123");
+        newUser.setConfirmPassword("123");
+        newUser.setPageLayout(VALID_PAGELAYOUT);
+        Errors errors = new BindException(newUser, NEW_USER);
+        expect(mockUserService.getUserByUsername(VALID_NAME)).andReturn(null);
+        replay(mockUserService);
+
+        newAccountValidator.validate(newUser, errors);
+
+        assertTrue("Validation errors", errors.hasErrors());
+        assertNotNull(errors.getFieldError(FIELD_PASSWORD));
+    }
+
+    @Test
+    public void testValidationFailsOnNonMatchingPassword() throws Exception {
+        NewUser newUser = new NewUser();
+        newUser.setUsername(VALID_NAME);
+        newUser.setPassword(VALID_PASSWORD);
+        newUser.setConfirmPassword("doesnotmatch");
+        newUser.setPageLayout(VALID_PAGELAYOUT);
+        Errors errors = new BindException(newUser, NEW_USER);
+        expect(mockUserService.getUserByUsername(VALID_NAME)).andReturn(null);
+        replay(mockUserService);
+
+        newAccountValidator.validate(newUser, errors);
+
+        assertTrue("Validation errors", errors.hasErrors());
+        assertNotNull(errors.getFieldError(FIELD_CONFIRM_PASSWORD));
+    }
+
+    @Before
+    public void setup() {
+        mockUserService = createMock("mockUserService", UserService.class);
+        newAccountValidator = new NewAccountValidator(mockUserService);
+    }
+
+
+}



Mime
View raw message