패스트캠퍼스
패스트캠퍼스 환급챌린지 9일차 : 9개 도메인 프로젝트로 끝내는 백엔드 웹 개발 (Java/Spring) 초격차 패키지 Online 강의 후기
이태우(1990년)
2025. 3. 13. 22:54
본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성하였습니다.
공부 시작 시각 인증
수강 인증 사진
어제 실습했던 것을 이어서 계속한다.
PositiveIntegerCounter.java
package org.fastcampus.common.domain;
public class PositiveIntegerCounter {
private int count;
public PositiveIntegerCounter() {
this.count = 0;
}
public void increase() {
this.count++;
}
public void decrease() {
if (count <= 0) {
return;
}
this.count--;
}
public int getCount() {
return count;
}
}
PositiveIntegerCounterTest.java
package org.fastcampus.common.domain;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PositiveIntegerCounterTest {
@Test
void givenCreated_whenIncrease_thenCountIsOne() {
// given
PositiveIntegerCounter counter = new PositiveIntegerCounter();
// when
counter.increase();
// then
assertEquals(1, counter.getCount());
}
@Test
void givenCreatedAndIncreased_whenDecrease_thenCountIsZero() {
// given
PositiveIntegerCounter counter = new PositiveIntegerCounter();
counter.increase();
// when
counter.decrease();
// then
assertEquals(0, counter.getCount());
}
@Test
void givenCreated_whenDecrease_thenCountIsZero() {
// given
PositiveIntegerCounter counter = new PositiveIntegerCounter();
// when
counter.decrease();
// then
assertEquals(0, counter.getCount());
}
}
UserInfoTest.java
package org.fastcampus.user.domain;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class UserInfoTest {
@Test
void givenNameAndProfileImage_whenCreate_thenThrowNothing() {
// given
String name = "abcd";
String profileImageUrl = "";
// when
// then
assertDoesNotThrow(() -> new UserInfo(name, profileImageUrl));
}
@Test
void givenBlandNameAndProfileImage_whenCreate_thenThrowError() {
// given
String name = null;
String profileImageUrl = "";
// when
// then
assertThrows(IllegalArgumentException.class, () -> new UserInfo(name, profileImageUrl));
}
}
User.java
package org.fastcampus.user.domain;
import java.util.Objects;
import org.fastcampus.common.domain.*;
public class User {
private final Long id;
private final UserInfo info;
private final PositiveIntegerCounter followingCount;
private final PositiveIntegerCounter followerCount;
public User(Long id, UserInfo userInfo) {
if (userInfo == null) {
throw new IllegalArgumentException();
}
this.id = id;
this.info = userInfo;
this.followingCount = new PositiveIntegerCounter();
this.followerCount = new PositiveIntegerCounter();
}
public void follow(User targetUser) {
if (targetUser.equals(this)) {
throw new IllegalArgumentException();
}
followingCount.increase();
targetUser.increaseFollowerCount();
}
public void unfollow(User targetUser) {
if (targetUser.equals(this)) {
throw new IllegalArgumentException();
}
followingCount.decrease();
targetUser.decreaseFollowerCount();
}
private void increaseFollowerCount() {
followerCount.increase();
}
private void decreaseFollowerCount() {
followerCount.decrease();
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return Objects.equals(id, user.id);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
public Long getId() {
return id;
}
public int followerCount() {
return followerCount.getCount();
}
public int followingCount() {
return followingCount.getCount();
}
}
UserTest.java
package org.fastcampus.user.domain;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class UserTest {
private final UserInfo userInfo = new UserInfo("test", "");
private User user1;
private User user2;
@BeforeEach
void init() {
user1 = new User(1L, userInfo);
user2 = new User(2L, userInfo);
}
@Test
void givenTwoUser_whenEqual_thenReturnFalse() {
// given
// when
boolean isSame = user1.equals(user2);
// then
assertEquals(false, isSame);
}
@Test
void givenTwoSameUser_whenEqual_thenReturnTrue() {
// given
User sameuser = new User(1L, userInfo);
// when
boolean isSame = user1.equals(sameuser);
// then
assertTrue(isSame);
}
@Test
void givenTwoSameUser_whenHashCode_thenNotEqual() {
// given
// when
int hashCode1 = user1.hashCode();
int hashCode2 = user2.hashCode();
// then
assertNotEquals(hashCode1, hashCode2);
}
@Test
void givenTwoUser_whenUser1FollowUser2_thenIncreaseUserCount() {
// given
// when
user1.follow(user2);
// then
assertEquals(1, user1.followingCount());
assertEquals(0, user1.followerCount());
assertEquals(0, user2.followingCount());
assertEquals(1, user2.followerCount());
}
@Test
void givenUser1FollowUser2_whenUser1UnFollowUser2_thenDecreaseUserCount() {
// given
user1.follow(user2);
// when
user1.unfollow(user2);
// then
assertEquals(0, user1.followingCount());
assertEquals(0, user1.followerCount());
assertEquals(0, user2.followingCount());
assertEquals(0, user2.followerCount());
}
@Test
void givenTwoUser_whenUser1UnFollowUser2_thenNotDecreaseUserCount() {
// given
// when
user1.unfollow(user2);
// then
assertEquals(0, user1.followingCount());
assertEquals(0, user1.followerCount());
assertEquals(0, user2.followingCount());
assertEquals(0, user2.followerCount());
}
}