Skip to content

Commit

Permalink
test: improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocnhan-tran1996 committed Jul 27, 2024
1 parent 0e3e0fe commit 905562b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .mvn/maven.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
revision=1.0.0
changelist=-SNAPSHOT
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.ngocnhan-tran1996</groupId>
<artifactId>spring-jdbc-oracle</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>${revision}{changelist}</version>
<name>Spring Jdbc Oracle</name>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion spring-jdbc-oracle-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>io.github.ngocnhan-tran1996</groupId>
<artifactId>spring-jdbc-oracle-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>${revision}{changelist}</version>

<properties>
<java.version>22</java.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.spring.jdbc.oracle.mapper;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import io.spring.jdbc.oracle.TestConfig;
import io.spring.jdbc.oracle.annotation.OracleParameter;
import io.spring.jdbc.oracle.converter.OracleConverter;
import io.spring.jdbc.oracle.converter.support.DefaultOracleConverters;
import io.spring.jdbc.oracle.exception.ValueException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
Expand All @@ -19,6 +21,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import oracle.jdbc.driver.OracleConnection;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
Expand All @@ -28,12 +31,11 @@
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;

@TestInstance(Lifecycle.PER_CLASS)
@SpringBootTest
@Import(TestConfig.class)
//@Import(TestConfig.class)
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class DelegateMapperTest {

Expand Down Expand Up @@ -90,6 +92,9 @@ void convert() {
LocalDate.now()
)
);

assertThat(mapper.convert(null))
.isNull();
}

void fromStruct_return_null_if_convert_process_is_failed(DelegateMapper<?> mapper) {
Expand Down Expand Up @@ -340,4 +345,81 @@ record Person(

}

@Nested
class MethodSpec {

@Test
void throw_exception_if_class_have_same_property_name() {

// assert
assertThatExceptionOfType(ValueException.class)
.isThrownBy(() -> DelegateMapper.newInstance(PersonSetterPojo.class));

assertThatExceptionOfType(ValueException.class)
.isThrownBy(() -> DelegateMapper.newInstance(PersonGetterPojo.class));

assertThatExceptionOfType(ValueException.class)
.isThrownBy(() -> DelegateMapper.newInstance(PersonRecord.class));
}

@Test
void throw_exception_if_record_class_has_no_field() {

// assert
assertThatExceptionOfType(ValueException.class)
.isThrownBy(() -> DelegateMapper.newInstance(PersonNoFieldRecord.class));
}

@Test
void mock_throw_exception_if_class_has_no_same_type() throws SQLException {

// arrange
var mapper = DelegateMapper.newInstance(PersonWrongBirthDatePojo.class);

// assert
var output = mapper.toStruct(
connection.unwrap(OracleConnection.class),
PERSON_TYPE,
new PersonWrongBirthDatePojo()
);
assertThat(output)
.isNull();

}

@Setter
static class PersonSetterPojo {

private String firstName;
@OracleParameter("firstName")
private String lastName;
}

@Getter
static class PersonGetterPojo {

private String firstName;
@OracleParameter("firstName")
private String lastName;
}

@Getter
static class PersonWrongBirthDatePojo {

private final String birthDate = "TEST";
}

record PersonRecord(
String firstName,
@OracleParameter("firstName")
String lastName) {

}

record PersonNoFieldRecord() {

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ void doExtractProperties(
String columnName,
OracleParameter oracleParameter) {

if (this.parameterByFieldName.containsKey(columnName)) {

throw new ValueException(UNIQUE_NAME);
}

// Record class will save columnName instead
var typeProperty = super.getTypeProperty(columnName, OracleParameter::output)
.apply(oracleParameter);
Expand Down

0 comments on commit 905562b

Please sign in to comment.