From 07c8165ba0c483a3e5938cb122f817e9bd5297ce Mon Sep 17 00:00:00 2001 From: f-lab-pig Date: Tue, 1 Jun 2021 03:19:18 +0900 Subject: [PATCH] =?UTF-8?q?#11,=20DB=20=EC=BB=A4=EB=84=A5=EC=85=98?= =?UTF-8?q?=EC=9D=84=20=EB=A7=BA=EB=8A=94=20=EA=B8=B0=EB=8A=A5=EA=B3=BC=20?= =?UTF-8?q?DB=20=EC=97=90=20=EC=97=91=EC=84=B8=EC=8A=A4=20=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20=EC=9C=84=ED=95=9C=20=EA=B8=B0=EB=8A=A5=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=9C=20=EC=B1=85=EC=9E=84=20=EA=B4=80=EA=B3=84=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlackPostofficeApplication.kt | 9 ----- .../blackpostoffice/dao/ConnectionMaker.kt | 9 +++++ .../blackpostoffice/dao/FConnectionMaker.kt | 17 +++++++++ .../flabedu/blackpostoffice/dao/UserDao.kt | 21 +++++------ src/main/resources/application.properties | 6 ---- .../blackpostoffice/{ => config}/H2Test.kt | 2 +- .../blackpostoffice/dao/UserDaoTest.kt | 35 +++++++++++++++++++ .../resources/application-test.properties | 6 ++++ src/test/resources/schema.sql | 7 ++++ 9 files changed, 86 insertions(+), 26 deletions(-) create mode 100644 src/main/kotlin/com/flabedu/blackpostoffice/dao/ConnectionMaker.kt create mode 100644 src/main/kotlin/com/flabedu/blackpostoffice/dao/FConnectionMaker.kt rename src/test/kotlin/com/flabedu/blackpostoffice/{ => config}/H2Test.kt (92%) create mode 100644 src/test/kotlin/com/flabedu/blackpostoffice/dao/UserDaoTest.kt create mode 100644 src/test/resources/application-test.properties create mode 100644 src/test/resources/schema.sql diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/BlackPostofficeApplication.kt b/src/main/kotlin/com/flabedu/blackpostoffice/BlackPostofficeApplication.kt index e82273a..2caad48 100644 --- a/src/main/kotlin/com/flabedu/blackpostoffice/BlackPostofficeApplication.kt +++ b/src/main/kotlin/com/flabedu/blackpostoffice/BlackPostofficeApplication.kt @@ -10,13 +10,4 @@ class BlackPostofficeApplication fun main(args: Array) { runApplication(*args) - - val userDao = UserDao() - val user = User("dudrnxps", "박영환", "1234") - - userDao.add(user) - - val result = userDao.get("dudrnxps") - - print(result.toString()) } diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/dao/ConnectionMaker.kt b/src/main/kotlin/com/flabedu/blackpostoffice/dao/ConnectionMaker.kt new file mode 100644 index 0000000..117d81b --- /dev/null +++ b/src/main/kotlin/com/flabedu/blackpostoffice/dao/ConnectionMaker.kt @@ -0,0 +1,9 @@ +package com.flabedu.blackpostoffice.dao + +import java.sql.Connection +import kotlin.jvm.Throws + +interface ConnectionMaker { + @Throws + fun makeConnection(): Connection +} \ No newline at end of file diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/dao/FConnectionMaker.kt b/src/main/kotlin/com/flabedu/blackpostoffice/dao/FConnectionMaker.kt new file mode 100644 index 0000000..aae86fd --- /dev/null +++ b/src/main/kotlin/com/flabedu/blackpostoffice/dao/FConnectionMaker.kt @@ -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 + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt b/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt index 1c16216..f2b7325 100644 --- a/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt +++ b/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt @@ -6,15 +6,17 @@ 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) @@ -22,14 +24,13 @@ class UserDao { 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) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index fe1e595..e69de29 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 \ No newline at end of file diff --git a/src/test/kotlin/com/flabedu/blackpostoffice/H2Test.kt b/src/test/kotlin/com/flabedu/blackpostoffice/config/H2Test.kt similarity index 92% rename from src/test/kotlin/com/flabedu/blackpostoffice/H2Test.kt rename to src/test/kotlin/com/flabedu/blackpostoffice/config/H2Test.kt index 5908cb7..709789e 100644 --- a/src/test/kotlin/com/flabedu/blackpostoffice/H2Test.kt +++ b/src/test/kotlin/com/flabedu/blackpostoffice/config/H2Test.kt @@ -1,4 +1,4 @@ -package com.flabedu.blackpostoffice +package com.flabedu.blackpostoffice.config import junit.framework.Assert.assertEquals import org.junit.jupiter.api.Test diff --git a/src/test/kotlin/com/flabedu/blackpostoffice/dao/UserDaoTest.kt b/src/test/kotlin/com/flabedu/blackpostoffice/dao/UserDaoTest.kt new file mode 100644 index 0000000..84d87c3 --- /dev/null +++ b/src/test/kotlin/com/flabedu/blackpostoffice/dao/UserDaoTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/src/test/resources/application-test.properties b/src/test/resources/application-test.properties new file mode 100644 index 0000000..1181f63 --- /dev/null +++ b/src/test/resources/application-test.properties @@ -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 \ No newline at end of file diff --git a/src/test/resources/schema.sql b/src/test/resources/schema.sql new file mode 100644 index 0000000..9d0d884 --- /dev/null +++ b/src/test/resources/schema.sql @@ -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 +); \ No newline at end of file