Permalink
Browse files

refactor Gateway into separate InMemoryGateway derivatives. E2P2 Review.

  • Loading branch information...
unclebob committed Jul 16, 2014
1 parent d33b497 commit a9d56985ad784323a053f62a65428831a5f29f6e
@@ -1,5 +1,5 @@
|import|
-|cleancoderscom.fixtures|
+|cleancoderscom.tests.fixtures|
|library|
|codecast presentation|
@@ -2,4 +2,4 @@
----
!define TEST_SYSTEM {slim}
-!path target/classes
+!path target/classes
@@ -2,20 +2,12 @@
import java.util.List;
-public interface Gateway {
+public interface CodecastGateway {
List<Codecast> findAllCodecastsSortedChronologically();
void delete(Codecast codecast);
Codecast save(Codecast codecast);
- User save(User user);
-
- void save(License license);
-
- User findUserByName(String username);
-
Codecast findCodecastByTitle(String codecastTitle);
-
- List<License> findLicensesForUserAndCodecast(User user, Codecast codecast);
}
@@ -1,5 +1,7 @@
package cleancoderscom;
public class Context {
- public static Gateway gateway;
+ public static UserGateway userGateway;
+ public static CodecastGateway codecastGateway;
+ public static LicenseGateway licenseGateway;
}
@@ -2,7 +2,7 @@
import java.util.Objects;
-public class Entity {
+public class Entity implements Cloneable {
private String id;
public boolean isSame(Entity entity) {
@@ -16,4 +16,8 @@ public void setId(String id) {
public String getId() {
return id;
}
+
+ public Object clone() throws CloneNotSupportedException {
+ return super.clone();
+ }
}
@@ -0,0 +1,9 @@
+package cleancoderscom;
+
+import java.util.List;
+
+public interface LicenseGateway {
+ License save(License license);
+
+ List<License> findLicensesForUserAndCodecast(User user, Codecast codecast);
+}
@@ -1,75 +0,0 @@
-package cleancoderscom;
-
-import java.util.*;
-
-public class MockGateway implements Gateway {
-
- private List<Codecast> codecasts;
- private List<User> users;
- private ArrayList<License> licenses;
-
- public MockGateway() {
- codecasts = new ArrayList<Codecast>();
- users = new ArrayList<User>();
- licenses = new ArrayList<License>();
- }
-
- public List<Codecast> findAllCodecastsSortedChronologically()
- {
- List<Codecast> sortedCodecasts = new ArrayList<Codecast>(codecasts);
- Collections.sort(sortedCodecasts, new Comparator<Codecast>() {
- public int compare(Codecast o1, Codecast o2) {
- return o1.getPublicationDate().compareTo(o2.getPublicationDate());
- }
- });
- return sortedCodecasts;
- }
-
- public void delete(Codecast codecast) {
- codecasts.remove(codecast);
- }
-
- public Codecast save(Codecast codecast) {
- codecasts.add((Codecast)establishId(codecast));
- return codecast;
- }
-
- public User save(User user) {
- users.add((User)establishId(user));
- return user;
- }
-
- private Entity establishId(Entity entity) {
- if(entity.getId() == null)
- entity.setId(UUID.randomUUID().toString());
- return entity;
- }
-
- public void save(License license) {
- licenses.add(license);
- }
-
- public User findUserByName(String username) {
- for (User user : users) {
- if (user.getUserName().equals(username))
- return user;
- }
- return null;
- }
-
- public Codecast findCodecastByTitle(String codecastTitle) {
- for (Codecast codecast : codecasts)
- if (codecast.getTitle().equals(codecastTitle))
- return codecast;
- return null;
- }
-
- public List<License> findLicensesForUserAndCodecast(User user, Codecast codecast) {
- List<License> results = new ArrayList<License>();
- for (License license : licenses) {
- if (license.getUser().isSame(user) && license.getCodecast().isSame(codecast))
- results.add(license);
- }
- return results;
- }
-}
@@ -12,7 +12,7 @@
public List<PresentableCodecast> presentCodecasts(User loggedInUser) {
ArrayList<PresentableCodecast> presentableCodecasts = new ArrayList<PresentableCodecast>();
- List<Codecast> allCodecasts = Context.gateway.findAllCodecastsSortedChronologically();
+ List<Codecast> allCodecasts = Context.codecastGateway.findAllCodecastsSortedChronologically();
for (Codecast codecast : allCodecasts)
presentableCodecasts.add(formatCodecast(loggedInUser, codecast));
@@ -30,7 +30,7 @@ private PresentableCodecast formatCodecast(User loggedInUser, Codecast codecast)
}
public boolean isLicensedFor(License.LicenseType licenseType, User user, Codecast codecast) {
- List<License> licenses = Context.gateway.findLicensesForUserAndCodecast(user, codecast);
+ List<License> licenses = Context.licenseGateway.findLicensesForUserAndCodecast(user, codecast);
for (License l : licenses) {
if (l.getType() == licenseType)
return true;
@@ -0,0 +1,7 @@
+package cleancoderscom;
+
+public interface UserGateway {
+ User save(User user);
+
+ User findUserByName(String username);
+}
@@ -1,5 +1,6 @@
-package cleancoderscom;
+package cleancoderscom.tests;
+import cleancoderscom.*;
import de.bechte.junit.runners.HierarchicalContextRunner;
import org.junit.Before;
import org.junit.Test;
@@ -15,14 +16,13 @@
@RunWith(HierarchicalContextRunner.class)
public class PresentCodecastUseCaseTest {
-
private User user;
private PresentCodecastUseCase useCase;
@Before
public void setUp() {
- Context.gateway = new MockGateway();
- user = Context.gateway.save(new User("User"));
+ TestSetup.addInMemoryGatewaysToContext();
+ user = Context.userGateway.save(new User("User"));
useCase = new PresentCodecastUseCase();
}
@@ -40,7 +40,7 @@ public void noneArePresented() throws Exception {
@Before
public void setupCodecast() {
- codecast = Context.gateway.save(new Codecast());
+ codecast = Context.codecastGateway.save(new Codecast());
}
@Test
@@ -75,7 +75,7 @@ public void presentedCodecastShowsNotViewable() throws Exception {
@Before
public void setupLicense() {
viewLicense = new License(VIEWING, user, codecast);
- Context.gateway.save(viewLicense);
+ Context.licenseGateway.save(viewLicense);
}
@Test
@@ -85,13 +85,13 @@ public void userCanViewCodecast() throws Exception {
@Test
public void unlicensedUserCannotViewOtherUsersCodecast() throws Exception {
- User otherUser = Context.gateway.save(new User("otherUser"));
+ User otherUser = Context.userGateway.save(new User("otherUser"));
assertFalse(useCase.isLicensedFor(VIEWING, otherUser, codecast));
}
@Test
public void presentedCodecastIsViewable() throws Exception {
- Context.gateway.save(new License(VIEWING, user, codecast));
+ Context.licenseGateway.save(new License(VIEWING, user, codecast));
List<PresentableCodecast> presentableCodecasts = useCase.presentCodecasts(user);
PresentableCodecast presentableCodecast = presentableCodecasts.get(0);
assertTrue(presentableCodecast.isViewable);
@@ -104,7 +104,7 @@ public void presentedCodecastIsViewable() throws Exception {
@Before
public void setupDownloadLicense() {
downloadLicense = new License(DOWNLOADING, user, codecast);
- Context.gateway.save(downloadLicense);
+ Context.licenseGateway.save(downloadLicense);
}
@Test
@@ -0,0 +1,14 @@
+package cleancoderscom.tests;
+
+import cleancoderscom.Context;
+import cleancoderscom.tests.doubles.InMemoryCodecastGateway;
+import cleancoderscom.tests.doubles.InMemoryLicenseGateway;
+import cleancoderscom.tests.doubles.InMemoryUserGateway;
+
+public class TestSetup {
+ public static void addInMemoryGatewaysToContext() {
+ Context.userGateway = new InMemoryUserGateway();
+ Context.licenseGateway = new InMemoryLicenseGateway();
+ Context.codecastGateway = new InMemoryCodecastGateway();
+ }
+}
@@ -0,0 +1,45 @@
+package cleancoderscom.tests.doubles;
+
+import cleancoderscom.Entity;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+public class GatewayUtilities<T extends Entity> {
+ private List<T> entities;
+
+ public GatewayUtilities() {
+ this.entities = new ArrayList<T>();
+ }
+
+ protected T establishId(T entity) {
+ if(entity.getId() == null)
+ entity.setId(UUID.randomUUID().toString());
+ return entity;
+ }
+
+ @SuppressWarnings("unchecked")
+ public List<T> getEntities() {
+ List<T> newEntities = new ArrayList<T>();
+ for (T entity : entities)
+ try {
+ newEntities.add((T) entity.clone());
+ } catch (CloneNotSupportedException e) {
+ throw new UnCloneableEntity();
+ }
+ return newEntities;
+ }
+
+ public T save(T entity) {
+ entities.add(establishId(entity));
+ return entity;
+ }
+
+ public void delete(T entity) {
+ entities.remove(entity);
+ }
+
+ private static class UnCloneableEntity extends RuntimeException {
+ }
+}
@@ -0,0 +1,25 @@
+package cleancoderscom.tests.doubles;
+
+import cleancoderscom.*;
+
+import java.util.*;
+
+public class InMemoryCodecastGateway extends GatewayUtilities<Codecast> implements CodecastGateway {
+ public List<Codecast> findAllCodecastsSortedChronologically()
+ {
+ List<Codecast> sortedCodecasts = new ArrayList<Codecast>(getEntities());
+ Collections.sort(sortedCodecasts, new Comparator<Codecast>() {
+ public int compare(Codecast o1, Codecast o2) {
+ return o1.getPublicationDate().compareTo(o2.getPublicationDate());
+ }
+ });
+ return sortedCodecasts;
+ }
+
+ public Codecast findCodecastByTitle(String codecastTitle) {
+ for (Codecast codecast : getEntities())
+ if (codecast.getTitle().equals(codecastTitle))
+ return codecast;
+ return null;
+ }
+}
@@ -0,0 +1,16 @@
+package cleancoderscom.tests.doubles;
+
+import cleancoderscom.*;
+
+import java.util.*;
+
+public class InMemoryLicenseGateway extends GatewayUtilities<License> implements LicenseGateway {
+ public List<License> findLicensesForUserAndCodecast(User user, Codecast codecast) {
+ List<License> results = new ArrayList<License>();
+ for (License license : getEntities()) {
+ if (license.getUser().isSame(user) && license.getCodecast().isSame(codecast))
+ results.add(license);
+ }
+ return results;
+ }
+}
@@ -0,0 +1,14 @@
+package cleancoderscom.tests.doubles;
+
+import cleancoderscom.User;
+import cleancoderscom.UserGateway;
+
+public class InMemoryUserGateway extends GatewayUtilities<User> implements UserGateway {
+ public User findUserByName(String username) {
+ for (User user : getEntities()) {
+ if (user.getUserName().equals(username))
+ return user;
+ }
+ return null;
+ }
+}
Oops, something went wrong.

0 comments on commit a9d5698

Please sign in to comment.