Search in sources :

Example 11 with QualifiedType

use of org.jdbi.v3.core.qualifier.QualifiedType in project jdbi by jdbi.

the class TestMultipleConfigurationModules method testGlobalModules.

@Test
public void testGlobalModules() {
    Injector inj = GuiceTestSupport.createTestInjector(new AbstractJdbiConfigurationModule() {

        @Override
        public void configureJdbi() {
            bindColumnMapper().to(DummyAMapper.class);
            bindColumnMapper(A_TYPE).to(DummyAMapper.class);
            bindCustomizer().toInstance(jdbi -> {
            });
        }
    }, new AbstractJdbiConfigurationModule() {

        @Override
        public void configureJdbi() {
            bindColumnMapper().to(DummyBMapper.class);
            bindColumnMapper(B_TYPE).to(DummyBMapper.class);
            bindCustomizer().toInstance(jdbi -> {
            });
        }
    });
    assertNotNull(inj);
    Map<QualifiedType<?>, ColumnMapper<?>> qualifiedGlobalMappers = inj.getInstance(Key.get(new TypeLiteral<Map<QualifiedType<?>, ColumnMapper<?>>>() {
    }, JdbiGlobal.class));
    assertEquals(2, qualifiedGlobalMappers.size());
    assertTrue(qualifiedGlobalMappers.containsKey(A_TYPE));
    assertTrue(qualifiedGlobalMappers.containsKey(B_TYPE));
    ColumnMapper<?> aMapper = qualifiedGlobalMappers.get(A_TYPE);
    ColumnMapper<?> bMapper = qualifiedGlobalMappers.get(B_TYPE);
    Set<ColumnMapper<?>> globalMappers = inj.getInstance(Key.get(new TypeLiteral<Set<ColumnMapper<?>>>() {
    }, JdbiGlobal.class));
    assertEquals(2, globalMappers.size());
    assertTrue(globalMappers.contains(aMapper));
    assertTrue(globalMappers.contains(bMapper));
    Set<GuiceJdbiCustomizer> customizers = inj.getInstance(Key.get(new TypeLiteral<Set<GuiceJdbiCustomizer>>() {
    }, JdbiGlobal.class));
    assertEquals(2, customizers.size());
}
Also used : Module(com.google.inject.Module) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Key(com.google.inject.Key) PARAMETER(java.lang.annotation.ElementType.PARAMETER) Singleton(javax.inject.Singleton) Retention(java.lang.annotation.Retention) Inject(javax.inject.Inject) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Map(java.util.Map) Qualifier(javax.inject.Qualifier) DataSource(javax.sql.DataSource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) QualifiedType(org.jdbi.v3.core.qualifier.QualifiedType) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) JdbiGlobal(org.jdbi.v3.guice.internal.JdbiGlobal) GuiceTestSupport(org.jdbi.v3.guice.util.GuiceTestSupport) Jdbi(org.jdbi.v3.core.Jdbi) FIELD(java.lang.annotation.ElementType.FIELD) Set(java.util.Set) Target(java.lang.annotation.Target) Names(com.google.inject.name.Names) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) StatementContext(org.jdbi.v3.core.statement.StatementContext) Injector(com.google.inject.Injector) Test(org.junit.jupiter.api.Test) METHOD(java.lang.annotation.ElementType.METHOD) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) TypeLiteral(com.google.inject.TypeLiteral) JdbiGlobal(org.jdbi.v3.guice.internal.JdbiGlobal) TypeLiteral(com.google.inject.TypeLiteral) QualifiedType(org.jdbi.v3.core.qualifier.QualifiedType) Injector(com.google.inject.Injector) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) Test(org.junit.jupiter.api.Test)

Example 12 with QualifiedType

use of org.jdbi.v3.core.qualifier.QualifiedType in project jdbi by jdbi.

the class TestMultipleConfigurationModules method testLocalModules.

@Test
public void testLocalModules() {
    Injector inj = GuiceTestSupport.createTestInjector(new AbstractJdbiConfigurationModule(Foo.class) {

        @Override
        public void configureJdbi() {
            bindColumnMapper().to(DummyAMapper.class);
            bindColumnMapper(A_TYPE).to(DummyAMapper.class);
            bindCustomizer().toInstance(jdbi -> {
            });
        }
    }, new AbstractJdbiConfigurationModule(Foo.class) {

        @Override
        public void configureJdbi() {
            bindColumnMapper().to(DummyBMapper.class);
            bindColumnMapper(B_TYPE).to(DummyBMapper.class);
            bindCustomizer().toInstance(jdbi -> {
            });
        }
    });
    assertNotNull(inj);
    Map<QualifiedType<?>, ColumnMapper<?>> qualifiedMappers = inj.getInstance(Key.get(new TypeLiteral<Map<QualifiedType<?>, ColumnMapper<?>>>() {
    }, Foo.class));
    assertEquals(2, qualifiedMappers.size());
    assertTrue(qualifiedMappers.containsKey(A_TYPE));
    assertTrue(qualifiedMappers.containsKey(B_TYPE));
    ColumnMapper<?> aMapper = qualifiedMappers.get(A_TYPE);
    ColumnMapper<?> bMapper = qualifiedMappers.get(B_TYPE);
    Set<ColumnMapper<?>> globalMappers = inj.getInstance(Key.get(new TypeLiteral<Set<ColumnMapper<?>>>() {
    }, Foo.class));
    assertEquals(2, globalMappers.size());
    assertTrue(globalMappers.contains(aMapper));
    assertTrue(globalMappers.contains(bMapper));
    Set<GuiceJdbiCustomizer> customizers = inj.getInstance(Key.get(new TypeLiteral<Set<GuiceJdbiCustomizer>>() {
    }, Foo.class));
    assertEquals(2, customizers.size());
}
Also used : Module(com.google.inject.Module) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Key(com.google.inject.Key) PARAMETER(java.lang.annotation.ElementType.PARAMETER) Singleton(javax.inject.Singleton) Retention(java.lang.annotation.Retention) Inject(javax.inject.Inject) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Map(java.util.Map) Qualifier(javax.inject.Qualifier) DataSource(javax.sql.DataSource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) QualifiedType(org.jdbi.v3.core.qualifier.QualifiedType) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) JdbiGlobal(org.jdbi.v3.guice.internal.JdbiGlobal) GuiceTestSupport(org.jdbi.v3.guice.util.GuiceTestSupport) Jdbi(org.jdbi.v3.core.Jdbi) FIELD(java.lang.annotation.ElementType.FIELD) Set(java.util.Set) Target(java.lang.annotation.Target) Names(com.google.inject.name.Names) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) StatementContext(org.jdbi.v3.core.statement.StatementContext) Injector(com.google.inject.Injector) Test(org.junit.jupiter.api.Test) METHOD(java.lang.annotation.ElementType.METHOD) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) TypeLiteral(com.google.inject.TypeLiteral) TypeLiteral(com.google.inject.TypeLiteral) QualifiedType(org.jdbi.v3.core.qualifier.QualifiedType) Injector(com.google.inject.Injector) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) Test(org.junit.jupiter.api.Test)

Example 13 with QualifiedType

use of org.jdbi.v3.core.qualifier.QualifiedType in project jdbi by jdbi.

the class TypeResolvingCodecFactoryTest method testNegativeStillWorks.

@Test
public void testNegativeStillWorks() {
    CodecFactory factory = TypeResolvingCodecFactory.forSingleCodec(SET_CODEC_TYPE, new GenericSetCodec());
    QualifiedType<Set<Integer>> integerSetType = QualifiedType.of(new GenericType<Set<Integer>>() {
    });
    Optional<Function<Object, Argument>> result = factory.prepare(integerSetType, new ConfigRegistry());
    assertFalse(result.isPresent());
    QualifiedType<HashSet<Integer>> concreteIntegerType = QualifiedType.of(new GenericType<HashSet<Integer>>() {
    });
    result = factory.prepare(concreteIntegerType, new ConfigRegistry());
    assertFalse(result.isPresent());
    QualifiedType<String> stringType = QualifiedType.of(String.class);
    result = factory.prepare(stringType, new ConfigRegistry());
    assertFalse(result.isPresent());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Function(java.util.function.Function) ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) CodecFactory(org.jdbi.v3.core.codec.CodecFactory) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 14 with QualifiedType

use of org.jdbi.v3.core.qualifier.QualifiedType in project jdbi by jdbi.

the class TypeResolvingCodecFactoryTest method testInterfaceCodec.

@Test
public void testInterfaceCodec() {
    CodecFactory factory = TypeResolvingCodecFactory.forSingleCodec(XINTERFACE_CODEC_TYPE, new StringXInterfaceCodec());
    QualifiedType<?> xClassType = QualifiedType.of(XClass.class);
    Optional<Function<Object, Argument>> result = factory.prepare(xClassType, new ConfigRegistry());
    assertTrue(result.isPresent());
    QualifiedType<?> yClassType = QualifiedType.of(YClass.class);
    result = factory.prepare(yClassType, new ConfigRegistry());
    assertTrue(result.isPresent());
    QualifiedType<?> zClassType = QualifiedType.of(ZClass.class);
    result = factory.prepare(zClassType, new ConfigRegistry());
    assertTrue(result.isPresent());
}
Also used : Function(java.util.function.Function) ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) CodecFactory(org.jdbi.v3.core.codec.CodecFactory) Test(org.junit.jupiter.api.Test)

Aggregations

QualifiedType (org.jdbi.v3.core.qualifier.QualifiedType)8 StatementContext (org.jdbi.v3.core.statement.StatementContext)7 SQLException (java.sql.SQLException)6 Map (java.util.Map)6 Test (org.junit.jupiter.api.Test)6 ResultSet (java.sql.ResultSet)5 List (java.util.List)5 Qualifiers (org.jdbi.v3.core.qualifier.Qualifiers)5 ArrayList (java.util.ArrayList)4 Optional (java.util.Optional)4 Function (java.util.function.Function)4 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)4 ColumnMapper (org.jdbi.v3.core.mapper.ColumnMapper)4 String.format (java.lang.String.format)3 Set (java.util.Set)3 Jdbi (org.jdbi.v3.core.Jdbi)3 RowMapper (org.jdbi.v3.core.mapper.RowMapper)3 Injector (com.google.inject.Injector)2 Key (com.google.inject.Key)2 Module (com.google.inject.Module)2