Skip to content

Commit

Permalink
use @BeforeAll for test setup, not throws Exception from constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
kookubo committed Feb 6, 2024
1 parent 1b8b282 commit db0bd21
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 124 deletions.
2 changes: 1 addition & 1 deletion modules/kvs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sourceSets {
}
}

spotbugsKvsIntegrationTest.enabled = false
spotbugsKvsIntegrationTest.enabled = true

configurations {
kvsIntegrationTestImplementation.extendsFrom testImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

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

import com.tsurugidb.tsubakuro.kvs.KvsClient;
import com.tsurugidb.tsubakuro.kvs.PutType;
import com.tsurugidb.tsubakuro.kvs.Record;
import com.tsurugidb.tsubakuro.kvs.RecordBuffer;
import com.tsurugidb.tsubakuro.kvs.RemoveType;
import com.tsurugidb.tsubakuro.kvs.util.TestBase;
import com.tsurugidb.tsubakuro.kvs.util.Utils;

class BasicPutGetRemoveTest extends TestBase {
class BasicPutGetRemoveTest {

private static final String TABLE_NAME = "table" + BasicPutGetRemoveTest.class.getSimpleName();
private static final String KEY_NAME = "k1";
private static final String VALUE_NAME = "v1";

BasicPutGetRemoveTest() throws Exception {
@BeforeAll
static void setup() throws Exception {
String schema = String.format("%s BIGINT PRIMARY KEY, %s BIGINT", KEY_NAME, VALUE_NAME);
createTable(TABLE_NAME, schema);
Utils.createTable(TABLE_NAME, schema);
}

private static void checkRecord(Record record, long key1, long value1) throws Exception {
Expand All @@ -37,7 +39,7 @@ private static void checkRecord(Record record, long key1, long value1) throws Ex
public void basicPutGetRemove() throws Exception {
final long key1 = 1L;
final long value1 = 100L;
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
try (var tx = kvs.beginTransaction().await()) {
RecordBuffer buffer = new RecordBuffer();
buffer.add(KEY_NAME, key1);
Expand Down Expand Up @@ -77,7 +79,7 @@ public void putTypes() throws Exception {
final long value2 = 201L;
final long value3 = 202L;
RecordBuffer buffer = new RecordBuffer();
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
// {}; add initial (key1, value1)
try (var tx = kvs.beginTransaction().await()) {
buffer.add(KEY_NAME, key1);
Expand Down Expand Up @@ -223,7 +225,7 @@ public void removeTypes() throws Exception {
final long key2 = 3L;
final long value = 100L;
RecordBuffer buffer = new RecordBuffer();
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
try (var tx = kvs.beginTransaction().await()) {
buffer.add(KEY_NAME, key1);
buffer.add(VALUE_NAME, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

import com.tsurugidb.tsubakuro.kvs.BatchScript;
import com.tsurugidb.tsubakuro.kvs.KvsClient;
import com.tsurugidb.tsubakuro.kvs.util.TestBase;
import com.tsurugidb.tsubakuro.kvs.util.Utils;

class BatchTest extends TestBase {
class BatchTest {

@Test
public void notSupportedYet() throws Exception {
BatchScript script = new BatchScript();
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
try (var tx = kvs.beginTransaction().await()) {
assertThrows(UnsupportedOperationException.class, () -> kvs.batch(tx, script).await());
kvs.rollback(tx).await();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import com.tsurugidb.tsubakuro.kvs.KvsServiceCode;
import com.tsurugidb.tsubakuro.kvs.KvsServiceException;
import com.tsurugidb.tsubakuro.kvs.TransactionOption;
import com.tsurugidb.tsubakuro.kvs.util.TestBase;
import com.tsurugidb.tsubakuro.kvs.util.Utils;

class BeginTest extends TestBase {
class BeginTest {

@Test
public void occ() throws Exception {
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
try (var tx = kvs.beginTransaction().await()) {
kvs.rollback(tx);
}
Expand All @@ -31,14 +31,14 @@ public void occ() throws Exception {

@Test
public void occWithArgs() throws Exception {
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
{
var opt = TransactionOption.forShortTransaction().withLabel("abc").build();
KvsServiceException ex = assertThrows(KvsServiceException.class, () -> {
kvs.beginTransaction(opt).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
}
{
for (var p : Priority.values()) {
Expand All @@ -50,22 +50,22 @@ public void occWithArgs() throws Exception {
kvs.beginTransaction(opt).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
}
}
}
}

@Test
public void ltxWithArgs() throws Exception {
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
{
var opt = TransactionOption.forLongTransaction().withLabel("abc").build();
KvsServiceException ex = assertThrows(KvsServiceException.class, () -> {
kvs.beginTransaction(opt).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
}
{
for (var p : Priority.values()) {
Expand All @@ -77,7 +77,7 @@ public void ltxWithArgs() throws Exception {
kvs.beginTransaction(opt).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
}
}
{
Expand All @@ -86,51 +86,51 @@ public void ltxWithArgs() throws Exception {
kvs.beginTransaction(opt).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
}
{
var opt = TransactionOption.forLongTransaction().addWritePreserve("table1").build();
KvsServiceException ex = assertThrows(KvsServiceException.class, () -> {
kvs.beginTransaction(opt).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
}
{
var opt = TransactionOption.forLongTransaction().addInclusiveReadArea("table1").build();
KvsServiceException ex = assertThrows(KvsServiceException.class, () -> {
kvs.beginTransaction(opt).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
}
{
var opt = TransactionOption.forLongTransaction().addExclusiveReadArea("table1").build();
KvsServiceException ex = assertThrows(KvsServiceException.class, () -> {
kvs.beginTransaction(opt).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
}
}
}

@Test
public void otherTypes() throws Exception {
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
{
KvsServiceException ex = assertThrows(KvsServiceException.class, () -> {
kvs.beginTransaction(TransactionOption.forLongTransaction().build()).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
}
{
KvsServiceException ex = assertThrows(KvsServiceException.class, () -> {
kvs.beginTransaction(TransactionOption.forReadOnlyTransaction().build()).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

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

import com.tsurugidb.tsubakuro.kvs.CommitType;
Expand All @@ -11,22 +12,23 @@
import com.tsurugidb.tsubakuro.kvs.KvsServiceException;
import com.tsurugidb.tsubakuro.kvs.PutType;
import com.tsurugidb.tsubakuro.kvs.RecordBuffer;
import com.tsurugidb.tsubakuro.kvs.util.TestBase;
import com.tsurugidb.tsubakuro.kvs.util.Utils;

class CommitTest extends TestBase {
class CommitTest {

private static final String TABLE_NAME = "table" + CommitTest.class.getSimpleName();
private static final String KEY_NAME = "k1";
private static final String VALUE_NAME = "v1";

CommitTest() throws Exception {
@BeforeAll
static void setup() throws Exception {
String schema = String.format("%s BIGINT PRIMARY KEY, %s BIGINT", KEY_NAME, VALUE_NAME);
createTable(TABLE_NAME, schema);
Utils.createTable(TABLE_NAME, schema);
}

@Test
public void unspecified() throws Exception {
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
try (var tx = kvs.beginTransaction().await()) {
RecordBuffer buffer = new RecordBuffer();
buffer.add(KEY_NAME, 1L);
Expand All @@ -39,7 +41,7 @@ public void unspecified() throws Exception {

@Test
public void multiCommit() throws Exception {
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
try (var tx = kvs.beginTransaction().await()) {
RecordBuffer buffer = new RecordBuffer();
buffer.add(KEY_NAME, 1L);
Expand All @@ -57,7 +59,7 @@ public void multiCommit() throws Exception {

@Test
public void opsAfterCommit() throws Exception {
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
try (var tx = kvs.beginTransaction().await()) {
RecordBuffer buffer = new RecordBuffer();
buffer.add(KEY_NAME, 1L);
Expand Down Expand Up @@ -93,7 +95,7 @@ public void opsAfterCommit() throws Exception {

@Test
public void commitTypes() throws Exception {
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
for (var cmtType : CommitType.values()) {
if (cmtType == CommitType.UNSPECIFIED) {
continue;
Expand All @@ -107,7 +109,7 @@ public void commitTypes() throws Exception {
kvs.commit(tx, cmtType).await();
});
assertEquals(KvsServiceCode.NOT_IMPLEMENTED, ex.getDiagnosticCode());
System.err.println(getLineNumber() + "\t" + ex.getMessage());
System.err.println(Utils.getLineNumber() + "\t" + ex.getMessage());
// rollback and dispose will be called at tx.close()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import com.tsurugidb.tsubakuro.kvs.Record;
import com.tsurugidb.tsubakuro.kvs.RecordBuffer;
import com.tsurugidb.tsubakuro.kvs.Values;
import com.tsurugidb.tsubakuro.kvs.util.TestBase;
import com.tsurugidb.tsubakuro.kvs.util.Utils;

class DataTypesTest extends TestBase {
class DataTypesTest {

private static final String TABLE_NAME = "table" + DataTypesTest.class.getSimpleName();
private static final String KEY_NAME = "k1";
Expand All @@ -30,7 +30,7 @@ class DataTypesTest extends TestBase {
private static final int DECIMAL_SCALE = 2;

private static BigDecimal toBigDecimal(KvsData.Value v) {
return toBigDecimal(v, DECIMAL_SCALE);
return Utils.toBigDecimal(v, DECIMAL_SCALE);
}

private static void checkValue(KvsData.Value expected, KvsData.Value value) throws Exception {
Expand Down Expand Up @@ -58,7 +58,7 @@ private static void checkRecord(Record record, KvsData.Value key1, KvsData.Value

private static void checkPutGet(KvsData.Value key1, KvsData.Value value1) throws Exception {
RecordBuffer buffer = new RecordBuffer();
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
try (var tx = kvs.beginTransaction().await()) {
buffer.add(KEY_NAME, key1);
buffer.add(VALUE_NAME, value1);
Expand All @@ -79,7 +79,7 @@ private static void checkPutGet(KvsData.Value key1, KvsData.Value value1) throws

private static void checkPutNG(KvsData.Value key1, KvsData.Value value1, KvsServiceCode code) throws Exception {
RecordBuffer buffer = new RecordBuffer();
try (var session = getNewSession(); var kvs = KvsClient.attach(session)) {
try (var session = Utils.getNewSession(); var kvs = KvsClient.attach(session)) {
try (var tx = kvs.beginTransaction().await()) {
buffer.add(KEY_NAME, key1);
buffer.add(VALUE_NAME, value1);
Expand All @@ -96,9 +96,9 @@ private static String schema(String typeName) {
return String.format("%s %s PRIMARY KEY, %s %s", KEY_NAME, typeName, VALUE_NAME, typeName);
}

private void checkDataType(String typeName, KvsData.Value key1, KvsData.Value value1) throws Exception {
private static void checkDataType(String typeName, KvsData.Value key1, KvsData.Value value1) throws Exception {
// see jogasaki/docs/value_limit.md
createTable(TABLE_NAME, schema(typeName));
Utils.createTable(TABLE_NAME, schema(typeName));
checkPutGet(key1, value1);
}

Expand Down Expand Up @@ -142,7 +142,7 @@ public void stringTest() throws Exception {
public void char10Test() throws Exception {
final String key1 = "1234567890"; // OK
final String value1 = "abcdefghij"; // OK
createTable(TABLE_NAME, schema("char(10)"));
Utils.createTable(TABLE_NAME, schema("char(10)"));
checkPutGet(Values.of(key1), Values.of(value1));
//
checkPutGet(Values.of(""), Values.of(""));
Expand All @@ -161,7 +161,7 @@ public void char10Test() throws Exception {
public void varchar10Test() throws Exception {
final String key1 = "1234567890"; // OK
final String value1 = "abcdefghij"; // OK
createTable(TABLE_NAME, schema("varchar(10)"));
Utils.createTable(TABLE_NAME, schema("varchar(10)"));
checkPutGet(Values.of(key1), Values.of(value1));
//
checkPutGet(Values.of(""), Values.of(""));
Expand Down Expand Up @@ -194,7 +194,7 @@ public void decimalTest() throws Exception {
public void decimalScaleTest() throws Exception {
final BigDecimal key1 = new BigDecimal("12.34");
final BigDecimal value1 = new BigDecimal("56.78");
createTable(TABLE_NAME, schema("decimal(4," + DECIMAL_SCALE + ")"));
Utils.createTable(TABLE_NAME, schema("decimal(4," + DECIMAL_SCALE + ")"));
checkPutGet(Values.of(key1), Values.of(value1));
// OK: too short integer part
checkPutGet(Values.of(new BigDecimal("1.45")), Values.of(new BigDecimal("5.67")));
Expand Down
Loading

0 comments on commit db0bd21

Please sign in to comment.