From 3f5edaffa317ba88b000ec36d841e85da6f918cc Mon Sep 17 00:00:00 2001 From: wantaek Date: Wed, 9 Sep 2026 14:54:36 +0900 Subject: [PATCH] =?UTF-8?q?fix(sample):=20=EB=93=B1=EB=A1=9D/=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=ED=8F=BC=EC=9D=98=20maxlength=EB=A5=BC=20SAMPLE=20?= =?UTF-8?q?=EC=BB=AC=EB=9F=BC=20=ED=8F=AD=EC=97=90=20=EB=A7=9E=EC=B6=98?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 카테고리명은 maxlength=60인데 SAMPLE.NAME은 VARCHAR(50)이고, 등록자는 maxlength=60인데 SAMPLE.REG_USER는 VARCHAR(10)이다. 같은 폼의 설명만 DESCRIPTION VARCHAR(200)에 맞춰 maxlength=200과 '/200' 카운터를 쓴다. 한도를 넘겨 입력하면 검증 메시지 없이 INSERT까지 내려가 잘림 예외가 나고 sample/egovSampleError(500)로 떨어진다. 등록자 11자는 'size limit: 10 table: SAMPLE column: REG_USER', 카테고리명 51자는 'size limit: 50 table: SAMPLE column: NAME'이다. 컬럼 폭은 src/main/resources/db/sampledb.sql과 script/ddl 8종이 NAME 50 / DESCRIPTION 200 / REG_USER 10으로 일치한다. 등록·수정 두 화면이 렌더한 maxlength를 컬럼 폭과 대조하는 테스트를 추가한다. --- .../thymeleaf/sample/egovSampleRegister.html | 6 +- ...vSampleControllerTestRegisterViewTest.java | 105 ++++++++++++++++++ 2 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 src/test/java/egovframework/example/sample/web/EgovSampleControllerTestRegisterViewTest.java diff --git a/src/main/resources/templates/thymeleaf/sample/egovSampleRegister.html b/src/main/resources/templates/thymeleaf/sample/egovSampleRegister.html index e68c076..5f87e2a 100644 --- a/src/main/resources/templates/thymeleaf/sample/egovSampleRegister.html +++ b/src/main/resources/templates/thymeleaf/sample/egovSampleRegister.html @@ -97,7 +97,7 @@

- +
@@ -133,12 +133,12 @@

- -
diff --git a/src/test/java/egovframework/example/sample/web/EgovSampleControllerTestRegisterViewTest.java b/src/test/java/egovframework/example/sample/web/EgovSampleControllerTestRegisterViewTest.java new file mode 100644 index 0000000..4565a3c --- /dev/null +++ b/src/test/java/egovframework/example/sample/web/EgovSampleControllerTestRegisterViewTest.java @@ -0,0 +1,105 @@ +package egovframework.example.sample.web; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +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.test.web.servlet.MockMvc; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +/** + * [게시판][EgovSampleController.addSampleView, updateSampleView] Controller 단위 테스트 + * + * 등록/수정 화면의 maxlength 가 SAMPLE 테이블 컬럼 폭(NAME 50, DESCRIPTION 200, REG_USER 10)을 + * 넘지 않는지 확인한다. 넘으면 입력값이 INSERT/UPDATE 단계에서 잘림 예외로 떨어진다. + * + * @author wantaek + * @since 2026-09-09 + * + */ + +@SpringBootTest +@AutoConfigureMockMvc + +@RequiredArgsConstructor +@Slf4j +class EgovSampleControllerTestRegisterViewTest { + + /** SAMPLE.NAME 컬럼 폭 */ + private static final int NAME_COLUMN_SIZE = 50; + + /** SAMPLE.DESCRIPTION 컬럼 폭 */ + private static final int DESCRIPTION_COLUMN_SIZE = 200; + + /** SAMPLE.REG_USER 컬럼 폭 */ + private static final int REG_USER_COLUMN_SIZE = 10; + + @Autowired + private MockMvc mockMvc; + + @Test + void testAddSampleView() throws Exception { + // given, when + final String html = mockMvc.perform( + + post("/addSampleView.do") + + ).andExpect(status().isOk()) + + .andExpect(view().name("sample/egovSampleRegister")) + + .andReturn().getResponse().getContentAsString(); + + if (log.isDebugEnabled()) { + log.debug("html={}", html); + } + + // then + assertEquals(NAME_COLUMN_SIZE, maxlength(html, "name"), "등록 화면 카테고리명 maxlength"); + assertEquals(DESCRIPTION_COLUMN_SIZE, maxlength(html, "description"), "등록 화면 설명 maxlength"); + assertEquals(REG_USER_COLUMN_SIZE, maxlength(html, "regUser"), "등록 화면 등록자 maxlength"); + } + + @Test + void testUpdateSampleView() throws Exception { + // given, when + final String html = mockMvc.perform( + + post("/updateSampleView.do") + + .param("id", "SAMPLE-00001") + + ).andExpect(status().isOk()) + + .andExpect(view().name("sample/egovSampleRegister")) + + .andReturn().getResponse().getContentAsString(); + + if (log.isDebugEnabled()) { + log.debug("html={}", html); + } + + // then + assertEquals(NAME_COLUMN_SIZE, maxlength(html, "name"), "수정 화면 카테고리명 maxlength"); + assertEquals(DESCRIPTION_COLUMN_SIZE, maxlength(html, "description"), "수정 화면 설명 maxlength"); + assertEquals(REG_USER_COLUMN_SIZE, maxlength(html, "regUser"), "수정 화면 등록자 maxlength"); + } + + private int maxlength(final String html, final String id) { + final Matcher matcher = Pattern.compile("id=\"" + id + "\"[^>]*maxlength=\"(\\d+)\"").matcher(html); + assertTrue(matcher.find(), id + " 입력 항목의 maxlength"); + return Integer.parseInt(matcher.group(1)); + } + +}