Search in sources :

Example 1 with PersistenceUnitInfo

use of jakarta.persistence.spi.PersistenceUnitInfo 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 2 with PersistenceUnitInfo

use of jakarta.persistence.spi.PersistenceUnitInfo 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 3 with PersistenceUnitInfo

use of jakarta.persistence.spi.PersistenceUnitInfo in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testExampleComplex.

@Test
public void testExampleComplex() throws Exception {
    DataSource ds = new DriverManagerDataSource();
    String resource = "/org/springframework/orm/jpa/persistence-complex.xml";
    MapDataSourceLookup dataSourceLookup = new MapDataSourceLookup();
    Map<String, DataSource> dataSources = new HashMap<>();
    dataSources.put("jdbc/MyPartDB", ds);
    dataSources.put("jdbc/MyDB", ds);
    dataSourceLookup.setDataSources(dataSources);
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), dataSourceLookup);
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertThat(info.length).isEqualTo(2);
    PersistenceUnitInfo pu1 = info[0];
    assertThat(pu1.getPersistenceUnitName()).isEqualTo("pu1");
    assertThat(pu1.getPersistenceProviderClassName()).isEqualTo("com.acme.AcmePersistence");
    assertThat(pu1.getMappingFileNames().size()).isEqualTo(1);
    assertThat(pu1.getMappingFileNames().get(0)).isEqualTo("ormap2.xml");
    assertThat(pu1.getJarFileUrls().size()).isEqualTo(1);
    assertThat(pu1.getJarFileUrls().get(0)).isEqualTo(new ClassPathResource("order.jar").getURL());
    assertThat(pu1.excludeUnlistedClasses()).isFalse();
    assertThat(pu1.getTransactionType()).isSameAs(PersistenceUnitTransactionType.RESOURCE_LOCAL);
    Properties props = pu1.getProperties();
    assertThat(props.keySet().size()).isEqualTo(2);
    assertThat(props.getProperty("com.acme.persistence.sql-logging")).isEqualTo("on");
    assertThat(props.getProperty("foo")).isEqualTo("bar");
    assertThat(pu1.getNonJtaDataSource()).isNull();
    assertThat(pu1.getJtaDataSource()).isSameAs(ds);
    assertThat(pu1.excludeUnlistedClasses()).as("Exclude unlisted should default false in 1.0.").isFalse();
    PersistenceUnitInfo pu2 = info[1];
    assertThat(pu2.getTransactionType()).isSameAs(PersistenceUnitTransactionType.JTA);
    assertThat(pu2.getPersistenceProviderClassName()).isEqualTo("com.acme.AcmePersistence");
    assertThat(pu2.getMappingFileNames().size()).isEqualTo(1);
    assertThat(pu2.getMappingFileNames().get(0)).isEqualTo("order2.xml");
    // the following assertions fail only during coverage runs
    // assertEquals(1, pu2.getJarFileUrls().size());
    // assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), pu2.getJarFileUrls().get(0));
    assertThat(pu2.excludeUnlistedClasses()).isTrue();
    assertThat(pu2.getJtaDataSource()).isNull();
    assertThat(pu2.getNonJtaDataSource()).isEqualTo(ds);
    assertThat(pu2.excludeUnlistedClasses()).as("Exclude unlisted should be true when no value.").isTrue();
}
Also used : HashMap(java.util.HashMap) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Properties(java.util.Properties) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) ClassPathResource(org.springframework.core.io.ClassPathResource) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) MapDataSourceLookup(org.springframework.jdbc.datasource.lookup.MapDataSourceLookup) Test(org.junit.jupiter.api.Test)

Example 4 with PersistenceUnitInfo

use of jakarta.persistence.spi.PersistenceUnitInfo 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 PersistenceUnitInfo

use of jakarta.persistence.spi.PersistenceUnitInfo 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

PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)13 Test (org.junit.jupiter.api.Test)11 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)10 JndiDataSourceLookup (org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup)9 ClassPathResource (org.springframework.core.io.ClassPathResource)4 HashMap (java.util.HashMap)2 DataSource (javax.sql.DataSource)2 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)2 LinkedHashMap (java.util.LinkedHashMap)1 Properties (java.util.Properties)1 EntityManagerFactoryBuilderImpl (org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl)1 PersistenceUnitInfoDescriptor (org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor)1 SimpleNamingContextBuilder (org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder)1 MapDataSourceLookup (org.springframework.jdbc.datasource.lookup.MapDataSourceLookup)1 LocalContainerEntityManagerFactoryBean (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)1