diff --git a/src/main/java/org/olat/user/propertyhandlers/DatePropertyHandler.java b/src/main/java/org/olat/user/propertyhandlers/DatePropertyHandler.java index c77d1153d7eb1ee09b3c2bd18f1d3136b6fb220b..49f5cedf3b702143d0a3e7b20e27faaaaad91a28 100644 --- a/src/main/java/org/olat/user/propertyhandlers/DatePropertyHandler.java +++ b/src/main/java/org/olat/user/propertyhandlers/DatePropertyHandler.java @@ -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; } diff --git a/src/test/java/org/olat/test/AllTestsJunit4.java b/src/test/java/org/olat/test/AllTestsJunit4.java index cc9d7b0ee65c373ca27db04a9958c111c8705c42..fb37e5d1d474ef528d0eb3e2ae756c704a25591d 100644 --- a/src/test/java/org/olat/test/AllTestsJunit4.java +++ b/src/test/java/org/olat/test/AllTestsJunit4.java @@ -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, /** diff --git a/src/test/java/org/olat/user/propertyhandlers/DatePropertyHandlerTest.java b/src/test/java/org/olat/user/propertyhandlers/DatePropertyHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..dff1ab2272e7de2e6ca92206bce8cc00793d40fd --- /dev/null +++ b/src/test/java/org/olat/user/propertyhandlers/DatePropertyHandlerTest.java @@ -0,0 +1,71 @@ +/** + * <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); + } +}