|
| 1 | +/* |
| 2 | + * Copyright 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 | +package org.springframework.data.jpa.repository.support; |
| 17 | + |
| 18 | +import static org.hamcrest.CoreMatchers.*; |
| 19 | +import static org.junit.Assert.*; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import javax.persistence.EntityManager; |
| 25 | + |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | +import org.mockito.Mockito; |
| 29 | +import org.springframework.asm.ClassWriter; |
| 30 | +import org.springframework.asm.Opcodes; |
| 31 | +import org.springframework.instrument.classloading.ShadowingClassLoader; |
| 32 | +import org.springframework.util.ClassUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for PersistenceProvider detection logic in {@link PersistenceProvider}. |
| 36 | + * |
| 37 | + * @author Thomas Darimont |
| 38 | + */ |
| 39 | +public class PersistenceProviderTests { |
| 40 | + |
| 41 | + private ShadowingClassLoader shadowingClassLoader; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setup() { |
| 45 | + shadowingClassLoader = new ShadowingClassLoader(getClass().getClassLoader()); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @see DATAJPA-444 |
| 50 | + */ |
| 51 | + @Test |
| 52 | + public void detectsHibernatePersistenceProviderForHibernateVersionLessThan4dot3() throws Exception { |
| 53 | + |
| 54 | + shadowingClassLoader.excludePackage("org.hibernate"); |
| 55 | + |
| 56 | + EntityManager em = mockProviderSpecificEntityManagerInterface(PersistenceProvider.Constants.HIBERNATE_ENTITY_MANAGER_INTERFACE); |
| 57 | + |
| 58 | + assertThat(PersistenceProvider.fromEntityManager(em), is(PersistenceProvider.HIBERNATE)); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @see DATAJPA-444 |
| 63 | + */ |
| 64 | + @Test |
| 65 | + public void detectsHibernatePersistenceProviderForHibernateVersionGreaterEqual4dot3() throws Exception { |
| 66 | + |
| 67 | + shadowingClassLoader.excludePackage("org.hibernate"); |
| 68 | + |
| 69 | + EntityManager em = mockProviderSpecificEntityManagerInterface(PersistenceProvider.Constants.HIBERNATE43_ENTITY_MANAGER_INTERFACE); |
| 70 | + |
| 71 | + assertThat(PersistenceProvider.fromEntityManager(em), is(PersistenceProvider.HIBERNATE)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void detectsOpenJpaPersistenceProvider() throws Exception { |
| 76 | + |
| 77 | + shadowingClassLoader.excludePackage("org.apache.openjpa.persistence"); |
| 78 | + |
| 79 | + EntityManager em = mockProviderSpecificEntityManagerInterface(PersistenceProvider.Constants.OPENJPA_ENTITY_MANAGER_INTERFACE); |
| 80 | + |
| 81 | + assertThat(PersistenceProvider.fromEntityManager(em), is(PersistenceProvider.OPEN_JPA)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void detectsEclipseLinkPersistenceProvider() throws Exception { |
| 86 | + |
| 87 | + shadowingClassLoader.excludePackage("org.eclipse.persistence.jpa"); |
| 88 | + |
| 89 | + EntityManager em = mockProviderSpecificEntityManagerInterface(PersistenceProvider.Constants.ECLIPSELINK_ENTITY_MANAGER_INTERFACE); |
| 90 | + |
| 91 | + assertThat(PersistenceProvider.fromEntityManager(em), is(PersistenceProvider.ECLIPSELINK)); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void fallbackToGenericJpaForUnknownPersistenceProvider() throws Exception { |
| 96 | + |
| 97 | + EntityManager em = mockProviderSpecificEntityManagerInterface("foo.bar.unknown.jpa.JpaEntityManager"); |
| 98 | + |
| 99 | + assertThat(PersistenceProvider.fromEntityManager(em), is(PersistenceProvider.GENERIC_JPA)); |
| 100 | + } |
| 101 | + |
| 102 | + private EntityManager mockProviderSpecificEntityManagerInterface(String interfaceName) throws ClassNotFoundException { |
| 103 | + |
| 104 | + Class<?> providerSpecificEntityManagerInterface = InterfaceGenerator.generate(interfaceName, shadowingClassLoader, |
| 105 | + EntityManager.class); |
| 106 | + |
| 107 | + EntityManager em = EntityManager.class.cast(Mockito.mock(providerSpecificEntityManagerInterface)); |
| 108 | + Mockito.when(em.getDelegate()).thenReturn(em); // delegate is used to determine the classloader of the provider |
| 109 | + // specific interface, therefore we return the proxied |
| 110 | + // EntityManager. |
| 111 | + |
| 112 | + return em; |
| 113 | + } |
| 114 | + |
| 115 | + static class InterfaceGenerator implements Opcodes { |
| 116 | + |
| 117 | + public static Class<?> generate(final String interfaceName, ClassLoader parentClassLoader, |
| 118 | + final Class<?>... interfaces) throws ClassNotFoundException { |
| 119 | + |
| 120 | + class CustomClassLoader extends ClassLoader { |
| 121 | + |
| 122 | + public CustomClassLoader(ClassLoader parent) { |
| 123 | + super(parent); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + protected Class<?> findClass(String name) throws ClassNotFoundException { |
| 128 | + |
| 129 | + if (name.equals(interfaceName)) { |
| 130 | + |
| 131 | + byte[] byteCode = generateByteCodeForInterface(interfaceName, interfaces); |
| 132 | + return defineClass(name, byteCode, 0, byteCode.length); |
| 133 | + } |
| 134 | + |
| 135 | + return super.findClass(name); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return new CustomClassLoader(parentClassLoader).loadClass(interfaceName); |
| 140 | + } |
| 141 | + |
| 142 | + private static byte[] generateByteCodeForInterface(final String interfaceName, Class<?>... interfacesToImplement) { |
| 143 | + |
| 144 | + String interfaceResourcePath = ClassUtils.convertClassNameToResourcePath(interfaceName); |
| 145 | + ClassWriter cw = new ClassWriter(false); |
| 146 | + cw.visit(V1_6, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, interfaceResourcePath, null, "java/lang/Object", |
| 147 | + toResourcePaths(interfacesToImplement)); |
| 148 | + cw.visitSource(interfaceResourcePath + ".java", null); |
| 149 | + cw.visitEnd(); |
| 150 | + |
| 151 | + return cw.toByteArray(); |
| 152 | + } |
| 153 | + |
| 154 | + private static String[] toResourcePaths(Class<?>... interfacesToImplement) { |
| 155 | + |
| 156 | + List<String> interfaceResourcePaths = new ArrayList<String>(interfacesToImplement.length); |
| 157 | + for (Class<?> iface : interfacesToImplement) { |
| 158 | + interfaceResourcePaths.add(ClassUtils.convertClassNameToResourcePath(iface.getName())); |
| 159 | + } |
| 160 | + |
| 161 | + return interfaceResourcePaths.toArray(new String[interfaceResourcePaths.size()]); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments