From 07fe560d96f0330930dbe78c5199c35c1df389dc Mon Sep 17 00:00:00 2001 From: "Robert C. Martin" Date: Mon, 19 May 2014 11:26:35 -0500 Subject: [PATCH] E3 First Tomato --- .../PresentViewableCodecasts/content.txt | 8 ++++---- src/cleancoderscom/Codecast.java | 8 +++++--- src/cleancoderscom/Gateway.java | 2 +- src/cleancoderscom/MockGateway.java | 15 ++++++++++----- src/cleancoderscom/PresentCodecastUseCase.java | 7 +++++-- .../PresentCodecastUseCaseTest.java | 7 +++++-- .../fixtures/CodecastPresentation.java | 4 ++-- src/cleancoderscom/fixtures/GivenCodecasts.java | 8 ++++++-- 8 files changed, 38 insertions(+), 21 deletions(-) diff --git a/FitNesseRoot/CleanCoders/EpisodeTwoAndThreePresentCodecasts/PresentViewableCodecasts/content.txt b/FitNesseRoot/CleanCoders/EpisodeTwoAndThreePresentCodecasts/PresentViewableCodecasts/content.txt index ff4dcd4..776f0f2 100644 --- a/FitNesseRoot/CleanCoders/EpisodeTwoAndThreePresentCodecasts/PresentViewableCodecasts/content.txt +++ b/FitNesseRoot/CleanCoders/EpisodeTwoAndThreePresentCodecasts/PresentViewableCodecasts/content.txt @@ -11,8 +11,8 @@ |then the following codecasts will be presented for U| | Ordered query:of code casts | -| title | picture | description | viewable | downloadable | -| C | C | C | - | -| -| A | A | A | + | - | -| B | B | B | - | -| +| title | publication date | picture | description | viewable | downloadable | +| C | | C | C | - | -| +| A | | A | A | + | - | +| B | | B | B | - | -| diff --git a/src/cleancoderscom/Codecast.java b/src/cleancoderscom/Codecast.java index 44f2796..f23f582 100644 --- a/src/cleancoderscom/Codecast.java +++ b/src/cleancoderscom/Codecast.java @@ -1,14 +1,16 @@ package cleancoderscom; +import java.util.Date; + public class Codecast extends Entity { private String title; - private String publicationDate; + private Date publicationDate = new Date(); public void setTitle(String title) { this.title = title; } - public void setPublicationDate(String publicationDate) { + public void setPublicationDate(Date publicationDate) { this.publicationDate = publicationDate; } @@ -16,7 +18,7 @@ public String getTitle() { return title; } - public String getPublicationDate() { + public Date getPublicationDate() { return publicationDate; } } diff --git a/src/cleancoderscom/Gateway.java b/src/cleancoderscom/Gateway.java index 55b7ee1..a1e809c 100644 --- a/src/cleancoderscom/Gateway.java +++ b/src/cleancoderscom/Gateway.java @@ -3,7 +3,7 @@ import java.util.List; public interface Gateway { - List findAllCodecasts(); + List findAllCodecastsSortedChronologically(); void delete(Codecast codecast); diff --git a/src/cleancoderscom/MockGateway.java b/src/cleancoderscom/MockGateway.java index f5c0c4e..128fa9e 100644 --- a/src/cleancoderscom/MockGateway.java +++ b/src/cleancoderscom/MockGateway.java @@ -1,8 +1,6 @@ package cleancoderscom; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; +import java.util.*; public class MockGateway implements Gateway { @@ -16,8 +14,15 @@ public MockGateway() { licenses = new ArrayList(); } - public List findAllCodecasts() { - return codecasts; + public List findAllCodecastsSortedChronologically() + { + List sortedCodecasts = new ArrayList(codecasts); + Collections.sort(sortedCodecasts, new Comparator() { + public int compare(Codecast o1, Codecast o2) { + return o1.getPublicationDate().compareTo(o2.getPublicationDate()); + } + }); + return sortedCodecasts; } public void delete(Codecast codecast) { diff --git a/src/cleancoderscom/PresentCodecastUseCase.java b/src/cleancoderscom/PresentCodecastUseCase.java index f0e6c63..31fc807 100644 --- a/src/cleancoderscom/PresentCodecastUseCase.java +++ b/src/cleancoderscom/PresentCodecastUseCase.java @@ -1,17 +1,20 @@ package cleancoderscom; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; public class PresentCodecastUseCase { + private static SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/YYYY"); + public List presentCodecasts(User loggedInUser) { ArrayList presentableCodecasts = new ArrayList(); - List allCodecasts = Context.gateway.findAllCodecasts(); + List allCodecasts = Context.gateway.findAllCodecastsSortedChronologically(); for (Codecast codecast : allCodecasts) { PresentableCodecast cc = new PresentableCodecast(); cc.title = codecast.getTitle(); - cc.publicationDate = codecast.getPublicationDate(); + cc.publicationDate = dateFormat.format(codecast.getPublicationDate()); cc.isViewable = isLicensedToViewCodecast(loggedInUser, codecast); presentableCodecasts.add(cc); } diff --git a/src/cleancoderscom/PresentCodecastUseCaseTest.java b/src/cleancoderscom/PresentCodecastUseCaseTest.java index 2bac4a9..93573b2 100644 --- a/src/cleancoderscom/PresentCodecastUseCaseTest.java +++ b/src/cleancoderscom/PresentCodecastUseCaseTest.java @@ -3,6 +3,8 @@ import org.junit.Before; import org.junit.Test; +import java.util.Date; +import java.util.GregorianCalendar; import java.util.List; import static org.junit.Assert.*; @@ -53,12 +55,13 @@ public void presentingNoCodecasts() throws Exception { @Test public void presentOneCodecast() throws Exception { codecast.setTitle("Some Title"); - codecast.setPublicationDate("Tomorrow"); + Date now = new GregorianCalendar(2014, 4, 19).getTime(); + codecast.setPublicationDate(now); List presentableCodecasts = useCase.presentCodecasts(user); assertEquals(1, presentableCodecasts.size()); PresentableCodecast presentableCodecast = presentableCodecasts.get(0); assertEquals("Some Title", presentableCodecast.title); - assertEquals("Tomorrow", presentableCodecast.publicationDate); + assertEquals("5/19/2014", presentableCodecast.publicationDate); } @Test diff --git a/src/cleancoderscom/fixtures/CodecastPresentation.java b/src/cleancoderscom/fixtures/CodecastPresentation.java index 751db6c..01b92a7 100644 --- a/src/cleancoderscom/fixtures/CodecastPresentation.java +++ b/src/cleancoderscom/fixtures/CodecastPresentation.java @@ -45,11 +45,11 @@ public String presentationUser() { } public boolean clearCodecasts() { - List codecasts = Context.gateway.findAllCodecasts(); + List codecasts = Context.gateway.findAllCodecastsSortedChronologically(); for (Codecast codecast : new ArrayList(codecasts)) { Context.gateway.delete(codecast); } - return Context.gateway.findAllCodecasts().size() == 0; + return Context.gateway.findAllCodecastsSortedChronologically().size() == 0; } public int countOfCodecastsPresented() { diff --git a/src/cleancoderscom/fixtures/GivenCodecasts.java b/src/cleancoderscom/fixtures/GivenCodecasts.java index 9f3c7e6..17d6d89 100644 --- a/src/cleancoderscom/fixtures/GivenCodecasts.java +++ b/src/cleancoderscom/fixtures/GivenCodecasts.java @@ -3,9 +3,13 @@ import cleancoderscom.Codecast; import cleancoderscom.Context; +import java.text.ParseException; +import java.text.SimpleDateFormat; + public class GivenCodecasts { private String title; private String publicationDate; + private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY"); public void setTitle(String title) { this.title = title; @@ -15,10 +19,10 @@ public void setPublished(String publicationDate) { this.publicationDate = publicationDate; } - public void execute() { + public void execute() throws ParseException { Codecast codecast = new Codecast(); codecast.setTitle(title); - codecast.setPublicationDate(publicationDate); + codecast.setPublicationDate(dateFormat.parse(publicationDate)); Context.gateway.save(codecast); }