Search in sources :

Example 16 with EntityManagerFactoryImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testServerDetectionLogging.

/**
 * Bug #476018
 * This test verifies that EclipseLink prints server detection related log
 * messages at appropriate level - FINE in session log, FINER in other cases
 */
public void testServerDetectionLogging() {
    if (isOnServer()) {
        return;
    }
    closeEntityManagerFactory();
    SessionLog original = AbstractSessionLog.getLog();
    try {
        AbstractSessionLog.setLog(new LogWrapper());
        // check session log for "Configured server platform message"
        Map<String, Object> properties = new HashMap<>();
        properties.putAll(JUnitTestCaseHelper.getDatabaseProperties());
        properties.put(PersistenceUnitProperties.LOGGING_LEVEL, original.getLevelString());
        properties.put(PersistenceUnitProperties.LOGGING_LOGGER, LogWrapper.class.getName());
        EntityManagerFactoryImpl emf = (EntityManagerFactoryImpl) Persistence.createEntityManagerFactory(getPersistenceUnitName(), properties);
        emf.refreshMetadata(properties);
        SimpleSessionLogWrapper wr = (SimpleSessionLogWrapper) emf.getServerSession().getSessionLog();
        assertEquals("configured_server_platform should be printed at FINE level", wr.getLevel() <= SessionLog.FINE, wr.expected());
        // it may happen that logging is not fully configured as that happens
        // after detecting server platform; in such case, messages are printed out
        // by an instance of DefaultSessionLog
        LogWrapper lw = new LogWrapper("detect_server_platform");
        AbstractSessionLog.setLog(lw);
        AbstractSessionLog.getLog().setSession(null);
        lw.setSession(null);
        ServerPlatformUtils.detectServerPlatform(null);
        assertEquals("detect_server_platform should be printed at FINER level", lw.getLevel() <= SessionLog.FINER, lw.expected());
        lw = new LogWrapper("detect_server_platform");
        AbstractSessionLog.setLog(lw);
        Session ss = wr.getSession();
        AbstractSessionLog.getLog().setSession(ss);
        AbstractSessionLog.getLog().getSession().setSessionLog(lw);
        lw.setSession(ss);
        ServerPlatformUtils.detectServerPlatform((ServerSession) ss);
        assertEquals("detect_server_platform should be printed at FINER level", lw.getLevel() <= SessionLog.FINER, lw.expected());
    } finally {
        AbstractSessionLog.setLog(original);
        closeEntityManagerFactory();
    }
}
Also used : SessionLog(org.eclipse.persistence.logging.SessionLog) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) HashMap(java.util.HashMap) SimpleSessionLogWrapper(org.eclipse.persistence.testing.tests.weaving.SimpleSessionLogWrapper) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) SimpleSessionLogWrapper(org.eclipse.persistence.testing.tests.weaving.SimpleSessionLogWrapper) ClientSession(org.eclipse.persistence.sessions.server.ClientSession) Session(org.eclipse.persistence.sessions.Session) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 17 with EntityManagerFactoryImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.

the class AdvancedMultiTenantSchemaJunitTest method testPolicyConfigurationDefault.

public void testPolicyConfigurationDefault() {
    if (!getPlatform().isMySQL()) {
        warning("this test is supported on MySQL only");
        return;
    }
    if (skipTest) {
        warning("this test requires DB schema created in testSetup to be available.");
        return;
    }
    // default configuration: shared EMF = true, shared cache = false
    // strategy = 'external'
    emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), emfProperties);
    ServerSession session = ((EntityManagerFactoryImpl) emf).getServerSession();
    MultitenantPolicy policy = session.getProject().getMultitenantPolicy();
    assertNotNull("project MultitenantPolicy is null", policy);
    assertTrue("not SchemaPerMultitenantPolicy", policy.isSchemaPerMultitenantPolicy());
    assertTrue("not shared EMF", ((SchemaPerMultitenantPolicy) policy).shouldUseSharedEMF());
    assertFalse("shared cache", ((SchemaPerMultitenantPolicy) policy).shouldUseSharedCache());
    assertEquals(PersistenceUnitProperties.MULTITENANT_SCHEMA_PROPERTY_DEFAULT, ((SchemaPerMultitenantPolicy) policy).getContextProperty());
    assertEquals(EntityManagerProperties.MULTITENANT_SCHEMA_PROPERTY_DEFAULT, ((SchemaPerMultitenantPolicy) policy).getContextProperty());
    assertTrue("unknown context property", session.getMultitenantContextProperties().contains(PersistenceUnitProperties.MULTITENANT_SCHEMA_PROPERTY_DEFAULT));
}
Also used : ServerSession(org.eclipse.persistence.sessions.server.ServerSession) SchemaPerMultitenantPolicy(org.eclipse.persistence.descriptors.SchemaPerMultitenantPolicy) MultitenantPolicy(org.eclipse.persistence.descriptors.MultitenantPolicy) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)

Example 18 with EntityManagerFactoryImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testEMFClose.

public void testEMFClose() {
    // This test tests the bug fix for 260511
    // The NPE would be thrown if the EnityManager
    // was created through the constructor
    String errorMsg = "";
    EntityManagerFactory em = new EntityManagerFactoryImpl(JUnitTestCase.getServerSession());
    try {
        em.close();
    } catch (RuntimeException ex) {
        errorMsg = "EMFClose: " + ex.getMessage() + ";";
    }
    if (errorMsg.length() > 0) {
        fail(errorMsg);
    }
}
Also used : EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)

Example 19 with EntityManagerFactoryImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.

the class TestJNDIConnector method testWasTargetServerLookupType.

@Test
public void testWasTargetServerLookupType() {
    ServerSession session = ((EntityManagerFactoryImpl) wasEmf).getServerSession();
    _connector.connect(new Properties(), session);
    Assert.assertEquals(String.class, _handler.getParamType());
}
Also used : ServerSession(org.eclipse.persistence.sessions.server.ServerSession) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) Properties(java.util.Properties) Test(org.junit.Test)

Example 20 with EntityManagerFactoryImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.

the class DynamicTestHelper method createEMF.

public static EntityManagerFactory createEMF(String emName) {
    PersistenceContentHandler myContentHandler = new PersistenceContentHandler();
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser sp = spf.newSAXParser();
        XMLReader xmlReader = sp.getXMLReader();
        XMLExceptionHandler xmlErrorHandler = new XMLExceptionHandler();
        xmlReader.setErrorHandler(xmlErrorHandler);
        xmlReader.setContentHandler(myContentHandler);
        InputSource inputSource = new InputSource(new StringReader(DYNAMIC_PERSISTENCE_XML));
        xmlReader.parse(inputSource);
    } catch (Exception e) {
        return null;
    }
    // only ever one
    final SEPersistenceUnitInfo puInfo = myContentHandler.getPersistenceUnits().get(0);
    puInfo.setPersistenceUnitRootUrl(dynamicTestUrl);
    PersistenceProvider provider = new PersistenceProvider() {

        @Override
        public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
            return null;
        }

        @Override
        public EntityManagerFactory createEntityManagerFactory(String emName, Map map) {
            if (emName.equals(puInfo.getPersistenceUnitName())) {
                EntityManagerSetupImpl entityManagerSetupImpl = new EntityManagerSetupImpl(DYNAMIC_PERSISTENCE_NAME, DYNAMIC_PERSISTENCE_NAME);
                map.put(PersistenceUnitProperties.WEAVING, "static");
                puInfo.getProperties().put(PersistenceUnitProperties.EXCLUDE_ECLIPSELINK_ORM_FILE, "true");
                entityManagerSetupImpl.predeploy(puInfo, map);
                return new EntityManagerFactoryImpl(entityManagerSetupImpl, map);
            } else {
                return null;
            }
        }

        @Override
        public ProviderUtil getProviderUtil() {
            return null;
        }

        @Override
        public void generateSchema(PersistenceUnitInfo info, Map map) {
        }

        @Override
        public boolean generateSchema(String persistenceUnitName, Map map) {
            // TODO Auto-generated method stub
            return false;
        }
    };
    return provider.createEntityManagerFactory(emName, getDatabaseProperties());
}
Also used : InputSource(org.xml.sax.InputSource) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PersistenceContentHandler(org.eclipse.persistence.internal.jpa.deployment.xml.parser.PersistenceContentHandler) StringReader(java.io.StringReader) EntityManagerSetupImpl(org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl) SAXParser(javax.xml.parsers.SAXParser) XMLExceptionHandler(org.eclipse.persistence.internal.jpa.deployment.xml.parser.XMLExceptionHandler) Map(java.util.Map) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

EntityManagerFactoryImpl (org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)22 ServerSession (org.eclipse.persistence.sessions.server.ServerSession)10 HashMap (java.util.HashMap)9 Test (org.junit.Test)8 EntityManager (jakarta.persistence.EntityManager)5 Properties (java.util.Properties)5 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)4 EntityManagerSetupImpl (org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl)4 EntityTransaction (jakarta.persistence.EntityTransaction)3 PersistenceException (jakarta.persistence.PersistenceException)3 Map (java.util.Map)3 PersistenceUnitLoadingException (org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)3 SEPersistenceUnitInfo (org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo)3 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)3 Dog (org.eclipse.persistence.jpa.test.basic.model.Dog)3 Person (org.eclipse.persistence.jpa.test.basic.model.Person)3 XmlFish (org.eclipse.persistence.jpa.test.basic.model.XmlFish)3 ConnectionPolicy (org.eclipse.persistence.sessions.server.ConnectionPolicy)3 RollbackException (jakarta.persistence.RollbackException)2 MultitenantPolicy (org.eclipse.persistence.descriptors.MultitenantPolicy)2