Search in sources :

Example 11 with PhoneNumber

use of example.domain.PhoneNumber in project doma by domaframework.

the class ScalarsTest method testWrapDomain_null.

@Test
public void testWrapDomain_null() {
    Supplier<Scalar<?, ?>> supplier = Scalars.wrap(null, PhoneNumber.class, false, classHelper);
    assertNotNull(supplier);
    Scalar<?, ?> scalar = supplier.get();
    assertTrue(scalar.get() instanceof PhoneNumber);
    assertTrue(scalar.getAsNonOptional() instanceof PhoneNumber);
    PhoneNumber phoneNumber = (PhoneNumber) scalar.get();
    assertNull(phoneNumber.getValue());
    Wrapper<?> wrapper = scalar.getWrapper();
    assertEquals(StringWrapper.class, wrapper.getClass());
    assertNull(wrapper.get());
}
Also used : PhoneNumber(example.domain.PhoneNumber) InternationalPhoneNumber(example.domain.InternationalPhoneNumber) Test(org.junit.jupiter.api.Test)

Example 12 with PhoneNumber

use of example.domain.PhoneNumber in project doma by domaframework.

the class ScalarsTest method testWrapDomain_subclass.

@Test
public void testWrapDomain_subclass() {
    PhoneNumber phoneNumber = new InternationalPhoneNumber("123-456-789");
    Supplier<Scalar<?, ?>> supplier = Scalars.wrap(phoneNumber, InternationalPhoneNumber.class, false, classHelper);
    assertNotNull(supplier);
    Scalar<?, ?> scalar = supplier.get();
    assertTrue(scalar.get() instanceof InternationalPhoneNumber);
    Wrapper<?> wrapper = scalar.getWrapper();
    assertEquals(StringWrapper.class, wrapper.getClass());
    assertEquals("123-456-789", wrapper.get());
}
Also used : PhoneNumber(example.domain.PhoneNumber) InternationalPhoneNumber(example.domain.InternationalPhoneNumber) InternationalPhoneNumber(example.domain.InternationalPhoneNumber) Test(org.junit.jupiter.api.Test)

Example 13 with PhoneNumber

use of example.domain.PhoneNumber in project doma by domaframework.

the class SqlParserTest method testBindVariable_domain.

@Test
public void testBindVariable_domain() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("phone", new Value(PhoneNumber.class, new PhoneNumber("01-2345-6789")));
    String testSql = "select * from aaa where phone = /*phone*/'111'";
    SqlParser parser = new SqlParser(testSql);
    SqlNode sqlNode = parser.parse();
    PreparedSql sql = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED).build(sqlNode, Function.identity());
    assertEquals("select * from aaa where phone = ?", sql.getRawSql());
    assertEquals("select * from aaa where phone = '01-2345-6789'", sql.getFormattedSql());
    assertEquals(1, sql.getParameters().size());
    assertEquals("01-2345-6789", sql.getParameters().get(0).getWrapper().get());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) Value(org.seasar.doma.internal.expr.Value) PhoneNumber(example.domain.PhoneNumber) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 14 with PhoneNumber

use of example.domain.PhoneNumber in project doma by domaframework.

the class DomainResultListHandlerTest method testHandle.

@Test
public void testHandle() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("x"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("01-2345-6789"));
    resultSet.rows.add(new RowData("12-3456-7890"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    DomainResultListHandler<String, PhoneNumber> handler = new DomainResultListHandler<>(_PhoneNumber.getSingletonInternal());
    List<PhoneNumber> results = handler.handle(resultSet, query, (i, next) -> {
    }).get();
    assertEquals(2, results.size());
    assertEquals("01-2345-6789", results.get(0).getValue());
    assertEquals("12-3456-7890", results.get(1).getValue());
}
Also used : SqlFileUtil(org.seasar.doma.internal.jdbc.util.SqlFileUtil) BeforeEach(org.junit.jupiter.api.BeforeEach) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) PhoneNumber(example.domain.PhoneNumber) example.domain._PhoneNumber(example.domain._PhoneNumber) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) List(java.util.List) SqlLogType(org.seasar.doma.jdbc.SqlLogType) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) Method(java.lang.reflect.Method) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) PhoneNumber(example.domain.PhoneNumber) example.domain._PhoneNumber(example.domain._PhoneNumber) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 15 with PhoneNumber

use of example.domain.PhoneNumber in project doma by domaframework.

the class DomainSingleResultHandlerTest method testHandle_NonUniqueResultException.

@Test
public void testHandle_NonUniqueResultException() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("phoneNumber"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("01-2345-6789"));
    resultSet.rows.add(new RowData("02-2345-6789"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    DomainSingleResultHandler<String, PhoneNumber> handler = new DomainSingleResultHandler<>(_PhoneNumber.getSingletonInternal());
    try {
        handler.handle(resultSet, query, (i, next) -> {
        });
        fail();
    } catch (NonUniqueResultException expected) {
    }
}
Also used : NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) PhoneNumber(example.domain.PhoneNumber) example.domain._PhoneNumber(example.domain._PhoneNumber) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Aggregations

PhoneNumber (example.domain.PhoneNumber)17 Test (org.junit.jupiter.api.Test)17 example.domain._PhoneNumber (example.domain._PhoneNumber)5 ColumnMetaData (org.seasar.doma.internal.jdbc.mock.ColumnMetaData)5 MockResultSet (org.seasar.doma.internal.jdbc.mock.MockResultSet)5 MockResultSetMetaData (org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData)5 RowData (org.seasar.doma.internal.jdbc.mock.RowData)5 ClassHelper (org.seasar.doma.jdbc.ClassHelper)5 InternationalPhoneNumber (example.domain.InternationalPhoneNumber)4 Optional (java.util.Optional)4 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)4 SqlFileSelectQuery (org.seasar.doma.jdbc.query.SqlFileSelectQuery)3 Method (java.lang.reflect.Method)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 TestInfo (org.junit.jupiter.api.TestInfo)2 SqlFileUtil (org.seasar.doma.internal.jdbc.util.SqlFileUtil)2 NonUniqueResultException (org.seasar.doma.jdbc.NonUniqueResultException)2 PreparedSql (org.seasar.doma.jdbc.PreparedSql)2 Reference (org.seasar.doma.jdbc.Reference)2