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

Commit

Permalink
[#11] 회원 정보 저장 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
parkyounghwan committed Jun 15, 2021
1 parent 8b6bfd8 commit 1aadf01
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 187 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ out/

### VS Code ###
.vscode/

src/main/resources/parkyounghwan/application-password.properties
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ repositories {

dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.boot:spring-boot-starter-jdbc")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import org.springframework.core.env.Environment
import javax.sql.DataSource

@Configuration
@PropertySource("classpath:parkyounghwan/application-parkyounghwan.yml")
@PropertySource(
"classpath:parkyounghwan/application-parkyounghwan.properties",
"classpath:parkyounghwan/application-password.properties"
)
class CustomDataSourceConfig {

val env: Environment
Expand All @@ -19,12 +22,12 @@ class CustomDataSourceConfig {
this.env = env
}

@Bean("dataSource")
@Bean
fun customDataSource(): DataSource =
DataSourceBuilder.create()
.driverClassName(this.env.getProperty("spring.datasource.driver-class-name"))
.url(this.env.getProperty("spring.datasource.url"))
.username(this.env.getProperty("spring.datasource.username"))
.password(this.env.getProperty("spring.datasource.password"))
.driverClassName(env.getProperty("spring.datasource.driver-class-name"))
.url(env.getProperty("spring.datasource.url"))
.username(env.getProperty("spring.datasource.username"))
.password(env.getProperty("spring.datasource.password"))
.build()
}
Original file line number Diff line number Diff line change
@@ -1,70 +1,8 @@
package com.flabedu.blackpostoffice.parkyounghwan.domain

import java.lang.IllegalArgumentException

data class User (val id: Long, val name: String, val email: String, val password: String)

/*
class User() {
var id: Int = 0
var name: String = ""
set(value) {
if (value.length < 2 || value.length > 13) {
throw IllegalArgumentException("INVALID NAME VALUE")
} else {
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 IllegalArgumentException("INVALID EMAIL VALUE")
} else {
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 IllegalArgumentException("INVALID PASSWORD VALUE")
} else {
try {
field = value
} catch (e: Exception) {
throw Exception()
} finally {
field = ""
}
}
}
constructor(id: Int, name: String, email: String, password: String) : this() {
this.id = id
this.name = name
this.email = email
this.password = password
}
}
*/
data class User (
var id: Long = 0,
var name: String = "",
var email: String = "",
var password: String = "",
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package com.flabedu.blackpostoffice.parkyounghwan.mapper

import com.flabedu.blackpostoffice.parkyounghwan.domain.User
import org.apache.ibatis.annotations.Mapper
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Component

@Mapper
@Qualifier("pUserMapper")
@Component("parkyounghwan.UserMapper")
interface UserMapper {
fun selectUserById(id: Int): User
fun selectUserByEmail(email: String): User
fun insertUser(user: User): Int
fun deleteAllUser(): Int
}
13 changes: 3 additions & 10 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
# default profiles
spring:
datasource:
hikari:
driver-class-name: org.h2.Driver
jdbc-url: jdbc:h2:mem:test
username: sa
password:

h2:
console:
enabled: true
profiles:
active: local
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/black-postoffice?serverTimeZone=UTC$CharacterEncoding=UTF-8
spring.datasource.username=root
14 changes: 0 additions & 14 deletions src/main/resources/parkyounghwan/application-parkyounghwan.yml

This file was deleted.

13 changes: 11 additions & 2 deletions src/main/resources/parkyounghwan/mapper/UserMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.flabedu.blackpostoffice.parkyounghwan.mapper.UserMapper">
<select id="selectUserById" resultType="com.flabedu.blackpostoffice.parkyounghwan.domain.User">
select * from user where id = #{id}
<select id="selectUserByEmail" resultType="com.flabedu.blackpostoffice.parkyounghwan.domain.User">
SELECT * FROM USER WHERE email = #{email}
</select>

<insert id="insertUser" parameterType="com.flabedu.blackpostoffice.parkyounghwan.domain.User" useGeneratedKeys="true" keyProperty="id">
INSERT INTO USER (name, email, password)
VALUES (#{name}, #{email}, #{password})
</insert>

<delete id="deleteAllUser">
DELETE FROM USER
</delete>
</mapper>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.flabedu.blackpostoffice.parkyounghwan.mapper

import com.flabedu.blackpostoffice.parkyounghwan.domain.User
import junit.framework.Assert.assertEquals
import org.junit.jupiter.api.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -12,16 +14,19 @@ import org.springframework.test.context.junit4.SpringRunner
class UserMapperTest {

@Autowired
@Qualifier("pUserMapper")
@Qualifier("parkyounghwan.UserMapper")
lateinit var userMapper: UserMapper

@Test
fun `UserMapperTest 클래스 호출 테스트`() {
println("hello `UserMapperTest`")
}
fun `회원 정보 저장 테스트`() {
val user = User(0, "홍길동", "honggildong@korea.com", "1234")

@Test
fun `UserMapper 저장 된 유저 ID 로 불러오기`() {
print(userMapper.selectUserById(1))
assertEquals(0, userMapper.deleteAllUser())
assertEquals(1, userMapper.insertUser(user))

val result: User = userMapper.selectUserByEmail("honggildong@korea.com")

assertEquals("홍길동", result.name)
assertEquals(1, userMapper.deleteAllUser())
}
}

0 comments on commit 1aadf01

Please sign in to comment.