Search in sources :

Example 1 with JndiDataSourceLookup

use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project herd by FINRAOS.

the class DaoEnvSpringModuleConfig method herdDataSource.

/**
 * The data source for the application.
 *
 * @return the data source.
 */
@Bean
public static DataSource herdDataSource() {
    // Access the environment using the application context holder since we're in a static method that doesn't have access to the environment in any
    // other way.
    Environment environment = ApplicationContextHolder.getApplicationContext().getEnvironment();
    // Get the configuration property for the data source JNDI name.
    String dataSourceJndiName = ConfigurationHelper.getProperty(ConfigurationValue.HERD_DATA_SOURCE_JNDI_NAME, environment);
    // Return a new JNDI data source.
    return new JndiDataSourceLookup().getDataSource(dataSourceJndiName);
}
Also used : Environment(org.springframework.core.env.Environment) JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) Bean(org.springframework.context.annotation.Bean)

Example 2 with JndiDataSourceLookup

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

the class PersistenceXmlParsingTests method testJpa2ExcludeUnlisted.

@Test
public void testJpa2ExcludeUnlisted() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-exclude-2.0.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertThat(info).isNotNull();
    assertThat(info.length).as("The number of persistence units is incorrect.").isEqualTo(4);
    PersistenceUnitInfo noExclude = info[0];
    assertThat(noExclude).as("noExclude should not be null.").isNotNull();
    assertThat(noExclude.getPersistenceUnitName()).as("noExclude name is not correct.").isEqualTo("NoExcludeElement");
    assertThat(noExclude.excludeUnlistedClasses()).as("Exclude unlisted still defaults to false in 2.0.").isFalse();
    PersistenceUnitInfo emptyExclude = info[1];
    assertThat(emptyExclude).as("emptyExclude should not be null.").isNotNull();
    assertThat(emptyExclude.getPersistenceUnitName()).as("emptyExclude name is not correct.").isEqualTo("EmptyExcludeElement");
    assertThat(emptyExclude.excludeUnlistedClasses()).as("emptyExclude should be true.").isTrue();
    PersistenceUnitInfo trueExclude = info[2];
    assertThat(trueExclude).as("trueExclude should not be null.").isNotNull();
    assertThat(trueExclude.getPersistenceUnitName()).as("trueExclude name is not correct.").isEqualTo("TrueExcludeElement");
    assertThat(trueExclude.excludeUnlistedClasses()).as("trueExclude should be true.").isTrue();
    PersistenceUnitInfo falseExclude = info[3];
    assertThat(falseExclude).as("falseExclude should not be null.").isNotNull();
    assertThat(falseExclude.getPersistenceUnitName()).as("falseExclude name is not correct.").isEqualTo("FalseExcludeElement");
    assertThat(falseExclude.excludeUnlistedClasses()).as("falseExclude should be false.").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 3 with JndiDataSourceLookup

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

the class PersistenceXmlParsingTests method testExample5.

@Test
public void testExample5() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-example5.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertThat(info).isNotNull();
    assertThat(info.length).isEqualTo(1);
    assertThat(info[0].getPersistenceUnitName()).isEqualTo("OrderManagement5");
    assertThat(info[0].getMappingFileNames().size()).isEqualTo(2);
    assertThat(info[0].getMappingFileNames().get(0)).isEqualTo("order1.xml");
    assertThat(info[0].getMappingFileNames().get(1)).isEqualTo("order2.xml");
    assertThat(info[0].getJarFileUrls().size()).isEqualTo(2);
    assertThat(info[0].getJarFileUrls().get(0)).isEqualTo(new ClassPathResource("order.jar").getURL());
    assertThat(info[0].getJarFileUrls().get(1)).isEqualTo(new ClassPathResource("order-supplemental.jar").getURL());
    assertThat(info[0].getPersistenceProviderClassName()).isEqualTo("com.acme.AcmePersistence");
    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) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 4 with JndiDataSourceLookup

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

the class PersistenceXmlParsingTests method testExample1.

@Test
public void testExample1() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-example1.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertThat(info).isNotNull();
    assertThat(info.length).isEqualTo(1);
    assertThat(info[0].getPersistenceUnitName()).isEqualTo("OrderManagement");
    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 5 with JndiDataSourceLookup

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

the class PersistenceXmlParsingTests method testExample3.

@Test
public void testExample3() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-example3.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertThat(info).isNotNull();
    assertThat(info.length).isEqualTo(1);
    assertThat(info[0].getPersistenceUnitName()).isEqualTo("OrderManagement3");
    assertThat(info[0].getJarFileUrls().size()).isEqualTo(2);
    assertThat(info[0].getJarFileUrls().get(0)).isEqualTo(new ClassPathResource("order.jar").getURL());
    assertThat(info[0].getJarFileUrls().get(1)).isEqualTo(new ClassPathResource("order-supplemental.jar").getURL());
    assertThat(info[0].getProperties().keySet().size()).isEqualTo(0);
    assertThat(info[0].getJtaDataSource()).isNull();
    assertThat(info[0].getNonJtaDataSource()).isNull();
    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) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

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