You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rave.apache.org by ja...@apache.org on 2011/08/16 14:37:11 UTC

svn commit: r1158237 - in /incubator/rave/trunk/rave-portal/src: main/java/org/apache/rave/portal/web/validator/ test/java/org/apache/rave/portal/web/validator/

Author: jasha
Date: Tue Aug 16 12:37:11 2011
New Revision: 1158237

URL: http://svn.apache.org/viewvc?rev=1158237&view=rev
Log:
RAVE-192 added junit tests for UserProfileValidator
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/UserProfileValidatorTest.java
Modified:
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/UserProfileValidator.java
    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/UserProfileValidator.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/UserProfileValidator.java?rev=1158237&r1=1158236&r2=1158237&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/UserProfileValidator.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/UserProfileValidator.java Tue Aug 16 12:37:11 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.User;
 import org.apache.rave.portal.service.UserService;
 import org.slf4j.Logger;
@@ -33,56 +31,51 @@ import org.springframework.validation.Va
 
 public class UserProfileValidator implements Validator {
 
-	 protected final Logger logger=LoggerFactory.getLogger(getClass());
-	 
-	 private UserService userService;
-
-	 @Autowired
-	 public UserProfileValidator(UserService userService) {
-		 this.userService = userService;
-	 }
-	 
-	 public boolean supports(Class aClass){
-		  return UserProfileValidator.class.equals(aClass);
-	 }
-	 
-	 public void validate(Object obj, Errors errors){
-		  logger.debug("Validator called");
-		  User user = (User) obj;
-		  List<ObjectError> errorList = new ArrayList<ObjectError>();
-		  
-		  //check if the password is null
-		  if(user.getPassword() == "") {
-			  errors.rejectValue("password", "password.required");
-			  logger.error("Password required");
-		  }
-		  
-		  //check if the password length is less than 4
-		  else if(user.getPassword().length() < 4) {
-			  errors.rejectValue("password", "password.invalid.length");
-			  logger.error("Password must be at least 4 characters long");
-		  }
-		  
-		  //check if the confirm password is null
-		  if(user.getConfirmPassword() == "") {
-			  errors.rejectValue("confirmPassword", "confirmPassword.required");
-			  logger.error("Confirm Password required");
-		  }
-		  
-		  //check if the confirm password matches the previous entered password
-		  if(user.getConfirmPassword().length() != user.getPassword().length() || user.getConfirmPassword().compareTo(user.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 UserProfileValidator(UserService userService) {
+        this.userService = userService;
+    }
+
+    public boolean supports(Class aClass) {
+        return User.class.isAssignableFrom(aClass);
+    }
+
+    public void validate(Object obj, Errors errors) {
+        logger.debug("Validator called");
+        User user = (User) obj;
+
+        //check if the password is null or empty
+        if (StringUtils.isBlank(user.getPassword())) {
+            errors.rejectValue("password", "password.required");
+            logger.info("Password required");
+        }
+        //check if the password length is less than 4
+        else if (user.getPassword().length() < 4) {
+            errors.rejectValue("password", "password.invalid.length");
+            logger.info("Password must be at least 4 characters long");
+        }
+        //check if the confirm password is null or empty
+        if (StringUtils.isBlank(user.getConfirmPassword())) {
+            errors.rejectValue("confirmPassword", "confirmPassword.required");
+            logger.info("Confirm Password required");
+        }
+
+        //check if the confirm password matches the previous entered password
+        if (user.getConfirmPassword() != null && !(user.getConfirmPassword().equals(user.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

Modified: 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=1158237&r1=1158236&r2=1158237&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/NewAccountValidatorTest.java (original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/NewAccountValidatorTest.java Tue Aug 16 12:37:11 2011
@@ -51,7 +51,8 @@ public class NewAccountValidatorTest {
 
     @Test
     public void testSupports() throws Exception {
-        assertTrue(newAccountValidator.supports(NewUser.class));
+        assertTrue("Can validate org.apache.rave.portal.model.NewUser",
+                newAccountValidator.supports(NewUser.class));
     }
 
     @Test

Added: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/UserProfileValidatorTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/UserProfileValidatorTest.java?rev=1158237&view=auto
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/UserProfileValidatorTest.java (added)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/UserProfileValidatorTest.java Tue Aug 16 12:37:11 2011
@@ -0,0 +1,113 @@
+/*
+ * 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.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;
+
+/**
+ * Test class for {@link UserProfileValidator}
+ */
+public class UserProfileValidatorTest {
+    private static final String VALID_NAME = "valid.name";
+    private static final String VALID_PASSWORD = "valid.password";
+    private static final String FIELD_PASSWORD = "password";
+    private static final String FIELD_CONFIRM_PASSWORD = "confirmPassword";
+    private static final String USER = "user";
+
+    private UserProfileValidator validator;
+
+    @Test
+    public void testSupports() throws Exception {
+        assertTrue("Can validate org.apache.rave.portal.model.User", validator.supports(User.class));
+    }
+
+    @Test
+    public void testValidate() throws Exception {
+        User user = new User();
+        user.setUsername(VALID_NAME);
+        user.setPassword(VALID_PASSWORD);
+        user.setConfirmPassword(VALID_PASSWORD);
+
+        Errors errors = new BindException(user, USER);
+        validator.validate(user, errors);
+
+        assertFalse("No errors", errors.hasErrors());
+    }
+
+    @Test
+    public void testValidateFailsOnEmptyPassword() throws Exception {
+        User user = new User();
+        user.setUsername(VALID_NAME);
+
+        Errors errors = new BindException(user, USER);
+        validator.validate(user, errors);
+
+        assertTrue("Validation errors", errors.hasErrors());
+        assertNotNull(errors.getFieldError(FIELD_PASSWORD));
+
+    }
+
+    @Test
+    public void testValidateFailsOnShortPassword() throws Exception {
+        User user = new User();
+        user.setUsername(VALID_NAME);
+        user.setPassword("123");
+        user.setPassword("123");
+
+        Errors errors = new BindException(user, USER);
+        validator.validate(user, errors);
+
+        assertTrue("Validation errors", errors.hasErrors());
+        assertNotNull(errors.getFieldError(FIELD_PASSWORD));
+
+    }
+
+    @Test
+    public void testValidateFailsOnPasswordMismatch() throws Exception {
+        User user = new User();
+        user.setUsername(VALID_NAME);
+        user.setPassword(VALID_PASSWORD);
+        user.setConfirmPassword("DoesNotMatch");
+
+        Errors errors = new BindException(user, USER);
+        validator.validate(user, errors);
+
+        assertTrue("Validation errors", errors.hasErrors());
+        assertNotNull(errors.getFieldError(FIELD_CONFIRM_PASSWORD));
+
+    }
+
+
+    @Before
+    public void setUp() throws Exception {
+        UserService mockUserService = createMock(UserService.class);
+        validator = new UserProfileValidator(mockUserService);
+    }
+}