Search in sources :

Example 1 with SimpleNamingContextBuilder

use of org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder 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 2 with SimpleNamingContextBuilder

use of org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder in project spring-framework by spring-projects.

the class JtaTransactionManagerSerializationTests method serializable.

@Test
public void serializable() throws Exception {
    UserTransaction ut1 = mock(UserTransaction.class);
    UserTransaction ut2 = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    JtaTransactionManager jtam = new JtaTransactionManager();
    jtam.setUserTransaction(ut1);
    jtam.setTransactionManager(tm);
    jtam.setRollbackOnCommitFailure(true);
    jtam.afterPropertiesSet();
    SimpleNamingContextBuilder jndiEnv = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
    jndiEnv.bind(JtaTransactionManager.DEFAULT_USER_TRANSACTION_NAME, ut2);
    JtaTransactionManager serializedJtatm = SerializationTestUtils.serializeAndDeserialize(jtam);
    // should do client-side lookup
    assertThat(serializedJtatm.logger).as("Logger must survive serialization").isNotNull();
    assertThat(serializedJtatm.getUserTransaction() == ut2).as("UserTransaction looked up on client").isTrue();
    assertThat(serializedJtatm.getTransactionManager()).as("TransactionManager didn't survive").isNull();
    assertThat(serializedJtatm.isRollbackOnCommitFailure()).isTrue();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) SimpleNamingContextBuilder(org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(jakarta.transaction.TransactionManager) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)2 SimpleNamingContextBuilder (org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder)2 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)1 TransactionManager (jakarta.transaction.TransactionManager)1 UserTransaction (jakarta.transaction.UserTransaction)1 DataSource (javax.sql.DataSource)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)1 JndiDataSourceLookup (org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup)1 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)1