Skip to content

Commit

Permalink
Automated Error Report: NumberFormatException on AddPersonController -
Browse files Browse the repository at this point in the history
TRUNK-2751

Automated Error Report: NumberFormatException on AddPersonController - TRUNK-2752

Applying auto format - TRUNK-2751

Added a unit test to ensure that things work fine for a valid birthdate - TRUNK-2751
  • Loading branch information
wluyima committed May 10, 2013
1 parent 2983879 commit 83febc7
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
Expand Up @@ -15,6 +15,8 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -116,6 +118,8 @@ protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse
* form/command object to load into the request
*
* @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
* @should catch an invalid birthdate
* @should catch pass for a valid birthdate
*/
@Override
protected List<PersonListItem> formBackingObject(HttpServletRequest request) throws ServletException {
Expand All @@ -140,14 +144,28 @@ protected List<PersonListItem> formBackingObject(HttpServletRequest request) thr

Integer d = null;
birthdate = birthdate.trim();

String birthyear = "";
if (birthdate.length() > 6)
birthyear = birthdate.substring(6); //parse out the year. assuming XX-XX-XXXX

age = age.trim();
int birthyear = -1;

try {
//Do these if there's a value in the birthdate string
if (birthdate.length() > 0) {
Date birthdateFormatted = (Date) Context.getDateFormat().parse(birthdate);
Calendar calender = Calendar.getInstance();
calender.setTime(birthdateFormatted);
birthyear = calender.get(Calendar.YEAR);
}
}
catch (ParseException e) {
// In theory, this should never happen -- the date selector should never allowed the
// user set an invalid date, but never know the scripts could be broken
if (log.isDebugEnabled())
log.debug("Parse exception occurred : " + e);
invalidAgeFormat = true;
}

if (birthyear.length() > 3)
// -1 means the birth-year has not defined.
if (birthyear != -1)
d = Integer.valueOf(birthyear);
else if (age.length() > 0) {
Calendar c = Calendar.getInstance();
Expand Down
@@ -0,0 +1,77 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.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://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.web.controller.person;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.openmrs.web.test.BaseWebContextSensitiveTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

/**
* Tests for the {@link AddPersonController} which handles the Add Person.form page.
*/
public class AddPersonControllerTest extends BaseWebContextSensitiveTest {

/**
* @see AddPersonController#formBackingObject(HttpServletRequest)
* @verifies catch an invalid birthdate
*/
@Test
public void formBackingObject_shouldCatchAnInvalidBirthdate() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
HttpServletResponse response = new MockHttpServletResponse();

request.setParameter("addName", "Gayan Perera");
request.setParameter("addBirthdate", "03/07/199s");
request.setParameter("addGender", "M");
request.setParameter("personType", "patient");
request.setParameter("viewType", "edit");

AddPersonController controller = (AddPersonController) applicationContext.getBean("addPerson");
ModelAndView mav = controller.handleRequest(request, response);

assertNotNull(mav);
assertEquals("Person.birthdate.required", mav.getModel().get("errorMessage"));
}

/**
* @see AddPersonController#formBackingObject(HttpServletRequest)
* @verifies catch pass for a valid birthdate
*/
@Test
public void formBackingObject_shouldCatchPassForAValidBirthdate() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
HttpServletResponse response = new MockHttpServletResponse();

request.setParameter("addName", "Gayan Perera");
request.setParameter("addBirthdate", "03/07/1990");
request.setParameter("addGender", "M");
request.setParameter("personType", "patient");
request.setParameter("viewType", "edit");

AddPersonController controller = (AddPersonController) applicationContext.getBean("addPerson");
ModelAndView mav = controller.handleRequest(request, response);

assertNotNull(mav);
assertTrue(mav.getModel().isEmpty());
}
}

0 comments on commit 83febc7

Please sign in to comment.