You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2006/07/25 11:08:12 UTC
svn commit: r425342 - in /myfaces/tomahawk/trunk/core/src:
main/java/org/apache/myfaces/custom/date/
main/resources/org/apache/myfaces/custom/calendar/resource/
main/tld/tomahawk-entities/ test/java/org/apache/myfaces/custom/date/
Author: mmarinschek
Date: Tue Jul 25 02:08:11 2006
New Revision: 425342
URL: http://svn.apache.org/viewvc?rev=425342&view=rev
Log:
thanks to cagatay civici for this patch (including nice test cases, very good)!
Added:
myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/UserDataTest.java
Modified:
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlInputDate.java
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlInputDateTag.java
myfaces/tomahawk/trunk/core/src/main/resources/org/apache/myfaces/custom/calendar/resource/popcalendar.js
myfaces/tomahawk/trunk/core/src/main/tld/tomahawk-entities/tomahawk_input_date_attributes.xml
myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/HtmlDateRenderCactus.java
myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/HtmlDateRendererTest.java
Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java?rev=425342&r1=425341&r2=425342&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java Tue Jul 25 02:08:11 2006
@@ -73,7 +73,7 @@
HtmlInputDate inputDate = (HtmlInputDate) uiComponent;
Locale currentLocale = facesContext.getViewRoot().getLocale();
UserData userData = (UserData) inputDate.getSubmittedValue();
- if( userData == null )
+ if( userData == null )
userData = inputDate.getUserData(currentLocale);
String type = inputDate.getType();
boolean ampm = inputDate.isAmpm();
@@ -164,8 +164,9 @@
}
int selectedMonth = userData.getMonth() == null ? -1 : Integer.parseInt(userData.getMonth())-1;
-
+
String[] months = HtmlCalendarRenderer.mapMonths(new DateFormatSymbols(currentLocale));
+ encodeEmptyInputMonthSelection(uiComponent, writer, selectedMonth);
for (int i = 0; i < months.length; i++) {
String monthName = months[i];
String monthNumber = Integer.toString(i+1);
@@ -186,6 +187,17 @@
writer.writeText("", null);
writer.endElement(HTML.SELECT_ELEM);
}
+
+ protected void encodeEmptyInputMonthSelection(UIComponent component, ResponseWriter writer, int selectedMonth) throws IOException{
+ writer.startElement(HTML.OPTION_ELEM, component);
+ writer.writeAttribute(HTML.VALUE_ATTR, "-1", null);
+
+ if(selectedMonth == -1)
+ writer.writeAttribute(HTML.SELECTED_ATTR, HTML.SELECTED_ATTR, null);
+
+ writer.writeText(((HtmlInputDate)component).getEmptyMonthSelection(), null);
+ writer.endElement(HTML.OPTION_ELEM);
+ }
protected void encodeInputYear(UIComponent uiComponent, ResponseWriter writer, String clientId,
UserData userData, boolean disabled, boolean readonly) throws IOException {
@@ -238,6 +250,7 @@
DateFormatSymbols symbols = new DateFormatSymbols(currentLocale);
int selectedAmpm = userData.getAmpm() == null ? -1 : Integer.parseInt(userData.getAmpm());
+ encodeEmtypAmpmChoice(uiComponent, writer, selectedAmpm);
encodeAmpmChoice(symbols, uiComponent, writer, Calendar.AM, selectedAmpm);
encodeAmpmChoice(symbols, uiComponent, writer, Calendar.PM, selectedAmpm);
@@ -247,6 +260,17 @@
writer.endElement(HTML.SELECT_ELEM);
}
+ protected void encodeEmtypAmpmChoice(UIComponent component, ResponseWriter writer, int selectedAmpm) throws IOException{
+ writer.startElement(HTML.OPTION_ELEM, component);
+ writer.writeAttribute(HTML.VALUE_ATTR, "-1", null);
+
+ if(selectedAmpm == -1)
+ writer.writeAttribute(HTML.SELECTED_ATTR, HTML.SELECTED_ATTR, null);
+
+ writer.writeText(((HtmlInputDate)component).getEmptyAmpmSelection(), null);
+ writer.endElement(HTML.OPTION_ELEM);
+ }
+
protected void encodePopupCalendarButton(FacesContext facesContext, UIComponent uiComponent, ResponseWriter writer, String clientId, Locale currentLocale) throws IOException{
DateFormatSymbols symbols = new DateFormatSymbols(currentLocale);
@@ -323,7 +347,6 @@
userData.setAmpm( (String) requestMap.get(clientId + ID_AMPM_POSTFIX) );
}
}
-
inputDate.setSubmittedValue( userData );
}
Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlInputDate.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlInputDate.java?rev=425342&r1=425341&r2=425342&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlInputDate.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlInputDate.java Tue Jul 25 02:08:11 2006
@@ -22,6 +22,7 @@
import java.util.Locale;
import java.util.TimeZone;
+import org.apache.commons.lang.StringUtils;
import org.apache.myfaces.component.UserRoleAware;
import org.apache.myfaces.component.UserRoleUtils;
import org.apache.myfaces.shared_tomahawk.util._ComponentUtils;
@@ -53,6 +54,8 @@
private Boolean _popupCalendar = null;
private String _timeZone = null;
private Boolean _ampm = null;
+ private String _emptyMonthSelection = null;
+ private String _emptyAmpmSelection = null;
private Boolean _disabled = null;
@@ -62,7 +65,7 @@
}
public UserData getUserData(Locale currentLocale){
- return new UserData((Date) getValue(), currentLocale, getTimeZone(), isAmpm());
+ return new UserData((Date) getValue(), currentLocale, getTimeZone(), isAmpm(), getType());
}
public String getType() {
@@ -103,6 +106,26 @@
public void setTimeZone(String timeZone){
_timeZone = timeZone;
}
+
+ public String getEmptyMonthSelection() {
+ if (_emptyMonthSelection != null) return _emptyMonthSelection;
+ ValueBinding vb = getValueBinding("emptyMonthSelection");
+ return vb != null ? _ComponentUtils.getStringValue(getFacesContext(), vb) : "";
+ }
+
+ public void setEmptyMonthSelection(String string) {
+ _emptyMonthSelection = string;
+ }
+
+ public String getEmptyAmpmSelection() {
+ if (_emptyAmpmSelection != null) return _emptyAmpmSelection;
+ ValueBinding vb = getValueBinding("emptyAmpmSelection");
+ return vb != null ? _ComponentUtils.getStringValue(getFacesContext(), vb) : "";
+ }
+
+ public void setEmptyAmpmSelection(String string) {
+ _emptyAmpmSelection = string;
+ }
public boolean isReadonly(){
if (_readonly != null) return _readonly.booleanValue();
@@ -148,7 +171,7 @@
}
public Object saveState(FacesContext context) {
- Object values[] = new Object[9];
+ Object values[] = new Object[11];
values[0] = super.saveState(context);
values[1] = _type;
values[2] = _popupCalendar;
@@ -158,6 +181,8 @@
values[6] = _visibleOnUserRole;
values[7] = _timeZone;
values[8] = _ampm;
+ values[9] = _emptyMonthSelection;
+ values[10] = _emptyAmpmSelection;
return values;
}
@@ -172,6 +197,8 @@
_visibleOnUserRole = (String)values[6];
_timeZone = (String)values[7];
_ampm = (Boolean)values[8];
+ _emptyMonthSelection = (String)values[9];
+ _emptyAmpmSelection = (String)values[10];
}
public static class UserData implements Serializable {
@@ -185,17 +212,21 @@
private TimeZone timeZone = null;
private String ampm;
private boolean uses_ampm;
+ private String type;
- public UserData(Date date, Locale currentLocale, String _timeZone, boolean uses_ampm){
+ public UserData(Date date, Locale currentLocale, String _timeZone, boolean uses_ampm, String type){
this.uses_ampm = uses_ampm;
- if( date == null )
- date = new Date();
+ this.type = type;
Calendar calendar = Calendar.getInstance(currentLocale);
if (_timeZone != null) {
timeZone = TimeZone.getTimeZone(_timeZone);
calendar.setTimeZone(timeZone);
}
+
+ if(date == null)
+ return;
+
calendar.setTime( date );
day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
month = Integer.toString(calendar.get(Calendar.MONTH)+1);
@@ -222,23 +253,36 @@
if (timeZone != null)
tempCalendar.setTimeZone(timeZone);
try{
- tempCalendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(day));
- tempCalendar.set(Calendar.MONTH,Integer.parseInt(month)-1);
- tempCalendar.set(Calendar.YEAR,Integer.parseInt(year));
- if (uses_ampm) {
- int int_hours = Integer.parseInt(hours);
- // ampm hours must be in range 0-11 to be handled right; we have to handle "12" specially
- if (int_hours == 12) {
- int_hours = 0;
+ if(!isSubmitValid(uses_ampm, type)) {
+ return null;
+ }
+
+ if(! (type.equals( "time" ) || type.equals( "short_time" )) ) {
+ tempCalendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(day));
+ tempCalendar.set(Calendar.MONTH,Integer.parseInt(month)-1);
+ tempCalendar.set(Calendar.YEAR,Integer.parseInt(year));
+ }
+
+ if(! type.equals( "date" )) {
+
+ if (uses_ampm) {
+ int int_hours = Integer.parseInt(hours);
+ // ampm hours must be in range 0-11 to be handled right; we have to handle "12" specially
+ if (int_hours == 12) {
+ int_hours = 0;
+ }
+ tempCalendar.set(Calendar.HOUR,int_hours);
+ tempCalendar.set(Calendar.AM_PM,Integer.parseInt(ampm));
+ } else {
+ tempCalendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hours));
}
- tempCalendar.set(Calendar.HOUR,int_hours);
- tempCalendar.set(Calendar.AM_PM,Integer.parseInt(ampm));
- } else {
- tempCalendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hours));
- }
- tempCalendar.set(Calendar.MINUTE,Integer.parseInt(minutes));
- tempCalendar.set(Calendar.SECOND,Integer.parseInt(seconds));
- tempCalendar.set(Calendar.MILLISECOND, 0);
+ tempCalendar.set(Calendar.MINUTE,Integer.parseInt(minutes));
+
+ if (type.equals("full") || type.equals("time")) {
+ tempCalendar.set(Calendar.SECOND,Integer.parseInt(seconds));
+ }
+ }
+ tempCalendar.set(Calendar.MILLISECOND, 0);
retDate = tempCalendar.getTime();
} catch (NumberFormatException e) {
throw new ParseException(e.getMessage(),0);
@@ -262,7 +306,40 @@
return "0"+i;
return Integer.toString(i);
}
-
+
+ private boolean isDateSubmitted(boolean usesAmpm, String type) {
+ boolean isDateSubmitted = ! (StringUtils.isEmpty(getDay()) && getMonth().equals("-1") && StringUtils.isEmpty(getYear()));
+ if(usesAmpm)
+ isDateSubmitted = isDateSubmitted || isAmpmSubmitted();
+ return isDateSubmitted;
+ }
+
+ private boolean isTimeSubmitted(boolean usesAmpm, String type) {
+ boolean isTimeSubmitted = ! (StringUtils.isEmpty(getHours()) && StringUtils.isEmpty(getMinutes()));
+ if(type.equals("time") || type.equals("full") || type.equals("both"))
+ isTimeSubmitted = isTimeSubmitted || ! StringUtils.isEmpty(getSeconds());
+ if(usesAmpm)
+ isTimeSubmitted = isTimeSubmitted || isAmpmSubmitted();
+ return isTimeSubmitted;
+ }
+
+ private boolean isSubmitValid(boolean usesAmpm, String type) {
+ if(type.equals("date"))
+ return isDateSubmitted(usesAmpm, type);
+ else if(type.equals("time") || (type.equals("short_time")))
+ return isTimeSubmitted(usesAmpm, type);
+ else if(type.equals("full") || type.equals("both"))
+ return isDateSubmitted(usesAmpm, type) || isTimeSubmitted(usesAmpm, type);
+ else
+ return false;
+ }
+
+ private boolean isAmpmSubmitted() {
+ if(getAmpm() == null)
+ return false;
+ else
+ return ! getAmpm().equals("-1");
+ }
public String getDay() {
return formatedInt( day );
@@ -310,6 +387,13 @@
}
public void setAmpm(String ampm) {
this.ampm = ampm;
+ }
+
+ public String getType() {
+ return type;
+ }
+ public void setType(String type) {
+ this.type = type;
}
}
}
Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlInputDateTag.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlInputDateTag.java?rev=425342&r1=425341&r2=425342&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlInputDateTag.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/date/HtmlInputDateTag.java Tue Jul 25 02:08:11 2006
@@ -38,6 +38,8 @@
private String ampm;
private String popupCalendar;
private String timeZone;
+ private String emptyMonthSelection;
+ private String emptyAmpmSelection;
// UIComponent attributes --> already implemented in UIComponentTagBase
@@ -62,6 +64,8 @@
ampm=null;
popupCalendar=null;
timeZone = null;
+ emptyMonthSelection=null;
+ emptyAmpmSelection=null;
}
protected void setProperties(UIComponent component) {
@@ -71,6 +75,8 @@
setBooleanProperty(component, "popupCalendar", popupCalendar);
setBooleanProperty(component, "ampm", ampm);
setStringProperty(component, "timeZone", timeZone);
+ setStringProperty(component, "emptyMonthSelection", emptyMonthSelection);
+ setStringProperty(component, "emptyAmpmSelection", emptyAmpmSelection);
setStringProperty(component, UserRoleAware.ENABLED_ON_USER_ROLE_ATTR, enabledOnUserRole);
setStringProperty(component, UserRoleAware.VISIBLE_ON_USER_ROLE_ATTR, visibleOnUserRole);
@@ -100,4 +106,21 @@
{
this.timeZone = timeZone;
}
+
+ public String getEmptyMonthSelection() {
+ return emptyMonthSelection;
+ }
+
+ public void setEmptyMonthSelection(String emptyMonthSelection) {
+ this.emptyMonthSelection = emptyMonthSelection;
+ }
+
+ public String getEmptyAmpmSelection() {
+ return emptyAmpmSelection;
+ }
+
+ public void setEmptyAmpmSelection(String emptyAmpmSelection) {
+ this.emptyAmpmSelection = emptyAmpmSelection;
+ }
+
}
Modified: myfaces/tomahawk/trunk/core/src/main/resources/org/apache/myfaces/custom/calendar/resource/popcalendar.js
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/resources/org/apache/myfaces/custom/calendar/resource/popcalendar.js?rev=425342&r1=425341&r2=425342&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/resources/org/apache/myfaces/custom/calendar/resource/popcalendar.js (original)
+++ myfaces/tomahawk/trunk/core/src/main/resources/org/apache/myfaces/custom/calendar/resource/popcalendar.js Tue Jul 25 02:08:11 2006
@@ -1300,9 +1300,9 @@
this.myFacesInputDateClientId = clientId;
this.dateFormat = format;
- this.selectedDate.date = parseInt(this._formatInt(document.getElementById(clientId + ".day").value), 10);
- this.selectedDate.month = parseInt(this._formatInt(document.getElementById(clientId + ".month").value), 10) - 1;
- this.selectedDate.year = parseInt(this._formatInt(document.getElementById(clientId + ".year").value), 10);
+ this.selectedDate.date = document.getElementById(clientId + ".day").value != "" ? parseInt(this._formatInt(document.getElementById(clientId + ".day").value), 10) : this.dateNow;
+ this.selectedDate.month = document.getElementById(clientId + ".month").value != "-1" ? parseInt(this._formatInt(document.getElementById(clientId + ".month").value), 10) - 1 : this.monthNow;
+ this.selectedDate.year = document.getElementById(clientId + ".year").value != "" ? parseInt(this._formatInt(document.getElementById(clientId + ".year").value), 10) : this.yearNow;
this.ctlNow = document.getElementById(clientId + ".day");
this._popUpCalendar_Show(document.getElementById(clientId + ".day"));
}
Modified: myfaces/tomahawk/trunk/core/src/main/tld/tomahawk-entities/tomahawk_input_date_attributes.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/tld/tomahawk-entities/tomahawk_input_date_attributes.xml?rev=425342&r1=425341&r2=425342&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/tld/tomahawk-entities/tomahawk_input_date_attributes.xml (original)
+++ myfaces/tomahawk/trunk/core/src/main/tld/tomahawk-entities/tomahawk_input_date_attributes.xml Tue Jul 25 02:08:11 2006
@@ -26,3 +26,19 @@
use 24hr time. Default false.
</description>
</attribute>
+ <attribute>
+ <name>emptyMonthSelection</name>
+ <required>false</required>
+ <rtexprvalue>false</rtexprvalue>
+ <description>
+ Label to be used when displaying an empty month selection
+ </description>
+ </attribute>
+ <attribute>
+ <name>emptyAmpmSelection</name>
+ <required>false</required>
+ <rtexprvalue>false</rtexprvalue>
+ <description>
+ Label to be used when displaying an empty ampm selection
+ </description>
+ </attribute>
Modified: myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/HtmlDateRenderCactus.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/HtmlDateRenderCactus.java?rev=425342&r1=425341&r2=425342&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/HtmlDateRenderCactus.java (original)
+++ myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/HtmlDateRenderCactus.java Tue Jul 25 02:08:11 2006
@@ -86,7 +86,7 @@
inputDate.setId("test");
inputDate.setType("date");
Date today = new Date();
- inputDate.setSubmittedValue(new UserData(today, Locale.ENGLISH, null,false));
+ inputDate.setSubmittedValue(new UserData(today, Locale.ENGLISH, null,false, "date"));
HtmlDateRenderer subject = new HtmlDateRenderer();
// decode
subject.decode(facesContext, inputDate);
Modified: myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/HtmlDateRendererTest.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/HtmlDateRendererTest.java?rev=425342&r1=425341&r2=425342&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/HtmlDateRendererTest.java (original)
+++ myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/HtmlDateRendererTest.java Tue Jul 25 02:08:11 2006
@@ -86,7 +86,7 @@
inputDate.setId("test");
inputDate.setType("date");
Date today = new Date();
- inputDate.setSubmittedValue(new UserData(today, Locale.ENGLISH, null, true));
+ inputDate.setSubmittedValue(new UserData(today, Locale.ENGLISH, null, true, "date"));
HtmlDateRenderer subject = new HtmlDateRenderer();
// setup the request map
Map map = new HashMap();
Added: myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/UserDataTest.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/UserDataTest.java?rev=425342&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/UserDataTest.java (added)
+++ myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/date/UserDataTest.java Tue Jul 25 02:08:11 2006
@@ -0,0 +1,210 @@
+package org.apache.myfaces.custom.date;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+
+import org.apache.myfaces.custom.date.HtmlInputDate.UserData;
+
+import junit.framework.TestCase;
+
+public class UserDataTest extends TestCase {
+
+ private UserData userData;
+
+ public void testConstructorDoesNotSetDateVariablesWhenTheDateIsNull() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", false, "date");
+ assertNull(userData.getYear());
+ assertNull(userData.getDay());
+ assertNull(userData.getHours());
+ }
+
+ public void testParseReturnsNullWhenTypeIfDateAndComponentNotUsed() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", false, "date");
+ userData.setDay("");
+ userData.setMonth("-1");
+ userData.setYear("");
+ try {
+ Date date = userData.parse();
+ assertNull(date);
+ } catch (ParseException e) {
+ fail();
+ }
+ }
+
+ public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeDate() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", false, "date");
+ userData.setDay("40");
+ userData.setMonth("-1");
+ userData.setYear("2005");
+ try {
+ userData.parse();
+ fail();
+ } catch (ParseException e) {
+ }
+ }
+
+ public void testParseGivesCorrectDateWhenValidDataIsEnteredForTypeDate() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", false, "date");
+ userData.setDay("20");
+ userData.setMonth("7");
+ userData.setYear("2006");
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(Calendar.DAY_OF_MONTH, 20);
+ calendar.set(Calendar.MONTH, 6);
+ calendar.set(Calendar.YEAR, 2006);
+
+ //only day, month and year is considered for equality
+ SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
+ try {
+ Date date1 = userData.parse();
+ Date date2 = calendar.getTime();
+ assertEquals(formatter.format(date1), formatter.format(date2));
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void testParseReturnsNullWhenTypeIsTimeAndComponentNotUsed() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", false, "time");
+ userData.setHours("");
+ userData.setMinutes("");
+ userData.setSeconds("");
+
+ try {
+ assertNull(userData.parse());
+ } catch (ParseException e) {
+ fail();
+ }
+ }
+
+ public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeTime() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", false, "time");
+ userData.setHours("25");
+ userData.setMinutes("");
+ userData.setSeconds("10");
+
+ try {
+ userData.parse();
+ fail();
+ } catch (ParseException e) {
+ }
+ }
+
+ public void testParseGivesCorrectDateWhenValidDataIsEnteredForTypeTime() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", false, "time");
+ userData.setHours("10");
+ userData.setMinutes("50");
+ userData.setSeconds("30");
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(Calendar.HOUR_OF_DAY, 11);
+ calendar.set(Calendar.MINUTE, 50);
+ calendar.set(Calendar.SECOND, 30);
+
+ //hour, minute, seconds is considered for equality
+ SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
+ try {
+ Date date1 = userData.parse();
+ Date date2 = calendar.getTime();
+ assertEquals(formatter.format(date1), formatter.format(date2));
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void testParseReturnsNullWhenTypeIsFullAndComponentNotUsed() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", true, "full");
+ userData.setDay("");
+ userData.setMonth("-1");
+ userData.setYear("");
+ userData.setHours("");
+ userData.setMinutes("");
+ userData.setSeconds("");
+ userData.setAmpm("-1");
+
+ try {
+ assertNull(userData.parse());
+ } catch (ParseException e) {
+ fail();
+ }
+ }
+
+ public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeFullCase1() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", true, "full");
+ //date is used, time is not
+ userData.setDay("40");
+ userData.setMonth("-1");
+ userData.setYear("");
+ userData.setHours("");
+ userData.setMinutes("");
+ userData.setSeconds("");
+ userData.setAmpm("-1");
+
+ try {
+ userData.parse();
+ fail();
+ } catch (ParseException e) {
+ }
+ }
+
+ public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeFullCase2() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", true, "full");
+ //date is used time is not
+ userData.setDay("");
+ userData.setMonth("0");
+ userData.setYear("2005");
+ userData.setHours("");
+ userData.setMinutes("");
+ userData.setSeconds("");
+ userData.setAmpm("-1");
+
+ try {
+ userData.parse();
+ fail();
+ } catch (ParseException e) {
+ }
+ }
+
+ public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeFullCase3() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", true, "full");
+ //date is used time is not
+ userData.setDay("5");
+ userData.setMonth("10");
+ userData.setYear("2005");
+ userData.setHours("");
+ userData.setMinutes("");
+ userData.setSeconds("");
+ userData.setAmpm("-1");
+
+ try {
+ userData.parse();
+ fail();
+ } catch (ParseException e) {
+ }
+ }
+
+ public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeFullCase4() {
+ userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", true, "full");
+ //date is not used but time is
+ userData.setDay("");
+ userData.setMonth("-1");
+ userData.setYear("");
+ userData.setHours("");
+ userData.setMinutes("30");
+ userData.setSeconds("");
+ userData.setAmpm("-1");
+
+ try {
+ userData.parse();
+ fail();
+ } catch (ParseException e) {
+ }
+ }
+
+
+
+}