├── .gitignore
├── README.md
├── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── maven-settings.xml
├── settings.gradle
├── slides
└── Getting started with Test Driven Development_10-2019.pdf
├── src
├── main
│ └── java
│ │ └── com
│ │ └── cefalo
│ │ └── tdd
│ │ ├── Customer.java
│ │ ├── PasswordValidator.java
│ │ ├── SimpleCalculator.java
│ │ └── StringHelper.java
└── test
│ └── java
│ └── com
│ └── cefalo
│ └── tdd
│ ├── CustomerTests.java
│ ├── PasswordValidatorTests.java
│ ├── SimpleCalculatorTests.java
│ └── StringHelperTests.java
└── step-by-step-tdd
├── CustomerTests01.java
├── CustomerTests02.java
├── CustomerTests03.java
├── CustomerTests04.java
├── CustomerTests05.java
├── CustomerTests06.java
├── CustomerTests07.java
├── CustomerTests08.java
├── CustomerTests09.java
├── CustomerTests10.java
└── CustomerTests11.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Operating System Files
2 | *.DS_Store
3 | Thumbs.db
4 | *.sw?
5 | .#*
6 | *#
7 | *~
8 | *.sublime-*
9 |
10 | # Build Artifacts
11 | .gradle/
12 | build/
13 | target/
14 | bin/
15 | dependency-reduced-pom.xml
16 |
17 | # Eclipse Project Files
18 |
19 | .classpath
20 | .project
21 | .settings/
22 |
23 | # IntelliJ IDEA Files
24 |
25 | *.iml
26 | *.ipr
27 | *.iws
28 | *.idea
29 | out/
30 |
31 | README.html
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Let's Learn Test Driven Development
2 | This repository is created for learning and exploring Test Driven Development in Java using JUnit
3 |
4 | ## Required Tools
5 | * JDK 1.8 or higher
6 | * Git
7 | * One of the following build tools:
8 | * Gradle 5.4.1 or higher
9 | * Maven 3.6.0 or higher
10 |
11 | ## Build Instruction
12 | First, clone this repository into your development environment by using the following command: \
13 | `git clone https://github.com/Cefalo/lets-learn-tdd`
14 |
15 | By default, this project can be built using **Gradle**.
16 |
17 | Run the following command from your terminal to build this project using Gradle:
18 |
19 | `gradlew build`
20 |
21 | If Gradle is not installed, then this command will install Gradle automatically and
22 | then build the project.
23 |
24 | This project can also be built using **Maven**. For this first, you will need to
25 | rename the maven-settings.xml to pom.xml. After that, run the following command:
26 |
27 | `mvn clean install`
28 |
29 | ## Problem Set and Solutions
30 | In this repository, we tried to solve the following sample problems using TDD:
31 |
32 | 1. **Truncate As from only first 2 characters of a String** \
33 | Unit tests are defined at: [StringHelperTests.java](src/test/java/com/cefalo/tdd/StringHelperTests.java) \
34 | Functionality implemented at the truncateFirst2As() method of [StringHelper.java](src/main/java/com/cefalo/tdd/StringHelper.java)
35 |
36 | 2. **Reverse the last 2 characters of a String** \
37 | Unit tests are defined at: [StringHelperTests.java](src/test/java/com/cefalo/tdd/StringHelperTests.java) \
38 | Functionality implemented at the swapLastTwoChars() method of [StringHelper.java](src/main/java/com/cefalo/tdd/StringHelper.java)
39 |
40 | 3. **Password Validation Rules** \
41 | Unit tests are defined at: [PasswordValidatorTests.java](src/test/java/com/cefalo/tdd/PasswordValidatorTests.java) \
42 | Functionality implemented at [PasswordValidator.java](src/main/java/com/cefalo/tdd/PasswordValidator.java)
43 |
44 | 4. **Simple Calculator for simple addition and division** \
45 | Unit tests are defined at: [SimpleCalculatorTests.java](src/test/java/com/cefalo/tdd/SimpleCalculatorTests.java) \
46 | Functionality implemented at [SimpleCalculator.java](src/main/java/com/cefalo/tdd/SimpleCalculator.java)
47 |
48 | 5. **Customer with reward points and redemption** \
49 | This problem will be used in the workshop to learn step by step Test Driven Development. \
50 | All the steps of the TDD are already present in this [directory](step-by-step-tdd). \
51 | Unit tests will be defined at: [CustomerTests.java](src/test/java/com/cefalo/tdd/CustomerTests.java) \
52 | Functionality will be implemented at [Customer.java](src/main/java/com/cefalo/tdd/Customer.java)
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'java'
3 | }
4 |
5 | group 'com.cefalo'
6 | version '1.0-SNAPSHOT'
7 |
8 | sourceCompatibility = 1.8
9 |
10 | repositories {
11 | mavenCentral()
12 | }
13 |
14 | dependencies {
15 | testImplementation("org.junit.jupiter:junit-jupiter-api:5.5.2")
16 | testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.5.2")
17 | }
18 |
19 | test {
20 | useJUnitPlatform()
21 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cefalo/lets-learn-tdd/d200530324e789443f7ea3cef5a64446fe237b35/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # http://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 | # Determine the Java command to use to start the JVM.
86 | if [ -n "$JAVA_HOME" ] ; then
87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
88 | # IBM's JDK on AIX uses strange locations for the executables
89 | JAVACMD="$JAVA_HOME/jre/sh/java"
90 | else
91 | JAVACMD="$JAVA_HOME/bin/java"
92 | fi
93 | if [ ! -x "$JAVACMD" ] ; then
94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
95 |
96 | Please set the JAVA_HOME variable in your environment to match the
97 | location of your Java installation."
98 | fi
99 | else
100 | JAVACMD="java"
101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
102 |
103 | Please set the JAVA_HOME variable in your environment to match the
104 | location of your Java installation."
105 | fi
106 |
107 | # Increase the maximum file descriptors if we can.
108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
109 | MAX_FD_LIMIT=`ulimit -H -n`
110 | if [ $? -eq 0 ] ; then
111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
112 | MAX_FD="$MAX_FD_LIMIT"
113 | fi
114 | ulimit -n $MAX_FD
115 | if [ $? -ne 0 ] ; then
116 | warn "Could not set maximum file descriptor limit: $MAX_FD"
117 | fi
118 | else
119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
120 | fi
121 | fi
122 |
123 | # For Darwin, add options to specify how the application appears in the dock
124 | if $darwin; then
125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
126 | fi
127 |
128 | # For Cygwin, switch paths to Windows format before running java
129 | if $cygwin ; then
130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
132 | JAVACMD=`cygpath --unix "$JAVACMD"`
133 |
134 | # We build the pattern for arguments to be converted via cygpath
135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
136 | SEP=""
137 | for dir in $ROOTDIRSRAW ; do
138 | ROOTDIRS="$ROOTDIRS$SEP$dir"
139 | SEP="|"
140 | done
141 | OURCYGPATTERN="(^($ROOTDIRS))"
142 | # Add a user-defined pattern to the cygpath arguments
143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
145 | fi
146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
147 | i=0
148 | for arg in "$@" ; do
149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
151 |
152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
154 | else
155 | eval `echo args$i`="\"$arg\""
156 | fi
157 | i=$((i+1))
158 | done
159 | case $i in
160 | (0) set -- ;;
161 | (1) set -- "$args0" ;;
162 | (2) set -- "$args0" "$args1" ;;
163 | (3) set -- "$args0" "$args1" "$args2" ;;
164 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
165 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
166 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
167 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
168 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
169 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
170 | esac
171 | fi
172 |
173 | # Escape application args
174 | save () {
175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
176 | echo " "
177 | }
178 | APP_ARGS=$(save "$@")
179 |
180 | # Collect all arguments for the java command, following the shell quoting and substitution rules
181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
182 |
183 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
184 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
185 | cd "$(dirname "$0")"
186 | fi
187 |
188 | exec "$JAVACMD" "$@"
189 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem http://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
33 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
34 |
35 | @rem Find java.exe
36 | if defined JAVA_HOME goto findJavaFromJavaHome
37 |
38 | set JAVA_EXE=java.exe
39 | %JAVA_EXE% -version >NUL 2>&1
40 | if "%ERRORLEVEL%" == "0" goto init
41 |
42 | echo.
43 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
44 | echo.
45 | echo Please set the JAVA_HOME variable in your environment to match the
46 | echo location of your Java installation.
47 |
48 | goto fail
49 |
50 | :findJavaFromJavaHome
51 | set JAVA_HOME=%JAVA_HOME:"=%
52 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
53 |
54 | if exist "%JAVA_EXE%" goto init
55 |
56 | echo.
57 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
58 | echo.
59 | echo Please set the JAVA_HOME variable in your environment to match the
60 | echo location of your Java installation.
61 |
62 | goto fail
63 |
64 | :init
65 | @rem Get command-line arguments, handling Windows variants
66 |
67 | if not "%OS%" == "Windows_NT" goto win9xME_args
68 |
69 | :win9xME_args
70 | @rem Slurp the command line arguments.
71 | set CMD_LINE_ARGS=
72 | set _SKIP=2
73 |
74 | :win9xME_args_slurp
75 | if "x%~1" == "x" goto execute
76 |
77 | set CMD_LINE_ARGS=%*
78 |
79 | :execute
80 | @rem Setup the command line
81 |
82 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
83 |
84 | @rem Execute Gradle
85 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
86 |
87 | :end
88 | @rem End local scope for the variables with windows NT shell
89 | if "%ERRORLEVEL%"=="0" goto mainEnd
90 |
91 | :fail
92 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
93 | rem the _cmd.exe /c_ return code!
94 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
95 | exit /b 1
96 |
97 | :mainEnd
98 | if "%OS%"=="Windows_NT" endlocal
99 |
100 | :omega
101 |
--------------------------------------------------------------------------------
/maven-settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | 4.0.0
7 |
8 | com.cefalo
9 | lets-learn-tdd
10 | 1.0-SNAPSHOT
11 |
12 | lets-learn-tdd
13 |
14 |
15 | Cefalo Bangladesh Ltd.
16 | https://www.cefalo.com/en
17 |
18 |
19 |
20 |
21 | fmshaon
22 | Ferdous Mahmud Shaon
23 | fmshaon@cefalo.com
24 |
25 |
26 |
27 |
28 |
29 | UTF-8
30 | UTF-8
31 | 8
32 | ${java.version}
33 | ${java.version}
34 | 5.5.2
35 |
36 |
37 |
38 |
39 | org.junit.jupiter
40 | junit-jupiter-api
41 | ${junit-platform.version}
42 | test
43 |
44 |
45 | org.junit.jupiter
46 | junit-jupiter-engine
47 | ${junit-platform.version}
48 | test
49 |
50 |
51 |
52 |
53 |
54 |
55 | org.apache.maven.plugins
56 | maven-compiler-plugin
57 | 3.8.1
58 |
59 |
60 | org.apache.maven.plugins
61 | maven-surefire-plugin
62 | 2.22.2
63 |
64 |
65 | org.apache.maven.plugins
66 | maven-failsafe-plugin
67 | 2.22.2
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'lets-learn-tdd'
2 |
3 |
--------------------------------------------------------------------------------
/slides/Getting started with Test Driven Development_10-2019.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cefalo/lets-learn-tdd/d200530324e789443f7ea3cef5a64446fe237b35/slides/Getting started with Test Driven Development_10-2019.pdf
--------------------------------------------------------------------------------
/src/main/java/com/cefalo/tdd/Customer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | /**
13 | * @author Ferdous Mahmud Shaon
14 | * @author last modified by $
15 | * @version $ $
16 | */
17 | public class Customer {
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/cefalo/tdd/PasswordValidator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Header$
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import java.util.Arrays;
13 | import java.util.List;
14 |
15 | /**
16 | * @author Ferdous Mahmud Shaon
17 | * @author last modified by $Author$
18 | * @version $Revision$ $Date$
19 | */
20 | public class PasswordValidator {
21 | /*
22 | * Definition of valid password
23 | * - Min Length: 6 characters
24 | * - Max Length: 12 characters
25 | * - Must have at least 1 lowercase character
26 | * - Must have at least 1 uppercase character
27 | * - Must have at least 1 numeric character
28 | * - Must have at least 1 of these special characters (!, @, #, $, %, ^, &)
29 | */
30 |
31 | public static boolean isValidPassword(final String pPassword) {
32 | if(pPassword.length()<6 || pPassword.length()>12) {
33 | return false;
34 | }
35 |
36 | boolean hasLowerCaseCharacter = false;
37 | boolean hasUpperCaseCharacter = false;
38 | boolean hasNumericCharacter = false;
39 | boolean hasSpecialCharacter = false;
40 | final List specialCharacters = Arrays.asList('!', '@', '#', '$', '%', '^', '&');
41 |
42 | for(int i=0;iFerdous Mahmud Shaon
14 | * @author last modified by $Author$
15 | * @version $Revision$ $Date$
16 | */
17 | public class SimpleCalculator {
18 | public double add(final double pNumber1, final double pNumber2) {
19 | return pNumber1 + pNumber2;
20 | }
21 |
22 | public double divide(final double pDividend, final double pDivisor) {
23 | if(pDivisor==0) {
24 | throw new IllegalArgumentException("Divisor cannot be 0");
25 | }
26 | return pDividend / pDivisor;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/cefalo/tdd/StringHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Header$
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | /**
13 | * @author Ferdous Mahmud Shaon
14 | * @author last modified by $Author$
15 | * @version $Revision$ $Date$
16 | */
17 | public class StringHelper {
18 |
19 | /*
20 | * Sample Input and Output
21 | * "BCDE" => "BCDE"
22 | * "ABCD" => "BCD"
23 | * "AACD" => "CD"
24 | * "BACD" => "BCD"
25 | * "AAAA" => "AA"
26 | * "MNAA" => "MNAA"*
27 | * "A" => ""
28 | * "" => ""
29 | */
30 | public static String truncateFirst2As(final String pInput) {
31 | if(pInput.length()<2) return pInput.replaceAll("A", "");
32 |
33 | String firstTwoChars = pInput.substring(0,2);
34 | String remainingChars = pInput.substring(2);
35 | String output = firstTwoChars.replaceAll("A","").concat(remainingChars);
36 |
37 | return output;
38 | }
39 |
40 | /*
41 | * Sample input and Output:
42 | * "AB" => "BA"
43 | * "ABCD" => "ABDC"
44 | * "AACDEFGHIJ" => "AACDEFGHJI"
45 | * "A => "A"
46 | * "" => ""
47 | */
48 | public static String swapLastTwoChars(final String pInput) {
49 | final int length = pInput.length();
50 |
51 | if(length<2) return pInput;
52 |
53 | String inputWithoutLastTwoChars = pInput.substring(0,length-2);
54 | String lastTwoChars = pInput.substring(length-2);
55 | String reverseLasTwoChars = new StringBuilder(lastTwoChars).reverse().toString();
56 |
57 | return inputWithoutLastTwoChars.concat(reverseLasTwoChars);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/test/java/com/cefalo/tdd/CustomerTests.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import static org.junit.jupiter.api.Assertions.assertThrows;
16 |
17 | /**
18 | * @author Ferdous Mahmud Shaon
19 | * @author last modified by $
20 | * @version $ $
21 | */
22 |
23 | /*
24 | * Customer has following properties:
25 | * Name: name of the customer, String, Mandatory
26 | *
27 | * RewardPoints: the total reward points of a customer, default value: 0, integer
28 | * value can only be set >=0, otherwise throws IllegalArgumentException
29 | *
30 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
31 | * total RewardPoints count += ceil(amount * 0.1)
32 | * the amount cannot be <0, otherwise throw IllegalArgumentException
33 | *
34 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
35 | * if the given redeem points > total RewardPoints count, throw IllegalArgumentException
36 | *
37 | * */
38 | public class CustomerTests {
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/test/java/com/cefalo/tdd/PasswordValidatorTests.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Header$
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.Test;
13 |
14 | import static org.junit.jupiter.api.Assertions.assertTrue;
15 | import static org.junit.jupiter.api.Assertions.assertFalse;
16 |
17 | /**
18 | * @author Ferdous Mahmud Shaon
19 | * @author last modified by $Author$
20 | * @version $Revision$ $Date$
21 | */
22 | public class PasswordValidatorTests {
23 | /*
24 | * Definition of valid password
25 | * - Min Length: 6 characters
26 | * - Max Length: 12 characters
27 | * - Must have at least 1 lowercase character
28 | * - Must have at least 1 uppercase character
29 | * - Must have at least 1 numeric character
30 | * - Must have at least 1 of these special characters (!, @, #, $, %, ^, &)
31 | */
32 | @Test
33 | public void testSmallerLengthPassword() {
34 | assertFalse(PasswordValidator.isValidPassword("abc12"));
35 | }
36 |
37 | @Test
38 | public void testBiggerLengthPassword() {
39 | assertFalse(PasswordValidator.isValidPassword("abcdefg123456"));
40 | }
41 |
42 | @Test
43 | public void testUpperCaseOnlyPassword() {
44 | assertFalse(PasswordValidator.isValidPassword("ABCDEF"));
45 | }
46 |
47 | @Test
48 | public void testLowerCaseOnlyPassword() {
49 | assertFalse(PasswordValidator.isValidPassword("abcdef"));
50 | }
51 |
52 | @Test
53 | public void testAlphabetOnlyPassword() {
54 | assertFalse(PasswordValidator.isValidPassword("abcDEF"));
55 | }
56 |
57 | @Test
58 | public void testAlphaNumericOnlyPassword() {
59 | assertFalse(PasswordValidator.isValidPassword("abcDEF123"));
60 | }
61 |
62 | @Test
63 | public void testLowerCaseMissingPassword() {
64 | assertFalse(PasswordValidator.isValidPassword("ABC@DEF123"));
65 | }
66 |
67 | @Test
68 | public void tesInvalidSpecialCharPassword() {
69 | assertFalse(PasswordValidator.isValidPassword("abc)@DEF123"));
70 | }
71 |
72 | @Test
73 | public void testValidPassword() {
74 | assertTrue(PasswordValidator.isValidPassword("abc@DEF123"));
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/src/test/java/com/cefalo/tdd/SimpleCalculatorTests.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Header$
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.BeforeAll;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import static org.junit.jupiter.api.Assertions.assertEquals;
16 | import static org.junit.jupiter.api.Assertions.assertThrows;
17 |
18 | /**
19 | * @author Ferdous Mahmud Shaon
20 | * @author last modified by $Author$
21 | * @version $Revision$ $Date$
22 | */
23 | public class SimpleCalculatorTests {
24 | private static SimpleCalculator simpleCalculator;
25 |
26 | @BeforeAll
27 | public static void setupSimpleCalculator() {
28 | simpleCalculator = new SimpleCalculator();
29 | }
30 |
31 | @Test
32 | public void testAddition() {
33 | assertEquals(5, simpleCalculator.add(2,3) );
34 | }
35 |
36 | @Test
37 | public void testDivision() {
38 | assertEquals(5, simpleCalculator.divide(10,2) );
39 | }
40 |
41 | @Test
42 | public void testDivision_zero_divisor() {
43 | assertThrows(IllegalArgumentException.class,() -> simpleCalculator.divide(10,0));
44 | }
45 | }
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/src/test/java/com/cefalo/tdd/StringHelperTests.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Header$
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.Test;
13 |
14 | import static org.junit.jupiter.api.Assertions.assertEquals;
15 |
16 | /**
17 | * @author Ferdous Mahmud Shaon
18 | * @author last modified by $Author$
19 | * @version $Revision$ $Date$
20 | */
21 | public class StringHelperTests {
22 | /*
23 | * Truncate A if it is present in the first two characters of a String
24 | * Sample input and Output:
25 | * "BCDE" => "BCDE"
26 | * "ABCD" => "BCD"
27 | * "AACD" => "CD"
28 | * "BACD" => "BCD"
29 | * "AAAA" => "AA"
30 | * "MNAA" => "MNAA"
31 | * "A" => ""
32 | * "" => ""
33 | */
34 | @Test
35 | public void testTruncateFirst2As_ANotPresent() {
36 | assertEquals("BCDE", StringHelper.truncateFirst2As("BCDE"));
37 | }
38 |
39 | @Test
40 | public void testTruncateFirst2As_AInFirstCharOnly() {
41 | assertEquals("BCD", StringHelper.truncateFirst2As("ABCD"));
42 | }
43 |
44 | @Test
45 | public void testTruncateFirst2As_AInFirstTwoCharsOnly() {
46 | assertEquals("CD", StringHelper.truncateFirst2As("AACD"));
47 | }
48 |
49 | @Test
50 | public void testTruncateFirst2As_AInSecondCharOnly() {
51 | assertEquals("BCD", StringHelper.truncateFirst2As("BACD"));
52 | }
53 |
54 | @Test
55 | public void testTruncateFirst2As_AInAllChars() {
56 | assertEquals("AA", StringHelper.truncateFirst2As("AAAA"));
57 | }
58 |
59 | @Test
60 | public void testTruncateFirst2As_ANotInFirstTwoChars() {
61 | assertEquals("MNAA", StringHelper.truncateFirst2As("MNAA"));
62 | }
63 |
64 | @Test
65 | public void testTruncateFirst2As_AInOneCharString() {
66 | assertEquals("", StringHelper.truncateFirst2As("A"));
67 | }
68 |
69 | @Test
70 | public void testTruncateFirst2As_EmptyString() {
71 | assertEquals("", StringHelper.truncateFirst2As(""));
72 | }
73 |
74 | /*
75 | * Swap tbe last two characters of a String
76 | * Sample input and Output:
77 | * "AB" => "BA"
78 | * "ABCD" => "ABDC"
79 | * "AACDEFGHIJ" => "AACDEFGHJI"
80 | * "A => "A"
81 | * "" => ""
82 | */
83 |
84 | @Test
85 | public void testSwapLastTwoChars_TwoCharOnlyString() {
86 | assertEquals("BA", StringHelper.swapLastTwoChars("AB"));
87 | }
88 | @Test
89 | public void testSwapLastTwoChars_MoreThanTwoCharString() {
90 | assertEquals("ABDC", StringHelper.swapLastTwoChars("ABCD"));
91 | }
92 | @Test
93 | public void testSwapLastTwoChars_LargeString() {
94 | assertEquals("AACDEFGHJI", StringHelper.swapLastTwoChars("AACDEFGHIJ"));
95 | }
96 | @Test
97 | public void testSwapLastTwoChars_OneCharString() {
98 | assertEquals("A", StringHelper.swapLastTwoChars("A"));
99 | }
100 | @Test
101 | public void testSwapLastTwoChars_EmptyString() {
102 | assertEquals("", StringHelper.swapLastTwoChars(""));
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests01.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.Test;
13 |
14 | import static org.junit.jupiter.api.Assertions.assertEquals;
15 |
16 | /**
17 | * @author Ferdous Mahmud Shaon
18 | * @author last modified by $
19 | * @version $ $
20 | */
21 | public class CustomerTests01 {
22 | /*
23 | * Customer has following properties:
24 | * Name: name of the customer, String, Mandatory
25 | *
26 | * RewardPoints: the total reward points of a customer, default value: 0, integer
27 | * value can only be set >=0, otherwise throws IllegalArgumentException
28 | *
29 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
30 | * total RewardPoints count += ceil(amount * 0.1)
31 | * the amount cannot be <0, otherwise throw IllegalArgumentException
32 | *
33 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
34 | * if the given number of points < total RewardPoints count
35 | * otherwise throw IllegalArgumentException
36 | *
37 | * */
38 | @Test
39 | public void testCustomerWithNameOnly() {
40 | Customer customer = new Customer("Arnab");
41 | assertEquals("Arnab", customer.getName());
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests02.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.Test;
13 |
14 | import static org.junit.jupiter.api.Assertions.assertEquals;
15 |
16 | /**
17 | * @author Ferdous Mahmud Shaon
18 | * @author last modified by $
19 | * @version $ $
20 | */
21 | public class CustomerTests02 {
22 | /*
23 | * Customer has following properties:
24 | * Name: name of the customer, String, Mandatory
25 | *
26 | * RewardPoints: the total reward points of a customer, default value: 0, integer
27 | * value can only be set >=0, otherwise throws IllegalArgumentException
28 | *
29 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
30 | * total RewardPoints count += ceil(amount * 0.1)
31 | * the amount cannot be <0, otherwise throw IllegalArgumentException
32 | *
33 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
34 | * if the given number of points < total RewardPoints count
35 | * otherwise throw IllegalArgumentException
36 | *
37 | * */
38 | @Test
39 | public void testCustomerWithNameOnly() {
40 | Customer customer = new Customer("Arnab");
41 | assertEquals("Arnab", customer.getName());
42 | }
43 | @Test
44 | public void testCustomerWithRewardPoints() {
45 | Customer customer = new Customer("Arnab");
46 | customer.setRewardPoints(100);
47 | assertEquals(100, customer.getRewardPoints());
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests03.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.Test;
13 |
14 | import static org.junit.jupiter.api.Assertions.assertEquals;
15 |
16 | /**
17 | * @author Ferdous Mahmud Shaon
18 | * @author last modified by $
19 | * @version $ $
20 | */
21 | public class CustomerTests03 {
22 | /*
23 | * Customer has following properties:
24 | * Name: name of the customer, String, Mandatory
25 | *
26 | * RewardPoints: the total reward points of a customer, default value: 0, integer
27 | * value can only be set >=0, otherwise throws IllegalArgumentException
28 | *
29 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
30 | * total RewardPoints count += ceil(amount * 0.1)
31 | * the amount cannot be <0, otherwise throw IllegalArgumentException
32 | *
33 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
34 | * if the given number of points < total RewardPoints count
35 | * otherwise throw IllegalArgumentException
36 | *
37 | * */
38 | @Test
39 | public void testCustomerWithNameOnly() {
40 | Customer customer = new Customer("Arnab");
41 | assertEquals("Arnab", customer.getName());
42 | }
43 | @Test
44 | public void testCustomerWithRewardPoints() {
45 | Customer customer = new Customer("Arnab");
46 | customer.setRewardPoints(100);
47 | assertEquals(100, customer.getRewardPoints());
48 | }
49 | @Test
50 | public void testCustomerWithDefaultRewardPoints() {
51 | Customer customer = new Customer("Arnab");
52 | assertEquals(0, customer.getRewardPoints());
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests04.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import static org.junit.jupiter.api.Assertions.assertEquals;
16 |
17 | /**
18 | * @author Ferdous Mahmud Shaon
19 | * @author last modified by $
20 | * @version $ $
21 | */
22 | public class CustomerTests04 {
23 | /*
24 | * Customer has following properties:
25 | * Name: name of the customer, String, Mandatory
26 | *
27 | * RewardPoints: the total reward points of a customer, default value: 0, integer
28 | * value can only be set >=0, otherwise throws IllegalArgumentException
29 | *
30 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
31 | * total RewardPoints count += ceil(amount * 0.1)
32 | * the amount cannot be <0, otherwise throw IllegalArgumentException
33 | *
34 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
35 | * if the given number of points < total RewardPoints count
36 | * otherwise throw IllegalArgumentException
37 | *
38 | * */
39 |
40 | private Customer customer;
41 |
42 | @BeforeEach
43 | public void setUpCustomer() {
44 | customer = new Customer("Arnab");
45 | }
46 | @Test
47 | public void testCustomerWithNameOnly() {
48 | assertEquals("Arnab", customer.getName());
49 | }
50 | @Test
51 | public void testCustomerWithRewardPoints() {
52 | customer.setRewardPoints(100);
53 | assertEquals(100, customer.getRewardPoints());
54 | }
55 | @Test
56 | public void testCustomerWithDefaultRewardPoints() {
57 | assertEquals(0, customer.getRewardPoints());
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests05.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import static org.junit.jupiter.api.Assertions.assertEquals;
16 | import static org.junit.jupiter.api.Assertions.assertThrows;
17 |
18 | /**
19 | * @author Ferdous Mahmud Shaon
20 | * @author last modified by $
21 | * @version $ $
22 | */
23 | public class CustomerTests05 {
24 | /*
25 | * Customer has following properties:
26 | * Name: name of the customer, String, Mandatory
27 | *
28 | * RewardPoints: the total reward points of a customer, default value: 0, integer
29 | * value can only be set >=0, otherwise throws IllegalArgumentException
30 | *
31 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
32 | * total RewardPoints count += ceil(amount * 0.1)
33 | * the amount cannot be <0, otherwise throw IllegalArgumentException
34 | *
35 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
36 | * if the given number of points < total RewardPoints count
37 | * otherwise throw IllegalArgumentException
38 | *
39 | * */
40 |
41 | private Customer customer;
42 |
43 | @BeforeEach
44 | public void setUpCustomer() {
45 | customer = new Customer("Arnab");
46 | }
47 | @Test
48 | public void testCustomerWithNameOnly() {
49 | assertEquals("Arnab", customer.getName());
50 | }
51 | @Test
52 | public void testCustomerWithRewardPoints() {
53 | customer.setRewardPoints(100);
54 | assertEquals(100, customer.getRewardPoints());
55 | }
56 | @Test
57 | public void testCustomerWithDefaultRewardPoints() {
58 | assertEquals(0, customer.getRewardPoints());
59 | }
60 | @Test
61 | public void testCustomerWithNegativeRewardPoints() {
62 | assertThrows(IllegalArgumentException.class, () -> customer.setRewardPoints(-10));
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests06.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import static org.junit.jupiter.api.Assertions.assertEquals;
16 | import static org.junit.jupiter.api.Assertions.assertThrows;
17 |
18 | /**
19 | * @author Ferdous Mahmud Shaon
20 | * @author last modified by $
21 | * @version $ $
22 | */
23 | public class CustomerTests06 {
24 | /*
25 | * Customer has following properties:
26 | * Name: name of the customer, String, Mandatory
27 | *
28 | * RewardPoints: the total reward points of a customer, default value: 0, integer
29 | * value can only be set >=0, otherwise throws IllegalArgumentException
30 | *
31 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
32 | * total RewardPoints count += ceil(amount * 0.1)
33 | * the amount cannot be <0, otherwise throw IllegalArgumentException
34 | *
35 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
36 | * if the given number of points < total RewardPoints count
37 | * otherwise throw IllegalArgumentException
38 | *
39 | * */
40 |
41 | private Customer customer;
42 |
43 | @BeforeEach
44 | public void setUpCustomer() {
45 | customer = new Customer("Arnab");
46 | }
47 | @Test
48 | public void testCustomerWithNameOnly() {
49 | assertEquals("Arnab", customer.getName());
50 | }
51 | @Test
52 | public void testCustomerWithRewardPoints() {
53 | customer.setRewardPoints(100);
54 | assertEquals(100, customer.getRewardPoints());
55 | }
56 | @Test
57 | public void testCustomerWithDefaultRewardPoints() {
58 | assertEquals(0, customer.getRewardPoints());
59 | }
60 | @Test
61 | public void testCustomerWithNegativeRewardPoints() {
62 | assertThrows(IllegalArgumentException.class, () -> customer.setRewardPoints(-10));
63 | }
64 | @Test
65 | public void testCustomerWithPurchaseGoods() {
66 | customer.setRewardPoints(100);
67 | customer.purchaseGoods(200);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests07.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import static org.junit.jupiter.api.Assertions.assertEquals;
16 | import static org.junit.jupiter.api.Assertions.assertThrows;
17 |
18 | /**
19 | * @author Ferdous Mahmud Shaon
20 | * @author last modified by $
21 | * @version $ $
22 | */
23 | public class CustomerTests07 {
24 | /*
25 | * Customer has following properties:
26 | * Name: name of the customer, String, Mandatory
27 | *
28 | * RewardPoints: the total reward points of a customer, default value: 0, integer
29 | * value can only be set >=0, otherwise throws IllegalArgumentException
30 | *
31 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
32 | * total RewardPoints count += ceil(amount * 0.1)
33 | * the amount cannot be <0, otherwise throw IllegalArgumentException
34 | *
35 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
36 | * if the given number of points < total RewardPoints count
37 | * otherwise throw IllegalArgumentException
38 | *
39 | * */
40 |
41 | private Customer customer;
42 |
43 | @BeforeEach
44 | public void setUpCustomer() {
45 | customer = new Customer("Arnab");
46 | }
47 | @Test
48 | public void testCustomerWithNameOnly() {
49 | assertEquals("Arnab", customer.getName());
50 | }
51 | @Test
52 | public void testCustomerWithRewardPoints() {
53 | customer.setRewardPoints(100);
54 | assertEquals(100, customer.getRewardPoints());
55 | }
56 | @Test
57 | public void testCustomerWithDefaultRewardPoints() {
58 | assertEquals(0, customer.getRewardPoints());
59 | }
60 | @Test
61 | public void testCustomerWithNegativeRewardPoints() {
62 | assertThrows(IllegalArgumentException.class, () -> customer.setRewardPoints(-10));
63 | }
64 | @Test
65 | public void testCustomerWithPurchaseGoods() {
66 | customer.setRewardPoints(100);
67 | customer.purchaseGoods(200);
68 | assertEquals(120,customer.getRewardPoints());
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests08.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import static org.junit.jupiter.api.Assertions.assertEquals;
16 | import static org.junit.jupiter.api.Assertions.assertThrows;
17 |
18 | /**
19 | * @author Ferdous Mahmud Shaon
20 | * @author last modified by $
21 | * @version $ $
22 | */
23 | /*
24 | * Customer has following properties:
25 | * Name: name of the customer, String, Mandatory
26 | *
27 | * RewardPoints: the total reward points of a customer, default value: 0, integer
28 | * value can only be set >=0, otherwise throws IllegalArgumentException
29 | *
30 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
31 | * total RewardPoints count += ceil(amount * 0.1)
32 | * the amount cannot be <0, otherwise throw IllegalArgumentException
33 | *
34 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
35 | * if the given number of points < total RewardPoints count
36 | * otherwise throw IllegalArgumentException
37 | *
38 | * */
39 | public class CustomerTests08 {
40 | private Customer customer;
41 |
42 | @BeforeEach
43 | public void setUpCustomer() {
44 | customer = new Customer("Arnab");
45 | }
46 | @Test
47 | public void testCustomerWithNameOnly() {
48 | assertEquals("Arnab", customer.getName());
49 | }
50 | @Test
51 | public void testCustomerWithRewardPoints() {
52 | customer.setRewardPoints(100);
53 | assertEquals(100, customer.getRewardPoints());
54 | }
55 | @Test
56 | public void testCustomerWithDefaultRewardPoints() {
57 | assertEquals(0, customer.getRewardPoints());
58 | }
59 | @Test
60 | public void testCustomerWithNegativeRewardPoints() {
61 | assertThrows(IllegalArgumentException.class, () -> customer.setRewardPoints(-10));
62 | }
63 | @Test
64 | public void testCustomerWithPurchaseGoods() {
65 | customer.setRewardPoints(100);
66 | customer.purchaseGoods(200);
67 | assertEquals(120,customer.getRewardPoints());
68 | }
69 | @Test
70 | public void testCustomerWithPurchaseGoods_CeilRewardPoints() {
71 | customer.setRewardPoints(100);
72 | customer.purchaseGoods(244);
73 | assertEquals(125,customer.getRewardPoints());
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests09.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import static org.junit.jupiter.api.Assertions.assertEquals;
16 | import static org.junit.jupiter.api.Assertions.assertThrows;
17 |
18 | /**
19 | * @author Ferdous Mahmud Shaon
20 | * @author last modified by $
21 | * @version $ $
22 | */
23 | /*
24 | * Customer has following properties:
25 | * Name: name of the customer, String, Mandatory
26 | *
27 | * RewardPoints: the total reward points of a customer, default value: 0, integer
28 | * value can only be set >=0, otherwise throws IllegalArgumentException
29 | *
30 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
31 | * total RewardPoints count += ceil(amount * 0.1)
32 | * the amount cannot be <0, otherwise throw IllegalArgumentException
33 | *
34 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
35 | * if the given number of points < total RewardPoints count
36 | * otherwise throw IllegalArgumentException
37 | *
38 | * */
39 | public class CustomerTests09 {
40 | private Customer customer;
41 |
42 | @BeforeEach
43 | public void setUpCustomer() {
44 | customer = new Customer("Arnab");
45 | }
46 | @Test
47 | public void testCustomerWithNameOnly() {
48 | assertEquals("Arnab", customer.getName());
49 | }
50 | @Test
51 | public void testCustomerWithRewardPoints() {
52 | customer.setRewardPoints(100);
53 | assertEquals(100, customer.getRewardPoints());
54 | }
55 | @Test
56 | public void testCustomerWithDefaultRewardPoints() {
57 | assertEquals(0, customer.getRewardPoints());
58 | }
59 | @Test
60 | public void testCustomerWithNegativeRewardPoints() {
61 | assertThrows(IllegalArgumentException.class, () -> customer.setRewardPoints(-10));
62 | }
63 | @Test
64 | public void testCustomerWithPurchaseGoods() {
65 | customer.setRewardPoints(100);
66 | customer.purchaseGoods(200);
67 | assertEquals(120,customer.getRewardPoints());
68 | }
69 | @Test
70 | public void testCustomerWithPurchaseGoods_CeilRewardPoints() {
71 | customer.setRewardPoints(100);
72 | customer.purchaseGoods(244);
73 | assertEquals(125,customer.getRewardPoints());
74 | }
75 | @Test
76 | public void testCustomerWithPurchaseGoods_invalidAmount() {
77 | customer.setRewardPoints(100);
78 | assertThrows(IllegalArgumentException.class, () -> customer.purchaseGoods(-244));
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests10.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import static org.junit.jupiter.api.Assertions.assertEquals;
16 | import static org.junit.jupiter.api.Assertions.assertThrows;
17 |
18 | /**
19 | * @author Ferdous Mahmud Shaon
20 | * @author last modified by $
21 | * @version $ $
22 | */
23 | /*
24 | * Customer has following properties:
25 | * Name: name of the customer, String, Mandatory
26 | *
27 | * RewardPoints: the total reward points of a customer, default value: 0, integer
28 | * value can only be set >=0, otherwise throws IllegalArgumentException
29 | *
30 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
31 | * total RewardPoints count += ceil(amount * 0.1)
32 | * the amount cannot be <0, otherwise throw IllegalArgumentException
33 | *
34 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
35 | * if the given number of points < total RewardPoints count
36 | * otherwise throw IllegalArgumentException
37 | *
38 | * */
39 | public class CustomerTests10 {
40 | private Customer customer;
41 |
42 | @BeforeEach
43 | public void setUpCustomer() {
44 | customer = new Customer("Arnab");
45 | }
46 | @Test
47 | public void testCustomerWithNameOnly() {
48 | assertEquals("Arnab", customer.getName());
49 | }
50 | @Test
51 | public void testCustomerWithRewardPoints() {
52 | customer.setRewardPoints(100);
53 | assertEquals(100, customer.getRewardPoints());
54 | }
55 | @Test
56 | public void testCustomerWithDefaultRewardPoints() {
57 | assertEquals(0, customer.getRewardPoints());
58 | }
59 | @Test
60 | public void testCustomerWithNegativeRewardPoints() {
61 | assertThrows(IllegalArgumentException.class, () -> customer.setRewardPoints(-10));
62 | }
63 | @Test
64 | public void testCustomerWithPurchaseGoods() {
65 | customer.setRewardPoints(100);
66 | customer.purchaseGoods(200);
67 | assertEquals(120,customer.getRewardPoints());
68 | }
69 | @Test
70 | public void testCustomerWithPurchaseGoods_CeilRewardPoints() {
71 | customer.setRewardPoints(100);
72 | customer.purchaseGoods(244);
73 | assertEquals(125,customer.getRewardPoints());
74 | }
75 | @Test
76 | public void testCustomerWithPurchaseGoods_invalidAmount() {
77 | customer.setRewardPoints(100);
78 | assertThrows(IllegalArgumentException.class, () -> customer.purchaseGoods(-244));
79 | }
80 | @Test
81 | public void testCustomerRedeemPoints() {
82 | customer.setRewardPoints(200);
83 | customer.redeemPoints(50);
84 | assertEquals(150, customer.getRewardPoints());
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/step-by-step-tdd/CustomerTests11.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $
3 | *
4 | * Copyright (C) 2019 Cefalo AS.
5 | * All Rights Reserved. No use, copying or distribution of this
6 | * work may be made except in accordance with a valid license
7 | * agreement from Cefalo AS. This notice must be included on all
8 | * copies, modifications and derivatives of this work.
9 | */
10 | package com.cefalo.tdd;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import static org.junit.jupiter.api.Assertions.assertEquals;
16 | import static org.junit.jupiter.api.Assertions.assertThrows;
17 |
18 | /**
19 | * @author Ferdous Mahmud Shaon
20 | * @author last modified by $
21 | * @version $ $
22 | */
23 | /*
24 | * Customer has following properties:
25 | * Name: name of the customer, String, Mandatory
26 | *
27 | * RewardPoints: the total reward points of a customer, default value: 0, integer
28 | * value can only be set >=0, otherwise throws IllegalArgumentException
29 | *
30 | * purchaseGoods(int amount): increase the total RewardPoints count based on the following formula:
31 | * total RewardPoints count += ceil(amount * 0.1)
32 | * the amount cannot be <0, otherwise throw IllegalArgumentException
33 | *
34 | * redeemPoints(int points): decrease the total RewardPoints count by the given number of points
35 | * if the given number of points < total RewardPoints count
36 | * otherwise throw IllegalArgumentException
37 | *
38 | * */
39 | public class CustomerTests11 {
40 | private Customer customer;
41 |
42 | @BeforeEach
43 | public void setUpCustomer() {
44 | customer = new Customer("Arnab");
45 | }
46 | @Test
47 | public void testCustomerWithNameOnly() {
48 | assertEquals("Arnab", customer.getName());
49 | }
50 | @Test
51 | public void testCustomerWithRewardPoints() {
52 | customer.setRewardPoints(100);
53 | assertEquals(100, customer.getRewardPoints());
54 | }
55 | @Test
56 | public void testCustomerWithDefaultRewardPoints() {
57 | assertEquals(0, customer.getRewardPoints());
58 | }
59 | @Test
60 | public void testCustomerWithNegativeRewardPoints() {
61 | assertThrows(IllegalArgumentException.class, () -> customer.setRewardPoints(-10));
62 | }
63 | @Test
64 | public void testCustomerWithPurchaseGoods() {
65 | customer.setRewardPoints(100);
66 | customer.purchaseGoods(200);
67 | assertEquals(120,customer.getRewardPoints());
68 | }
69 | @Test
70 | public void testCustomerWithPurchaseGoods_CeilRewardPoints() {
71 | customer.setRewardPoints(100);
72 | customer.purchaseGoods(244);
73 | assertEquals(125,customer.getRewardPoints());
74 | }
75 | @Test
76 | public void testCustomerWithPurchaseGoods_invalidAmount() {
77 | customer.setRewardPoints(100);
78 | assertThrows(IllegalArgumentException.class, () -> customer.purchaseGoods(-244));
79 | }
80 | @Test
81 | public void testCustomerRedeemPoints() {
82 | customer.setRewardPoints(200);
83 | customer.redeemPoints(50);
84 | assertEquals(150, customer.getRewardPoints());
85 | }
86 | @Test
87 | public void testCustomerInvalidRedeemPoints() {
88 | customer.setRewardPoints(200);
89 | assertThrows(IllegalArgumentException.class, () -> customer.redeemPoints(250));
90 | }
91 | }
92 |
--------------------------------------------------------------------------------