Search in sources :

Example 6 with JndiDataSourceLookup

use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testExample6.

@Test
public void testExample6() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-example6.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertThat(info.length).isEqualTo(1);
    assertThat(info[0].getPersistenceUnitName()).isEqualTo("pu");
    assertThat(info[0].getProperties().keySet().size()).isEqualTo(0);
    assertThat(info[0].excludeUnlistedClasses()).as("Exclude unlisted should default false in 1.0.").isFalse();
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) Test(org.junit.jupiter.api.Test)

Example 7 with JndiDataSourceLookup

use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testExample4.

@Test
public void testExample4() throws Exception {
    SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
    DataSource ds = new DriverManagerDataSource();
    builder.bind("java:comp/env/jdbc/MyDB", ds);
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-example4.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertThat(info).isNotNull();
    assertThat(info.length).isEqualTo(1);
    assertThat(info[0].getPersistenceUnitName()).isEqualTo("OrderManagement4");
    assertThat(info[0].getMappingFileNames().size()).isEqualTo(1);
    assertThat(info[0].getMappingFileNames().get(0)).isEqualTo("order-mappings.xml");
    assertThat(info[0].getManagedClassNames().size()).isEqualTo(3);
    assertThat(info[0].getManagedClassNames().get(0)).isEqualTo("com.acme.Order");
    assertThat(info[0].getManagedClassNames().get(1)).isEqualTo("com.acme.Customer");
    assertThat(info[0].getManagedClassNames().get(2)).isEqualTo("com.acme.Item");
    assertThat(info[0].excludeUnlistedClasses()).as("Exclude unlisted should be true when no value.").isTrue();
    assertThat(info[0].getTransactionType()).isSameAs(PersistenceUnitTransactionType.RESOURCE_LOCAL);
    assertThat(info[0].getProperties().keySet().size()).isEqualTo(0);
    builder.clear();
}
Also used : SimpleNamingContextBuilder(org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) Test(org.junit.jupiter.api.Test)

Example 8 with JndiDataSourceLookup

use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testNoSchemaPersistence.

@Disabled("not doing schema parsing anymore for JPA 2.0 compatibility")
@Test
public void testNoSchemaPersistence() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-no-schema.xml";
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> reader.readPersistenceUnitInfos(resource));
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 9 with JndiDataSourceLookup

use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project cuba by cuba-platform.

the class DataSourceProvider method getJndiDataSource.

protected DataSource getJndiDataSource(String jndiName) {
    Preconditions.checkNotNull(jndiName, "Jndi name is null");
    DataSourceLookup lookup = new JndiDataSourceLookup();
    return new ProxyDataSource(lookup.getDataSource(jndiName));
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) DataSourceLookup(org.springframework.jdbc.datasource.lookup.DataSourceLookup) JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) ProxyDataSource(com.haulmont.cuba.core.sys.jdbc.ProxyDataSource)

Example 10 with JndiDataSourceLookup

use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project spring-boot by spring-projects.

the class JndiDataSourceAutoConfiguration method dataSource.

@Bean(destroyMethod = "")
@ConditionalOnMissingBean
public DataSource dataSource(DataSourceProperties properties) {
    JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
    DataSource dataSource = dataSourceLookup.getDataSource(properties.getJndiName());
    excludeMBeanIfNecessary(dataSource, "dataSource");
    return dataSource;
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) DataSource(javax.sql.DataSource) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

JndiDataSourceLookup (org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup)17 Test (org.junit.jupiter.api.Test)11 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)11 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)9 DataSource (javax.sql.DataSource)4 Bean (org.springframework.context.annotation.Bean)3 ClassPathResource (org.springframework.core.io.ClassPathResource)3 Disabled (org.junit.jupiter.api.Disabled)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 ProxyDataSource (com.haulmont.cuba.core.sys.jdbc.ProxyDataSource)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 SneakyThrows (lombok.SneakyThrows)1 lombok.val (lombok.val)1 Test (org.junit.Test)1 SimpleNamingContextBuilder (org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder)1 Environment (org.springframework.core.env.Environment)1 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)1 DataSourceLookup (org.springframework.jdbc.datasource.lookup.DataSourceLookup)1 DataSourceLookupFailureException (org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException)1