Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

E3T3

  • Loading branch information...
commit 4b885f9b6787214078dc2a584bb3f2fcf1c98b4b 1 parent b2ac513
@unclebob unclebob authored
View
7 src/cleancoderscom/DownloadLicense.java
@@ -1,7 +0,0 @@
-package cleancoderscom;
-
-public class DownloadLicense extends License {
- public DownloadLicense(User user, Codecast codecast) {
- super(user, codecast);
- }
-}
View
12 src/cleancoderscom/License.java
@@ -1,15 +1,21 @@
package cleancoderscom;
public class License extends Entity {
+ public enum LicenseType {DOWNLOADING, VIEWING;}
+ private LicenseType type;
+
private User user;
private Codecast codecast;
-
- public License(User user, Codecast codecast) {
-
+ public License(LicenseType type, User user, Codecast codecast) {
+ this.type = type;
this.user = user;
this.codecast = codecast;
}
+ public LicenseType getType() {
+ return type;
+ }
+
public User getUser() {
return user;
}
View
34 src/cleancoderscom/PresentCodecastUseCase.java
@@ -4,34 +4,38 @@
import java.util.ArrayList;
import java.util.List;
+import static cleancoderscom.License.LicenseType.DOWNLOADING;
+import static cleancoderscom.License.LicenseType.VIEWING;
+
public class PresentCodecastUseCase {
private static SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy");
public List<PresentableCodecast> presentCodecasts(User loggedInUser) {
ArrayList<PresentableCodecast> presentableCodecasts = new ArrayList<PresentableCodecast>();
List<Codecast> allCodecasts = Context.gateway.findAllCodecastsSortedChronologically();
- for (Codecast codecast : allCodecasts) {
-
- PresentableCodecast cc = new PresentableCodecast();
- cc.title = codecast.getTitle();
- cc.publicationDate = dateFormat.format(codecast.getPublicationDate());
- cc.isViewable = isLicensedToViewCodecast(loggedInUser, codecast);
- cc.isDownloadable = isLicensedToDownloadCodecast(loggedInUser, codecast);
- presentableCodecasts.add(cc);
- }
+
+ for (Codecast codecast : allCodecasts)
+ presentableCodecasts.add(formatCodecast(loggedInUser, codecast));
+
return presentableCodecasts;
}
- public boolean isLicensedToViewCodecast(User user, Codecast codecast) {
- List<License> licenses = Context.gateway.findLicensesForUserAndCodecast(user, codecast);
- return !licenses.isEmpty();
+ private PresentableCodecast formatCodecast(User loggedInUser, Codecast codecast) {
+ PresentableCodecast cc = new PresentableCodecast();
+ cc.title = codecast.getTitle();
+ cc.publicationDate = dateFormat.format(codecast.getPublicationDate());
+ cc.isViewable = isLicensedFor(VIEWING, loggedInUser, codecast);
+ cc.isDownloadable = isLicensedFor(DOWNLOADING, loggedInUser, codecast);
+ return cc;
}
- public boolean isLicensedToDownloadCodecast(User user, Codecast codecast) {
+ public boolean isLicensedFor(License.LicenseType licenseType, User user, Codecast codecast) {
List<License> licenses = Context.gateway.findLicensesForUserAndCodecast(user, codecast);
- for (License l : licenses)
- if (l instanceof DownloadLicense)
+ for (License l : licenses) {
+ if (l.getType() == licenseType)
return true;
+ }
return false;
}
+
}
View
16 src/cleancoderscom/PresentCodecastUseCaseTest.java
@@ -7,6 +7,7 @@
import java.util.GregorianCalendar;
import java.util.List;
+import static cleancoderscom.License.LicenseType.*;
import static org.junit.Assert.*;
public class PresentCodecastUseCaseTest {
@@ -25,23 +26,23 @@ public void setUp() {
@Test
public void userWithoutViewLicense_cannotViewCodecast() throws Exception {
- assertFalse(useCase.isLicensedToViewCodecast(user, codecast));
+ assertFalse(useCase.isLicensedFor(VIEWING, user, codecast));
}
@Test
public void userWithViewLicense_canViewCodecast() throws Exception {
- License viewLicense = new License(user, codecast);
+ License viewLicense = new License(VIEWING, user, codecast);
Context.gateway.save(viewLicense);
- assertTrue(useCase.isLicensedToViewCodecast(user, codecast));
+ assertTrue(useCase.isLicensedFor(VIEWING, user, codecast));
}
@Test
public void userWithoutViewLicense_cannotViewOtherUsersCodecast() throws Exception {
User otherUser = Context.gateway.save(new User("otherUser"));
- License viewLicense = new License(user, codecast);
+ License viewLicense = new License(VIEWING, user, codecast);
Context.gateway.save(viewLicense);
- assertFalse(useCase.isLicensedToViewCodecast(otherUser, codecast));
+ assertFalse(useCase.isLicensedFor(VIEWING, otherUser, codecast));
}
@Test
@@ -73,7 +74,7 @@ public void presentedCodecastIsNotViewableIfNoLicense() throws Exception {
@Test
public void presentedCodecastIsViewableIfLicenseExists() throws Exception {
- Context.gateway.save(new License(user, codecast));
+ Context.gateway.save(new License(VIEWING, user, codecast));
List<PresentableCodecast> presentableCodecasts = useCase.presentCodecasts(user);
PresentableCodecast presentableCodecast = presentableCodecasts.get(0);
assertTrue(presentableCodecast.isViewable);
@@ -81,11 +82,12 @@ public void presentedCodecastIsViewableIfLicenseExists() throws Exception {
@Test
public void prestedCodecastIsDownloadableIfDownloadLicenseExists() throws Exception {
- License downloadLicense = new DownloadLicense(user, codecast);
+ License downloadLicense = new License(DOWNLOADING, user, codecast);
Context.gateway.save(downloadLicense);
List<PresentableCodecast> presentableCodecasts = useCase.presentCodecasts(user);
PresentableCodecast presentableCodecast = presentableCodecasts.get(0);
assertTrue(presentableCodecast.isDownloadable);
+ assertFalse(presentableCodecast.isViewable);
}
View
11 src/cleancoderscom/fixtures/CodecastPresentation.java
@@ -5,6 +5,9 @@
import java.util.ArrayList;
import java.util.List;
+import static cleancoderscom.License.LicenseType.DOWNLOADING;
+import static cleancoderscom.License.LicenseType.VIEWING;
+
public class CodecastPresentation {
private PresentCodecastUseCase useCase = new PresentCodecastUseCase();
public static GateKeeper gateKeeper = new GateKeeper();
@@ -31,17 +34,17 @@ public boolean loginUser(String username) {
public boolean createLicenseForViewing(String username, String codecastTitle) {
User user = Context.gateway.findUser(username);
Codecast codecast = Context.gateway.findCodecastByTitle(codecastTitle);
- License license = new License(user, codecast);
+ License license = new License(VIEWING, user, codecast);
Context.gateway.save(license);
- return useCase.isLicensedToViewCodecast(user, codecast);
+ return useCase.isLicensedFor(VIEWING, user, codecast);
}
public boolean createLicenseForDownloading(String username, String codecastTitle) {
User user = Context.gateway.findUser(username);
Codecast codecast = Context.gateway.findCodecastByTitle(codecastTitle);
- License license = new License(user, codecast);
+ License license = new License(DOWNLOADING, user, codecast);
Context.gateway.save(license);
- return useCase.isLicensedToDownloadCodecast(user, codecast);
+ return useCase.isLicensedFor(DOWNLOADING, user, codecast);
}
public String presentationUser() {
View
2  src/cleancoderscom/fixtures/OfCodeCasts.java
@@ -32,7 +32,7 @@
list("picture", pc.title),
list("description", pc.title),
list("viewable", pc.isViewable ? "+" : "-"),
- list("downloadable", false ? "+" : "-")}
+ list("downloadable", pc.isDownloadable ? "+" : "-")}
);
}
Please sign in to comment.
Something went wrong with that request. Please try again.