Jenkins
3 years ago
6 changed files with 376 additions and 0 deletions
-
59projjpn/src/main/java/de/hs_fulda/ciip/projjpn/Birthdate.java
-
31projjpn/src/main/java/de/hs_fulda/ciip/projjpn/Customers.java
-
41projjpn/src/main/java/de/hs_fulda/ciip/projjpn/User.java
-
63projjpn/src/test/java/de/hs_fulda/ciip/projjpn/BirthdateTest.java
-
162projjpn/src/test/java/de/hs_fulda/ciip/projjpn/CustomersTest.java
-
20projjpn/src/test/java/de/hs_fulda/ciip/projjpn/UserTest.java
@ -0,0 +1,59 @@ |
|||||
|
package de.hs_fulda.ciip.projjpn; |
||||
|
|
||||
|
public class Birthdate { |
||||
|
private int day; |
||||
|
private int month; |
||||
|
private int year; |
||||
|
|
||||
|
public Birthdate(int d, int m, int y) { |
||||
|
day = d; |
||||
|
month = m; |
||||
|
year = y; |
||||
|
} |
||||
|
|
||||
|
public int getDay() { |
||||
|
return day; |
||||
|
} |
||||
|
|
||||
|
public int getMonth() { |
||||
|
return month; |
||||
|
} |
||||
|
|
||||
|
public int getYear() { |
||||
|
return year; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @return Date Format DD.MM.YYYY |
||||
|
*/ |
||||
|
public String toString() { |
||||
|
return day + "." + month + "." + year; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* |
||||
|
* @param d Day |
||||
|
* @param m Month |
||||
|
* @param y Year |
||||
|
*/ |
||||
|
public void changeBirthdate(int d, int m, int y) { |
||||
|
day = d; |
||||
|
month = m; |
||||
|
year = y; |
||||
|
} |
||||
|
|
||||
|
public boolean isValid(int DD, int MM, int YYYY) { |
||||
|
if (DD < 1 || DD > 31) { |
||||
|
return false; |
||||
|
} |
||||
|
if (MM < 1 || MM > 12) { |
||||
|
return false; |
||||
|
} |
||||
|
if (YYYY < 1990 || YYYY > 2022) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
package de.hs_fulda.ciip.projjpn; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
|
||||
|
public class Customers { |
||||
|
HashMap<String, User> pool = new HashMap<String, User>(); |
||||
|
|
||||
|
public boolean nickNameAvailable(String nickname) { |
||||
|
User u = pool.get(nickname); |
||||
|
if (null == u) { |
||||
|
return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
public User registerUser(User user) { |
||||
|
return pool.putIfAbsent(user.nickName, user); |
||||
|
} |
||||
|
|
||||
|
public User deleteUser(String userNickname) { |
||||
|
return pool.remove(userNickname); |
||||
|
} |
||||
|
|
||||
|
public User getByNickname(String nickname) { |
||||
|
return pool.get(nickname); |
||||
|
} |
||||
|
|
||||
|
public int getCountOfUsers() { |
||||
|
return pool.size(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,41 @@ |
|||||
|
package de.hs_fulda.ciip.projjpn; |
||||
|
|
||||
|
public class User { |
||||
|
String firstName; |
||||
|
String lastName; |
||||
|
String nickName; |
||||
|
String eMail; |
||||
|
Birthdate birthdate; |
||||
|
|
||||
|
public User(String firstName, |
||||
|
String lastName, |
||||
|
String nickName, |
||||
|
String eMail, |
||||
|
Birthdate birthdate) { |
||||
|
this.firstName = firstName; |
||||
|
this.lastName = lastName; |
||||
|
this.nickName = nickName; |
||||
|
this.eMail = eMail; |
||||
|
this.birthdate = birthdate; |
||||
|
} |
||||
|
|
||||
|
public User(String nickName) { |
||||
|
this.nickName = nickName; |
||||
|
} |
||||
|
|
||||
|
public String getFirstName() { |
||||
|
return firstName; |
||||
|
} |
||||
|
public String getLastName() { |
||||
|
return lastName; |
||||
|
} |
||||
|
public String getNickName() { |
||||
|
return nickName; |
||||
|
} |
||||
|
public String getEMail() { |
||||
|
return eMail; |
||||
|
} |
||||
|
public Birthdate getBirthdate() { |
||||
|
return birthdate; |
||||
|
} |
||||
|
} |
@ -0,0 +1,63 @@ |
|||||
|
package de.hs_fulda.ciip.projjpn; |
||||
|
|
||||
|
import junit.framework.TestCase; |
||||
|
|
||||
|
public class BirthdateTest extends TestCase { |
||||
|
|
||||
|
public void testToString() { |
||||
|
|
||||
|
// Given |
||||
|
Birthdate b = new Birthdate(1, 1, 2000); |
||||
|
|
||||
|
// When |
||||
|
String expectedDate = "1.1.2000"; |
||||
|
|
||||
|
// Then |
||||
|
assertEquals(expectedDate, b.toString()); |
||||
|
} |
||||
|
|
||||
|
public void test_changeBirthdate() { |
||||
|
// Given |
||||
|
Birthdate b = new Birthdate(1, 1, 2000); |
||||
|
|
||||
|
// Change Birthdate |
||||
|
b.changeBirthdate(2, 3, 2001); |
||||
|
|
||||
|
// When |
||||
|
int expectedDay = 2; |
||||
|
int expectedMonth = 3; |
||||
|
int expectedYear = 2001; |
||||
|
|
||||
|
// Then |
||||
|
assertEquals(2, b.getDay()); |
||||
|
assertEquals(3, b.getMonth()); |
||||
|
assertEquals(2001, b.getYear()); |
||||
|
} |
||||
|
|
||||
|
public void test_rejectInvalidBirthday() { |
||||
|
Birthdate birthdate = new Birthdate(0, 0, 0); |
||||
|
boolean expectedResult = false; |
||||
|
boolean gotResult = birthdate.isValid(0, 0, 0); |
||||
|
assertEquals(expectedResult, gotResult); |
||||
|
|
||||
|
gotResult = birthdate.isValid(32, 13, 1990); |
||||
|
assertEquals(expectedResult, gotResult); |
||||
|
|
||||
|
gotResult = birthdate.isValid(31, 0, 1980); |
||||
|
assertEquals(expectedResult, gotResult); |
||||
|
|
||||
|
gotResult = birthdate.isValid(31, 13, 1980); |
||||
|
assertEquals(expectedResult, gotResult); |
||||
|
|
||||
|
gotResult = birthdate.isValid(1, 1, 3000); |
||||
|
assertEquals(expectedResult, gotResult); |
||||
|
|
||||
|
gotResult = birthdate.isValid(1, 1, 1900); |
||||
|
assertEquals(expectedResult, gotResult); |
||||
|
|
||||
|
expectedResult = true; |
||||
|
gotResult = birthdate.isValid(1, 5, 2020); |
||||
|
assertEquals(expectedResult, gotResult); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,162 @@ |
|||||
|
package de.hs_fulda.ciip.projjpn; |
||||
|
|
||||
|
import junit.framework.TestCase; |
||||
|
|
||||
|
public class CustomersTest extends TestCase { |
||||
|
|
||||
|
public void test_nicknameIsFree() { |
||||
|
// Given |
||||
|
Customers customers = new Customers(); |
||||
|
String availableNickname = "beastMaster64"; |
||||
|
|
||||
|
// When |
||||
|
boolean nicknameIsAvailable = customers.nickNameAvailable(availableNickname); |
||||
|
|
||||
|
// Then |
||||
|
assertTrue(nicknameIsAvailable); |
||||
|
} |
||||
|
|
||||
|
public void test_nicknameIsTaken() { |
||||
|
// Given |
||||
|
Customers customers = new Customers(); |
||||
|
String availableNickname = "beastMaster64"; |
||||
|
String takenNickname = "beastMaster64"; |
||||
|
User userToRegister = new User(availableNickname); |
||||
|
|
||||
|
customers.registerUser(userToRegister); |
||||
|
|
||||
|
// When |
||||
|
boolean nicknameIsTaken = customers.nickNameAvailable(availableNickname); |
||||
|
|
||||
|
// Then |
||||
|
assertFalse(nicknameIsTaken); |
||||
|
} |
||||
|
|
||||
|
public void test_registerSingleUser() { |
||||
|
// Given |
||||
|
Customers customers = new Customers(); |
||||
|
String expectedNickNameInput = "Mougli"; |
||||
|
User expectedNewUser = new User(expectedNickNameInput); |
||||
|
|
||||
|
// Add a user |
||||
|
customers.registerUser(expectedNewUser); |
||||
|
|
||||
|
// Get the expected User |
||||
|
User expectedUser = customers.getByNickname(expectedNickNameInput); |
||||
|
// |
||||
|
assertNotNull(expectedNewUser); |
||||
|
String expectedNickNameGotBack = expectedUser.nickName; |
||||
|
|
||||
|
// When |
||||
|
boolean userRegistered = expectedNickNameGotBack.equals(expectedNickNameInput); |
||||
|
|
||||
|
// Then |
||||
|
assertTrue(userRegistered); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Register multiple Users and then search for them in the same order. |
||||
|
*/ |
||||
|
public void test_registerMultipleUsers() { |
||||
|
// Given |
||||
|
Customers customers = new Customers(); |
||||
|
String[] expectedNickNamesInput = {"Mougli", "Tarkan", "beastMaster64", "BlaBlaPew", "MuchDoge_321"}; |
||||
|
User[] expectedNewUsers = new User[expectedNickNamesInput.length]; |
||||
|
|
||||
|
for (int i = 0; i < expectedNickNamesInput.length; i++) { |
||||
|
// Create user |
||||
|
expectedNewUsers[i] = new User(expectedNickNamesInput[i]); |
||||
|
|
||||
|
// Add user |
||||
|
customers.registerUser(expectedNewUsers[i]); |
||||
|
} |
||||
|
|
||||
|
// Get the expected Users |
||||
|
User foundUser; |
||||
|
String expectedNickNameGotBack; |
||||
|
boolean userRegistered; |
||||
|
|
||||
|
for (int i = 0; i < expectedNickNamesInput.length; i++) { |
||||
|
// Get user |
||||
|
foundUser = customers.getByNickname(expectedNickNamesInput[i]); |
||||
|
assertNotNull(foundUser); |
||||
|
|
||||
|
// When |
||||
|
expectedNickNameGotBack = foundUser.nickName; |
||||
|
userRegistered = expectedNickNameGotBack.equals(expectedNickNamesInput[i]); |
||||
|
|
||||
|
// Then |
||||
|
assertTrue(userRegistered); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void test_removeRegisteredUser() { |
||||
|
// Given |
||||
|
Customers customers = new Customers(); |
||||
|
String userToRemove = "beastMaster64"; |
||||
|
User userToRegister = new User("beastMaster64"); |
||||
|
customers.registerUser(userToRegister); |
||||
|
|
||||
|
// When |
||||
|
boolean userExists = customers.nickNameAvailable(userToRemove); |
||||
|
assertFalse(userExists); |
||||
|
|
||||
|
// Then |
||||
|
User removedUser = customers.deleteUser(userToRemove); |
||||
|
assertEquals(userToRemove, removedUser.nickName); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public void test_registerAndCountUsers() { |
||||
|
// Given |
||||
|
Customers customers = new Customers(); |
||||
|
int expectedRegisteredUsers = 20; |
||||
|
int actualRegisteredUsers = -1; |
||||
|
String nickname; |
||||
|
|
||||
|
// Prepare |
||||
|
for(int i = 0, j = 1; i < expectedRegisteredUsers; i++, j++) { |
||||
|
nickname = "beastMaster_" + j; |
||||
|
customers.registerUser(new User(nickname)); |
||||
|
} |
||||
|
actualRegisteredUsers = customers.getCountOfUsers(); |
||||
|
assertEquals(expectedRegisteredUsers, actualRegisteredUsers); |
||||
|
} |
||||
|
|
||||
|
public void test_createRegisterAndCheckUserData() { |
||||
|
// Given |
||||
|
Customers customers = new Customers(); |
||||
|
|
||||
|
String firstName = "Mia"; |
||||
|
String lastName = "Muster"; |
||||
|
String nickName = "harley"; |
||||
|
String eMail = "mia@muster.de"; |
||||
|
Birthdate birthdate = new Birthdate(30, 12, 1997); |
||||
|
|
||||
|
User userToCheck = new User(firstName, lastName, nickName, eMail, birthdate); |
||||
|
|
||||
|
// Register User |
||||
|
customers.registerUser(userToCheck); |
||||
|
|
||||
|
User gotUser = customers.getByNickname(nickName); |
||||
|
|
||||
|
assertNotNull(gotUser); |
||||
|
|
||||
|
// When |
||||
|
boolean correctFirstName = "Mia".equals(gotUser.getFirstName()); |
||||
|
boolean correctLastName = "Muster".equals(gotUser.getLastName()); |
||||
|
boolean correctNickName = "harley".equals(gotUser.getNickName()); |
||||
|
boolean correctEMail = "mia@muster.de".equals(gotUser.getEMail()); |
||||
|
boolean correctBirthdate = birthdate.toString().equals(gotUser.birthdate.toString()); |
||||
|
|
||||
|
// Then |
||||
|
assertTrue(correctFirstName); |
||||
|
assertTrue(correctLastName); |
||||
|
assertTrue(correctNickName); |
||||
|
assertTrue(correctEMail); |
||||
|
assertTrue(correctBirthdate); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
package de.hs_fulda.ciip.projjpn; |
||||
|
|
||||
|
import junit.framework.TestCase; |
||||
|
|
||||
|
public class UserTest extends TestCase { |
||||
|
public void test_initAndGetBirthdayOfUser() { |
||||
|
// Given |
||||
|
Customers customers = new Customers(); |
||||
|
String firstName = "Mia"; |
||||
|
String lastName = "Muster"; |
||||
|
String nickName = "harley"; |
||||
|
String eMail = "mia@muster.de"; |
||||
|
int DD = 30, MM = 12, YYYY = 1997; |
||||
|
String expectedBirthdate = new Birthdate(DD, MM, YYYY).toString(); |
||||
|
|
||||
|
User userToCheck = new User(firstName, lastName, nickName, eMail, new Birthdate(DD, MM, YYYY)); |
||||
|
String gotBirthdate = userToCheck.getBirthdate().toString(); |
||||
|
boolean gotExpectedBirthdayBack = gotBirthdate.equals(expectedBirthdate); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue