|
Spring Framework example source code file (PersistenceXmlParsingTests.java)
The Spring Framework PersistenceXmlParsingTests.java source code/* * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.orm.jpa.persistenceunit; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.persistence.spi.PersistenceUnitInfo; import javax.persistence.spi.PersistenceUnitTransactionType; import javax.sql.DataSource; import junit.framework.TestCase; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup; import org.springframework.jdbc.datasource.lookup.MapDataSourceLookup; import org.springframework.mock.jndi.SimpleNamingContextBuilder; /** * Unit and integration tests for the JPA XML resource parsing support. * * @author Costin Leau */ public class PersistenceXmlParsingTests extends TestCase { 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); assertNotNull(info); assertEquals(1, info.length); assertEquals("OrderManagement", info[0].getPersistenceUnitName()); } public void testExample2() throws Exception { PersistenceUnitReader reader = new PersistenceUnitReader( new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup()); String resource = "/org/springframework/orm/jpa/persistence-example2.xml"; PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource); assertNotNull(info); assertEquals(1, info.length); assertEquals("OrderManagement2", info[0].getPersistenceUnitName()); assertEquals(1, info[0].getMappingFileNames().size()); assertEquals("mappings.xml", info[0].getMappingFileNames().get(0)); assertEquals(0, info[0].getProperties().keySet().size()); } 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); assertNotNull(info); assertEquals(1, info.length); assertEquals("OrderManagement3", info[0].getPersistenceUnitName()); assertEquals(2, info[0].getJarFileUrls().size()); assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0)); assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1)); assertEquals(0, info[0].getProperties().keySet().size()); assertNull(info[0].getJtaDataSource()); assertNull(info[0].getNonJtaDataSource()); } 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); assertNotNull(info); assertEquals(1, info.length); assertEquals("OrderManagement4", info[0].getPersistenceUnitName()); assertEquals(1, info[0].getMappingFileNames().size()); assertEquals("order-mappings.xml", info[0].getMappingFileNames().get(0)); assertEquals(3, info[0].getManagedClassNames().size()); assertEquals("com.acme.Order", info[0].getManagedClassNames().get(0)); assertEquals("com.acme.Customer", info[0].getManagedClassNames().get(1)); assertEquals("com.acme.Item", info[0].getManagedClassNames().get(2)); assertTrue(info[0].excludeUnlistedClasses()); assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, info[0].getTransactionType()); assertEquals(0, info[0].getProperties().keySet().size()); // TODO this is undefined as yet. Do we look up Spring datasource? // assertNotNull(info[0].getNonJtaDataSource()); // // assertEquals(ds .toString(), // info[0].getNonJtaDataSource().toString()); builder.clear(); } 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); assertNotNull(info); assertEquals(1, info.length); assertEquals("OrderManagement5", info[0].getPersistenceUnitName()); assertEquals(2, info[0].getMappingFileNames().size()); assertEquals("order1.xml", info[0].getMappingFileNames().get(0)); assertEquals("order2.xml", info[0].getMappingFileNames().get(1)); assertEquals(2, info[0].getJarFileUrls().size()); assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0)); assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1)); assertEquals("com.acme.AcmePersistence", info[0].getPersistenceProviderClassName()); assertEquals(0, info[0].getProperties().keySet().size()); } 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 Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework PersistenceXmlParsingTests.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.