branch feature/1147-comments-sorting created (now e7c36e6)
This is an automated email from the git hooks/post-receive script. New change to branch feature/1147-comments-sorting in repository pollen_1.x. See http://git.chorem.org/pollen_1.x.git at e7c36e6 refs #1147 Implement a sort on poll's comments This branch includes the following new commits: new e7c36e6 refs #1147 Implement a sort on poll's comments The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit e7c36e65c5296dfd9a9ee260355ca10c9fa8be0a Author: Arnaud Thimel <thimel@codelutin.com> Date: Wed Nov 26 11:03:49 2014 +0100 refs #1147 Implement a sort on poll's comments -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/1147-comments-sorting in repository pollen_1.x. See http://git.chorem.org/pollen_1.x.git commit e7c36e65c5296dfd9a9ee260355ca10c9fa8be0a Author: Arnaud Thimel <thimel@codelutin.com> Date: Wed Nov 26 11:03:49 2014 +0100 refs #1147 Implement a sort on poll's comments --- .../business/persistence/CommentDAOImpl.java | 19 +++++++++- .../business/persistence/CommentDAOImplTest.java | 43 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/CommentDAOImpl.java b/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/CommentDAOImpl.java index 2298f4f..36be5ed 100644 --- a/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/CommentDAOImpl.java +++ b/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/CommentDAOImpl.java @@ -22,23 +22,38 @@ */ package org.chorem.pollen.business.persistence; +import com.google.common.base.Objects; import com.google.common.base.Preconditions; import org.chorem.pollen.entities.PollenDAOHelper; import org.nuiton.topia.TopiaException; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; +import java.util.Date; import java.util.List; public class CommentDAOImpl<E extends Comment> extends CommentDAOAbstract<E> { + protected static final Comparator<Comment> COMMENT_DATE_ASC_COMPARATOR = new Comparator<Comment>() { + @Override + public int compare(Comment o1, Comment o2) { + // AThimel 26/11/2014: For backward compatibility, in case some comment does not have a postDate (there is no notnull constraint) + Date d1 = Objects.firstNonNull(o1.getPostDate(), o1.getTopiaCreateDate()); + Date d2 = Objects.firstNonNull(o2.getPostDate(), o2.getTopiaCreateDate()); + return d1.compareTo(d2); + } + }; + public List<Comment> findAllComments(String pollId) throws TopiaException { Preconditions.checkNotNull(pollId); PollDAO dao = PollenDAOHelper.getPollDAO(context); Poll poll = dao.findByPollId(pollId); - return poll == null ? Collections.<Comment>emptyList() : new ArrayList<Comment>(poll.getComment()); - + List<Comment> comments = poll == null ? Collections.<Comment>emptyList() : new ArrayList<Comment>(poll.getComment()); + Collections.sort(comments, COMMENT_DATE_ASC_COMPARATOR); + return comments; } + } diff --git a/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/CommentDAOImplTest.java b/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/CommentDAOImplTest.java index 785e788..cba0908 100644 --- a/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/CommentDAOImplTest.java +++ b/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/CommentDAOImplTest.java @@ -27,6 +27,7 @@ import org.junit.Assert; import org.junit.Test; import org.nuiton.topia.TopiaContext; +import java.util.Date; import java.util.List; /** @@ -62,4 +63,46 @@ public class CommentDAOImplTest extends AbstractDAOTest { Assert.assertNotNull(actual); Assert.assertTrue(actual.isEmpty()); } + + @Test + // Test for http://chorem.org/issues/1147 + public void testFindAllCommentsAreSorted() throws Exception { + + TopiaContext tx = beginTransaction(); + CommentDAO dao = PollenDAOHelper.getCommentDAO(tx); + PollDAO pollDAO = PollenDAOHelper.getPollDAO(tx); + String pollId1 = "pollId1"; + Poll poll = pollDAO.create(Poll.PROPERTY_POLL_ID, pollId1); + + // Create 5 comments + for (int i = 0; i < 5; i++) { + + Date now = new Date(); + Comment comment = dao.create( + Comment.PROPERTY_TEXT, "comment" + i, + Comment.PROPERTY_POST_DATE, now); + poll.addComment(comment); + } + + // Add a new comment at the end of the list, but with older postDate, and check it is correctly placed in list + Comment comment = dao.create( + Comment.PROPERTY_TEXT, "comment" + 5, + Comment.PROPERTY_POST_DATE, new Date(0)); // 0 = january 1st 1970 + poll.addComment(comment); + + tx.commitTransaction(); + + List<Comment> comments = dao.findAllComments(pollId1); + Assert.assertNotNull(comments); + Assert.assertEquals(6, comments.size()); + + Date previousDate = new Date(-1); + for (Comment comm : comments) { + // Check that the comment's date is not before the previousDate + Assert.assertTrue(!comm.getPostDate().before(previousDate)); + previousDate = comm.getPostDate(); + } + + } + } -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
participants (1)
-
chorem.org scm