Search in sources :

Example 1 with MapDataSourceLookup

use of cn.taketoday.jdbc.datasource.lookup.MapDataSourceLookup in project today-framework by TAKETODAY.

the class PersistenceXmlParsingTests method testExampleComplex.

@Test
public void testExampleComplex() throws Exception {
    DataSource ds = new DriverManagerDataSource();
    String resource = "/cn/taketoday/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 PathMatchingPatternResourceLoader(), 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").getLocation(), 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(cn.taketoday.jdbc.datasource.DriverManagerDataSource) PathMatchingPatternResourceLoader(cn.taketoday.core.io.PathMatchingPatternResourceLoader) Properties(java.util.Properties) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) ClassPathResource(cn.taketoday.core.io.ClassPathResource) DriverManagerDataSource(cn.taketoday.jdbc.datasource.DriverManagerDataSource) DataSource(javax.sql.DataSource) MapDataSourceLookup(cn.taketoday.jdbc.datasource.lookup.MapDataSourceLookup) Test(org.junit.jupiter.api.Test)

Example 2 with MapDataSourceLookup

use of cn.taketoday.jdbc.datasource.lookup.MapDataSourceLookup in project today-infrastructure by TAKETODAY.

the class PersistenceXmlParsingTests method testExampleComplex.

@Test
public void testExampleComplex() throws Exception {
    DataSource ds = new DriverManagerDataSource();
    String resource = "/cn/taketoday/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 PathMatchingPatternResourceLoader(), 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").getLocation());
    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").getLocation(), 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(cn.taketoday.jdbc.datasource.DriverManagerDataSource) PathMatchingPatternResourceLoader(cn.taketoday.core.io.PathMatchingPatternResourceLoader) Properties(java.util.Properties) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) ClassPathResource(cn.taketoday.core.io.ClassPathResource) DriverManagerDataSource(cn.taketoday.jdbc.datasource.DriverManagerDataSource) DataSource(javax.sql.DataSource) MapDataSourceLookup(cn.taketoday.jdbc.datasource.lookup.MapDataSourceLookup) Test(org.junit.jupiter.api.Test)

Aggregations

ClassPathResource (cn.taketoday.core.io.ClassPathResource)2 PathMatchingPatternResourceLoader (cn.taketoday.core.io.PathMatchingPatternResourceLoader)2 DriverManagerDataSource (cn.taketoday.jdbc.datasource.DriverManagerDataSource)2 MapDataSourceLookup (cn.taketoday.jdbc.datasource.lookup.MapDataSourceLookup)2 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 DataSource (javax.sql.DataSource)2 Test (org.junit.jupiter.api.Test)2