|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, |
| 7 | + * or the Eclipse Distribution License v. 1.0 which is available at |
| 8 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 11 | + */ |
| 12 | +package org.eclipse.persistence.jpa; |
| 13 | + |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import jakarta.persistence.EntityManagerFactory; |
| 17 | +import jakarta.persistence.PersistenceConfiguration; |
| 18 | +import jakarta.persistence.spi.PersistenceUnitInfo; |
| 19 | +import jakarta.persistence.spi.ProviderUtil; |
| 20 | +import org.eclipse.persistence.config.PersistenceUnitProperties; |
| 21 | +import org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 26 | + |
| 27 | +public class PersistenceProviderTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void testCheckForProviderPropertyWithoutProperty() { |
| 31 | + Map<String, Object> properties = Map.of(); |
| 32 | + boolean result = PersistenceProvider.checkForProviderProperty(properties); |
| 33 | + assertTrue(result); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testCheckForProviderPropertyWithProviderClass() { |
| 38 | + Map<String, Object> properties = Map.of( |
| 39 | + PersistenceUnitProperties.PROVIDER, PersistenceProvider.class |
| 40 | + ); |
| 41 | + boolean result = PersistenceProvider.checkForProviderProperty(properties); |
| 42 | + assertTrue(result); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testCheckForProviderPropertyWithProviderString() { |
| 47 | + Map<String, Object> properties = Map.of( |
| 48 | + PersistenceUnitProperties.PROVIDER, PersistenceProvider.class.getName() |
| 49 | + ); |
| 50 | + boolean result = PersistenceProvider.checkForProviderProperty(properties); |
| 51 | + assertTrue(result); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testCheckForProviderPropertyWithEMFProviderClass() { |
| 56 | + Map<String, Object> properties = Map.of( |
| 57 | + PersistenceUnitProperties.PROVIDER, EntityManagerFactoryProvider.class |
| 58 | + ); |
| 59 | + boolean result = PersistenceProvider.checkForProviderProperty(properties); |
| 60 | + assertTrue(result); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testCheckForProviderPropertyWithEMFProviderString() { |
| 65 | + Map<String, Object> properties = Map.of( |
| 66 | + PersistenceUnitProperties.PROVIDER, EntityManagerFactoryProvider.class.getName() |
| 67 | + ); |
| 68 | + boolean result = PersistenceProvider.checkForProviderProperty(properties); |
| 69 | + assertTrue(result); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testCheckForProviderPropertyWithUnsupportedClass() { |
| 74 | + Map<String, Object> properties = Map.of( |
| 75 | + PersistenceUnitProperties.PROVIDER, Unsupported.class |
| 76 | + ); |
| 77 | + boolean result = PersistenceProvider.checkForProviderProperty(properties); |
| 78 | + assertFalse(result); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testCheckForProviderPropertyWithUnsupportedString() { |
| 83 | + Map<String, Object> properties = Map.of( |
| 84 | + PersistenceUnitProperties.PROVIDER, Unsupported.class.getName() |
| 85 | + ); |
| 86 | + boolean result = PersistenceProvider.checkForProviderProperty(properties); |
| 87 | + assertFalse(result); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void testIsProviderEclipseLinkWithoutProvider() { |
| 92 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 93 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 94 | + assertTrue(result); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testIsProviderEclipseLinkWithProvider() { |
| 99 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 100 | + configuration.provider(PersistenceProvider.class.getName()); |
| 101 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 102 | + assertTrue(result); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testIsProviderEclipseLinkWithEMFProvider() { |
| 107 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 108 | + configuration.provider(EntityManagerFactoryProvider.class.getName()); |
| 109 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 110 | + assertTrue(result); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testIsProviderEclipseLinkWithUnsupportedProvider() { |
| 115 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 116 | + configuration.provider(Unsupported.class.getName()); |
| 117 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 118 | + assertFalse(result); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void testIsProviderEclipseLinkWithProviderStringProperty() { |
| 123 | + Map<String, Object> properties = Map.of( |
| 124 | + PersistenceUnitProperties.PROVIDER, PersistenceProvider.class.getName() |
| 125 | + ); |
| 126 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 127 | + configuration.properties(properties); |
| 128 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 129 | + assertTrue(result); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testIsProviderEclipseLinkWithEMFProviderStringProperty() { |
| 134 | + Map<String, Object> properties = Map.of( |
| 135 | + PersistenceUnitProperties.PROVIDER, EntityManagerFactoryProvider.class.getName() |
| 136 | + ); |
| 137 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 138 | + configuration.properties(properties); |
| 139 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 140 | + assertTrue(result); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void testIsProviderEclipseLinkWithUnsupportedProviderStringProperty() { |
| 145 | + Map<String, Object> properties = Map.of( |
| 146 | + PersistenceUnitProperties.PROVIDER, Unsupported.class.getName() |
| 147 | + ); |
| 148 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 149 | + configuration.properties(properties); |
| 150 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 151 | + assertFalse(result); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void testIsProviderEclipseLinkWithProviderClassProperty() { |
| 156 | + Map<String, Object> properties = Map.of( |
| 157 | + PersistenceUnitProperties.PROVIDER, PersistenceProvider.class |
| 158 | + ); |
| 159 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 160 | + configuration.properties(properties); |
| 161 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 162 | + assertTrue(result); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + void testIsProviderEclipseLinkWithEMFProviderClassProperty() { |
| 167 | + Map<String, Object> properties = Map.of( |
| 168 | + PersistenceUnitProperties.PROVIDER, EntityManagerFactoryProvider.class |
| 169 | + ); |
| 170 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 171 | + configuration.properties(properties); |
| 172 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 173 | + assertTrue(result); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + void testIsProviderEclipseLinkWithUnsupportedProviderClassProperty() { |
| 178 | + Map<String, Object> properties = Map.of( |
| 179 | + PersistenceUnitProperties.PROVIDER, Unsupported.class |
| 180 | + ); |
| 181 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 182 | + configuration.properties(properties); |
| 183 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 184 | + assertFalse(result); |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + @Test |
| 189 | + void testIsProviderEclipseLinkWithProviderStringPropertyOverride() { |
| 190 | + Map<String, Object> properties = Map.of( |
| 191 | + PersistenceUnitProperties.PROVIDER, PersistenceProvider.class.getName() |
| 192 | + ); |
| 193 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 194 | + // Provider is set as unsupported, but supported property must override it |
| 195 | + configuration.provider(Unsupported.class.getName()); |
| 196 | + configuration.properties(properties); |
| 197 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 198 | + assertTrue(result); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + void testIsProviderEclipseLinkWithEMFProviderStringPropertyOverride() { |
| 203 | + Map<String, Object> properties = Map.of( |
| 204 | + PersistenceUnitProperties.PROVIDER, EntityManagerFactoryProvider.class.getName() |
| 205 | + ); |
| 206 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 207 | + // Provider is set as unsupported, but supported property must override it |
| 208 | + configuration.provider(Unsupported.class.getName()); |
| 209 | + configuration.properties(properties); |
| 210 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 211 | + assertTrue(result); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + void testIsProviderEclipseLinkWithUnsupportedProviderStringPropertyOverride() { |
| 216 | + Map<String, Object> properties = Map.of( |
| 217 | + PersistenceUnitProperties.PROVIDER, Unsupported.class.getName() |
| 218 | + ); |
| 219 | + PersistenceConfiguration configuration = new PersistenceConfiguration("Test"); |
| 220 | + // Provider is set as supported, but unsupported property must override it |
| 221 | + configuration.provider(PersistenceProvider.class.getName()); |
| 222 | + configuration.properties(properties); |
| 223 | + boolean result = PersistenceProvider.isProviderEclipseLink(configuration); |
| 224 | + assertFalse(result); |
| 225 | + } |
| 226 | + |
| 227 | + // PersistenceProvider that shall fail the check |
| 228 | + private static final class Unsupported implements jakarta.persistence.spi.PersistenceProvider { |
| 229 | + |
| 230 | + @Override |
| 231 | + public EntityManagerFactory createEntityManagerFactory(String emName, Map<?, ?> map) { |
| 232 | + return null; |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public EntityManagerFactory createEntityManagerFactory(PersistenceConfiguration configuration) { |
| 237 | + return null; |
| 238 | + } |
| 239 | + |
| 240 | + @Override |
| 241 | + public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map<?, ?> map) { |
| 242 | + return null; |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + public void generateSchema(PersistenceUnitInfo info, Map<?, ?> map) { |
| 247 | + |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + public boolean generateSchema(String persistenceUnitName, Map<?, ?> map) { |
| 252 | + return false; |
| 253 | + } |
| 254 | + |
| 255 | + @Override |
| 256 | + public ProviderUtil getProviderUtil() { |
| 257 | + return null; |
| 258 | + } |
| 259 | + |
| 260 | + } |
| 261 | + |
| 262 | +} |
0 commit comments