From: Gustavo Martin Morcuende Date: Sun, 27 Nov 2016 15:52:24 +0000 (+0100) Subject: different projects for codurance X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=a9a7b3aae964e6dae88698430146d7395b1061bb;p=JavaForFun different projects for codurance --- diff --git a/TDD/sandromancuso/README.md b/TDD/sandromancuso/README.md deleted file mode 100644 index 12e4c51..0000000 --- a/TDD/sandromancuso/README.md +++ /dev/null @@ -1,17 +0,0 @@ -Sandro Mancuso: testing a refactoring legacy code - -Rules for refactoring legacy code. - -1. You may not change production code if not covered by tests. Before refactoring some legacy code we must write a Unit Test for it. - - But some times for writing the test we must make modifications in the legacy code. If such modifications are required: just automated refactoring (via IDES) are allowed, if needed to write the test. - - -Steps: - -1. Start testing the legacy code following the shortest branch. - -2. In Unit Test we should not invoke other classes but the one being under test (the other classes should be stub, mocke, etc, etc) - -3. When the legacy code is covered by a unit test we may refactor it (never before) We have to refactor our legacy code from the deepest to the shortest branch. - diff --git a/TDD/sandromancuso/pom.xml b/TDD/sandromancuso/pom.xml deleted file mode 100644 index 91ce619..0000000 --- a/TDD/sandromancuso/pom.xml +++ /dev/null @@ -1,158 +0,0 @@ - - - 4.0.0 - org.craftedsw - sandromancuso - jar - 1.0-SNAPSHOT - tdd-by-example - http://gumartinm.name - TDD by example - - Gustavo Martin Morcuende - http://www.gumartinm.name - - - scm:git:http://git.gumartinm.name/TDDbyExample - http://git.gumartinm.name/TDDbyExample - - - UTF-8 - - - - - - ch.qos.logback - logback-classic - 1.1.2 - - - - - - - javax.inject - javax.inject - 1 - - - - junit - junit - 4.12-beta-1 - test - - - org.mockito - mockito-core - 2.0.31-beta - test - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.8 - 1.8 - - ${project.build.sourceEncoding} - -Xlint:deprecation - - - - org.apache.maven.plugins - maven-resources-plugin - 2.6 - - ${project.build.sourceEncoding} - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.17 - - - - org.apache.maven.plugins - maven-jar-plugin - 2.3.1 - - - - ${project.description} - ${project.version} - ${project.organization.name} - ${project.description} - ${project.version} - ${project.organization.name} - - - - - - - - - - - org.codehaus.mojo - cobertura-maven-plugin - 2.6 - - - - diff --git a/TDD/sandromancuso/romannumbers/pom.xml b/TDD/sandromancuso/romannumbers/pom.xml new file mode 100644 index 0000000..da85916 --- /dev/null +++ b/TDD/sandromancuso/romannumbers/pom.xml @@ -0,0 +1,158 @@ + + + 4.0.0 + org.craftedsw + romannumbers + jar + 1.0-SNAPSHOT + romannumbers + http://gumartinm.name + TDD codurance + + Gustavo Martin Morcuende + http://www.gumartinm.name + + + scm:git:http://git.gumartinm.name/TDDbyExample + http://git.gumartinm.name/TDDbyExample + + + UTF-8 + + + + + + ch.qos.logback + logback-classic + 1.1.2 + + + + + + + javax.inject + javax.inject + 1 + + + + junit + junit + 4.12-beta-1 + test + + + org.mockito + mockito-core + 2.0.31-beta + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + ${project.build.sourceEncoding} + -Xlint:deprecation + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.17 + + + + org.apache.maven.plugins + maven-jar-plugin + 2.3.1 + + + + ${project.description} + ${project.version} + ${project.organization.name} + ${project.description} + ${project.version} + ${project.organization.name} + + + + + + + + + + + org.codehaus.mojo + cobertura-maven-plugin + 2.6 + + + + diff --git a/TDD/sandromancuso/romannumbers/src/main/resources/log4j2.xml b/TDD/sandromancuso/romannumbers/src/main/resources/log4j2.xml new file mode 100644 index 0000000..2de3fd2 --- /dev/null +++ b/TDD/sandromancuso/romannumbers/src/main/resources/log4j2.xml @@ -0,0 +1,197 @@ + + + + + + target/fileappender.log + target/filerandomappender.log + + + + + + + + + + + + + + + + + + + + + + + + "%d{yyyyMMddHHmmssSSS} - %-5p - [%t] - %m%n" + UTF-8 + + + + + + + "%d{yyyyMMddHHmmssSSS} - %-5p - [%t] - %m%n" + UTF-8 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TDD/sandromancuso/romannumbers/src/main/resources/logback.xml b/TDD/sandromancuso/romannumbers/src/main/resources/logback.xml new file mode 100644 index 0000000..685c34c --- /dev/null +++ b/TDD/sandromancuso/romannumbers/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + %d{yyyyMMddHHmmssSSS} - %-5p - [%t] - [%C] - %m%n + + + + + + diff --git a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java deleted file mode 100644 index a2ba5ee..0000000 --- a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.craftedsw.tripservicekata; - -import java.util.ArrayList; -import java.util.List; - -import org.craftedsw.tripservicekata.exception.UserNotLoggedInException; -import org.craftedsw.tripservicekata.trip.Trip; -import org.craftedsw.tripservicekata.trip.TripDAO; -import org.craftedsw.tripservicekata.user.User; -import org.craftedsw.tripservicekata.user.UserSession; - -public class TripService_Original { - - public List getTripsByUser(User user) throws UserNotLoggedInException { - List tripList = new ArrayList(); - User loggedUser = UserSession.getInstance().getLoggedUser(); - boolean isFriend = false; - if (loggedUser != null) { - for (User friend : user.getFriends()) { - if (friend.equals(loggedUser)) { - isFriend = true; - break; - } - } - if (isFriend) { - tripList = TripDAO.findTripsByUser(user); - } - return tripList; - } else { - throw new UserNotLoggedInException(); - } - } - -} diff --git a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java deleted file mode 100644 index 8f95bc9..0000000 --- a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.craftedsw.tripservicekata; - -import org.craftedsw.tripservicekata.trip.Trip; -import org.craftedsw.tripservicekata.user.User; - -public class UserBuilder { - private User[] friends = new User[]{}; - private Trip[] trips = new Trip[]{}; - - public static UserBuilder aUser() { - return new UserBuilder(); - } - - public UserBuilder withTrips(Trip...trips) { - this.trips = trips; - return this; - } - - public UserBuilder friendsWith(User...friends) { - this.friends = friends; - return this; - } - - public User build() { - User user = new User(); - addTripsTo(user); - addFriendsTo(user); - return user; - } - - private void addFriendsTo(User user) { - for (User friend : friends) { - user.addFriend(friend); - } - } - - private void addTripsTo(User user) { - for (Trip trip : trips) { - user.addTrip(trip); - } - } -} diff --git a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java deleted file mode 100644 index 828353e..0000000 --- a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.craftedsw.tripservicekata.exception; - -public class CollaboratorCallException extends RuntimeException { - - private static final long serialVersionUID = -4584041339906109902L; - - public CollaboratorCallException() { - super(); - } - - public CollaboratorCallException(String message, - Throwable cause) { - super(message, cause); - } - - public CollaboratorCallException(String message) { - super(message); - } - - public CollaboratorCallException(Throwable cause) { - super(cause); - } - - -} diff --git a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java deleted file mode 100644 index 880bf20..0000000 --- a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.craftedsw.tripservicekata.exception; - -public class UserNotLoggedInException extends RuntimeException { - - private static final long serialVersionUID = 8959479918185637340L; - -} diff --git a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java deleted file mode 100644 index f12ff3c..0000000 --- a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.craftedsw.tripservicekata.trip; - -public class Trip { - -} diff --git a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java deleted file mode 100644 index ab859ed..0000000 --- a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.craftedsw.tripservicekata.trip; - -import java.util.List; - -import org.craftedsw.tripservicekata.exception.CollaboratorCallException; -import org.craftedsw.tripservicekata.user.User; - -public class TripDAO { - - public static List findTripsByUser(User user) { - throw new CollaboratorCallException( - "TripDAO should not be invoked on an unit test."); - } - - // At the end and by means of many code refactoring our app - // will end up using the instance method. In the meanwhile we - // will not be able to remove the static method but at least - // we are offering something that can be tested. - // Be careful when refactoring this code because in real life - // findTripsByUser for sure will be used in many places but again - // in the meanwhile with this code we can write unit tests (we - // always need to write a test of our legacy code before refactoring it) - public List tripsBy(User user) { - return TripDAO.findTripsByUser(user); - } - -} \ No newline at end of file diff --git a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java deleted file mode 100644 index 276710f..0000000 --- a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.craftedsw.tripservicekata.trip; - -import java.util.ArrayList; -import java.util.List; - -import javax.inject.Inject; - -import org.craftedsw.tripservicekata.exception.UserNotLoggedInException; -import org.craftedsw.tripservicekata.user.User; - -public class TripService { - - private final TripDAO tripDAO; - - @Inject - public TripService(TripDAO tripDAO) { - this.tripDAO = tripDAO; - } - - public List getTripsByUser(User user, User loggedInUser) throws UserNotLoggedInException { - validate(loggedInUser); - - return user.isFriendsWith(loggedInUser) - ? tripsBy(user) - : noTrips(); - } - - private void validate(User loggedInUser) { - if (loggedInUser == null) { - throw new UserNotLoggedInException(); - } - } - - private ArrayList noTrips() { - return new ArrayList<>(); - } - - private List tripsBy(User user) { - return tripDAO.tripsBy(user); - } -} diff --git a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/user/User.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/user/User.java deleted file mode 100644 index d038fe8..0000000 --- a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/user/User.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.craftedsw.tripservicekata.user; - -import java.util.ArrayList; -import java.util.List; - -import org.craftedsw.tripservicekata.trip.Trip; - -public class User { - - private List trips = new ArrayList(); - private List friends = new ArrayList(); - - public List getFriends() { - return friends; - } - - public void addFriend(User user) { - friends.add(user); - } - - public void addTrip(Trip trip) { - trips.add(trip); - } - - public List trips() { - return trips; - } - - public boolean isFriendsWith(User anotherUser) { - return friends.contains(anotherUser); - } - -} diff --git a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java deleted file mode 100644 index 6b91c0d..0000000 --- a/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.craftedsw.tripservicekata.user; - -import org.craftedsw.tripservicekata.exception.CollaboratorCallException; - -public class UserSession { - - private static final UserSession userSession = new UserSession(); - - private UserSession() { - } - - public static UserSession getInstance() { - return userSession; - } - - public User getLoggedUser() { - throw new CollaboratorCallException( - "UserSession.getLoggedUser() should not be called in an unit test"); - } - -} diff --git a/TDD/sandromancuso/src/main/resources/log4j2.xml b/TDD/sandromancuso/src/main/resources/log4j2.xml deleted file mode 100644 index 2de3fd2..0000000 --- a/TDD/sandromancuso/src/main/resources/log4j2.xml +++ /dev/null @@ -1,197 +0,0 @@ - - - - - - target/fileappender.log - target/filerandomappender.log - - - - - - - - - - - - - - - - - - - - - - - - "%d{yyyyMMddHHmmssSSS} - %-5p - [%t] - %m%n" - UTF-8 - - - - - - - "%d{yyyyMMddHHmmssSSS} - %-5p - [%t] - %m%n" - UTF-8 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/TDD/sandromancuso/src/main/resources/logback.xml b/TDD/sandromancuso/src/main/resources/logback.xml deleted file mode 100644 index 685c34c..0000000 --- a/TDD/sandromancuso/src/main/resources/logback.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - %d{yyyyMMddHHmmssSSS} - %-5p - [%t] - [%C] - %m%n - - - - - - diff --git a/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java b/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java deleted file mode 100644 index 34f6cc5..0000000 --- a/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.craftedsw.tripservicekata.trip; - -import org.craftedsw.tripservicekata.exception.CollaboratorCallException; -import org.craftedsw.tripservicekata.user.User; -import org.junit.Test; - -public class TripDAOShould { - - @Test(expected = CollaboratorCallException.class) public void - throw_exception_when_retrieving_user_trips() { - new TripDAO().tripsBy(new User()); - } -} diff --git a/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java b/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java deleted file mode 100644 index 1111b54..0000000 --- a/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.craftedsw.tripservicekata.trip; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import static org.mockito.BDDMockito.given; - -import java.util.List; - -import org.craftedsw.tripservicekata.UserBuilder; -import org.craftedsw.tripservicekata.exception.UserNotLoggedInException; -import org.craftedsw.tripservicekata.user.User; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Spy; -import org.mockito.runners.MockitoJUnitRunner; - -@RunWith(MockitoJUnitRunner.class) -public class TripServiceShould { - - private static final User GUEST = null; - private static final User UNUSED_USER = null; - private static final User REGISTERED_USER = new User(); - private static final User ANOTHER_USER = new User(); - private static final Trip TO_BRAZIL = new Trip(); - private static final Trip TO_BERLIN = new Trip(); - - @Mock TripDAO tripDAO; - @InjectMocks @Spy private TripService tripService; - - @Before - public void setUp() { - tripService = new TripService(tripDAO); - } - - @Test(expected=UserNotLoggedInException.class) public void - throw_an_exception_when_user_is_not_logged_in() { - tripService.getTripsByUser(UNUSED_USER, GUEST); - } - - @Test public void - not_return_any_trips_when_users_are_not_friends() { - User friend = UserBuilder.aUser() - .friendsWith(ANOTHER_USER) - .withTrips(TO_BRAZIL) - .build(); - - List friendTrips = tripService.getTripsByUser(friend, REGISTERED_USER); - // You must always begin writing the assert. - // Remember: the assert must match the unit test method's name!! - // In this case, no trips must be returned. - assertThat(friendTrips.size(), is(0)); - } - - @Test public void - return_friend_trips_when_users_are_friends() { - User friend = UserBuilder.aUser() - .friendsWith(ANOTHER_USER, REGISTERED_USER) - .withTrips(TO_BRAZIL, TO_BERLIN) - .build(); - given(tripDAO.tripsBy(friend)).willReturn(friend.trips()); - - List friendTrips = tripService.getTripsByUser(friend, REGISTERED_USER); - // You must always begin writing the assert. - // Remember: the assert must match the unit test method's name!! - // In this case, no trips must be returned. - assertThat(friendTrips.size(), is(2)); - } -} diff --git a/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java b/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java deleted file mode 100644 index dfe2aee..0000000 --- a/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.craftedsw.tripservicekata.user; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - -import org.craftedsw.tripservicekata.UserBuilder; -import org.junit.Test; - -public class UserShould { - - private static final User JOHN = new User(); - private static final User PAUL = new User(); - - @Test public void - inform_when_users_are_not_friends() { - User user = UserBuilder.aUser() - .friendsWith(JOHN) - .build(); - - assertThat(user.isFriendsWith(PAUL), is(false)); - } - - @Test public void - inform_when_users_are_friends() { - User user = UserBuilder.aUser() - .friendsWith(JOHN, PAUL) - .build(); - - assertThat(user.isFriendsWith(PAUL), is(true)); - } -} diff --git a/TDD/sandromancuso/tripservice/README.md b/TDD/sandromancuso/tripservice/README.md new file mode 100644 index 0000000..12e4c51 --- /dev/null +++ b/TDD/sandromancuso/tripservice/README.md @@ -0,0 +1,17 @@ +Sandro Mancuso: testing a refactoring legacy code + +Rules for refactoring legacy code. + +1. You may not change production code if not covered by tests. Before refactoring some legacy code we must write a Unit Test for it. + + But some times for writing the test we must make modifications in the legacy code. If such modifications are required: just automated refactoring (via IDES) are allowed, if needed to write the test. + + +Steps: + +1. Start testing the legacy code following the shortest branch. + +2. In Unit Test we should not invoke other classes but the one being under test (the other classes should be stub, mocke, etc, etc) + +3. When the legacy code is covered by a unit test we may refactor it (never before) We have to refactor our legacy code from the deepest to the shortest branch. + diff --git a/TDD/sandromancuso/tripservice/pom.xml b/TDD/sandromancuso/tripservice/pom.xml new file mode 100644 index 0000000..9aac466 --- /dev/null +++ b/TDD/sandromancuso/tripservice/pom.xml @@ -0,0 +1,158 @@ + + + 4.0.0 + org.craftedsw + tripservce + jar + 1.0-SNAPSHOT + tripservice + http://gumartinm.name + TDD codurance + + Gustavo Martin Morcuende + http://www.gumartinm.name + + + scm:git:http://git.gumartinm.name/TDDbyExample + http://git.gumartinm.name/TDDbyExample + + + UTF-8 + + + + + + ch.qos.logback + logback-classic + 1.1.2 + + + + + + + javax.inject + javax.inject + 1 + + + + junit + junit + 4.12-beta-1 + test + + + org.mockito + mockito-core + 2.0.31-beta + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + ${project.build.sourceEncoding} + -Xlint:deprecation + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.17 + + + + org.apache.maven.plugins + maven-jar-plugin + 2.3.1 + + + + ${project.description} + ${project.version} + ${project.organization.name} + ${project.description} + ${project.version} + ${project.organization.name} + + + + + + + + + + + org.codehaus.mojo + cobertura-maven-plugin + 2.6 + + + + diff --git a/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java new file mode 100644 index 0000000..a2ba5ee --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java @@ -0,0 +1,34 @@ +package org.craftedsw.tripservicekata; + +import java.util.ArrayList; +import java.util.List; + +import org.craftedsw.tripservicekata.exception.UserNotLoggedInException; +import org.craftedsw.tripservicekata.trip.Trip; +import org.craftedsw.tripservicekata.trip.TripDAO; +import org.craftedsw.tripservicekata.user.User; +import org.craftedsw.tripservicekata.user.UserSession; + +public class TripService_Original { + + public List getTripsByUser(User user) throws UserNotLoggedInException { + List tripList = new ArrayList(); + User loggedUser = UserSession.getInstance().getLoggedUser(); + boolean isFriend = false; + if (loggedUser != null) { + for (User friend : user.getFriends()) { + if (friend.equals(loggedUser)) { + isFriend = true; + break; + } + } + if (isFriend) { + tripList = TripDAO.findTripsByUser(user); + } + return tripList; + } else { + throw new UserNotLoggedInException(); + } + } + +} diff --git a/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java new file mode 100644 index 0000000..8f95bc9 --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java @@ -0,0 +1,42 @@ +package org.craftedsw.tripservicekata; + +import org.craftedsw.tripservicekata.trip.Trip; +import org.craftedsw.tripservicekata.user.User; + +public class UserBuilder { + private User[] friends = new User[]{}; + private Trip[] trips = new Trip[]{}; + + public static UserBuilder aUser() { + return new UserBuilder(); + } + + public UserBuilder withTrips(Trip...trips) { + this.trips = trips; + return this; + } + + public UserBuilder friendsWith(User...friends) { + this.friends = friends; + return this; + } + + public User build() { + User user = new User(); + addTripsTo(user); + addFriendsTo(user); + return user; + } + + private void addFriendsTo(User user) { + for (User friend : friends) { + user.addFriend(friend); + } + } + + private void addTripsTo(User user) { + for (Trip trip : trips) { + user.addTrip(trip); + } + } +} diff --git a/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java new file mode 100644 index 0000000..828353e --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java @@ -0,0 +1,25 @@ +package org.craftedsw.tripservicekata.exception; + +public class CollaboratorCallException extends RuntimeException { + + private static final long serialVersionUID = -4584041339906109902L; + + public CollaboratorCallException() { + super(); + } + + public CollaboratorCallException(String message, + Throwable cause) { + super(message, cause); + } + + public CollaboratorCallException(String message) { + super(message); + } + + public CollaboratorCallException(Throwable cause) { + super(cause); + } + + +} diff --git a/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java new file mode 100644 index 0000000..880bf20 --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java @@ -0,0 +1,7 @@ +package org.craftedsw.tripservicekata.exception; + +public class UserNotLoggedInException extends RuntimeException { + + private static final long serialVersionUID = 8959479918185637340L; + +} diff --git a/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java new file mode 100644 index 0000000..f12ff3c --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java @@ -0,0 +1,5 @@ +package org.craftedsw.tripservicekata.trip; + +public class Trip { + +} diff --git a/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java new file mode 100644 index 0000000..ab859ed --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java @@ -0,0 +1,27 @@ +package org.craftedsw.tripservicekata.trip; + +import java.util.List; + +import org.craftedsw.tripservicekata.exception.CollaboratorCallException; +import org.craftedsw.tripservicekata.user.User; + +public class TripDAO { + + public static List findTripsByUser(User user) { + throw new CollaboratorCallException( + "TripDAO should not be invoked on an unit test."); + } + + // At the end and by means of many code refactoring our app + // will end up using the instance method. In the meanwhile we + // will not be able to remove the static method but at least + // we are offering something that can be tested. + // Be careful when refactoring this code because in real life + // findTripsByUser for sure will be used in many places but again + // in the meanwhile with this code we can write unit tests (we + // always need to write a test of our legacy code before refactoring it) + public List tripsBy(User user) { + return TripDAO.findTripsByUser(user); + } + +} \ No newline at end of file diff --git a/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java new file mode 100644 index 0000000..276710f --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java @@ -0,0 +1,41 @@ +package org.craftedsw.tripservicekata.trip; + +import java.util.ArrayList; +import java.util.List; + +import javax.inject.Inject; + +import org.craftedsw.tripservicekata.exception.UserNotLoggedInException; +import org.craftedsw.tripservicekata.user.User; + +public class TripService { + + private final TripDAO tripDAO; + + @Inject + public TripService(TripDAO tripDAO) { + this.tripDAO = tripDAO; + } + + public List getTripsByUser(User user, User loggedInUser) throws UserNotLoggedInException { + validate(loggedInUser); + + return user.isFriendsWith(loggedInUser) + ? tripsBy(user) + : noTrips(); + } + + private void validate(User loggedInUser) { + if (loggedInUser == null) { + throw new UserNotLoggedInException(); + } + } + + private ArrayList noTrips() { + return new ArrayList<>(); + } + + private List tripsBy(User user) { + return tripDAO.tripsBy(user); + } +} diff --git a/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/user/User.java b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/user/User.java new file mode 100644 index 0000000..d038fe8 --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/user/User.java @@ -0,0 +1,33 @@ +package org.craftedsw.tripservicekata.user; + +import java.util.ArrayList; +import java.util.List; + +import org.craftedsw.tripservicekata.trip.Trip; + +public class User { + + private List trips = new ArrayList(); + private List friends = new ArrayList(); + + public List getFriends() { + return friends; + } + + public void addFriend(User user) { + friends.add(user); + } + + public void addTrip(Trip trip) { + trips.add(trip); + } + + public List trips() { + return trips; + } + + public boolean isFriendsWith(User anotherUser) { + return friends.contains(anotherUser); + } + +} diff --git a/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java new file mode 100644 index 0000000..6b91c0d --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java @@ -0,0 +1,21 @@ +package org.craftedsw.tripservicekata.user; + +import org.craftedsw.tripservicekata.exception.CollaboratorCallException; + +public class UserSession { + + private static final UserSession userSession = new UserSession(); + + private UserSession() { + } + + public static UserSession getInstance() { + return userSession; + } + + public User getLoggedUser() { + throw new CollaboratorCallException( + "UserSession.getLoggedUser() should not be called in an unit test"); + } + +} diff --git a/TDD/sandromancuso/tripservice/src/main/resources/log4j2.xml b/TDD/sandromancuso/tripservice/src/main/resources/log4j2.xml new file mode 100644 index 0000000..2de3fd2 --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/resources/log4j2.xml @@ -0,0 +1,197 @@ + + + + + + target/fileappender.log + target/filerandomappender.log + + + + + + + + + + + + + + + + + + + + + + + + "%d{yyyyMMddHHmmssSSS} - %-5p - [%t] - %m%n" + UTF-8 + + + + + + + "%d{yyyyMMddHHmmssSSS} - %-5p - [%t] - %m%n" + UTF-8 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TDD/sandromancuso/tripservice/src/main/resources/logback.xml b/TDD/sandromancuso/tripservice/src/main/resources/logback.xml new file mode 100644 index 0000000..685c34c --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + %d{yyyyMMddHHmmssSSS} - %-5p - [%t] - [%C] - %m%n + + + + + + diff --git a/TDD/sandromancuso/tripservice/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java b/TDD/sandromancuso/tripservice/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java new file mode 100644 index 0000000..34f6cc5 --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java @@ -0,0 +1,13 @@ +package org.craftedsw.tripservicekata.trip; + +import org.craftedsw.tripservicekata.exception.CollaboratorCallException; +import org.craftedsw.tripservicekata.user.User; +import org.junit.Test; + +public class TripDAOShould { + + @Test(expected = CollaboratorCallException.class) public void + throw_exception_when_retrieving_user_trips() { + new TripDAO().tripsBy(new User()); + } +} diff --git a/TDD/sandromancuso/tripservice/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java b/TDD/sandromancuso/tripservice/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java new file mode 100644 index 0000000..1111b54 --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java @@ -0,0 +1,71 @@ +package org.craftedsw.tripservicekata.trip; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.mockito.BDDMockito.given; + +import java.util.List; + +import org.craftedsw.tripservicekata.UserBuilder; +import org.craftedsw.tripservicekata.exception.UserNotLoggedInException; +import org.craftedsw.tripservicekata.user.User; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class TripServiceShould { + + private static final User GUEST = null; + private static final User UNUSED_USER = null; + private static final User REGISTERED_USER = new User(); + private static final User ANOTHER_USER = new User(); + private static final Trip TO_BRAZIL = new Trip(); + private static final Trip TO_BERLIN = new Trip(); + + @Mock TripDAO tripDAO; + @InjectMocks @Spy private TripService tripService; + + @Before + public void setUp() { + tripService = new TripService(tripDAO); + } + + @Test(expected=UserNotLoggedInException.class) public void + throw_an_exception_when_user_is_not_logged_in() { + tripService.getTripsByUser(UNUSED_USER, GUEST); + } + + @Test public void + not_return_any_trips_when_users_are_not_friends() { + User friend = UserBuilder.aUser() + .friendsWith(ANOTHER_USER) + .withTrips(TO_BRAZIL) + .build(); + + List friendTrips = tripService.getTripsByUser(friend, REGISTERED_USER); + // You must always begin writing the assert. + // Remember: the assert must match the unit test method's name!! + // In this case, no trips must be returned. + assertThat(friendTrips.size(), is(0)); + } + + @Test public void + return_friend_trips_when_users_are_friends() { + User friend = UserBuilder.aUser() + .friendsWith(ANOTHER_USER, REGISTERED_USER) + .withTrips(TO_BRAZIL, TO_BERLIN) + .build(); + given(tripDAO.tripsBy(friend)).willReturn(friend.trips()); + + List friendTrips = tripService.getTripsByUser(friend, REGISTERED_USER); + // You must always begin writing the assert. + // Remember: the assert must match the unit test method's name!! + // In this case, no trips must be returned. + assertThat(friendTrips.size(), is(2)); + } +} diff --git a/TDD/sandromancuso/tripservice/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java b/TDD/sandromancuso/tripservice/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java new file mode 100644 index 0000000..dfe2aee --- /dev/null +++ b/TDD/sandromancuso/tripservice/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java @@ -0,0 +1,31 @@ +package org.craftedsw.tripservicekata.user; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +import org.craftedsw.tripservicekata.UserBuilder; +import org.junit.Test; + +public class UserShould { + + private static final User JOHN = new User(); + private static final User PAUL = new User(); + + @Test public void + inform_when_users_are_not_friends() { + User user = UserBuilder.aUser() + .friendsWith(JOHN) + .build(); + + assertThat(user.isFriendsWith(PAUL), is(false)); + } + + @Test public void + inform_when_users_are_friends() { + User user = UserBuilder.aUser() + .friendsWith(JOHN, PAUL) + .build(); + + assertThat(user.isFriendsWith(PAUL), is(true)); + } +}