Skip to content

Commit ef1d1d0

Browse files
HW-52174. Changed due to the review
1 parent 22f9f80 commit ef1d1d0

File tree

4 files changed

+37
-107
lines changed

4 files changed

+37
-107
lines changed

ui/src/main/java/com/hyperwallet/android/ui/view/widget/DateParseException.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

ui/src/main/java/com/hyperwallet/android/ui/view/widget/DateUtil.java

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.text.ParseException;
2727
import java.text.SimpleDateFormat;
2828
import java.util.Calendar;
29-
import java.util.Date;
3029
import java.util.Locale;
3130

3231
/**
@@ -51,18 +50,13 @@ public DateUtil() {
5150
*
5251
* @param serverDate Date from server
5352
* @return String date in the format by BestDateTimePattern or dd MMMM yyyy otherwise
54-
* @throws DateParseException unable to convert server date to widget format
5553
*/
5654
@NonNull
57-
String convertDateFromServerToWidgetFormat(@Nullable final String serverDate) throws DateParseException {
58-
try {
59-
if (isServerDateValid(serverDate)) {
60-
final Calendar calendar = Calendar.getInstance();
61-
calendar.setTime(mServerDateFormat.parse(serverDate));
62-
return mWidgetDateFormat.format(calendar.getTime());
63-
}
64-
} catch (NumberFormatException | ParseException | IndexOutOfBoundsException e) {
65-
throw new DateParseException("Unable to convert date from server to widget format", e.getCause());
55+
String convertDateFromServerToWidgetFormat(@Nullable final String serverDate) throws ParseException {
56+
if (!TextUtils.isEmpty(serverDate)) {
57+
final Calendar calendar = Calendar.getInstance();
58+
calendar.setTime(mServerDateFormat.parse(serverDate));
59+
return mWidgetDateFormat.format(calendar.getTime());
6660
}
6761
return "";
6862
}
@@ -87,32 +81,14 @@ public String buildDateFromDateDialogToServerFormat(final int year, final int mo
8781
*
8882
* @param serverDate Date from server
8983
* @return Calendar with date from server
90-
* @throws DateParseException unable to convert server date to calendar format
9184
*/
9285
@NonNull
93-
public Calendar convertDateFromServerFormatToCalendar(@Nullable final String serverDate) throws DateParseException {
86+
public Calendar convertDateFromServerFormatToCalendar(@Nullable final String serverDate) throws ParseException {
9487
final Calendar calendar = Calendar.getInstance();
95-
try {
96-
if (isServerDateValid(serverDate)) {
97-
calendar.setTime(mServerDateFormat.parse(serverDate));
98-
}
99-
} catch (ParseException e) {
100-
throw new DateParseException("Unable to convert server date to calendar format", e.getCause());
88+
if (!TextUtils.isEmpty(serverDate)) {
89+
calendar.setTime(mServerDateFormat.parse(serverDate));
10190
}
10291
return calendar;
10392
}
10493

105-
private boolean isServerDateValid(@Nullable final String serverDate) throws DateParseException {
106-
Date date = null;
107-
if (!TextUtils.isEmpty(serverDate)) {
108-
SimpleDateFormat formatter = new SimpleDateFormat(SERVER_DATE_PATTERN, Locale.getDefault());
109-
formatter.setLenient(Boolean.FALSE); // make it strict
110-
try {
111-
date = formatter.parse(serverDate);
112-
} catch (ParseException e) {
113-
throw new DateParseException("Unable to parse server date to date format", e.getCause());
114-
}
115-
}
116-
return date != null;
117-
}
11894
}

ui/src/main/java/com/hyperwallet/android/ui/view/widget/DateWidget.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import com.hyperwallet.android.hyperwallet_ui.R;
3434
import com.hyperwallet.android.model.meta.field.HyperwalletField;
3535

36+
import java.text.ParseException;
37+
3638
public class DateWidget extends AbstractWidget implements DateChangedListener {
3739

3840
private final DateUtil mDateUtil;
@@ -67,7 +69,7 @@ public View getView(@NonNull final ViewGroup viewGroup) {
6769
try {
6870
mEditText.setText(mDateUtil.convertDateFromServerToWidgetFormat(
6971
TextUtils.isEmpty(mDefaultValue) ? mValue = mField.getValue() : mDefaultValue));
70-
} catch (DateParseException e) {
72+
} catch (ParseException e) {
7173
mEditText.setText("");
7274
}
7375
setIdFromFieldLabel(mTextInputLayout);
@@ -115,7 +117,7 @@ public void onUpdate(@Nullable final String selectedDate) {
115117
mEditText.setText(mDateUtil.convertDateFromServerToWidgetFormat(selectedDate));
116118
mListener.saveTextChanged(getName(), getValue());
117119
mListener.valueChanged();
118-
} catch (DateParseException e) {
120+
} catch (ParseException e) {
119121
mEditText.setText(selectedDate);
120122
}
121123
}

ui/src/test/java/com/hyperwallet/android/ui/view/widget/DateUtilTest.java

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,59 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
55

6+
import org.junit.Rule;
67
import org.junit.Test;
8+
import org.junit.rules.ExpectedException;
79
import org.junit.runner.RunWith;
810
import org.robolectric.RobolectricTestRunner;
911

12+
import java.text.ParseException;
1013
import java.util.Arrays;
1114
import java.util.Calendar;
1215
import java.util.Collection;
13-
import java.util.List;
1416

1517
@RunWith(RobolectricTestRunner.class)
1618
public class DateUtilTest {
1719
private final DateUtil mDateUtil = new DateUtil();
1820

21+
@Rule
22+
public final ExpectedException mThrown = ExpectedException.none();
1923

2024
@Test
21-
public void testParseIncorrectDateFromServerToWidget() throws Exception {
22-
final List<String> inputParamList = buildParamsDateFromServerToWidget();
23-
for (String serverDate : inputParamList) {
24-
boolean isIncorrect = false;
25-
try {
26-
mDateUtil.convertDateFromServerToWidgetFormat(serverDate);
27-
} catch (DateParseException e) {
28-
isIncorrect = true;
29-
}
30-
assertThat(isIncorrect, is(true));
31-
}
32-
}
33-
34-
@Test
35-
public void testParseCorrectDateFromServerToWidget() throws Exception {
25+
public void testConvertDateFromServerToWidgetFormat() throws Exception {
3626
String serverDate = "2005-05-23";
3727
String widgetDate = "23 May 2005";
3828
assertThat(mDateUtil.convertDateFromServerToWidgetFormat(serverDate), is(widgetDate));
29+
}
30+
31+
@Test
32+
public void testBuildParamsDateFromServerToWidget_whenIncorrectDate() throws Exception {
33+
mThrown.expect(ParseException.class);
34+
mDateUtil.convertDateFromServerToWidgetFormat("1990-01");
35+
}
36+
37+
@Test
38+
public void testConvertDateFromServerToWidgetFormat_whenDateIsNullOrEmpty() throws Exception {
3939
assertThat(mDateUtil.convertDateFromServerToWidgetFormat(""), is(""));
4040
assertThat(mDateUtil.convertDateFromServerToWidgetFormat(null), is(""));
4141
}
4242

4343
@Test
44-
public void testParseIncorrectDateFromServerToCalendar() throws Exception {
45-
List<String> inputParamList = buildParamsDateFromServerToCalendar();
46-
for (String serverDate : inputParamList) {
47-
boolean isIncorrect = false;
48-
try {
49-
mDateUtil.convertDateFromServerFormatToCalendar(serverDate).getTime();
50-
} catch (DateParseException e) {
51-
isIncorrect = true;
52-
}
53-
assertThat(isIncorrect, is(true));
54-
}
44+
public void testBuildParamsDateFromServerToCalendar_whenIncorrectDate() throws Exception {
45+
mThrown.expect(ParseException.class);
46+
mDateUtil.convertDateFromServerFormatToCalendar("123-32").getTime();
5547
}
5648

5749
@Test
58-
public void testParseCorrectDateFromServerToCalendar() throws DateParseException {
50+
public void testConvertDateFromServerFormatToCalendar_whenDateIsNullOrEmpty() throws ParseException {
5951
assertThat(mDateUtil.convertDateFromServerFormatToCalendar(null).getTime().toString(),
6052
is(Calendar.getInstance().getTime().toString()));
6153
assertThat(mDateUtil.convertDateFromServerFormatToCalendar("").getTime().toString(),
6254
is(Calendar.getInstance().getTime().toString()));
55+
}
56+
57+
@Test
58+
public void testConvertDateFromServerFormatToCalendar() throws ParseException {
6359
String serverDate = "2005-05-23";
6460
final Calendar mayCalendar = Calendar.getInstance();
6561
mayCalendar.set(2005, 4, 23, 0, 0, 0);
@@ -68,7 +64,7 @@ public void testParseCorrectDateFromServerToCalendar() throws DateParseException
6864
}
6965

7066
@Test
71-
public void testParseDateFromDialogToServerFormat() {
67+
public void testBuildDateFromDateDialogToServerFormat() {
7268
String widgetDate;
7369
int year;
7470
int month;
@@ -83,34 +79,6 @@ public void testParseDateFromDialogToServerFormat() {
8379
}
8480
}
8581

86-
private List<String> buildParamsDateFromServerToWidget() {
87-
return Arrays.asList(
88-
"0",
89-
"1990-01",
90-
"1990-03-111",
91-
"10-20-1",
92-
"2190-13-1",
93-
"2190-00-1",
94-
"2190-01-00",
95-
"2190-01-0",
96-
"2190-01-32"
97-
);
98-
}
99-
100-
private List<String> buildParamsDateFromServerToCalendar() {
101-
return Arrays.asList(
102-
"0",
103-
"1990-01",
104-
"1990-03-111",
105-
"10-20-1",
106-
"19-1102-1",
107-
"2190-13-1",
108-
"2190-00-1",
109-
"2190-01-00",
110-
"2190-01-0"
111-
);
112-
}
113-
11482
private Collection<Object[]> buildParamsFromDialogToServer() {
11583
return Arrays.asList(new Object[][]{
11684
{1900, 0, 1, "1900-01-01"},

0 commit comments

Comments
 (0)