Skip to content

Commit

Permalink
add and fix testcases.
Browse files Browse the repository at this point in the history
  • Loading branch information
HidekiSugimoto189 committed Feb 12, 2024
1 parent 548f571 commit ebb73bd
Show file tree
Hide file tree
Showing 21 changed files with 782 additions and 906 deletions.
44 changes: 28 additions & 16 deletions src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ public void rollback() {
*/
@Override
public SqlQuery query(final String sqlName) {
if ("".equals(sqlName)) {
throw new IllegalArgumentException("sqlName is required.");
}
return new SqlQueryImpl(this, context().setSqlName(sqlName));
}

Expand All @@ -458,10 +461,7 @@ public SqlQuery query(final String sqlName) {
*/
@Override
public SqlQuery query(final Supplier<String> supplier) {
if (supplier == null) {
throw new IllegalArgumentException("supplier is required.");
}
return new SqlQueryImpl(this, context().setSqlName(supplier.get()));
return this.query(supplier.get());
}

/**
Expand All @@ -471,6 +471,9 @@ public SqlQuery query(final Supplier<String> supplier) {
*/
@Override
public SqlQuery queryWith(final String sql) {
if (sql == null || "".equals(sql)) {
throw new IllegalArgumentException("sql is required.");
}
return new SqlQueryImpl(this, context().setSql(sql));
}

Expand All @@ -481,6 +484,9 @@ public SqlQuery queryWith(final String sql) {
*/
@Override
public SqlUpdate update(final String sqlName) {
if ("".equals(sqlName)) {
throw new IllegalArgumentException("sqlName is required.");
}
return new SqlUpdateImpl(this, context().setSqlName(sqlName));
}

Expand All @@ -491,10 +497,7 @@ public SqlUpdate update(final String sqlName) {
*/
@Override
public SqlUpdate update(final Supplier<String> supplier) {
if (supplier == null) {
throw new IllegalArgumentException("supplier is required.");
}
return new SqlUpdateImpl(this, context().setSqlName(supplier.get()));
return this.update(supplier.get());
}

/**
Expand All @@ -504,6 +507,9 @@ public SqlUpdate update(final Supplier<String> supplier) {
*/
@Override
public SqlUpdate updateWith(final String sql) {
if (sql == null || "".equals(sql)) {
throw new IllegalArgumentException("sql is required.");
}
return new SqlUpdateImpl(this, context().setSql(sql));
}

Expand All @@ -514,6 +520,9 @@ public SqlUpdate updateWith(final String sql) {
*/
@Override
public SqlBatch batch(final String sqlName) {
if ("".equals(sqlName)) {
throw new IllegalArgumentException("sqlName is required.");
}
return new SqlBatchImpl(this, context().setSqlName(sqlName));
}

Expand All @@ -524,10 +533,7 @@ public SqlBatch batch(final String sqlName) {
*/
@Override
public SqlBatch batch(final Supplier<String> supplier) {
if (supplier == null) {
throw new IllegalArgumentException("supplier is required.");
}
return new SqlBatchImpl(this, context().setSqlName(supplier.get()));
return this.batch(supplier.get());
}

/**
Expand All @@ -537,6 +543,9 @@ public SqlBatch batch(final Supplier<String> supplier) {
*/
@Override
public SqlBatch batchWith(final String sql) {
if (sql == null || "".equals(sql)) {
throw new IllegalArgumentException("sql is required.");
}
return new SqlBatchImpl(this, context().setSql(sql));
}

Expand All @@ -547,6 +556,9 @@ public SqlBatch batchWith(final String sql) {
*/
@Override
public Procedure proc(final String sqlName) {
if ("".equals(sqlName)) {
throw new IllegalArgumentException("sqlName is required.");
}
return new ProcedureImpl(this, context().setSqlName(sqlName));
}

Expand All @@ -557,10 +569,7 @@ public Procedure proc(final String sqlName) {
*/
@Override
public Procedure proc(final Supplier<String> supplier) {
if (supplier == null) {
throw new IllegalArgumentException("supplier is required.");
}
return new ProcedureImpl(this, context().setSqlName(supplier.get()));
return this.proc(supplier.get());
}

/**
Expand All @@ -570,6 +579,9 @@ public Procedure proc(final Supplier<String> supplier) {
*/
@Override
public Procedure procWith(final String sql) {
if (sql == null || "".equals(sql)) {
throw new IllegalArgumentException("sql is required.");
}
return new ProcedureImpl(this, context().setSql(sql));
}

Expand Down
7 changes: 0 additions & 7 deletions src/main/java/jp/co/future/uroborosql/SqlUpdateImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
* @author H.Sugimoto
*/
final class SqlUpdateImpl extends AbstractSqlFluent<SqlUpdate> implements SqlUpdate {
/** バッチ処理を行うかどうか */
private boolean batch = false;

/**
* コンストラクタ
*
Expand All @@ -38,10 +35,6 @@ final class SqlUpdateImpl extends AbstractSqlFluent<SqlUpdate> implements SqlUpd
*/
@Override
public int count() {
if (batch) {
throw new IllegalStateException(
"Since the parameter has already been set with addBatch() method, please call batch() method.");
}
try {
return agent().update(context());
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ private String getSubstringByte(final Object obj, final int capacity) throws Cha
}

var ce = Charset.forName(ENCODING_SHIFT_JIS).newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.reset();
if (capacity >= ce.maxBytesPerChar() * str.length()) {
return str;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface TransformContext {
/**
* 実行時SQL取得
*
* @return 実行k時SQL
* @return 実行時SQL
*/
String getExecutableSql();

Expand Down
54 changes: 53 additions & 1 deletion src/test/java/jp/co/future/uroborosql/ProcedureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.api.Test;

import jp.co.future.uroborosql.config.SqlConfig;
import jp.co.future.uroborosql.exception.UroborosqlRuntimeException;

public class ProcedureTest {
/**
Expand All @@ -20,7 +21,8 @@ public class ProcedureTest {

@BeforeEach
public void setUp() {
config = UroboroSQL.builder("jdbc:h2:mem:" + this.getClass().getSimpleName() + ";DB_CLOSE_DELAY=-1", "sa", "sa").build();
config = UroboroSQL.builder("jdbc:h2:mem:" + this.getClass().getSimpleName() + ";DB_CLOSE_DELAY=-1", "sa", "sa")
.build();
try (var agent = config.agent()) {
agent.required(() -> {
agent.updateWith("DROP ALIAS IF EXISTS MYFUNCTION").count();
Expand Down Expand Up @@ -109,4 +111,54 @@ void testCallStoredFunctionWithinTransaction() {
});
}
}

/**
* SQLファイルが存在しない場合のテストケース。
*/
@Test
void testNotFoundFile() throws Exception {
Assertions.assertThrowsExactly(UroborosqlRuntimeException.class, () -> {
try (var agent = config.agent()) {
var ctx = agent.context().setSqlName("file");
agent.procedure(ctx);
}
});
}

/**
* SQLファイルが空文字の場合のテストケース。
*/
@Test
void testSqlNameEmpty() throws Exception {
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
try (var agent = config.agent()) {
agent.proc("");
}
});
}

/**
* プロシージャ実行処理のテストケース(SQLがNULLの場合)。
*/
@Test
void testProcWithSqlNull() throws Exception {
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
try (var agent = config.agent()) {
agent.procWith(null);
}
});
}

/**
* プロシージャ実行処理のテストケース(SQLがEmptyの場合)。
*/
@Test
void testProcWithSqlEmpty() throws Exception {
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
try (var agent = config.agent()) {
agent.procWith("");
}
});
}

}
10 changes: 8 additions & 2 deletions src/test/java/jp/co/future/uroborosql/SqlAgentRetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ public class SqlAgentRetryTest {

@BeforeEach
public void setUp() throws Exception {
config = UroboroSQL.builder(DriverManager.getConnection("jdbc:h2:mem:" + this.getClass().getSimpleName() + ";DB_CLOSE_DELAY=-1")).build();
config.getSqlAgentProvider().setSqlRetryCodeList(List.of("54", "60", "30006"));
config = UroboroSQL
.builder(DriverManager
.getConnection("jdbc:h2:mem:" + this.getClass().getSimpleName() + ";DB_CLOSE_DELAY=-1"))
.build();
config.getSqlAgentProvider()
.setSqlRetryCodeList(List.of("54", "60", "30006"))
.setDefaultMaxRetryCount(2)
.setDefaultSqlRetryWaitTime(10);

agent = config.agent();

Expand Down
42 changes: 33 additions & 9 deletions src/test/java/jp/co/future/uroborosql/SqlBatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.math.BigDecimal;
import java.nio.file.Paths;
Expand All @@ -14,6 +13,7 @@
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import jp.co.future.uroborosql.converter.MapResultSetConverter;
Expand Down Expand Up @@ -375,16 +375,40 @@ void testExecuteManyInsert() throws Exception {
*/
@Test
void testNotFoundFile() throws Exception {
try {
Assertions.assertThrowsExactly(UroborosqlRuntimeException.class, () -> {
var ctx = agent.context().setSqlName("file");
agent.batch(ctx);
// 例外が発生しなかった場合
fail();
} catch (UroborosqlRuntimeException ex) {
// OK
} catch (Exception e) {
fail(e.getMessage());
}
});
}

/**
* SQLファイルが空文字の場合のテストケース。
*/
@Test
void testSqlNameEmpty() throws Exception {
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
agent.batch("");
});
}

/**
* バッチ実行処理のテストケース(SQLがNULLの場合)。
*/
@Test
void testBatchWithSqlNull() throws Exception {
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
agent.batchWith(null);
});
}

/**
* バッチ実行処理のテストケース(SQLがEmptyの場合)。
*/
@Test
void testBatchWithSqlEmpty() throws Exception {
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
agent.batchWith("");
});
}

/**
Expand Down
Loading

0 comments on commit ebb73bd

Please sign in to comment.