Skip to content
Snippets Groups Projects
Commit 97767d14 authored by srosse's avatar srosse
Browse files

OO-5071: catch more exceptions parsing date user attribute

parent 82c7607a
No related branches found
No related tags found
No related merge requests found
......@@ -62,11 +62,6 @@ public class DatePropertyHandler extends AbstractUserPropertyHandler {
*/
private DateFormat INTERNAL_DATE_FORMATTER = new SimpleDateFormat("yyyyMMdd", Locale.GERMAN);
/**
* @Override
* @see org.olat.user.AbstractUserPropertyHandler#getUserProperty(org.olat.core.id.User, java.util.Locale)
*/
@Override
public String getUserProperty(User user, Locale locale) {
Date date = decode(getInternalValue(user));
......@@ -75,28 +70,18 @@ public class DatePropertyHandler extends AbstractUserPropertyHandler {
return Formatter.getInstance(locale).formatDate(date);
}
/**
* @see org.olat.user.propertyhandlers.UserPropertyHandler#updateUserFromFormItem(org.olat.core.id.User, org.olat.core.gui.components.form.flexible.FormItem)
*/
@Override
public void updateUserFromFormItem(User user, FormItem formItem) {
String internalValue = getStringValue(formItem);
setInternalValue(user, internalValue);
}
/**
* @see org.olat.user.propertyhandlers.UserPropertyHandler#getStringValue(org.olat.core.gui.components.form.flexible.FormItem)
*/
@Override
public String getStringValue(FormItem formItem) {
Date date = ((org.olat.core.gui.components.form.flexible.elements.DateChooser) formItem).getDate();
return encode(date);
}
/**
* @see org.olat.user.propertyhandlers.UserPropertyHandler#getStringValue(java.lang.String, java.util.Locale)
*/
@Override
public String getStringValue(String displayValue, Locale locale) {
if (StringHelper.containsNonWhitespace(displayValue)) {
......@@ -112,11 +97,6 @@ public class DatePropertyHandler extends AbstractUserPropertyHandler {
return null;
}
/**
*
* @see org.olat.user.propertyhandlers.UserPropertyHandler#addFormItem(java.util.Locale, org.olat.core.id.User, java.lang.String, boolean, org.olat.core.gui.components.form.flexible.FormItemContainer)
*/
@Override
public FormItem addFormItem(Locale locale, final User user, String usageIdentifyer, boolean isAdministrativeUser, FormItemContainer formItemContainer) {
org.olat.core.gui.components.form.flexible.elements.DateChooser dateElem = null;
......@@ -140,11 +120,6 @@ public class DatePropertyHandler extends AbstractUserPropertyHandler {
return dateElem;
}
/**
* @see org.olat.user.propertyhandlers.UserPropertyHandler#isValid(org.olat.core.gui.components.form.flexible.FormItem, java.util.Map)
*/
@Override
public boolean isValid(User user, FormItem formItem, Map<String,String> formContext) {
......@@ -155,9 +130,9 @@ public class DatePropertyHandler extends AbstractUserPropertyHandler {
}
List<ValidationStatus> validation = new ArrayList<>();
dateElem.validate(validation);
if (validation.size()==0){
if (validation.isEmpty()){
return true;
}else{
} else {
// errorkey should be set by dateElem.validate formItem.setErrorKey(i18nFormElementLabelKey()+ ".error", null);
return false;
}
......@@ -180,18 +155,15 @@ public class DatePropertyHandler extends AbstractUserPropertyHandler {
* @return
*/
private Date decode(String value) {
if ( ! StringHelper.containsNonWhitespace(value)) return null;
if (!StringHelper.containsNonWhitespace(value)) return null;
try {
return INTERNAL_DATE_FORMATTER.parse(value.trim());
} catch (ParseException e) {
} catch (ParseException | NumberFormatException e) {
log.warn("Could not parse BirthDayField from database", e);
return null;
}
}
/**
* @see org.olat.user.propertyhandlers.UserPropertyHandler#isValidValue(java.lang.String, org.olat.core.gui.components.form.ValidationError, java.util.Locale)
*/
@Override
public boolean isValidValue(User user, String value, ValidationError validationError, Locale locale) {
if (StringHelper.containsNonWhitespace(value)) {
......@@ -199,7 +171,7 @@ public class DatePropertyHandler extends AbstractUserPropertyHandler {
df.setLenient(false);
try {
df.parse(value.trim());
} catch (ParseException e) {
} catch (ParseException | NumberFormatException e) {
validationError.setErrorKey(i18nFormElementLabelKey()+ ".error");
return false;
}
......
......@@ -552,6 +552,7 @@ import org.junit.runners.Suite;
org.olat.shibboleth.handler.FirstValueHandlerTest.class,
org.olat.shibboleth.handler.SchacGenderHandlerTest.class,
org.olat.user.UserManagerImplTest.class,
org.olat.user.propertyhandlers.DatePropertyHandlerTest.class,
org.olat.user.propertyhandlers.LinkedinPropertyHandlerTest.class,
org.olat.core.gui.components.form.flexible.impl.elements.FileElementRendererTest.class,
/**
......
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.user.propertyhandlers;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.olat.user.UserImpl;
/**
*
* Initial date: 13 nov. 2020<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
@RunWith(Parameterized.class)
public class DatePropertyHandlerTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "", null },
{ ".", null },
{ ".67", null },
{ "\u0009", null },
{ "19990809", "09.08.1999" },
});
}
private String date;
private String userDate;
public DatePropertyHandlerTest(String date, String userDate) {
this.date = date;
this.userDate = userDate;
}
@Test
public void getUserProperty() {
UserImpl user = new UserImpl();
user.setUserProperty("birthDay", date);
DatePropertyHandler handler = new DatePropertyHandler();
handler.setName("birthDay");
String val = handler.getUserProperty(user, Locale.GERMAN);
Assert.assertEquals(val, userDate);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment