-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2단계 - JDBC 라이브러리 구현하기] 페드로(류형욱) 미션 제출합니다. (#763)
* test: Connection pool 학습 테스트 추가 * test: Transaction 학습 테스트 추가 * style(UserHistoryDao): 쿼리를 대문자로 변경 * refactor(RowMapper): RowMapper 함수형 인터페이스 추가 * refactor(JdbcTemplate): 좀 더 구체적인 예외를 던지도록 변경 * refactor(PreparedStatementSetter): PreparedStatement에 인자를 설정하는 인터페이스 추가 * refactor(PreParedStatementArgumentSetter): PreparedStatement에 인자를 설정하는 구현체를 사용하도록 변경 * test(PreparedStatementArgumentsSetterTest): Argument 설정 로직 테스트 추가 * style(UserDao): 코드 스타일 적용 * refactor(UserHistoryDao): RowMapper 인터페이스를 사용하도록 변경
- Loading branch information
Showing
15 changed files
with
243 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
jdbc/src/main/java/com/interface21/jdbc/core/PreparedStatementArgumentsSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.interface21.jdbc.core; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
public class PreparedStatementArgumentsSetter implements PreparedStatementSetter { | ||
|
||
private static final int STATEMENT_ARGUMENT_OFFSET = 1; | ||
|
||
private final Object[] args; | ||
|
||
public PreparedStatementArgumentsSetter(Object... args) { | ||
this.args = args; | ||
} | ||
|
||
@Override | ||
public void setValues(PreparedStatement ps) throws SQLException { | ||
if (args == null) { | ||
return; | ||
} | ||
|
||
for (int argsIndex = 0; argsIndex < args.length; argsIndex++) { | ||
ps.setObject(argsIndex + STATEMENT_ARGUMENT_OFFSET, args[argsIndex]); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
jdbc/src/main/java/com/interface21/jdbc/core/PreparedStatementSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.interface21.jdbc.core; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
@FunctionalInterface | ||
public interface PreparedStatementSetter { | ||
|
||
void setValues(PreparedStatement ps) throws SQLException; | ||
} |
10 changes: 10 additions & 0 deletions
10
jdbc/src/main/java/com/interface21/jdbc/core/RowMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.interface21.jdbc.core; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
@FunctionalInterface | ||
public interface RowMapper<T> { | ||
|
||
T mapRow(ResultSet rs) throws SQLException; | ||
} |
65 changes: 65 additions & 0 deletions
65
jdbc/src/test/java/com/interface21/jdbc/core/PreparedStatementArgumentsSetterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.interface21.jdbc.core; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class PreparedStatementArgumentsSetterTest { | ||
|
||
private PreparedStatement preparedStatement; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
preparedStatement = mock(PreparedStatement.class); | ||
} | ||
|
||
@DisplayName("파라미터가 전달되지 않으면 아무런 값도 설정하지 않는다.") | ||
@Test | ||
void setNilArguments() throws SQLException { | ||
// given | ||
PreparedStatementArgumentsSetter argumentSetter = new PreparedStatementArgumentsSetter(); | ||
|
||
// when | ||
argumentSetter.setValues(preparedStatement); | ||
|
||
// then | ||
verify(preparedStatement, never()).setObject(anyInt(), any()); | ||
} | ||
|
||
@DisplayName("파라미터에 null이 전달되면 아무런 값도 설정하지 않는다.") | ||
@Test | ||
void setNullArguments() throws SQLException { | ||
// given | ||
PreparedStatementArgumentsSetter argumentSetter = new PreparedStatementArgumentsSetter(null); | ||
|
||
// when | ||
argumentSetter.setValues(preparedStatement); | ||
|
||
// then | ||
verify(preparedStatement, never()).setObject(anyInt(), any()); | ||
} | ||
|
||
@DisplayName("파라미터 값을 올바르게 설정한다.") | ||
@Test | ||
void setValues() throws SQLException { | ||
// given | ||
Object[] args = {"Hello", ",", " ", "world", "!"}; | ||
PreparedStatementArgumentsSetter argumentSetter = new PreparedStatementArgumentsSetter(args); | ||
|
||
// when | ||
argumentSetter.setValues(preparedStatement); | ||
|
||
// then | ||
for (int i = 0; i < args.length; i++) { | ||
verify(preparedStatement).setObject(i + 1, args[i]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.