View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.orm.jpa;
18  
19  import javax.persistence.EntityManager;
20  import javax.persistence.Query;
21  
22  import org.springframework.test.jpa.AbstractJpaTests;
23  import org.springframework.transaction.support.TransactionSynchronizationManager;
24  
25  /**
26   * @author Rod Johnson
27   * @author Juergen Hoeller
28   */
29  @SuppressWarnings("deprecation")
30  public abstract class AbstractEntityManagerFactoryIntegrationTests extends AbstractJpaTests {
31  
32  	public static final String[] ECLIPSELINK_CONFIG_LOCATIONS = new String[] {
33  			"/org/springframework/orm/jpa/eclipselink/eclipselink-manager.xml", "/org/springframework/orm/jpa/memdb.xml",
34  			"/org/springframework/orm/jpa/inject.xml"};
35  
36  	public static final String[] HIBERNATE_CONFIG_LOCATIONS = new String[] {
37  			"/org/springframework/orm/jpa/hibernate/hibernate-manager.xml", "/org/springframework/orm/jpa/memdb.xml",
38  			"/org/springframework/orm/jpa/inject.xml"};
39  
40  	public static final String[] OPENJPA_CONFIG_LOCATIONS = new String[] {
41  			"/org/springframework/orm/jpa/openjpa/openjpa-manager.xml", "/org/springframework/orm/jpa/memdb.xml",
42  			"/org/springframework/orm/jpa/inject.xml"};
43  
44  
45  	public static Provider getProvider() {
46  		String provider = System.getProperty("org.springframework.orm.jpa.provider");
47  		if (provider != null) {
48  			if (provider.toLowerCase().contains("hibernate")) {
49  				return Provider.HIBERNATE;
50  			}
51  			if (provider.toLowerCase().contains("openjpa")) {
52  				return Provider.OPENJPA;
53  			}
54  		}
55  		return Provider.ECLIPSELINK;
56  	}
57  
58  
59  	@Override
60  	protected String getActualOrmXmlLocation() {
61  		// Specify that we do NOT want to find such a file.
62  		return null;
63  	}
64  
65  	@Override
66  	protected String[] getConfigLocations() {
67  		Provider provider = getProvider();
68  		switch (provider) {
69  			case ECLIPSELINK:
70  				return ECLIPSELINK_CONFIG_LOCATIONS;
71  			case HIBERNATE:
72  				return HIBERNATE_CONFIG_LOCATIONS;
73  			case OPENJPA:
74  				return OPENJPA_CONFIG_LOCATIONS;
75  			default:
76  				throw new IllegalStateException("Unknown provider: " + provider);
77  		}
78  	}
79  
80  	@Override
81  	protected void onTearDownAfterTransaction() throws Exception {
82  		assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
83  		assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
84  		assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
85  		assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
86  	}
87  
88  	protected int countRowsInTable(EntityManager em, String tableName) {
89  		Query query = em.createNativeQuery("SELECT COUNT(0) FROM " + tableName);
90  		return ((Number) query.getSingleResult()).intValue();
91  	}
92  
93  
94  	public enum Provider {
95  
96  		ECLIPSELINK, HIBERNATE, OPENJPA
97  	}
98  
99  }