From 272e867c8d96fb9ce70bd07d78f5f9e752917334 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Thu, 10 Oct 2019 14:45:01 +0200 Subject: [PATCH] Revamp the way rss/atom/json dates are parsed and fix date tests --- .../readrops/app/activities/ItemActivity.java | 2 +- .../app/adapters/MainItemListAdapter.java | 2 +- .../app/repositories/LocalFeedRepository.java | 2 +- .../com/readrops/app/utils/DateUtils.java | 51 ++++++++++++------- .../com/readrops/app/utils/ItemMatcher.java | 40 ++++++--------- .../java/com/readrops/app/DateUtilsTest.java | 44 ++++++++++++++++ .../com/readrops/app/ExampleUnitTest.java | 50 ------------------ .../readropslibrary/utils/ParseException.java | 5 ++ 8 files changed, 101 insertions(+), 95 deletions(-) create mode 100644 app/src/test/java/com/readrops/app/DateUtilsTest.java delete mode 100644 app/src/test/java/com/readrops/app/ExampleUnitTest.java create mode 100644 readropslibrary/src/main/java/com/readrops/readropslibrary/utils/ParseException.java diff --git a/app/src/main/java/com/readrops/app/activities/ItemActivity.java b/app/src/main/java/com/readrops/app/activities/ItemActivity.java index 7293a22c..afa79e07 100644 --- a/app/src/main/java/com/readrops/app/activities/ItemActivity.java +++ b/app/src/main/java/com/readrops/app/activities/ItemActivity.java @@ -120,7 +120,7 @@ public class ItemActivity extends AppCompatActivity { this.itemWithFeed = itemWithFeed; Item item = itemWithFeed.getItem(); - date.setText(DateUtils.formatedDateTimeByLocal(item.getPubDate())); + date.setText(DateUtils.formattedDateTimeByLocal(item.getPubDate())); if (item.getImageLink() == null) toolbar.setTitle(itemWithFeed.getFeedName()); diff --git a/app/src/main/java/com/readrops/app/adapters/MainItemListAdapter.java b/app/src/main/java/com/readrops/app/adapters/MainItemListAdapter.java index 0cbba064..fa18d3f8 100644 --- a/app/src/main/java/com/readrops/app/adapters/MainItemListAdapter.java +++ b/app/src/main/java/com/readrops/app/adapters/MainItemListAdapter.java @@ -288,7 +288,7 @@ public class MainItemListAdapter extends PagedListAdapter 0) { + if (item.getMediaContents() != null && !item.getMediaContents().isEmpty()) { for (RSSMediaContent mediaContent : item.getMediaContents()) { if (mediaContent.getMedium() != null && Utils.isTypeImage(mediaContent.getMedium())) { newItem.setImageLink(mediaContent.getUrl()); @@ -135,7 +119,11 @@ public final class ItemMatcher { dbItem.setGuid(item.getId()); dbItem.setTitle(Jsoup.parse(item.getTitle()).text().trim()); - dbItem.setPubDate(DateUtils.stringToDateTime(item.getUpdated(), DateUtils.ATOM_JSON_DATE_FORMAT)); + try { + dbItem.setPubDate(DateUtils.stringToLocalDateTime(item.getUpdated())); + } catch (Exception e) { + throw new ParseException(); + } dbItem.setLink(item.getUrl()); dbItem.setFeedId(feed.getId()); @@ -160,7 +148,11 @@ public final class ItemMatcher { dbItem.setGuid(item.getId()); dbItem.setTitle(Jsoup.parse(item.getTitle()).text().trim()); - dbItem.setPubDate(DateUtils.stringToDateTime(item.getPubDate(), DateUtils.ATOM_JSON_DATE_FORMAT)); + try { + dbItem.setPubDate(DateUtils.stringToLocalDateTime(item.getPubDate())); + } catch (Exception e) { + throw new ParseException(); + } dbItem.setLink(item.getUrl()); diff --git a/app/src/test/java/com/readrops/app/DateUtilsTest.java b/app/src/test/java/com/readrops/app/DateUtilsTest.java new file mode 100644 index 00000000..3ecfc545 --- /dev/null +++ b/app/src/test/java/com/readrops/app/DateUtilsTest.java @@ -0,0 +1,44 @@ +package com.readrops.app; + +import com.readrops.app.utils.DateUtils; + +import org.joda.time.LocalDateTime; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class DateUtilsTest { + + @Test + public void rssDateTest() { + LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46); + + assertEquals(0, dateTime.compareTo(DateUtils.stringToLocalDateTime("Fri, 04 Jan 2019 22:21:46 GMT"))); + } + + @Test + public void rssDate2Test() { + LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46); + + assertEquals(0, dateTime.compareTo(DateUtils.stringToLocalDateTime("Fri, 04 Jan 2019 22:21:46 +0000"))); + } + + @Test + public void atomJsonDateTest() { + LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46); + + assertEquals(0, dateTime.compareTo(DateUtils.stringToLocalDateTime("2019-01-04T22:21:46+00:00"))); + } + + @Test + public void atomJsonDate2Test() { + LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46); + + assertEquals(0, dateTime.compareTo(DateUtils.stringToLocalDateTime("2019-01-04T22:21:46-0000"))); + } +} \ No newline at end of file diff --git a/app/src/test/java/com/readrops/app/ExampleUnitTest.java b/app/src/test/java/com/readrops/app/ExampleUnitTest.java deleted file mode 100644 index 2aed6884..00000000 --- a/app/src/test/java/com/readrops/app/ExampleUnitTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.readrops.app; - -import com.readrops.app.utils.DateUtils; - -import org.joda.time.DateTimeZone; -import org.joda.time.LocalDateTime; -import org.junit.Test; - -import java.text.ParseException; - -import static org.junit.Assert.assertEquals; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - - @Test - public void rssDateTest() { - try { - LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46); - - assertEquals(0, dateTime.compareTo(DateUtils.stringToDateTime("Fri, 04 Jan 2019 22:21:46 +0000", DateUtils.RSS_2_DATE_FORMAT))); - assertEquals(0, dateTime.compareTo(DateUtils.stringToDateTime("Fri, 04 Jan 2019 22:21:46 GMT", DateUtils.RSS_2_DATE_FORMAT_2))); - } catch (ParseException e) { - e.printStackTrace(); - } - } - - @Test - public void atomJsonDateTest() { - try { - LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46); - - assertEquals(0, dateTime.compareTo(DateUtils.stringToDateTime("2019-01-04T22:21:46+00:00", DateUtils.ATOM_JSON_DATE_FORMAT))); - assertEquals(0, dateTime.compareTo(DateUtils.stringToDateTime("2019-01-04T22:21:46-0000", DateUtils.ATOM_JSON_DATE_FORMAT))); - } catch (ParseException e) { - e.printStackTrace(); - } - } - - @Test - public void timeStamptoDateTest() { - LocalDateTime localDateTime = new LocalDateTime(1367270544 * 1000L, DateTimeZone.getDefault()); - - assertEquals(0, localDateTime.compareTo(new LocalDateTime(2013, 4, 29, 21, 22, 24))); - } -} \ No newline at end of file diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/ParseException.java b/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/ParseException.java new file mode 100644 index 00000000..67d60f1e --- /dev/null +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/ParseException.java @@ -0,0 +1,5 @@ +package com.readrops.readropslibrary.utils; + +public class ParseException extends Exception { + +}