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

Commit

Permalink
#11, DB 커넥션을 맺는 기능과 DB 에 엑세스 하기 위한 기능에 대한 책임 관계 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
f-lab-pig committed May 31, 2021
1 parent bde5487 commit 07c8165
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,4 @@ class BlackPostofficeApplication

fun main(args: Array<String>) {
runApplication<BlackPostofficeApplication>(*args)

val userDao = UserDao()
val user = User("dudrnxps", "박영환", "1234")

userDao.add(user)

val result = userDao.get("dudrnxps")

print(result.toString())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.flabedu.blackpostoffice.dao

import java.sql.Connection
import kotlin.jvm.Throws

interface ConnectionMaker {
@Throws
fun makeConnection(): Connection
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.flabedu.blackpostoffice.dao

import java.sql.Connection
import java.sql.DriverManager
import kotlin.jvm.Throws

class FConnectionMaker: ConnectionMaker {

@Throws
override fun makeConnection(): Connection {
Class.forName("org.h2.Driver")

val c: Connection = DriverManager.getConnection("jdbc:h2:mem:test", "sa", "")

return c
}
}
21 changes: 11 additions & 10 deletions src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,31 @@ import kotlin.jvm.Throws

class UserDao {

@Throws
fun add(user: User): Unit {
Class.forName("org.h2.Driver")
val connectionMaker: ConnectionMaker

val conn = DriverManager.getConnection("jdbc:h2:mem:test", "SA", "")
var ps: PreparedStatement = conn.prepareStatement("CREATE TABLE USERS(id varchar(10) primary key, name varchar(20) not null, password varchar(10) not null)")
ps.executeUpdate()
constructor(connectionMaker: ConnectionMaker) {
this.connectionMaker = connectionMaker
}

@Throws
fun add(user: User) {
val c: Connection = connectionMaker.makeConnection()
val ps: PreparedStatement = c.prepareStatement("INSERT INTO USERS(id, name, password) VALUES(?, ?, ?)")

ps = conn.prepareStatement("INSERT INTO USERS(id, name, password) VALUES(?, ?, ?)")
ps.setString(1, user.id)
ps.setString(2, user.name)
ps.setString(3, user.password)

ps.executeUpdate()

ps.close()
conn.close()
c.close()
}

@Throws
fun get(id: String): User {
Class.forName("org.h2.Driver")

val c: Connection = DriverManager.getConnection("jdbc:h2:mem:test", "SA", "")
val c: Connection = connectionMaker.makeConnection()
val ps: PreparedStatement = c.prepareStatement("SELECT * FROM USERS WHERE id = ?")
ps.setString(1, id)

Expand Down
6 changes: 0 additions & 6 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

spring.h2.console.enabled=true
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.flabedu.blackpostoffice
package com.flabedu.blackpostoffice.config

import junit.framework.Assert.assertEquals
import org.junit.jupiter.api.Test
Expand Down
35 changes: 35 additions & 0 deletions src/test/kotlin/com/flabedu/blackpostoffice/dao/UserDaoTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.flabedu.blackpostoffice.dao

import com.flabedu.blackpostoffice.domain.User
import junit.framework.Assert.assertEquals
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

@RunWith(SpringJUnit4ClassRunner::class)
@SpringBootTest(properties = ["classpath:application.properties"])
class UserDaoTest {

@Test
fun `유저 등록 테스드`() {
val connectionMaker: ConnectionMaker = FConnectionMaker()

val dao = UserDao(connectionMaker)

val user = User()

user.id = "dudrnxps"
user.name = "박영환"
user.password = "1234"

dao.add(user)

assertEquals(user.id, "dudrnxps")

val user2: User = dao.get(user.id)

assertEquals(user.id, user2.id)
assertEquals(user.password, user2.password)
}
}
6 changes: 6 additions & 0 deletions src/test/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spring.datasource.url=jdbc:h2:mem:test;INIT=RUNSCRIPT FROM './src/main/resources/schema.sql'
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

spring.h2.console.enabled=true
7 changes: 7 additions & 0 deletions src/test/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DROP TABLE IF EXISTS users;

CREATE TABLE users (
id VARCHAR(10) PRIMARY KEY,
name VARCHAR(20) NOT NULL,
password VARCHAR(10) NOT NULL
);

0 comments on commit 07c8165

Please sign in to comment.