From 83a22bc65e99853724ad643b3c813deebf795904 Mon Sep 17 00:00:00 2001 From: parkyounghwan Date: Tue, 8 Jun 2021 02:37:07 +0900 Subject: [PATCH] =?UTF-8?q?[#11]=20'User'=20=ED=95=84=EB=93=9C=20=EC=9C=A0?= =?UTF-8?q?=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flabedu/blackpostoffice/dao/UserDao.kt | 7 ++- .../flabedu/blackpostoffice/domain/User.kt | 45 ++++++++++++++----- .../blackpostoffice/UserFieldCheckTest.kt | 27 ----------- .../domain/UserValidationCheckTest.kt | 43 ++++++++++++++++++ 4 files changed, 83 insertions(+), 39 deletions(-) delete mode 100644 src/test/kotlin/com/flabedu/blackpostoffice/UserFieldCheckTest.kt create mode 100644 src/test/kotlin/com/flabedu/blackpostoffice/domain/UserValidationCheckTest.kt diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt b/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt index 2e74fbf..cdd10ee 100644 --- a/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt +++ b/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt @@ -6,6 +6,7 @@ import org.springframework.stereotype.Repository import java.sql.Connection import java.sql.PreparedStatement import java.sql.ResultSet +import java.sql.SQLException import javax.sql.DataSource @Repository @@ -15,8 +16,9 @@ class UserDao { private lateinit var dataSource: DataSource private lateinit var user: User - @Throws fun add(user: User) { + throw Exception() + val connection: Connection = dataSource.connection val ps: PreparedStatement = connection.prepareStatement("INSERT INTO USERS(id, name, password) VALUES(?, ?, ?)") @@ -30,8 +32,9 @@ class UserDao { connection.close() } - @Throws fun get(id: Int): User { + throw Exception() + val connection: Connection = dataSource.connection val ps: PreparedStatement = connection.prepareStatement("SELECT * FROM USERS WHERE id = ?") ps.setInt(1, id) diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/domain/User.kt b/src/main/kotlin/com/flabedu/blackpostoffice/domain/User.kt index 497ef8d..6528594 100644 --- a/src/main/kotlin/com/flabedu/blackpostoffice/domain/User.kt +++ b/src/main/kotlin/com/flabedu/blackpostoffice/domain/User.kt @@ -2,37 +2,62 @@ package com.flabedu.blackpostoffice.domain import java.lang.IllegalArgumentException -class User () { +class User() { var id: Int = 0 + var name: String = "" set(value) { - if (value.length > 11) { - throw Exception("INVALIDNAMEVALUE") + if (value.length < 2 || value.length > 13) { + throw IllegalArgumentException("INVALID NAME VALUE") } else { - field = value + try { + field = value + } catch (e: Exception) { + // TODO: 회원 이름 등록 시 예상하지 못한 예외 처리 + throw Exception() + } finally { + field = "" + } } } + var email: String = "" set(value) { val regex = Regex("[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*\\.[a-zA-Z]{2,3}") + if (!value.matches(regex)) { - throw Exception("INVALID EMAIL VALUE") + throw IllegalArgumentException("INVALID EMAIL VALUE") } else { - field = value + try { + field = value + } catch (e: Exception) { + // TODO: 회원 이메일 등록 시 예상하지 못한 예외 처리 + throw Exception() + } finally { + field = "" + } } } + // 숫자, 문자, 특수문자 모두 포함 (8~15자) var password: String = "" set(value) { val regex = Regex("^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[\$@\$!%*#?&]).{8,15}.\$") - if(!value.matches(regex)) { - throw Exception("INVALID PASSWORD VALUE") + + if (!value.matches(regex)) { + throw IllegalArgumentException("INVALID PASSWORD VALUE") } else { - field = value + try { + field = value + } catch (e: Exception) { + throw Exception() + } finally { + field = "" + } } } - constructor(id: Int, name: String, email: String, password: String): this() { + constructor(id: Int, name: String, email: String, password: String) : this() { this.id = id this.name = name this.email = email diff --git a/src/test/kotlin/com/flabedu/blackpostoffice/UserFieldCheckTest.kt b/src/test/kotlin/com/flabedu/blackpostoffice/UserFieldCheckTest.kt deleted file mode 100644 index ebf7195..0000000 --- a/src/test/kotlin/com/flabedu/blackpostoffice/UserFieldCheckTest.kt +++ /dev/null @@ -1,27 +0,0 @@ -package com.flabedu.blackpostoffice - -import com.flabedu.blackpostoffice.domain.User -import org.junit.Rule -import org.junit.jupiter.api.Test -import org.junit.rules.ExpectedException -import org.junit.runner.RunWith -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner - -@RunWith(SpringJUnit4ClassRunner::class) -class UserFieldCheckTest { - - @Rule - val expectedException: ExpectedException = ExpectedException.none() - - @Test - fun `User Field 값 유효성 검증 테스트`() { -// expectedException.expect(Exception::class.java) -// expectedException.expectMessage("java.lang.Exception: INVALIDNAMEVALUE") - - val user = User() - - user.name = "123456712311" -// user.email = "parkyounghwan@gmailcom" -// user.password = "12parkyoung" - } -} \ No newline at end of file diff --git a/src/test/kotlin/com/flabedu/blackpostoffice/domain/UserValidationCheckTest.kt b/src/test/kotlin/com/flabedu/blackpostoffice/domain/UserValidationCheckTest.kt new file mode 100644 index 0000000..0d24416 --- /dev/null +++ b/src/test/kotlin/com/flabedu/blackpostoffice/domain/UserValidationCheckTest.kt @@ -0,0 +1,43 @@ +package com.flabedu.blackpostoffice.domain + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import org.junit.runner.RunWith +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner +import java.lang.IllegalArgumentException + +@RunWith(SpringJUnit4ClassRunner::class) +@SpringBootTest +class UserValidationCheckTest { + + @Test + fun `User 'name' field 유효성 검사`() { + val e: Exception = Assertions.assertThrows(IllegalArgumentException::class.java) { + val user = User() + user.name = "" + } + + Assertions.assertEquals("INVALID NAME VALUE", e.message) + } + + @Test + fun `User 'email' field 유효성 검사`() { + val e: Exception = Assertions.assertThrows(IllegalArgumentException::class.java) { + val user = User() + user.email = "" + } + + Assertions.assertEquals("INVALID EMAIL VALUE", e.message) + } + + @Test + fun `User 'password' field 유효성 검사`() { + val e: Exception = Assertions.assertThrows(IllegalArgumentException::class.java) { + val user = User() + user.password = "" + } + + Assertions.assertEquals("INVALID PASSWORD VALUE", e.message) + } +} \ No newline at end of file