Persistence { public static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName) { return createEntityManagerFactory(persistenceUnitName, Collections.emptyMap()); } private  Persistence(); static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName); static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName,
                                                                  final Map<String, String> properties); static PersistenceUtil getPersistenceUtil();  }
@Test void createEmfWithPropertiesInstantiatesPersistenceProviderFromProperties() { DefaultPersistenceProviderResolver.registerPersistenceProviderClass(TestPersistenceProvider.class); final Map<String, String> props = Collections.singletonMap(PersistenceProperties.JPA_PERSISTENCE_PROVIDER, TestPersistenceProvider.class.getName()); Persistence.createEntityManagerFactory("testPU", props); assertNotNull(TestPersistenceProvider.instance); assertEquals(1, TestPersistenceProvider.instance.createEmfCalled); }
@Test void createEmfThrowsIllegalArgumentWhenProviderIsNotConfigured() { DefaultPersistenceProviderResolver.registerPersistenceProviderClass(TestPersistenceProvider.class); final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Persistence.createEntityManagerFactory("testPU", Collections.emptyMap())); assertEquals("Missing persistence unit provider.", ex.getMessage()); }
@Test void createEmfThrowsIllegalArgumentWhenConfiguredClassIsNotPersistenceProvider() { final Map<String, String> props = Collections.singletonMap(PersistenceProperties.JPA_PERSISTENCE_PROVIDER, PersistenceTest.class.getName()); final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Persistence.createEntityManagerFactory("testPU", props)); assertThat(ex.getMessage(), containsString( "Type " + PersistenceTest.class.getName() + " is not a PersistenceProvider implementation")); }