Search in sources :

Example 26 with Address

use of ca.bc.gov.hlth.hnweb.model.v3.Address in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testNoPersistOnFlushProperties.

// Test Not using the persist operation on commit.
public void testNoPersistOnFlushProperties() {
    // Properties only works in jse.
    if (isOnServer()) {
        return;
    }
    Map properties = new HashMap();
    properties.put(EntityManagerProperties.PERSISTENCE_CONTEXT_COMMIT_WITHOUT_PERSIST_RULES, "true");
    EntityManager em = createEntityManager(properties);
    beginTransaction(em);
    Employee employee = new Employee();
    employee.setLastName("SomeName");
    Address addr = new Address();
    addr.setCity("Douglas");
    try {
        em.persist(employee);
        em.flush();
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(employee);
        closeEntityManager(em);
        em = createEntityManager(properties);
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        employee.setAddress(addr);
        addr.getEmployees().add(employee);
        em.flush();
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(employee);
        closeEntityManager(em);
        em = createEntityManager(properties);
        clearCache();
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        employee.getAddress().setCountry("country");
        employee.getAddress().getEmployees().size();
        employee.setAddress((Address) null);
        em.remove(employee);
        commitTransaction(em);
        em = createEntityManager(properties);
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        assertNull("Employee Not Deleted", employee);
        commitTransaction(em);
        closeEntityManager(em);
    } catch (RuntimeException exception) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw exception;
    } finally {
        try {
            em = createEntityManager();
            clearCache();
            beginTransaction(em);
            em.remove(em.find(Address.class, addr.getID()));
            commitTransaction(em);
        } catch (RuntimeException ex) {
        // ignore
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee) Address(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 27 with Address

use of ca.bc.gov.hlth.hnweb.model.v3.Address in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method internalTestPostAcquirePreReleaseEvents.

public void internalTestPostAcquirePreReleaseEvents(boolean useExternalConnectionPool) {
    if (isOnServer()) {
        // Uses DefaultConnector.
        return;
    }
    SessionBroker broker = ((JpaEntityManagerFactory) getEntityManagerFactory()).getSessionBroker();
    // Testing ExclusiveConnectionMode.Isolated requires a session that has isolated descriptors.
    ServerSession ss = (ServerSession) broker.getSessionForClass(Address.class);
    if (ss.getPlatform().isSybase()) {
        warning("Warning Sybase Driver does not work with DriverWrapper, testPostAcquirePreReleaseEvents can't run on this platform.");
        return;
    }
    if (ss.getPlatform().isSymfoware()) {
        warning("Test testPostAcquirePreReleaseEvents skipped for this platform, " + "Symfoware platform doesn't support failover.");
        return;
    }
    // normally false; set to true for debug output for just this single test
    boolean shouldForceFinest = false;
    int originalLogLevel = -1;
    // cache the original driver name and connection string.
    String originalDriverName = ss.getLogin().getDriverClassName();
    String originalConnectionString = ss.getLogin().getConnectionString();
    // cache original connector for external connection pool case
    Connector originalConnector = ss.getLogin().getConnector();
    // the new driver name and connection string to be used by the test
    String newDriverName = DriverWrapper.class.getName();
    String newConnectionString = DriverWrapper.codeUrl(originalConnectionString);
    // setup the wrapper driver
    DriverWrapper.initialize(originalDriverName);
    // The test need to connect with the new driver and connection string.
    // That could be done in JPA:
    // // close the existing emf
    // closeEntityManagerFactory();
    // HashMap properties = new HashMap(JUnitTestCaseHelper.getDatabaseProperties());
    // properties.put(PersistenceUnitProperties.JDBC_DRIVER, newDriverName);
    // properties.put(PersistenceUnitProperties.JDBC_URL, newConnectionString);
    // emf = getEntityManagerFactory(properties);
    // However this only works in case closeEntityManagerFactory disconnects the original ServerSession,
    // which requires the factory to be the only one using the persistence unit.
    // Alternative - and faster - approach is to disconnect the original session directly
    // and then reconnected it with the new driver and connection string.
    broker.logout();
    if (useExternalConnectionPool) {
        ss.getLogin().setConnector(new JNDIConnector(new DataSourceImpl(null, newConnectionString, null, null)));
        ss.getLogin().useExternalConnectionPooling();
    } else {
        ss.getLogin().setDriverClassName(newDriverName);
        ss.getLogin().setConnectionString(newConnectionString);
    }
    if (shouldForceFinest) {
        if (broker.getLogLevel() != SessionLog.FINEST) {
            originalLogLevel = broker.getLogLevel();
            broker.setLogLevel(SessionLog.FINEST);
        }
    }
    // switch off reconnection
    boolean originalIsConnectionHealthValidatedOnError = ss.getLogin().isConnectionHealthValidatedOnError();
    ss.getLogin().setConnectionHealthValidatedOnError(false);
    // Using DriverWrapper the listener will repair connection on postAcquireConnection and break it on preReleaseConnection event.
    // Also the listener will verify that neither postAcquireConnection nor preReleaseConnection events not called two in a row.
    AcquireRepair_ReleaseBreak_Listener listener = new AcquireRepair_ReleaseBreak_Listener();
    ss.getEventManager().addListener(listener);
    // Driver's connect method will still work, however any method called on any acquired connection will throw SQLException.
    // On postAcquireConnection connection will be repaired; on preReleaseConnection - broken again.
    broker.log(SessionLog.FINEST, SessionLog.CONNECTION, "testPostAcquirePreReleaseEvents: DriverWrapper.breakOldConnections(); DriverWrapper.breakNewConnections();", null, null, false);
    DriverWrapper.breakOldConnections();
    DriverWrapper.breakNewConnections();
    broker.login();
    // test several configurations:
    // all exclusive connection modes
    String[] exclusiveConnectionModeArray = new String[] { ExclusiveConnectionMode.Transactional, ExclusiveConnectionMode.Isolated, ExclusiveConnectionMode.Always };
    // Normally the user wishing to use not pooled connection would specify user and password properties (and possibly db url, too).
    // However if these properties have the same values that those in the logging then no non pooled connection is created and the pooled one used instead.
    // In the test we must use the same user as already in the session login (don't know any others) that forces usage of ConnectionPolicy property.
    ConnectionPolicy connectionPolicy = (ConnectionPolicy) ss.getDefaultConnectionPolicy().clone();
    connectionPolicy.setLogin(ss.getLogin());
    connectionPolicy.setPoolName(null);
    try {
        HashMap emProperties = new HashMap(1);
        HashMap mapOfProperties = new HashMap(1);
        emProperties.put(PersistenceUnitProperties.COMPOSITE_UNIT_PROPERTIES, mapOfProperties);
        HashMap memberProperties = new HashMap();
        mapOfProperties.put(ss.getName(), memberProperties);
        String mode, pooled = "", exclusiveConnectionMode;
        for (int k = 0; k < 2; k++) {
            if (k == 1) {
                // use non pooled connections
                pooled = "non pooled; ";
                memberProperties.put(EntityManagerProperties.CONNECTION_POLICY, connectionPolicy);
            }
            for (int i = 0; i < exclusiveConnectionModeArray.length; i++) {
                exclusiveConnectionMode = exclusiveConnectionModeArray[i];
                for (int j = 0; j < 3; j++) {
                    // either beginning early transaction or not
                    boolean shouldBeginEarlyTransaction = (j == 2);
                    boolean shouldReadBeforeTransaction = (j == 1);
                    mode = pooled + exclusiveConnectionMode + (shouldBeginEarlyTransaction ? "; beginEarlyTransaction" : "") + (shouldReadBeforeTransaction ? "; readBeforeTransaction" : "");
                    broker.log(SessionLog.FINEST, SessionLog.CONNECTION, "testPostAcquirePreReleaseEvents: " + mode, null, null, false);
                    memberProperties.put(EntityManagerProperties.EXCLUSIVE_CONNECTION_MODE, exclusiveConnectionMode);
                    EntityManager em = createEntityManager(emProperties);
                    if (shouldReadBeforeTransaction) {
                        em.find(Address.class, 1);
                    }
                    Address address = null;
                    try {
                        em.getTransaction().begin();
                        if (shouldBeginEarlyTransaction) {
                            em.unwrap(UnitOfWorkImpl.class).beginEarlyTransaction();
                        }
                        address = new Address();
                        address.setCountry("testPostAcquirePreReleaseEvents");
                        em.persist(address);
                        em.getTransaction().commit();
                    } finally {
                        // expected exception - connection is invalid and cannot be reconnected.
                        if (em.getTransaction().isActive()) {
                            em.getTransaction().rollback();
                        }
                        closeEntityManager(em);
                    }
                    if (listener.hasAcquiredConnections()) {
                        fail(mode + " connection was not passed to preReleaseConnection event");
                    }
                }
            }
        }
    } finally {
        // clear the driver wrapper
        DriverWrapper.clear();
        // reconnect the session using the original driver and connection string
        ss.getEventManager().removeListener(listener);
        // clean-up
        // remove the inserted object
        EntityManager em = createEntityManager();
        em.getTransaction().begin();
        try {
            em.createQuery("DELETE FROM Address a WHERE a.country = 'testPostAcquirePreReleaseEvents'");
            em.getTransaction().commit();
        } finally {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            closeEntityManager(em);
            broker.logout();
            if (originalLogLevel >= 0) {
                broker.setLogLevel(originalLogLevel);
            }
            if (useExternalConnectionPool) {
                ss.getLogin().setConnector(originalConnector);
                ss.getLogin().dontUseExternalConnectionPooling();
            } else {
                ss.getLogin().setDriverClassName(originalDriverName);
                ss.getLogin().setConnectionString(originalConnectionString);
            }
            ss.getLogin().setConnectionHealthValidatedOnError(originalIsConnectionHealthValidatedOnError);
            broker.login();
        }
    }
}
Also used : Connector(org.eclipse.persistence.sessions.Connector) JNDIConnector(org.eclipse.persistence.sessions.JNDIConnector) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) Address(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address) HashMap(java.util.HashMap) DataSourceImpl(org.eclipse.persistence.internal.jpa.jdbc.DataSourceImpl) SessionBroker(org.eclipse.persistence.sessions.broker.SessionBroker) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) JNDIConnector(org.eclipse.persistence.sessions.JNDIConnector) ConnectionPolicy(org.eclipse.persistence.sessions.server.ConnectionPolicy)

Example 28 with Address

use of ca.bc.gov.hlth.hnweb.model.v3.Address in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testClassInstanceConverter.

public void testClassInstanceConverter() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Address add = new Address();
    add.setCity("St. Louis");
    add.setType(new Bungalow());
    em.persist(add);
    commitTransaction(em);
    int assignedSequenceNumber = add.getID();
    em.clear();
    getDatabaseSession().getIdentityMapAccessor().initializeAllIdentityMaps();
    add = em.find(Address.class, assignedSequenceNumber);
    assertTrue("Did not correctly persist a mapping using a class-instance converter", (add.getType() instanceof Bungalow));
    beginTransaction(em);
    add = em.find(Address.class, assignedSequenceNumber);
    em.remove(add);
    commitTransaction(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Address(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address) Bungalow(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Bungalow)

Example 29 with Address

use of ca.bc.gov.hlth.hnweb.model.v3.Address in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testIsLoadedWithoutReferenceAttribute.

public void testIsLoadedWithoutReferenceAttribute() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp = new Employee();
        emp.setFirstName("Abe");
        emp.setLastName("Jones");
        Address addr = new Address();
        addr.setCity("Palo Alto");
        emp.setAddress(addr);
        PhoneNumber pn = new PhoneNumber();
        pn.setNumber("1234456");
        pn.setType("Home");
        emp.addPhoneNumber(pn);
        pn.setOwner(emp);
        Employee manager = new Employee();
        manager.addManagedEmployee(emp);
        emp.setManager(manager);
        em.persist(emp);
        em.flush();
        em.clear();
        clearCache();
        emp = em.find(Employee.class, emp.getId());
        emp.getAddress().getCity();
        emp.getPhoneNumbers().size();
        ProviderUtil util = (new PersistenceProvider()).getProviderUtil();
        if (emp instanceof PersistenceWeaved) {
            assertTrue("ProviderUtil did not return LOADED for isLoaded for address when it should.", util.isLoadedWithReference(emp, "address").equals(LoadState.LOADED));
            assertTrue("ProviderUtil did not return NOT_LOADED for isLoaded for manager when it should.", util.isLoadedWithReference(emp, "manager").equals(LoadState.NOT_LOADED));
        } else {
            assertTrue("(Unweaved) ProviderUtil did not return LOADED for isLoaded for address when it should.", util.isLoadedWithReference(emp, "address").equals(LoadState.LOADED));
            assertTrue("(Unweaved) ProviderUtil did not return LOADED for isLoaded for manager when it should.", util.isLoadedWithReference(emp, "manager").equals(LoadState.LOADED));
        }
        assertTrue("ProviderUtil did not return LOADED for isLoaded for phoneNumbers when it should.", util.isLoadedWithReference(emp, "phoneNumbers").equals(LoadState.LOADED));
        assertTrue("ProviderUtil did not return NOT_LOADED for isLoaded for projects when it should.", util.isLoadedWithReference(emp, "projects").equals(LoadState.NOT_LOADED));
        assertTrue("ProviderUtil did not return UNKNOWN for isLoaded when it should.", util.isLoaded(new NonEntity()).equals(LoadState.UNKNOWN));
    } finally {
        rollbackTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ProviderUtil(jakarta.persistence.spi.ProviderUtil) Employee(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee) PersistenceWeaved(org.eclipse.persistence.internal.weaving.PersistenceWeaved) Address(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) PhoneNumber(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber) NonEntity(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.NonEntity)

Example 30 with Address

use of ca.bc.gov.hlth.hnweb.model.v3.Address in project java-compute by googleapis.

the class ITAddressesTest method testCRD.

@Test
public void testCRD() {
    insertAddress();
    Address address = addressesClient.get(DEFAULT_PROJECT, DEFAULT_REGION, name);
    Assert.assertEquals(name, address.getName());
    Assert.assertEquals("test", address.getDescription());
}
Also used : Address(com.google.cloud.compute.v1.Address) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)91 Address (org.orcid.jaxb.model.record_v2.Address)82 Address (org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address)45 Addresses (org.orcid.jaxb.model.record_v2.Addresses)43 ResearcherUrl (org.orcid.jaxb.model.record_v2.ResearcherUrl)43 Keyword (org.orcid.jaxb.model.record_v2.Keyword)42 OtherName (org.orcid.jaxb.model.record_v2.OtherName)42 PersonExternalIdentifier (org.orcid.jaxb.model.record_v2.PersonExternalIdentifier)42 Email (org.orcid.jaxb.model.record_v2.Email)41 EntityManager (jakarta.persistence.EntityManager)36 OtherNames (org.orcid.jaxb.model.record_v2.OtherNames)35 Biography (org.orcid.jaxb.model.record_v2.Biography)34 Emails (org.orcid.jaxb.model.record_v2.Emails)34 ResearcherUrls (org.orcid.jaxb.model.record_v2.ResearcherUrls)33 Keywords (org.orcid.jaxb.model.record_v2.Keywords)32 PersonExternalIdentifiers (org.orcid.jaxb.model.record_v2.PersonExternalIdentifiers)32 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)31 Name (org.orcid.jaxb.model.record_v2.Name)31 Person (org.orcid.jaxb.model.record_v2.Person)31 Employee (org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee)26