From 5ad7aef8162a6428b47b33ffbcfea97bc2dccb2a Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 27 Nov 2016 16:27:04 +0100 Subject: [PATCH] new projects, tdd and sandomancuso --- TDD/README.md | 17 -- TDD/pom.xml | 158 ----------------- TDD/sandromancuso/README.md | 17 ++ TDD/sandromancuso/pom.xml | 158 +++++++++++++++++ .../tripservicekata/TripService_Original.java | 34 ++++ .../org/craftedsw/tripservicekata/UserBuilder.java | 42 +++++ .../exception/CollaboratorCallException.java | 25 +++ .../exception/UserNotLoggedInException.java | 7 + .../org/craftedsw/tripservicekata/trip/Trip.java | 5 + .../craftedsw/tripservicekata/trip/TripDAO.java | 27 +++ .../tripservicekata/trip/TripService.java | 41 +++++ .../org/craftedsw/tripservicekata/user/User.java | 33 ++++ .../tripservicekata/user/UserSession.java | 21 +++ TDD/sandromancuso/src/main/resources/log4j2.xml | 197 +++++++++++++++++++++ TDD/sandromancuso/src/main/resources/logback.xml | 19 ++ .../tripservicekata/trip/TripDAOShould.java | 13 ++ .../tripservicekata/trip/TripServiceShould.java | 71 ++++++++ .../craftedsw/tripservicekata/user/UserShould.java | 31 ++++ .../java/de/example/model/mapper/UsersMapper.java | 11 -- .../de/example/model/resource/UserResource.java | 52 ------ .../java/de/example/services/UsersService.java | 13 -- .../de/example/services/impl/UsersServiceImpl.java | 37 ---- TDD/src/main/java/de/example/tdd/AddressBook.java | 12 -- TDD/src/main/java/de/example/tdd/Calculator.java | 15 -- TDD/src/main/java/de/example/tdd/Contact.java | 5 - TDD/src/main/java/de/example/tdd/Dollar.java | 13 -- TDD/src/main/java/de/example/tdd/Money.java | 5 - .../tripservicekata/TripService_Original.java | 34 ---- .../org/craftedsw/tripservicekata/UserBuilder.java | 42 ----- .../exception/CollaboratorCallException.java | 25 --- .../exception/UserNotLoggedInException.java | 7 - .../org/craftedsw/tripservicekata/trip/Trip.java | 5 - .../craftedsw/tripservicekata/trip/TripDAO.java | 27 --- .../tripservicekata/trip/TripService.java | 41 ----- .../org/craftedsw/tripservicekata/user/User.java | 33 ---- .../tripservicekata/user/UserSession.java | 21 --- TDD/src/main/resources/log4j2.xml | 197 --------------------- TDD/src/main/resources/logback.xml | 19 -- .../java/de/example/services/UsersServiceTest.java | 81 --------- .../test/java/de/example/tdd/AddressBookTest.java | 22 --- .../test/java/de/example/tdd/CalculatorTest.java | 44 ----- TDD/src/test/java/de/example/tdd/MoneyTest.java | 49 ----- .../tripservicekata/trip/TripDAOShould.java | 13 -- .../tripservicekata/trip/TripServiceShould.java | 71 -------- .../craftedsw/tripservicekata/user/UserShould.java | 31 ---- TDD/tdd/README.md | 17 ++ TDD/tdd/pom.xml | 152 ++++++++++++++++ .../java/de/example/model/mapper/UsersMapper.java | 11 ++ .../de/example/model/resource/UserResource.java | 52 ++++++ .../java/de/example/services/UsersService.java | 13 ++ .../de/example/services/impl/UsersServiceImpl.java | 37 ++++ .../src/main/java/de/example/tdd/AddressBook.java | 12 ++ .../src/main/java/de/example/tdd/Calculator.java | 15 ++ TDD/tdd/src/main/java/de/example/tdd/Contact.java | 5 + TDD/tdd/src/main/java/de/example/tdd/Dollar.java | 13 ++ TDD/tdd/src/main/java/de/example/tdd/Money.java | 5 + TDD/tdd/src/main/resources/log4j2.xml | 197 +++++++++++++++++++++ TDD/tdd/src/main/resources/logback.xml | 19 ++ .../java/de/example/services/UsersServiceTest.java | 81 +++++++++ .../test/java/de/example/tdd/AddressBookTest.java | 22 +++ .../test/java/de/example/tdd/CalculatorTest.java | 44 +++++ .../src/test/java/de/example/tdd/MoneyTest.java | 49 +++++ 62 files changed, 1485 insertions(+), 1100 deletions(-) delete mode 100644 TDD/README.md delete mode 100644 TDD/pom.xml create mode 100644 TDD/sandromancuso/README.md create mode 100644 TDD/sandromancuso/pom.xml create mode 100644 TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java create mode 100644 TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java create mode 100644 TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java create mode 100644 TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java create mode 100644 TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java create mode 100644 TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java create mode 100644 TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java create mode 100644 TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/user/User.java create mode 100644 TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java create mode 100644 TDD/sandromancuso/src/main/resources/log4j2.xml create mode 100644 TDD/sandromancuso/src/main/resources/logback.xml create mode 100644 TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java create mode 100644 TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java create mode 100644 TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java delete mode 100644 TDD/src/main/java/de/example/model/mapper/UsersMapper.java delete mode 100644 TDD/src/main/java/de/example/model/resource/UserResource.java delete mode 100644 TDD/src/main/java/de/example/services/UsersService.java delete mode 100644 TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java delete mode 100644 TDD/src/main/java/de/example/tdd/AddressBook.java delete mode 100644 TDD/src/main/java/de/example/tdd/Calculator.java delete mode 100644 TDD/src/main/java/de/example/tdd/Contact.java delete mode 100644 TDD/src/main/java/de/example/tdd/Dollar.java delete mode 100644 TDD/src/main/java/de/example/tdd/Money.java delete mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java delete mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java delete mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java delete mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java delete mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java delete mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java delete mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java delete mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/user/User.java delete mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java delete mode 100644 TDD/src/main/resources/log4j2.xml delete mode 100644 TDD/src/main/resources/logback.xml delete mode 100644 TDD/src/test/java/de/example/services/UsersServiceTest.java delete mode 100644 TDD/src/test/java/de/example/tdd/AddressBookTest.java delete mode 100644 TDD/src/test/java/de/example/tdd/CalculatorTest.java delete mode 100644 TDD/src/test/java/de/example/tdd/MoneyTest.java delete mode 100644 TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java delete mode 100644 TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java delete mode 100644 TDD/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java create mode 100644 TDD/tdd/README.md create mode 100644 TDD/tdd/pom.xml create mode 100644 TDD/tdd/src/main/java/de/example/model/mapper/UsersMapper.java create mode 100644 TDD/tdd/src/main/java/de/example/model/resource/UserResource.java create mode 100644 TDD/tdd/src/main/java/de/example/services/UsersService.java create mode 100644 TDD/tdd/src/main/java/de/example/services/impl/UsersServiceImpl.java create mode 100644 TDD/tdd/src/main/java/de/example/tdd/AddressBook.java create mode 100644 TDD/tdd/src/main/java/de/example/tdd/Calculator.java create mode 100644 TDD/tdd/src/main/java/de/example/tdd/Contact.java create mode 100644 TDD/tdd/src/main/java/de/example/tdd/Dollar.java create mode 100644 TDD/tdd/src/main/java/de/example/tdd/Money.java create mode 100644 TDD/tdd/src/main/resources/log4j2.xml create mode 100644 TDD/tdd/src/main/resources/logback.xml create mode 100644 TDD/tdd/src/test/java/de/example/services/UsersServiceTest.java create mode 100644 TDD/tdd/src/test/java/de/example/tdd/AddressBookTest.java create mode 100644 TDD/tdd/src/test/java/de/example/tdd/CalculatorTest.java create mode 100644 TDD/tdd/src/test/java/de/example/tdd/MoneyTest.java diff --git a/TDD/README.md b/TDD/README.md deleted file mode 100644 index 12e4c51..0000000 --- a/TDD/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/pom.xml b/TDD/pom.xml deleted file mode 100644 index e23cf6b..0000000 --- a/TDD/pom.xml +++ /dev/null @@ -1,158 +0,0 @@ - - - 4.0.0 - de.example.tdd - tdd-by-example - 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/README.md b/TDD/sandromancuso/README.md new file mode 100644 index 0000000..12e4c51 --- /dev/null +++ b/TDD/sandromancuso/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/pom.xml b/TDD/sandromancuso/pom.xml new file mode 100644 index 0000000..91ce619 --- /dev/null +++ b/TDD/sandromancuso/pom.xml @@ -0,0 +1,158 @@ + + + 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/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java new file mode 100644 index 0000000..a2ba5ee --- /dev/null +++ b/TDD/sandromancuso/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/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java new file mode 100644 index 0000000..8f95bc9 --- /dev/null +++ b/TDD/sandromancuso/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/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java new file mode 100644 index 0000000..828353e --- /dev/null +++ b/TDD/sandromancuso/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/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java new file mode 100644 index 0000000..880bf20 --- /dev/null +++ b/TDD/sandromancuso/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/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java new file mode 100644 index 0000000..f12ff3c --- /dev/null +++ b/TDD/sandromancuso/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/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java new file mode 100644 index 0000000..ab859ed --- /dev/null +++ b/TDD/sandromancuso/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/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java new file mode 100644 index 0000000..276710f --- /dev/null +++ b/TDD/sandromancuso/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/src/main/java/org/craftedsw/tripservicekata/user/User.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/user/User.java new file mode 100644 index 0000000..d038fe8 --- /dev/null +++ b/TDD/sandromancuso/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/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java b/TDD/sandromancuso/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java new file mode 100644 index 0000000..6b91c0d --- /dev/null +++ b/TDD/sandromancuso/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/src/main/resources/log4j2.xml b/TDD/sandromancuso/src/main/resources/log4j2.xml new file mode 100644 index 0000000..2de3fd2 --- /dev/null +++ b/TDD/sandromancuso/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/src/main/resources/logback.xml b/TDD/sandromancuso/src/main/resources/logback.xml new file mode 100644 index 0000000..685c34c --- /dev/null +++ b/TDD/sandromancuso/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + %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 new file mode 100644 index 0000000..34f6cc5 --- /dev/null +++ b/TDD/sandromancuso/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/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java b/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java new file mode 100644 index 0000000..1111b54 --- /dev/null +++ b/TDD/sandromancuso/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/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java b/TDD/sandromancuso/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java new file mode 100644 index 0000000..dfe2aee --- /dev/null +++ b/TDD/sandromancuso/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)); + } +} diff --git a/TDD/src/main/java/de/example/model/mapper/UsersMapper.java b/TDD/src/main/java/de/example/model/mapper/UsersMapper.java deleted file mode 100644 index aa45c04..0000000 --- a/TDD/src/main/java/de/example/model/mapper/UsersMapper.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.example.model.mapper; - -import de.example.model.resource.UserResource; - - -public interface UsersMapper { - - public UserResource findOne(long id); - - public void create(UserResource user); -} diff --git a/TDD/src/main/java/de/example/model/resource/UserResource.java b/TDD/src/main/java/de/example/model/resource/UserResource.java deleted file mode 100644 index b2e878a..0000000 --- a/TDD/src/main/java/de/example/model/resource/UserResource.java +++ /dev/null @@ -1,52 +0,0 @@ -package de.example.model.resource; - - -public class UserResource { - private final Long id; - private final String name; - - public UserResource(Long id, String name) { - this.id = id; - this.name = name; - } - - public Long getId() { - return id; - } - - - public String getName() { - return name; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - UserResource other = (UserResource)obj; - if (name == null) { - if (other.name != null) { - return false; - } - } else if (!name.equals(other.name)) { - return false; - } - return true; - } - -} diff --git a/TDD/src/main/java/de/example/services/UsersService.java b/TDD/src/main/java/de/example/services/UsersService.java deleted file mode 100644 index 20d416b..0000000 --- a/TDD/src/main/java/de/example/services/UsersService.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.example.services; - -import de.example.model.resource.UserResource; - - -public interface UsersService { - - UserResource findOneById(long id); - - void create(UserResource user); - - String spiedMethod(UserResource user); -} diff --git a/TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java b/TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java deleted file mode 100644 index d0a3a39..0000000 --- a/TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java +++ /dev/null @@ -1,37 +0,0 @@ -package de.example.services.impl; - -import java.util.Objects; - -import de.example.model.mapper.UsersMapper; -import de.example.model.resource.UserResource; -import de.example.services.UsersService; - - -public class UsersServiceImpl implements UsersService { - private final UsersMapper usersMapper; - - public UsersServiceImpl(final UsersMapper usersMapper) { - this.usersMapper = usersMapper; - } - - @Override - public UserResource findOneById(long id) { - return this.usersMapper.findOne(id); - - } - - @Override - public void create(final UserResource user) { - Objects.requireNonNull(user, "user"); - - this.spiedMethod(user); - - usersMapper.create(user); - } - - @Override - public String spiedMethod(final UserResource user) { - return user.getName(); - } - -} diff --git a/TDD/src/main/java/de/example/tdd/AddressBook.java b/TDD/src/main/java/de/example/tdd/AddressBook.java deleted file mode 100644 index b7c9215..0000000 --- a/TDD/src/main/java/de/example/tdd/AddressBook.java +++ /dev/null @@ -1,12 +0,0 @@ -package de.example.tdd; - -import java.util.List; - -public class AddressBook { - - public List getAllContacts() { - - return null; - } - -} diff --git a/TDD/src/main/java/de/example/tdd/Calculator.java b/TDD/src/main/java/de/example/tdd/Calculator.java deleted file mode 100644 index e8882fc..0000000 --- a/TDD/src/main/java/de/example/tdd/Calculator.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.example.tdd; - -public class Calculator { - - public int sum(final int augend, final int addend) { - - return augend + addend; - } - - public int product(final int multiplicand, final int multiplicier) { - - return multiplicand * multiplicier; - } - -} diff --git a/TDD/src/main/java/de/example/tdd/Contact.java b/TDD/src/main/java/de/example/tdd/Contact.java deleted file mode 100644 index 6570ea7..0000000 --- a/TDD/src/main/java/de/example/tdd/Contact.java +++ /dev/null @@ -1,5 +0,0 @@ -package de.example.tdd; - -public class Contact { - -} diff --git a/TDD/src/main/java/de/example/tdd/Dollar.java b/TDD/src/main/java/de/example/tdd/Dollar.java deleted file mode 100644 index 989b1c1..0000000 --- a/TDD/src/main/java/de/example/tdd/Dollar.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.example.tdd; - -public class Dollar { - int amount; - - public Dollar(final int amount) { - this.amount = amount; - } - - public void times(final int multiplier) { - amount *= multiplier; - } -} diff --git a/TDD/src/main/java/de/example/tdd/Money.java b/TDD/src/main/java/de/example/tdd/Money.java deleted file mode 100644 index 0b48902..0000000 --- a/TDD/src/main/java/de/example/tdd/Money.java +++ /dev/null @@ -1,5 +0,0 @@ -package de.example.tdd; - -public class Money { - -} diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java b/TDD/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java deleted file mode 100644 index a2ba5ee..0000000 --- a/TDD/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/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java b/TDD/src/main/java/org/craftedsw/tripservicekata/UserBuilder.java deleted file mode 100644 index 8f95bc9..0000000 --- a/TDD/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/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java b/TDD/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java deleted file mode 100644 index 828353e..0000000 --- a/TDD/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/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java b/TDD/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java deleted file mode 100644 index 880bf20..0000000 --- a/TDD/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/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java deleted file mode 100644 index f12ff3c..0000000 --- a/TDD/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/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java deleted file mode 100644 index ab859ed..0000000 --- a/TDD/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/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java deleted file mode 100644 index 276710f..0000000 --- a/TDD/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/src/main/java/org/craftedsw/tripservicekata/user/User.java b/TDD/src/main/java/org/craftedsw/tripservicekata/user/User.java deleted file mode 100644 index d038fe8..0000000 --- a/TDD/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/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java b/TDD/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java deleted file mode 100644 index 6b91c0d..0000000 --- a/TDD/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/src/main/resources/log4j2.xml b/TDD/src/main/resources/log4j2.xml deleted file mode 100644 index 2de3fd2..0000000 --- a/TDD/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/src/main/resources/logback.xml b/TDD/src/main/resources/logback.xml deleted file mode 100644 index 685c34c..0000000 --- a/TDD/src/main/resources/logback.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - %d{yyyyMMddHHmmssSSS} - %-5p - [%t] - [%C] - %m%n - - - - - - diff --git a/TDD/src/test/java/de/example/services/UsersServiceTest.java b/TDD/src/test/java/de/example/services/UsersServiceTest.java deleted file mode 100644 index 57ee670..0000000 --- a/TDD/src/test/java/de/example/services/UsersServiceTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package de.example.services; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; - -import de.example.model.mapper.UsersMapper; -import de.example.model.resource.UserResource; -import de.example.services.impl.UsersServiceImpl; - - -public class UsersServiceTest { - private static final long ID = 10L; - - private UsersService usersService; - private UsersMapper usersMapper; - - @Before - public void setUp() throws Exception { - usersMapper = mock(UsersMapper.class); - // USING spy because we want to spy UsersService.spiedMethod - // otherwise we wouldn't need org.mockito.Mockito.spy. - usersService = spy(new UsersServiceImpl(usersMapper)); - } - - @Test - public void givenSomeIdWhenFindUserThenReturnUser() { - // Arrange - UserResource expectedUser = new UserResource(ID, "Gustavo"); - when(usersMapper.findOne(ID)).thenReturn(expectedUser); - - // Act - UserResource user = usersService.findOneById(ID); - - // Assert - assertEquals(expectedUser, user); - } - - - @Test - public void givenNewUserDataWhenCreateNewUserThenCreateUser() { - // Arrange - UserResource expectedUser = new UserResource(ID, "Gustavo"); - UserResource newUser = new UserResource(null, "Gustavo"); - - // Act - usersService.create(newUser); - - // Assert - verify(usersService).spiedMethod(newUser); - verify(usersMapper).create(newUser); - assertEquals(expectedUser, newUser); - } - - @Test(expected=NullPointerException.class) - public void givenNoUserDataWhenCreateNewUserThenThrowNullPointerException() { - // Act - usersService.create(null); - } - - @Test - public void givenNewUserDataWhenCreateNewUserThenCreateUserWithArgumentCaptor() { - // Arrange - UserResource newUser = new UserResource(null, "Gustavo"); - ArgumentCaptor argument = ArgumentCaptor.forClass(UserResource.class); - - // Act - usersService.create(newUser); - - // Assert - verify(usersService).spiedMethod(newUser); - verify(usersMapper).create(argument.capture()); - assertEquals(argument.getValue(), newUser); - } -} diff --git a/TDD/src/test/java/de/example/tdd/AddressBookTest.java b/TDD/src/test/java/de/example/tdd/AddressBookTest.java deleted file mode 100644 index a1d8238..0000000 --- a/TDD/src/test/java/de/example/tdd/AddressBookTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.example.tdd; - -import static org.junit.Assert.assertEquals; - -import java.util.List; - -import org.junit.Test; - -public class AddressBookTest { - - @Test - public void testGivenAndExistingAddressBookWhenRetrievingTheContactsThenListWithContacts() { - //AddressBook addressBook = new AddressBook(); - - - //List contacts = addressBook.getAllContacts(); - //int size = contacts.size(); - - // Assert - assertEquals(16, 16); - } -} diff --git a/TDD/src/test/java/de/example/tdd/CalculatorTest.java b/TDD/src/test/java/de/example/tdd/CalculatorTest.java deleted file mode 100644 index 8c5f54c..0000000 --- a/TDD/src/test/java/de/example/tdd/CalculatorTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package de.example.tdd; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -public class CalculatorTest { - private Calculator calculator; - - @Before - public void setUp() throws Exception { - // Arrange - calculator = new Calculator(); - } - - @Test - public void testGivenTwoNumbersWhenAddingThenResult() { - // Arrange - - - // Act - int result = calculator.sum(2, 2); - - // Assert - assertEquals(4, result); - - - // Act - result = calculator.sum(8, 8); - - // Assert - assertEquals(16, result); - } - - @Test - public void testGivenTwoNumbersWhenMultiplicationThenProduct() { - - - assertEquals(16, calculator.product(4, 4)); - - assertEquals(64, calculator.product(8, 8)); - } -} diff --git a/TDD/src/test/java/de/example/tdd/MoneyTest.java b/TDD/src/test/java/de/example/tdd/MoneyTest.java deleted file mode 100644 index d778ee9..0000000 --- a/TDD/src/test/java/de/example/tdd/MoneyTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package de.example.tdd; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -public class MoneyTest { - - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - } - - @Test - public void testMultiplication() - { - Dollar five = new Dollar(5); - five.times(2); - - assertEquals(10, five.amount); - } -} diff --git a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java deleted file mode 100644 index 34f6cc5..0000000 --- a/TDD/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/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java deleted file mode 100644 index 1111b54..0000000 --- a/TDD/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/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java b/TDD/src/test/java/org/craftedsw/tripservicekata/user/UserShould.java deleted file mode 100644 index dfe2aee..0000000 --- a/TDD/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/tdd/README.md b/TDD/tdd/README.md new file mode 100644 index 0000000..12e4c51 --- /dev/null +++ b/TDD/tdd/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/tdd/pom.xml b/TDD/tdd/pom.xml new file mode 100644 index 0000000..1250f6d --- /dev/null +++ b/TDD/tdd/pom.xml @@ -0,0 +1,152 @@ + + + 4.0.0 + de.example.tdd + tdd-by-example + 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 + + + + + + + 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/tdd/src/main/java/de/example/model/mapper/UsersMapper.java b/TDD/tdd/src/main/java/de/example/model/mapper/UsersMapper.java new file mode 100644 index 0000000..aa45c04 --- /dev/null +++ b/TDD/tdd/src/main/java/de/example/model/mapper/UsersMapper.java @@ -0,0 +1,11 @@ +package de.example.model.mapper; + +import de.example.model.resource.UserResource; + + +public interface UsersMapper { + + public UserResource findOne(long id); + + public void create(UserResource user); +} diff --git a/TDD/tdd/src/main/java/de/example/model/resource/UserResource.java b/TDD/tdd/src/main/java/de/example/model/resource/UserResource.java new file mode 100644 index 0000000..b2e878a --- /dev/null +++ b/TDD/tdd/src/main/java/de/example/model/resource/UserResource.java @@ -0,0 +1,52 @@ +package de.example.model.resource; + + +public class UserResource { + private final Long id; + private final String name; + + public UserResource(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + + public String getName() { + return name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + UserResource other = (UserResource)obj; + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + return true; + } + +} diff --git a/TDD/tdd/src/main/java/de/example/services/UsersService.java b/TDD/tdd/src/main/java/de/example/services/UsersService.java new file mode 100644 index 0000000..20d416b --- /dev/null +++ b/TDD/tdd/src/main/java/de/example/services/UsersService.java @@ -0,0 +1,13 @@ +package de.example.services; + +import de.example.model.resource.UserResource; + + +public interface UsersService { + + UserResource findOneById(long id); + + void create(UserResource user); + + String spiedMethod(UserResource user); +} diff --git a/TDD/tdd/src/main/java/de/example/services/impl/UsersServiceImpl.java b/TDD/tdd/src/main/java/de/example/services/impl/UsersServiceImpl.java new file mode 100644 index 0000000..d0a3a39 --- /dev/null +++ b/TDD/tdd/src/main/java/de/example/services/impl/UsersServiceImpl.java @@ -0,0 +1,37 @@ +package de.example.services.impl; + +import java.util.Objects; + +import de.example.model.mapper.UsersMapper; +import de.example.model.resource.UserResource; +import de.example.services.UsersService; + + +public class UsersServiceImpl implements UsersService { + private final UsersMapper usersMapper; + + public UsersServiceImpl(final UsersMapper usersMapper) { + this.usersMapper = usersMapper; + } + + @Override + public UserResource findOneById(long id) { + return this.usersMapper.findOne(id); + + } + + @Override + public void create(final UserResource user) { + Objects.requireNonNull(user, "user"); + + this.spiedMethod(user); + + usersMapper.create(user); + } + + @Override + public String spiedMethod(final UserResource user) { + return user.getName(); + } + +} diff --git a/TDD/tdd/src/main/java/de/example/tdd/AddressBook.java b/TDD/tdd/src/main/java/de/example/tdd/AddressBook.java new file mode 100644 index 0000000..b7c9215 --- /dev/null +++ b/TDD/tdd/src/main/java/de/example/tdd/AddressBook.java @@ -0,0 +1,12 @@ +package de.example.tdd; + +import java.util.List; + +public class AddressBook { + + public List getAllContacts() { + + return null; + } + +} diff --git a/TDD/tdd/src/main/java/de/example/tdd/Calculator.java b/TDD/tdd/src/main/java/de/example/tdd/Calculator.java new file mode 100644 index 0000000..e8882fc --- /dev/null +++ b/TDD/tdd/src/main/java/de/example/tdd/Calculator.java @@ -0,0 +1,15 @@ +package de.example.tdd; + +public class Calculator { + + public int sum(final int augend, final int addend) { + + return augend + addend; + } + + public int product(final int multiplicand, final int multiplicier) { + + return multiplicand * multiplicier; + } + +} diff --git a/TDD/tdd/src/main/java/de/example/tdd/Contact.java b/TDD/tdd/src/main/java/de/example/tdd/Contact.java new file mode 100644 index 0000000..6570ea7 --- /dev/null +++ b/TDD/tdd/src/main/java/de/example/tdd/Contact.java @@ -0,0 +1,5 @@ +package de.example.tdd; + +public class Contact { + +} diff --git a/TDD/tdd/src/main/java/de/example/tdd/Dollar.java b/TDD/tdd/src/main/java/de/example/tdd/Dollar.java new file mode 100644 index 0000000..989b1c1 --- /dev/null +++ b/TDD/tdd/src/main/java/de/example/tdd/Dollar.java @@ -0,0 +1,13 @@ +package de.example.tdd; + +public class Dollar { + int amount; + + public Dollar(final int amount) { + this.amount = amount; + } + + public void times(final int multiplier) { + amount *= multiplier; + } +} diff --git a/TDD/tdd/src/main/java/de/example/tdd/Money.java b/TDD/tdd/src/main/java/de/example/tdd/Money.java new file mode 100644 index 0000000..0b48902 --- /dev/null +++ b/TDD/tdd/src/main/java/de/example/tdd/Money.java @@ -0,0 +1,5 @@ +package de.example.tdd; + +public class Money { + +} diff --git a/TDD/tdd/src/main/resources/log4j2.xml b/TDD/tdd/src/main/resources/log4j2.xml new file mode 100644 index 0000000..2de3fd2 --- /dev/null +++ b/TDD/tdd/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/tdd/src/main/resources/logback.xml b/TDD/tdd/src/main/resources/logback.xml new file mode 100644 index 0000000..685c34c --- /dev/null +++ b/TDD/tdd/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + %d{yyyyMMddHHmmssSSS} - %-5p - [%t] - [%C] - %m%n + + + + + + diff --git a/TDD/tdd/src/test/java/de/example/services/UsersServiceTest.java b/TDD/tdd/src/test/java/de/example/services/UsersServiceTest.java new file mode 100644 index 0000000..57ee670 --- /dev/null +++ b/TDD/tdd/src/test/java/de/example/services/UsersServiceTest.java @@ -0,0 +1,81 @@ +package de.example.services; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import de.example.model.mapper.UsersMapper; +import de.example.model.resource.UserResource; +import de.example.services.impl.UsersServiceImpl; + + +public class UsersServiceTest { + private static final long ID = 10L; + + private UsersService usersService; + private UsersMapper usersMapper; + + @Before + public void setUp() throws Exception { + usersMapper = mock(UsersMapper.class); + // USING spy because we want to spy UsersService.spiedMethod + // otherwise we wouldn't need org.mockito.Mockito.spy. + usersService = spy(new UsersServiceImpl(usersMapper)); + } + + @Test + public void givenSomeIdWhenFindUserThenReturnUser() { + // Arrange + UserResource expectedUser = new UserResource(ID, "Gustavo"); + when(usersMapper.findOne(ID)).thenReturn(expectedUser); + + // Act + UserResource user = usersService.findOneById(ID); + + // Assert + assertEquals(expectedUser, user); + } + + + @Test + public void givenNewUserDataWhenCreateNewUserThenCreateUser() { + // Arrange + UserResource expectedUser = new UserResource(ID, "Gustavo"); + UserResource newUser = new UserResource(null, "Gustavo"); + + // Act + usersService.create(newUser); + + // Assert + verify(usersService).spiedMethod(newUser); + verify(usersMapper).create(newUser); + assertEquals(expectedUser, newUser); + } + + @Test(expected=NullPointerException.class) + public void givenNoUserDataWhenCreateNewUserThenThrowNullPointerException() { + // Act + usersService.create(null); + } + + @Test + public void givenNewUserDataWhenCreateNewUserThenCreateUserWithArgumentCaptor() { + // Arrange + UserResource newUser = new UserResource(null, "Gustavo"); + ArgumentCaptor argument = ArgumentCaptor.forClass(UserResource.class); + + // Act + usersService.create(newUser); + + // Assert + verify(usersService).spiedMethod(newUser); + verify(usersMapper).create(argument.capture()); + assertEquals(argument.getValue(), newUser); + } +} diff --git a/TDD/tdd/src/test/java/de/example/tdd/AddressBookTest.java b/TDD/tdd/src/test/java/de/example/tdd/AddressBookTest.java new file mode 100644 index 0000000..a1d8238 --- /dev/null +++ b/TDD/tdd/src/test/java/de/example/tdd/AddressBookTest.java @@ -0,0 +1,22 @@ +package de.example.tdd; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Test; + +public class AddressBookTest { + + @Test + public void testGivenAndExistingAddressBookWhenRetrievingTheContactsThenListWithContacts() { + //AddressBook addressBook = new AddressBook(); + + + //List contacts = addressBook.getAllContacts(); + //int size = contacts.size(); + + // Assert + assertEquals(16, 16); + } +} diff --git a/TDD/tdd/src/test/java/de/example/tdd/CalculatorTest.java b/TDD/tdd/src/test/java/de/example/tdd/CalculatorTest.java new file mode 100644 index 0000000..8c5f54c --- /dev/null +++ b/TDD/tdd/src/test/java/de/example/tdd/CalculatorTest.java @@ -0,0 +1,44 @@ +package de.example.tdd; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class CalculatorTest { + private Calculator calculator; + + @Before + public void setUp() throws Exception { + // Arrange + calculator = new Calculator(); + } + + @Test + public void testGivenTwoNumbersWhenAddingThenResult() { + // Arrange + + + // Act + int result = calculator.sum(2, 2); + + // Assert + assertEquals(4, result); + + + // Act + result = calculator.sum(8, 8); + + // Assert + assertEquals(16, result); + } + + @Test + public void testGivenTwoNumbersWhenMultiplicationThenProduct() { + + + assertEquals(16, calculator.product(4, 4)); + + assertEquals(64, calculator.product(8, 8)); + } +} diff --git a/TDD/tdd/src/test/java/de/example/tdd/MoneyTest.java b/TDD/tdd/src/test/java/de/example/tdd/MoneyTest.java new file mode 100644 index 0000000..d778ee9 --- /dev/null +++ b/TDD/tdd/src/test/java/de/example/tdd/MoneyTest.java @@ -0,0 +1,49 @@ +package de.example.tdd; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MoneyTest { + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + @Test + public void testMultiplication() + { + Dollar five = new Dollar(5); + five.times(2); + + assertEquals(10, five.amount); + } +} -- 2.1.4