From 209b83feff381a3eadea02df747d5038104e45fa Mon Sep 17 00:00:00 2001 From: changhyeon Date: Fri, 28 Jul 2023 00:19:47 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=98=81=EC=86=8D=EC=84=B1=20=EC=BB=A8?= =?UTF-8?q?=ED=85=8D=EC=8A=A4=ED=8A=B8=20=EC=83=9D=EB=AA=85=EC=A3=BC?= =?UTF-8?q?=EA=B8=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootjpa/domain/Address.java | 9 +- .../springbootjpa/PersistenceContextTest.java | 124 ++++++++++++++++++ .../SpringbootJpaApplicationTests.java | 13 -- 3 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 src/test/java/com/programmers/springbootjpa/PersistenceContextTest.java delete mode 100644 src/test/java/com/programmers/springbootjpa/SpringbootJpaApplicationTests.java diff --git a/src/main/java/com/programmers/springbootjpa/domain/Address.java b/src/main/java/com/programmers/springbootjpa/domain/Address.java index 8a00c4540..47b087c2b 100644 --- a/src/main/java/com/programmers/springbootjpa/domain/Address.java +++ b/src/main/java/com/programmers/springbootjpa/domain/Address.java @@ -2,10 +2,13 @@ import jakarta.persistence.Column; import jakarta.persistence.Embeddable; +import lombok.AccessLevel; import lombok.Getter; +import lombok.NoArgsConstructor; @Embeddable @Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) public class Address { @Column(name = "street_address", nullable = false) @@ -15,5 +18,9 @@ public class Address { @Column(name = "zip_code", nullable = false) private Integer zipCode; - + public Address(String streetAddress, String detailedAddress, Integer zipCode) { + this.streetAddress = streetAddress; + this.detailedAddress = detailedAddress; + this.zipCode = zipCode; + } } diff --git a/src/test/java/com/programmers/springbootjpa/PersistenceContextTest.java b/src/test/java/com/programmers/springbootjpa/PersistenceContextTest.java new file mode 100644 index 000000000..a0de5a140 --- /dev/null +++ b/src/test/java/com/programmers/springbootjpa/PersistenceContextTest.java @@ -0,0 +1,124 @@ +package com.programmers.springbootjpa; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.programmers.springbootjpa.domain.Address; +import com.programmers.springbootjpa.domain.Customer; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.EntityTransaction; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class PersistenceContextTest { + + @Autowired + EntityManagerFactory entityManagerFactory; + + @Test + @DisplayName("고객을 영속 상태로 만들 수 있다.") + void persistCustomer() { + // given + EntityManager entityManager = entityManagerFactory.createEntityManager(); + EntityTransaction entityTransaction = entityManager.getTransaction(); + + entityTransaction.begin(); + Customer customer = Customer.builder() + .name("이현호") + .nickName("황창현") + .age(27) + .address(new Address("서울특별시 영등포구 도신로", "XX빌딩", 10000)) + .build(); + + // when + entityManager.persist(customer); + entityTransaction.commit(); + + Customer persistedCustomer = entityManager.find(Customer.class, 1L); + + // then + assertThat(persistedCustomer).usingRecursiveComparison().isEqualTo(customer); + } + + @Test + @DisplayName("고객을 준영속 상태로 만들 수 있다.") + void semiPersistCustomer() { + // given + EntityManager entityManager = entityManagerFactory.createEntityManager(); + EntityTransaction entityTransaction = entityManager.getTransaction(); + + entityTransaction.begin(); + Customer customer = Customer.builder() + .name("이현호") + .nickName("황창현") + .age(27) + .address(new Address("서울특별시 영등포구 도신로", "XX빌딩", 10000)) + .build(); + + // when + entityManager.persist(customer); + entityTransaction.commit(); + + entityManager.detach(customer); + boolean isSemiPersisted = entityManager.contains(customer); + + // then + assertThat(isSemiPersisted).isFalse(); + } + + @Test + @DisplayName("고객을 비영속 상태로 만들 수 있다.") + void nonPersistCustomer() { + // given + EntityManager entityManager = entityManagerFactory.createEntityManager(); + EntityTransaction entityTransaction = entityManager.getTransaction(); + + entityTransaction.begin(); + Customer customer = Customer.builder() + .name("이현호") + .nickName("황창현") + .age(27) + .address(new Address("서울특별시 영등포구 도신로", "XX빌딩", 10000)) + .build(); + + // when + entityTransaction.commit(); + + boolean isNonPersisted = entityManager.contains(customer); + + // then + assertThat(isNonPersisted).isFalse(); + } + + @Test + @DisplayName("고객을 영속성 컨텍스트에서 삭제할 수 있다.") + void removeCustomerFromPersistenceContext() { + // given + EntityManager entityManager = entityManagerFactory.createEntityManager(); + EntityTransaction entityTransaction = entityManager.getTransaction(); + + entityTransaction.begin(); + Customer customer = Customer.builder() + .name("이현호") + .nickName("황창현") + .age(27) + .address(new Address("서울특별시 영등포구 도신로", "XX빌딩", 10000)) + .build(); + + // when + entityManager.persist(customer); + entityTransaction.commit(); + entityManager.remove(customer); + + boolean isRemovedFromPersistenceContext = entityManager.contains(customer); + Customer actualCustomer = entityManager.find(Customer.class, 1L); + + // then + assertThat(isRemovedFromPersistenceContext).isFalse(); + assertThat(actualCustomer).isNull(); + } + +} diff --git a/src/test/java/com/programmers/springbootjpa/SpringbootJpaApplicationTests.java b/src/test/java/com/programmers/springbootjpa/SpringbootJpaApplicationTests.java deleted file mode 100644 index b530064fb..000000000 --- a/src/test/java/com/programmers/springbootjpa/SpringbootJpaApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.programmers.springbootjpa; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class SpringbootJpaApplicationTests { - - @Test - void contextLoads() { - } - -}