diff --git a/src/main/resources/templates/thymeleaf/sample/egovSampleList.html b/src/main/resources/templates/thymeleaf/sample/egovSampleList.html index 26441cb..613a29a 100644 --- a/src/main/resources/templates/thymeleaf/sample/egovSampleList.html +++ b/src/main/resources/templates/thymeleaf/sample/egovSampleList.html @@ -95,7 +95,7 @@

- + diff --git a/src/test/java/egovframework/example/sample/web/EgovSampleControllerTestSelectListRowNumberTest.java b/src/test/java/egovframework/example/sample/web/EgovSampleControllerTestSelectListRowNumberTest.java new file mode 100644 index 0000000..87bc6b7 --- /dev/null +++ b/src/test/java/egovframework/example/sample/web/EgovSampleControllerTestSelectListRowNumberTest.java @@ -0,0 +1,93 @@ +package egovframework.example.sample.web; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.egovframe.rte.fdl.property.impl.EgovPropertyServiceImpl; +import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; + +/** + * [게시판][EgovSampleController.selectList] 목록 행 번호 단위 테스트 + * + * 페이지당 건수(pageUnit)와 페이지 링크 묶음 크기(pageSize)를 다른 값으로 두고 + * 행 번호가 페이지당 건수를 따라가는지 확인한다. + * + * @author wantaek + * @since 2026-09-09 + */ +@SpringBootTest +@AutoConfigureMockMvc +class EgovSampleControllerTestSelectListRowNumberTest { + + private static final int PAGE_INDEX = 2; + + /** 목록 No 칸. 이름·설명 칸에는 숫자만 들어가는 값이 없어 이 칸만 잡힌다. */ + private static final Pattern ROW_NUMBER = Pattern.compile("(\\d+)"); + + @Autowired + private MockMvc mockMvc; + + /** + * 두 값이 같으면 행 번호 식이 어느 쪽을 쓰든 결과가 같아 검증이 되지 않으므로 + * pageUnit 과 pageSize 를 다르게 준다. + */ + @TestConfiguration + static class EgovConfigPropertiesTestConfig { + + @Bean(destroyMethod = "destroy") + EgovPropertyServiceImpl propertiesService() { + final Map properties = new HashMap<>(); + properties.put("pageUnit", "10"); + properties.put("pageSize", "5"); + + final EgovPropertyServiceImpl egovPropertyServiceImpl = new EgovPropertyServiceImpl(); + egovPropertyServiceImpl.setProperties(properties); + return egovPropertyServiceImpl; + } + + } + + @Test + @DisplayName("행 번호는 페이지 링크 묶음 크기가 아니라 페이지당 건수만큼 페이지마다 건너뛴다") + void test_행번호_페이지당건수() throws Exception { + // given + final MvcResult mvcResult = mockMvc + .perform(get("/egovSampleList.do").param("pageIndex", String.valueOf(PAGE_INDEX))) + .andExpect(status().isOk()) + .andReturn(); + + final PaginationInfo paginationInfo = (PaginationInfo) mvcResult.getModelAndView().getModel() + .get("paginationInfo"); + + assertThat(paginationInfo.getRecordCountPerPage()).isNotEqualTo(paginationInfo.getPageSize()); + assertThat(paginationInfo.getTotalRecordCount()).isGreaterThan(paginationInfo.getRecordCountPerPage()); + + // when + final String content = mvcResult.getResponse().getContentAsString(); + + // then + // 목록은 내림차순이므로 2페이지 첫 행 = 총건수 + 1 - ((2 - 1) * 페이지당건수 + 1) + final int expected = paginationInfo.getTotalRecordCount() + 1 + - ((PAGE_INDEX - 1) * paginationInfo.getRecordCountPerPage() + 1); + + final Matcher matcher = ROW_NUMBER.matcher(content); + assertThat(matcher.find()).isTrue(); + assertThat(Integer.parseInt(matcher.group(1))).isEqualTo(expected); + } + +}