org.springframework.jdbc.datasource.lookup.MapDataSourceLookup Java Examples

The following examples show how to use org.springframework.jdbc.datasource.lookup.MapDataSourceLookup. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PersistenceXmlParsingTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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);

	assertEquals(2, info.length);

	PersistenceUnitInfo pu1 = info[0];

	assertEquals("pu1", pu1.getPersistenceUnitName());

	assertEquals("com.acme.AcmePersistence", pu1.getPersistenceProviderClassName());

	assertEquals(1, pu1.getMappingFileNames().size());
	assertEquals("ormap2.xml", pu1.getMappingFileNames().get(0));

	assertEquals(1, pu1.getJarFileUrls().size());
	assertEquals(new ClassPathResource("order.jar").getURL(), pu1.getJarFileUrls().get(0));

	assertFalse(pu1.excludeUnlistedClasses());

	assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, pu1.getTransactionType());

	Properties props = pu1.getProperties();
	assertEquals(2, props.keySet().size());
	assertEquals("on", props.getProperty("com.acme.persistence.sql-logging"));
	assertEquals("bar", props.getProperty("foo"));

	assertNull(pu1.getNonJtaDataSource());

	assertSame(ds, pu1.getJtaDataSource());

	assertFalse("Exclude unlisted should default false in 1.0.", pu1.excludeUnlistedClasses());

	PersistenceUnitInfo pu2 = info[1];

	assertSame(PersistenceUnitTransactionType.JTA, pu2.getTransactionType());
	assertEquals("com.acme.AcmePersistence", pu2.getPersistenceProviderClassName());

	assertEquals(1, pu2.getMappingFileNames().size());
	assertEquals("order2.xml", pu2.getMappingFileNames().get(0));

	// the following assertions fail only during coverage runs
	// assertEquals(1, pu2.getJarFileUrls().size());
	// assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), pu2.getJarFileUrls().get(0));

	assertTrue(pu2.excludeUnlistedClasses());

	assertNull(pu2.getJtaDataSource());
	assertEquals(ds, pu2.getNonJtaDataSource());

	assertTrue("Exclude unlisted should be true when no value.", pu2.excludeUnlistedClasses());
}
 
Example #2
Source File: PersistenceXmlParsingTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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);

	assertEquals(2, info.length);

	PersistenceUnitInfo pu1 = info[0];

	assertEquals("pu1", pu1.getPersistenceUnitName());

	assertEquals("com.acme.AcmePersistence", pu1.getPersistenceProviderClassName());

	assertEquals(1, pu1.getMappingFileNames().size());
	assertEquals("ormap2.xml", pu1.getMappingFileNames().get(0));

	assertEquals(1, pu1.getJarFileUrls().size());
	assertEquals(new ClassPathResource("order.jar").getURL(), pu1.getJarFileUrls().get(0));

	assertFalse(pu1.excludeUnlistedClasses());

	assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, pu1.getTransactionType());

	Properties props = pu1.getProperties();
	assertEquals(2, props.keySet().size());
	assertEquals("on", props.getProperty("com.acme.persistence.sql-logging"));
	assertEquals("bar", props.getProperty("foo"));

	assertNull(pu1.getNonJtaDataSource());

	assertSame(ds, pu1.getJtaDataSource());

	assertFalse("Exclude unlisted should default false in 1.0.", pu1.excludeUnlistedClasses());

	PersistenceUnitInfo pu2 = info[1];

	assertSame(PersistenceUnitTransactionType.JTA, pu2.getTransactionType());
	assertEquals("com.acme.AcmePersistence", pu2.getPersistenceProviderClassName());

	assertEquals(1, pu2.getMappingFileNames().size());
	assertEquals("order2.xml", pu2.getMappingFileNames().get(0));

	// the following assertions fail only during coverage runs
	// assertEquals(1, pu2.getJarFileUrls().size());
	// assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), pu2.getJarFileUrls().get(0));

	assertTrue(pu2.excludeUnlistedClasses());

	assertNull(pu2.getJtaDataSource());
	assertEquals(ds, pu2.getNonJtaDataSource());

	assertTrue("Exclude unlisted should be true when no value.", pu2.excludeUnlistedClasses());
}
 
Example #3
Source File: PersistenceXmlParsingTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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<String, DataSource>();
	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);

	assertEquals(2, info.length);

	PersistenceUnitInfo pu1 = info[0];

	assertEquals("pu1", pu1.getPersistenceUnitName());

	assertEquals("com.acme.AcmePersistence", pu1.getPersistenceProviderClassName());

	assertEquals(1, pu1.getMappingFileNames().size());
	assertEquals("ormap2.xml", pu1.getMappingFileNames().get(0));

	assertEquals(1, pu1.getJarFileUrls().size());
	assertEquals(new ClassPathResource("order.jar").getURL(), pu1.getJarFileUrls().get(0));

	assertFalse(pu1.excludeUnlistedClasses());

	assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, pu1.getTransactionType());

	Properties props = pu1.getProperties();
	assertEquals(2, props.keySet().size());
	assertEquals("on", props.getProperty("com.acme.persistence.sql-logging"));
	assertEquals("bar", props.getProperty("foo"));

	assertNull(pu1.getNonJtaDataSource());

	assertSame(ds, pu1.getJtaDataSource());

	assertFalse("Exclude unlisted should default false in 1.0.", pu1.excludeUnlistedClasses());

	PersistenceUnitInfo pu2 = info[1];

	assertSame(PersistenceUnitTransactionType.JTA, pu2.getTransactionType());
	assertEquals("com.acme.AcmePersistence", pu2.getPersistenceProviderClassName());

	assertEquals(1, pu2.getMappingFileNames().size());
	assertEquals("order2.xml", pu2.getMappingFileNames().get(0));

	// the following assertions fail only during coverage runs
	// assertEquals(1, pu2.getJarFileUrls().size());
	// assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), pu2.getJarFileUrls().get(0));

	assertTrue(pu2.excludeUnlistedClasses());

	assertNull(pu2.getJtaDataSource());
	assertEquals(ds, pu2.getNonJtaDataSource());

	assertTrue("Exclude unlisted should be true when no value.", pu2.excludeUnlistedClasses());
}
 
Example #4
Source File: DefaultPersistenceUnitManager.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Specify the JDBC DataSources that the JPA persistence provider is supposed
 * to use for accessing the database, resolving data source names in
 * {@code persistence.xml} against Spring-managed DataSources.
 * <p>The specified Map needs to define data source names for specific DataSource
 * objects, matching the data source names used in {@code persistence.xml}.
 * If not specified, data source names will be resolved as JNDI names instead
 * (as defined by standard JPA).
 * @see org.springframework.jdbc.datasource.lookup.MapDataSourceLookup
 */
public void setDataSources(Map<String, DataSource> dataSources) {
	this.dataSourceLookup = new MapDataSourceLookup(dataSources);
}
 
Example #5
Source File: DefaultPersistenceUnitManager.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Specify the JDBC DataSources that the JPA persistence provider is supposed
 * to use for accessing the database, resolving data source names in
 * {@code persistence.xml} against Spring-managed DataSources.
 * <p>The specified Map needs to define data source names for specific DataSource
 * objects, matching the data source names used in {@code persistence.xml}.
 * If not specified, data source names will be resolved as JNDI names instead
 * (as defined by standard JPA).
 * @see org.springframework.jdbc.datasource.lookup.MapDataSourceLookup
 */
public void setDataSources(Map<String, DataSource> dataSources) {
	this.dataSourceLookup = new MapDataSourceLookup(dataSources);
}
 
Example #6
Source File: DefaultPersistenceUnitManager.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Specify the JDBC DataSources that the JPA persistence provider is supposed
 * to use for accessing the database, resolving data source names in
 * {@code persistence.xml} against Spring-managed DataSources.
 * <p>The specified Map needs to define data source names for specific DataSource
 * objects, matching the data source names used in {@code persistence.xml}.
 * If not specified, data source names will be resolved as JNDI names instead
 * (as defined by standard JPA).
 * @see org.springframework.jdbc.datasource.lookup.MapDataSourceLookup
 */
public void setDataSources(Map<String, DataSource> dataSources) {
	this.dataSourceLookup = new MapDataSourceLookup(dataSources);
}
 
Example #7
Source File: DefaultPersistenceUnitManager.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Specify the JDBC DataSources that the JPA persistence provider is supposed
 * to use for accessing the database, resolving data source names in
 * {@code persistence.xml} against Spring-managed DataSources.
 * <p>The specified Map needs to define data source names for specific DataSource
 * objects, matching the data source names used in {@code persistence.xml}.
 * If not specified, data source names will be resolved as JNDI names instead
 * (as defined by standard JPA).
 * @see org.springframework.jdbc.datasource.lookup.MapDataSourceLookup
 */
public void setDataSources(Map<String, DataSource> dataSources) {
	this.dataSourceLookup = new MapDataSourceLookup(dataSources);
}