DAO Medge Code Examples
Back to the Main Page

Updates

package au.id.medge.daotester.testsuite;

import au.id.medge.dao.listener.DatabaseActionEvent;
import au.id.medge.dao.utilities.DAOException;
import au.id.medge.tutorial.Person;
import java.sql.SQLException;

public class UpdateTests extends AbstractTestSuite {

    private static final String PERSON_ID = "a29762ce-4067-11ed-bb17-0001c02459a2";

    public UpdateTests() {
        super("Update");
    }

    private Person getPerson() {
        Person p = new Person();
        p.setPersonId(PERSON_ID);
        try {
            return getDAO().selectByKey(p);
        } catch(DAOException | SQLException ex) {
            reportFailure("Can't load person", ex);
        }
        return null;
    }


    private static final String TEST_SURNAME = "Smith";
    private static final String TEST_SURNAME_TEMP = "Jones";

    private void updatePersonInternal() {
        Person p = getPerson();
        p.setSurname(TEST_SURNAME_TEMP);
        try {
            p.update();
            p = getPerson();
            if (!p.getSurname().equalsIgnoreCase(TEST_SURNAME_TEMP)) {
                reportFailure("Failed person update internal");
            }
            disconnect();
        } catch(DAOException | SQLException ex) {
            reportFailure("Failed person update internal", ex);
        }
    }

    private void updatePerson() {
        Person p = getPerson();
        p.setSurname(TEST_SURNAME);
        try {
            p = getDAO().update(p);
            if (!p.getSurname().equalsIgnoreCase(TEST_SURNAME)) {
                reportFailure("Failed person update via DAO");
            }
            disconnect();
        } catch(DAOException | SQLException ex) {
            reportFailure("Failed person update via DAO", ex);
        }
    }

    private void updatePersonListeners() {
        getDAO().addDatabaseListener((DatabaseActionEvent e) -> {
            Person po = (Person)e.getObject();
            Person pd = (Person)e.getDatabaseObject();
            if (po == null || pd == null) {
                reportFailure("Missing objects");
            } else {
                reportSuccess(po.getSurname() + ":" + pd.getSurname());
            }
        });
        Person p = getPerson();
        p.setSurname(TEST_SURNAME_TEMP);
        try {
            p = getDAO().update(p);
            p.setSurname(TEST_SURNAME);
            p.update();
            disconnect();
        } catch(DAOException | SQLException ex) {
            reportFailure("Failed person update testing listeners", ex);
        }
    }

    @Override
    public void runTestSuite() {
        updatePersonInternal();
        updatePerson();
        updatePersonListeners();
    }

}
Back to the Main Page