Skip to content
This repository has been archived by the owner on Aug 14, 2022. It is now read-only.

Commit

Permalink
[#11] 'User' 필드 유효성 검사 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
parkyounghwan committed Jun 7, 2021
1 parent 96a21b3 commit 83a22bc
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 39 deletions.
7 changes: 5 additions & 2 deletions src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(?, ?, ?)")

Expand All @@ -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)
Expand Down
45 changes: 35 additions & 10 deletions src/main/kotlin/com/flabedu/blackpostoffice/domain/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 0 additions & 27 deletions src/test/kotlin/com/flabedu/blackpostoffice/UserFieldCheckTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 83a22bc

Please sign in to comment.