DAO Medge Code Examples
Back to the Main Page

All Selects

Pretty much all of the selects.

package au.id.medge.daotester.testsuite;

import au.id.medge.dao.BaseDatabaseObject;
import au.id.medge.dao.DAO;
import au.id.medge.dao.annotations.daoproperty;
import au.id.medge.dao.annotations.daotable;
import au.id.medge.dao.query.AggregatedObject;
import au.id.medge.dao.query.Filter;
import au.id.medge.dao.query.ListFilter;
import au.id.medge.dao.query.OrderBy;
import au.id.medge.dao.query.PropertyFilter;
import au.id.medge.dao.query.SelectColumn;
import au.id.medge.dao.utilities.DAOException;
import au.id.medge.tutorial.ContactInfo;
import au.id.medge.tutorial.Country;
import au.id.medge.tutorial.Person;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FullSelectTest extends AbstractTestSuite {

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

    private static final String TEST_GIVENS = "Colin";
    private static final String OTHER_TEST_GIVENS = "Fred";

    private static final String TEST_COUNTRY_ID = "AUSTRALIA";
    
    public FullSelectTest() {
        super("Full Select Tests");
    }

    private void testSelectClass() {
        try {
            List resultSet = getDAO().select(Person.class);
            if (resultSet.isEmpty()) {
                reportFailure("Select class failed");
            } else {
                reportSuccess("Select class succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectDAOTable() {
        try {
            daotable table = getDAO().getTable(Person.class);
            List resultSet = getDAO().select(table);
            if (resultSet.isEmpty()) {
                reportFailure("Select daotable failed");
            } else {
                reportSuccess("Select daotable succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectObject() {
        try {
            Person person = new Person();
            List resultSet = getDAO().select(person);
            if (resultSet.isEmpty()) {
                reportFailure("Select all objects failed");
            } else {
                reportSuccess("Select all objects succeeded");
            }
            int allCount = resultSet.size();
            person.setSurname(TEST_SURNAME);
            resultSet = getDAO().select(person);
            if (resultSet.isEmpty()) {
                reportFailure("Select some objects failed");
            } else {
                reportSuccess("Select some objects succeeded");
            }
            if (resultSet.size() > allCount) {
                reportFailure("Select some objects failed to filter");
            } else {
                reportSuccess("Select some objects succeeded with filter");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectObjectSort() {
        try {
            Person person = new Person();
            person.setSurname(TEST_SURNAME);
            daoproperty property = getDAO().getProperty(Person.class, Person.GIVENS);
            OrderBy orderBy = new OrderBy(property);
            List resultSet = getDAO().select(person, orderBy);
            Person firstPerson = resultSet.get(0);
            Person secondPerson = resultSet.get(1);
            if (firstPerson.getGivens().compareTo(secondPerson.getGivens()) > 0) {
                reportFailure("Select objects sorted failed");
            } else {
                reportSuccess("Select objects sorted succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectTableSelect() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty property2 = getDAO().getProperty(Person.class, Person.GIVENS);
            SelectColumn column1 = new SelectColumn(property1);
            SelectColumn column2 = new SelectColumn(property2);
            List resultSet = getDAO().select(table, column1, column2);
            boolean hasOnlySelectColumns = true;
            for (Person person : resultSet) {
                hasOnlySelectColumns &= (person.getGivens() != null && person.getPersonId() == null);
            }
            if (!hasOnlySelectColumns) {
                reportFailure("Select table select specific columns failed");
            } else {
                reportSuccess("Select table select specific columns succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectClassSelect() {
        try {
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty property2 = getDAO().getProperty(Person.class, Person.GIVENS);
            SelectColumn column1 = new SelectColumn(property1);
            SelectColumn column2 = new SelectColumn(property2);
            List resultSet = getDAO().select(Person.class, column1, column2);
            boolean hasOnlySelectColumns = true;
            for (Person person : resultSet) {
                hasOnlySelectColumns &= (person.getGivens() != null && person.getPersonId() == null);
            }
            if (!hasOnlySelectColumns) {
                reportFailure("Select class select specific columns failed");
            } else {
                reportSuccess("Select class select specific columns succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectTableSelectSort() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty property2 = getDAO().getProperty(Person.class, Person.GIVENS);
            SelectColumn column1 = new SelectColumn(property1);
            SelectColumn column2 = new SelectColumn(property2);
            OrderBy orderBy = new OrderBy(property1);
            List resultSet = getDAO().select(table, new SelectColumn[]{column1, column2}, orderBy);
            boolean hasOnlySelectColumns = true;
            for (Person person : resultSet) {
                hasOnlySelectColumns &= (person.getGivens() != null && person.getPersonId() == null);
            }
            boolean inRightOrder = true;
            for (int i = 1; i < resultSet.size(); i++) {
                Person firstPerson = resultSet.get(i - 1);
                Person secondPerson = resultSet.get(i);
                inRightOrder &= (firstPerson.getSurname().compareTo(secondPerson.getSurname()) <= 0);
            }
            if (!hasOnlySelectColumns) {
                reportFailure("Select table select specific columns sort [columns] failed");
            } else {
                reportSuccess("Select table select specific columns sort [columns] succeeded");
            }
            if (!inRightOrder) {
                reportFailure("Select table select specific columns sort [sort] failed");
            } else {
                reportSuccess("Select table select specific columns sort [sort]succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectClassSelectSort() {
        try {
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty property2 = getDAO().getProperty(Person.class, Person.GIVENS);
            SelectColumn column1 = new SelectColumn(property1);
            SelectColumn column2 = new SelectColumn(property2);
            OrderBy orderBy = new OrderBy(property1);
            List resultSet = getDAO().select(Person.class, new SelectColumn[]{column1, column2}, orderBy);
            boolean hasOnlySelectColumns = true;
            for (Person person : resultSet) {
                hasOnlySelectColumns &= (person.getGivens() != null && person.getPersonId() == null);
            }
            boolean inRightOrder = true;
            for (int i = 1; i < resultSet.size(); i++) {
                Person firstPerson = resultSet.get(i - 1);
                Person secondPerson = resultSet.get(i);
                inRightOrder &= (firstPerson.getSurname().compareTo(secondPerson.getSurname()) <= 0);
            }
            if (!hasOnlySelectColumns) {
                reportFailure("Select class select specific columns sort [columns] failed");
            } else {
                reportSuccess("Select class select specific columns sort [columns] succeeded");
            }
            if (!inRightOrder) {
                reportFailure("Select class select specific columns sort [sort] failed");
            } else {
                reportSuccess("Select class select specific columns sort [sort]succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectTableSelectSorts() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty property2 = getDAO().getProperty(Person.class, Person.GIVENS);
            SelectColumn column1 = new SelectColumn(property1);
            SelectColumn column2 = new SelectColumn(property2);
            OrderBy orderBy1 = new OrderBy(property1);
            OrderBy orderBy2 = new OrderBy(property2);
            List resultSet = getDAO().select(table, new SelectColumn[]{column1, column2}, new OrderBy[]{orderBy1, orderBy2});
            boolean hasOnlySelectColumns = true;
            for (Person person : resultSet) {
                hasOnlySelectColumns &= (person.getGivens() != null && person.getPersonId() == null);
            }
            boolean inRightOrder = true;
            for (int i = 1; i < resultSet.size(); i++) {
                Person firstPerson = resultSet.get(i - 1);
                Person secondPerson = resultSet.get(i);
                inRightOrder &= (firstPerson.getSurname().compareTo(secondPerson.getSurname()) <= 0);
                if (firstPerson.getSurname().compareTo(secondPerson.getSurname()) == 0) {
                    inRightOrder &= (firstPerson.getGivens().compareTo(secondPerson.getGivens()) <= 0);
                }
            }
            if (!hasOnlySelectColumns) {
                reportFailure("Select table select specific columns sorts [columns] failed");
            } else {
                reportSuccess("Select table select specific columns sorts [columns] succeeded");
            }
            if (!inRightOrder) {
                reportFailure("Select table select specific columns sorts [sort] failed");
            } else {
                reportSuccess("Select table select specific columns sorts [sort]succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectClassSelectSorts() {
        try {
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty property2 = getDAO().getProperty(Person.class, Person.GIVENS);
            SelectColumn column1 = new SelectColumn(property1);
            SelectColumn column2 = new SelectColumn(property2);
            OrderBy orderBy1 = new OrderBy(property1);
            OrderBy orderBy2 = new OrderBy(property2);
            List resultSet = getDAO().select(Person.class, new SelectColumn[]{column1, column2}, new OrderBy[]{orderBy1, orderBy2});
            boolean hasOnlySelectColumns = true;
            for (Person person : resultSet) {
                hasOnlySelectColumns &= (person.getGivens() != null && person.getPersonId() == null);
            }
            boolean inRightOrder = true;
            for (int i = 1; i < resultSet.size(); i++) {
                Person firstPerson = resultSet.get(i - 1);
                Person secondPerson = resultSet.get(i);
                inRightOrder &= (firstPerson.getSurname().compareTo(secondPerson.getSurname()) <= 0);
                if (firstPerson.getSurname().compareTo(secondPerson.getSurname()) == 0) {
                    inRightOrder &= (firstPerson.getGivens().compareTo(secondPerson.getGivens()) <= 0);
                }
            }
            if (!hasOnlySelectColumns) {
                reportFailure("Select class select specific columns sorts [columns] failed");
            } else {
                reportSuccess("Select class select specific columns sorts [columns] succeeded");
            }
            if (!inRightOrder) {
                reportFailure("Select class select specific columns sorts [sort] failed");
            } else {
                reportSuccess("Select class select specific columns sorts [sort]succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectTableSelectFilter() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty property2 = getDAO().getProperty(Person.class, Person.GIVENS);
            SelectColumn column1 = new SelectColumn(property1);
            SelectColumn column2 = new SelectColumn(property2);
            Filter filter = new PropertyFilter(table, property1, TEST_SURNAME);
            List resultSet = getDAO().select(table, new SelectColumn[]{column1, column2}, filter);
            boolean hasOnlySelectColumns = true;
            boolean hasOnlyOneSurname = true;
            for (Person person : resultSet) {
                hasOnlySelectColumns &= (person.getGivens() != null && person.getPersonId() == null);
            }
            for (Person person : resultSet) {
                hasOnlyOneSurname &= (person.getSurname().equalsIgnoreCase(TEST_SURNAME));
            }
            if (!hasOnlySelectColumns) {
                reportFailure("Select table select specific columns filter [columns] failed");
            } else {
                reportSuccess("Select table select specific columns filter [columns] succeeded");
            }
            if (!hasOnlyOneSurname) {
                reportFailure("Select table select specific columns filter [filter] failed");
            } else {
                reportSuccess("Select table select specific columns filter [filter] succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectClassSelectFilter() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty property2 = getDAO().getProperty(Person.class, Person.GIVENS);
            SelectColumn column1 = new SelectColumn(property1);
            SelectColumn column2 = new SelectColumn(property2);
            Filter filter = new PropertyFilter(table, property1, TEST_SURNAME);
            List resultSet = getDAO().select(Person.class, new SelectColumn[]{column1, column2}, filter);
            boolean hasOnlySelectColumns = true;
            for (Person person : resultSet) {
                hasOnlySelectColumns &= (person.getGivens() != null && person.getPersonId() == null);
            }
            boolean hasOnlyOneSurname = true;
            for (Person person : resultSet) {
                hasOnlyOneSurname &= (person.getSurname().equalsIgnoreCase(TEST_SURNAME));
            }
            if (!hasOnlySelectColumns) {
                reportFailure("Select class select specific columns filter [columns] failed");
            } else {
                reportSuccess("Select class select specific columns filter [columns] succeeded");
            }
            if (!hasOnlyOneSurname) {
                reportFailure("Select class select specific columns filter [filter] failed");
            } else {
                reportSuccess("Select class select specific columns filter [filter] succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectTableSelectSortsFilter() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty property2 = getDAO().getProperty(Person.class, Person.GIVENS);
            OrderBy orderBy2 = new OrderBy(property2);
            SelectColumn column1 = new SelectColumn(property1);
            SelectColumn column2 = new SelectColumn(property2);
            Filter filter = new PropertyFilter(table, property1, TEST_SURNAME);
            List resultSet = getDAO().select(table, new SelectColumn[]{column1, column2}, new OrderBy[]{orderBy2}, filter);
            boolean hasOnlySelectColumns = true;
            for (Person person : resultSet) {
                hasOnlySelectColumns &= (person.getGivens() != null && person.getPersonId() == null);
            }
            boolean hasOnlyOneSurname = true;
            for (Person person : resultSet) {
                hasOnlyOneSurname &= (person.getSurname().equalsIgnoreCase(TEST_SURNAME));
            }
            boolean inRightOrder = true;
            for (int i = 1; i < resultSet.size(); i++) {
                Person firstPerson = resultSet.get(i - 1);
                Person secondPerson = resultSet.get(i);
                inRightOrder &= (firstPerson.getGivens().compareTo(secondPerson.getGivens()) <= 0);
            }

            if (!hasOnlySelectColumns) {
                reportFailure("Select table select specific columns sort filter [columns] failed");
            } else {
                reportSuccess("Select table select specific columns sort filter [columns] succeeded");
            }
            if (!hasOnlyOneSurname) {
                reportFailure("Select table select specific columns sort filter [filter] failed");
            } else {
                reportSuccess("Select table select specific columns sort filter [filter] succeeded");
            }
            if (!inRightOrder) {
                reportFailure("Select table select specific columns sort filter [sort] failed");
            } else {
                reportSuccess("Select table select specific columns sort filter [sort]succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectClassSelectSortsFilter() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty property2 = getDAO().getProperty(Person.class, Person.GIVENS);
            OrderBy orderBy2 = new OrderBy(property2);
            SelectColumn column1 = new SelectColumn(property1);
            SelectColumn column2 = new SelectColumn(property2);
            Filter filter = new PropertyFilter(table, property1, TEST_SURNAME);
            List resultSet = getDAO().select(Person.class, new SelectColumn[]{column1, column2}, new OrderBy[]{orderBy2}, filter);
            boolean hasOnlySelectColumns = true;
            for (Person person : resultSet) {
                hasOnlySelectColumns &= (person.getGivens() != null && person.getPersonId() == null);
            }
            boolean hasOnlyOneSurname = true;
            for (Person person : resultSet) {
                hasOnlyOneSurname &= (person.getSurname().equalsIgnoreCase(TEST_SURNAME));
            }
            boolean inRightOrder = true;
            for (int i = 1; i < resultSet.size(); i++) {
                Person firstPerson = resultSet.get(i - 1);
                Person secondPerson = resultSet.get(i);
                inRightOrder &= (firstPerson.getGivens().compareTo(secondPerson.getGivens()) <= 0);
            }

            if (!hasOnlySelectColumns) {
                reportFailure("Select class select specific columns sort filter [columns] failed");
            } else {
                reportSuccess("Select class select specific columns sort filter [columns] succeeded");
            }
            if (!hasOnlyOneSurname) {
                reportFailure("Select class select specific columns sort filter [filter] failed");
            } else {
                reportSuccess("Select class select specific columns sort filter [filter] succeeded");
            }
            if (!inRightOrder) {
                reportFailure("Select class select specific columns sort filter [sort] failed");
            } else {
                reportSuccess("Select class select specific columns sort filter [sort]succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectClassSelectsMultiTable() {
        try {
            daotable tableA = getDAO().getTable(Person.class);
            daotable tableB = getDAO().getTable(ContactInfo.class);
            daoproperty propertyA1 = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty propertyA2 = getDAO().getProperty(Person.class, Person.GIVENS);
            daoproperty propertyB1 = getDAO().getProperty(ContactInfo.class, ContactInfo.CONTACTINFOTYPE);
            daoproperty propertyB2 = getDAO().getProperty(ContactInfo.class, ContactInfo.CONTACTINFO);

            SelectColumn columnA1 = new SelectColumn(tableA, propertyA1);
            SelectColumn columnA2 = new SelectColumn(propertyA2);
            SelectColumn columnB1 = new SelectColumn(tableB, propertyB1);
            SelectColumn columnB2 = new SelectColumn(tableB, propertyB2);
            Filter filter = new PropertyFilter(tableA, propertyA1, TEST_SURNAME);
            OrderBy orderByA2 = new OrderBy(propertyA2);
            OrderBy orderByB1 = new OrderBy(tableB, propertyB1);
            SelectColumn[] selectColumns = new SelectColumn[]{columnA1, columnA2, columnB1, columnB2};
            OrderBy[] orderBys = new OrderBy[]{orderByA2, orderByB1};
            List, BaseDatabaseObject>> resultSet = getDAO().selectMultipleTables(selectColumns, orderBys, filter);
            List people = new ArrayList<>();
            List contactInfos = new ArrayList<>();
            boolean allSet = true;
            for (Map, BaseDatabaseObject> map : resultSet) {
                Person person = (Person) map.get(Person.class);
                ContactInfo contactInfo = (ContactInfo) map.get(ContactInfo.class);
                if (person != null && contactInfo != null) {
                    people.add(person);
                    contactInfos.add(contactInfo);
                } else {
                    allSet = false;
                }
            }
            if (!allSet) {
                reportFailure("Entries on multi table select missing entries");
            } else {
                reportSuccess("Entries on multi table select all present");
            }
            boolean hasOnlyOneSurname = true;
            for (int i = 0; i < people.size(); i++) {
                Person person = people.get(i);
                hasOnlyOneSurname |= person.getSurname().equalsIgnoreCase(TEST_SURNAME);
            }
            if (!hasOnlyOneSurname) {
                reportFailure("Non-" + TEST_SURNAME + " included");
            } else {
                reportSuccess("All rows are " + TEST_SURNAME);
            }
            boolean peopleInRightOrder = true;
            for (int i = 1; i < people.size(); i++) {
                Person firstPerson = people.get(i - 1);
                Person secondPerson = people.get(i);
                peopleInRightOrder &= (firstPerson.getGivens().compareTo(secondPerson.getGivens()) <= 0);
            }
            if (!peopleInRightOrder) {
                reportFailure("Entries on multi table in wrong order [Person] entries");
            } else {
                reportSuccess("Entries on multi table in correct order [Person] entries");
            }
            boolean contactInRightOrder = true;
            for (int i = 10; i < contactInfos.size(); i++) {
                ContactInfo firstContactInfo = contactInfos.get(i - 1);
                ContactInfo secondContactInfo = contactInfos.get(i);
                contactInRightOrder &= (firstContactInfo.getContactInfoType().getCode().compareTo(secondContactInfo.getContactInfoType().getCode()) <= 0);
            }
            if (!contactInRightOrder) {
                reportFailure("Entries on multi table in wrong order [ContactInfo] entries");
            } else {
                reportSuccess("Entries on multi table in correct order [ContactInfo] entries");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectTableSorts() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property = getDAO().getProperty(Person.class, Person.SURNAME);
            OrderBy orderBy = new OrderBy(property);
            List resultSet = getDAO().select(table, orderBy);
            boolean peopleInRightOrder = true;
            for (int i = 1; i < resultSet.size(); i++) {
                Person firstPerson = resultSet.get(i - 1);
                Person secondPerson = resultSet.get(i);
                peopleInRightOrder &= (firstPerson.getSurname().compareTo(secondPerson.getSurname()) <= 0);
            }
            if (!peopleInRightOrder) {
                reportFailure("Straight select on a daotable sort failed");
            } else {
                reportSuccess("Straight select on a daotable sort succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectClassSorts() {
        try {
            daoproperty property = getDAO().getProperty(Person.class, Person.SURNAME);
            OrderBy orderBy = new OrderBy(property);
            List resultSet = getDAO().select(Person.class, orderBy);
            boolean peopleInRightOrder = true;
            for (int i = 1; i < resultSet.size(); i++) {
                Person firstPerson = resultSet.get(i - 1);
                Person secondPerson = resultSet.get(i);
                peopleInRightOrder &= (firstPerson.getSurname().compareTo(secondPerson.getSurname()) <= 0);
            }
            if (!peopleInRightOrder) {
                reportFailure("Straight select on a Class sort failed");
            } else {
                reportSuccess("Straight select on a Class sort succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectTableFilter() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property = getDAO().getProperty(table, Person.SURNAME);
            PropertyFilter filter = new PropertyFilter(table, property, TEST_SURNAME);
            List resultSet = getDAO().select(table, filter);
            boolean hasOnlyOneSurname = true;
            for (int i = 0; i < resultSet.size(); i++) {
                Person person = resultSet.get(i);
                hasOnlyOneSurname |= person.getSurname().equalsIgnoreCase(TEST_SURNAME);
            }
            if (!hasOnlyOneSurname) {
                reportFailure("Straight select on a daotable filter failed");
            } else {
                reportSuccess("Straight select on a daotable filter succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectClassFilter() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property = getDAO().getProperty(Person.class, Person.SURNAME);
            PropertyFilter filter = new PropertyFilter(Person.class, property, TEST_SURNAME);
            List resultSet = getDAO().select(table, filter);
            boolean hasOnlyOneSurname = true;
            for (int i = 0; i < resultSet.size(); i++) {
                Person person = resultSet.get(i);
                hasOnlyOneSurname |= person.getSurname().equalsIgnoreCase(TEST_SURNAME);
            }
            if (!hasOnlyOneSurname) {
                reportFailure("Straight select on a daotable filter failed");
            } else {
                reportSuccess("Straight select on a daotable filter succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectTableFilterSorts() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty propertyFilter = getDAO().getProperty(table, Person.SURNAME);
            daoproperty propertySort = getDAO().getProperty(table, Person.GIVENS);
            OrderBy orderBy = new OrderBy(propertySort);
            PropertyFilter filter = new PropertyFilter(table, propertyFilter, TEST_SURNAME);
            List resultSet = getDAO().select(table, filter, orderBy);
            boolean hasOnlyOneSurname = true;
            for (int i = 0; i < resultSet.size(); i++) {
                Person person = resultSet.get(i);
                hasOnlyOneSurname |= person.getSurname().equalsIgnoreCase(TEST_SURNAME);
            }
            if (!hasOnlyOneSurname) {
                reportFailure("Straight select on a daotable filter failed");
            }
            boolean peopleInRightOrder = true;
            for (int i = 1; i < resultSet.size(); i++) {
                Person firstPerson = resultSet.get(i - 1);
                Person secondPerson = resultSet.get(i);
                peopleInRightOrder &= (firstPerson.getGivens().compareTo(secondPerson.getGivens()) <= 0);
            }
            if (!peopleInRightOrder) {
                reportFailure("Straight select on a daotable sort failed");
            } else {
                reportSuccess("Straight select on a daotable sort succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectClassFilterSorts() {
        try {
            daoproperty propertyFilter = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty propertySort = getDAO().getProperty(Person.class, Person.GIVENS);
            OrderBy orderBy = new OrderBy(propertySort);
            PropertyFilter filter = new PropertyFilter(Person.class, propertyFilter, TEST_SURNAME);
            List resultSet = getDAO().select(Person.class, filter, orderBy);
            boolean hasOnlyOneSurname = true;
            for (int i = 0; i < resultSet.size(); i++) {
                Person person = resultSet.get(i);
                hasOnlyOneSurname |= person.getSurname().equalsIgnoreCase(TEST_SURNAME);
            }
            if (!hasOnlyOneSurname) {
                reportFailure("Straight select on a daotable filter failed");
            } else {
                reportSuccess("Straight select on a daotable filter succeeded");
            }
            boolean peopleInRightOrder = true;
            for (int i = 1; i < resultSet.size(); i++) {
                Person firstPerson = resultSet.get(i - 1);
                Person secondPerson = resultSet.get(i);
                peopleInRightOrder &= (firstPerson.getGivens().compareTo(secondPerson.getGivens()) <= 0);
            }
            if (!peopleInRightOrder) {
                reportFailure("Straight select on a daotable sort failed");
            } else {
                reportSuccess("Straight select on a daotable sort succeeded");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectByKeyObject() {
        try {
            Country country = new Country();
            country.setCountryId(TEST_COUNTRY_ID);
            country = getDAO().selectByKey(country);
            if (country == null) {
                reportFailure("Failed to retrieve country");
            } else {
                reportSuccess("Success in retrieving country");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectByKeyObjectCache() {
        try {
            Country country = new Country();
            country.setCountryId(TEST_COUNTRY_ID);
            country = getDAO().selectByKey(country, DAO.LOOKUP_TYPE.DATABASE_ONLY);
            if (country == null) {
                reportFailure("Failed to retrieve country [Database]");
            } else {
                reportSuccess("Success in retrieving country [Database]");
            }

            country = getDAO().selectByKey(country, DAO.LOOKUP_TYPE.CACHE_ONLY);
            if (country == null) {
                reportFailure("Failed to retrieve country [Cache]");
            } else {
                reportSuccess("Success in retrieving country [Cache]");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectOneTableFilter() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty propertyFilter = getDAO().getProperty(table, Person.SURNAME);
            PropertyFilter filter = new PropertyFilter(table, propertyFilter, TEST_SURNAME);
            Person person = getDAO().selectOne(table, filter);
            boolean hasOnlyOneSurname = person != null && person.getSurname().equalsIgnoreCase(TEST_SURNAME);
            if (!hasOnlyOneSurname) {
                reportFailure("Incorrect results on a select one table [Filter]");
            } else {
                reportSuccess("Correct results on a select one table [Filter]");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectOneClassFilter() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty propertyFilter = getDAO().getProperty(table, Person.SURNAME);
            PropertyFilter filter = new PropertyFilter(Person.class, propertyFilter, TEST_SURNAME);
            Person person = getDAO().selectOne(Person.class, filter);
            boolean hasOnlyOneSurname = person != null && person.getSurname().equalsIgnoreCase(TEST_SURNAME);
            if (!hasOnlyOneSurname) {
                reportFailure("Incorrect results on a select one class [Filter]");
            } else {
                reportSuccess("Correct results on a select one class [Filter]");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectOneTableFilterSort() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty propertyFilter = getDAO().getProperty(table, Person.SURNAME);
            daoproperty propertySort = getDAO().getProperty(table, Person.GIVENS);
            PropertyFilter filter = new PropertyFilter(table, propertyFilter, TEST_SURNAME);
            OrderBy orderBy = new OrderBy(propertySort);
            Person person = getDAO().selectOne(table, filter, orderBy);
            boolean hasOnlyOneSurname = person != null && person.getSurname().equalsIgnoreCase(TEST_SURNAME);
            boolean hasOnlyOneGivens = person != null && person.getGivens().equalsIgnoreCase(TEST_GIVENS);
            if (!hasOnlyOneSurname || !hasOnlyOneGivens) {
                reportFailure("Incorrect results on a select one table [Filter] [sort]");
            } else {
                reportSuccess("Correct results on a select one table [Filter] [sort]");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectOneClassFilterSort() {
        try {
            daoproperty propertyFilter = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty propertySort = getDAO().getProperty(Person.class, Person.GIVENS);
            PropertyFilter filter = new PropertyFilter(Person.class, propertyFilter, TEST_SURNAME);
            OrderBy orderBy = new OrderBy(propertySort);
            Person person = getDAO().selectOne(Person.class, filter, orderBy);
            boolean hasOnlyOneSurname = person.getSurname().equalsIgnoreCase(TEST_SURNAME);
            boolean hasOnlyOneGivens = person.getGivens().equalsIgnoreCase(TEST_GIVENS);
            if (!hasOnlyOneSurname || !hasOnlyOneGivens) {
                reportFailure("Incorrect results on a select one class [Filter][Sort]");
            } else {
                reportSuccess("Correct results on a select one class [Filter] [sort]");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectOneObject() {
        try {
            Person person = new Person();
            person.setSurname(TEST_SURNAME);
            person.setGivens(TEST_GIVENS);
            person = getDAO().selectOne(person);
            boolean hasOnlyOneSurname = person.getSurname().equalsIgnoreCase(TEST_SURNAME);
            boolean hasOnlyOneGivens = person.getGivens().equalsIgnoreCase(TEST_GIVENS);
            if (!hasOnlyOneSurname || !hasOnlyOneGivens) {
                reportFailure("Incorrect results on a select one Object");
            } else {
                reportSuccess("Correct results on a select one Object");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testLike() {
        try {
            daoproperty propertyForFilter = getDAO().getProperty(Person.class, Person.SURNAME);
            daoproperty propertyForLike = getDAO().getProperty(Person.class, Person.GIVENS);
            ListFilter filter = new ListFilter();
            filter.addFilter(new PropertyFilter(Person.class, propertyForFilter, TEST_SURNAME));
            filter.addFilter(new PropertyFilter(Person.class, propertyForLike, Filter.OPERAND.OPERAND_LIKE, "J%"));
            List people = getDAO().select(Person.class, filter);
            boolean hasOnlyOneSurname = true;
            boolean hasOnlyInitial = true;
            for (Person person : people) {
                hasOnlyOneSurname = person.getSurname().equalsIgnoreCase(TEST_SURNAME);
                hasOnlyInitial = person.getGivens().toLowerCase().startsWith("j");
            }
            if (!hasOnlyOneSurname || !hasOnlyInitial) {
                reportFailure("Incorrect results on a select with like");
            } else {
                reportSuccess("Correct results on a select with like");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testFunctions() {
        try {
            daoproperty property = getDAO().getProperty(Person.class, Person.SURNAME);
            PropertyFilter filter = new PropertyFilter(Person.class, property, "Edg");
            filter.setSelectFunction("substring", null, 1, 3);
            List people = getDAO().select(Person.class, filter);
            boolean hasOnlyOneSurname = true;
            int surnameCount = 0;
            for (Person person : people) {
                hasOnlyOneSurname = person.getSurname().equalsIgnoreCase(TEST_SURNAME);
                if (hasOnlyOneSurname) {
                    surnameCount++;
                }
            }
            if (!hasOnlyOneSurname || surnameCount != 2) {
                reportFailure("Incorrect results on a select with substring function on select");
            } else {
                reportSuccess("Correct results on a select with substring function on select");
            }

            filter = new PropertyFilter(Person.class, property, TEST_SURNAME);
            filter.setFilterFunction("substring", null, 1, 3);
            people = getDAO().select(Person.class, filter);
            if (!people.isEmpty()) {
                reportFailure("Incorrect results on a select with substring function on filter");
            } else {
                reportSuccess("Correct results on a select with substring function on filter");
            }

            filter = new PropertyFilter(Person.class, property, new String[]{TEST_SURNAME, OTHER_TEST_SURNAME});
            filter.setBothFunctions("substring", null, 1, 3);
            people = getDAO().select(Person.class, filter);
            boolean onlyRequested = true;
            int reqCount = 0;
            for (Person person : people) {
                onlyRequested = person.getSurname().equalsIgnoreCase(TEST_SURNAME)
                        || person.getSurname().equalsIgnoreCase(OTHER_TEST_SURNAME);
                if (onlyRequested) {
                    reqCount++;
                }
            }
            if (!onlyRequested || reqCount != 3) {
                reportFailure("Incorrect results on a select with substring function on both in 'in'");
            } else {
                reportSuccess("Correct results on a select with substring function on both in 'in'");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testSelectColumnFunction() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty property1 = getDAO().getProperty(Person.class, Person.SURNAME);
            SelectColumn column1 = new SelectColumn(property1);
            column1.setSelectFunction("substring", null, 1, 3);
            List resultSet = getDAO().select(table, column1);
            boolean correctValue = true;
            for (Person person : resultSet) {
                correctValue &= (person.getSurname() != null && person.getSurname().length() <= 3 && !person.getSurname().isBlank());
            }
            if (!correctValue) {
                reportFailure("Incorrect results on a selected columns with substring");
            } else {
                reportSuccess("Correct results on a selected columns with substring");
            }
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testAggregateFunction() {
        try {
            daotable table = getDAO().getTable(Person.class);
            daoproperty propertySurname = getDAO().getProperty(table, Person.SURNAME);
            daoproperty propertyGivens = getDAO().getProperty(table, Person.GIVENS);
            SelectColumn selectColumnSurname = new SelectColumn(propertySurname);
            SelectColumn selectColumnGivens = new SelectColumn(propertyGivens);
            PropertyFilter filter = new PropertyFilter(table, propertySurname, TEST_SURNAME);

            long count = testPersonCount(table);
            testPersonSurnameCount(table, filter, count);
            testPersonDistinctCount(selectColumnSurname, table, count);
            testMaxSurname(selectColumnSurname, table);
            testMinSurname(selectColumnSurname, table);
            testMaxFilterSurname(selectColumnGivens, table, filter);
            testMinFilterSurname(selectColumnGivens, table, filter);
            testFilterFunctionSurname(propertyGivens, table, filter);
            testAggregatePerson(selectColumnGivens, table, selectColumnSurname);
            testAggregatePersonHaving(selectColumnGivens, table, selectColumnSurname);
        } catch (DAOException ex) {
            reportFailure("DAO Failed", ex);
        } catch (SQLException ex) {
            reportFailure("SQL Failed", ex);
        }
    }

    private void testAggregatePersonHaving(SelectColumn selectColumnGivens, daotable table, SelectColumn selectColumnSurname) throws DAOException, SQLException {
        List> listAggregate
                = getDAO().selectAggregate("count",
                        selectColumnGivens,
                        table,
                        null,
                        new SelectColumn[]{selectColumnSurname},
                        null,
                        Filter.OPERAND.OPERAND_GREATER_THAN,
                        1);
        boolean personComplete = false;
        boolean aggIsNull = false;
        for (AggregatedObject agg : listAggregate) {
            Person p = agg.getDatabaseObject();
            Object a = agg.getAggregate();
            personComplete |= p.isCompletelyLoaded();
            aggIsNull |= a == null;
            if (a != null) {
                reportSuccess(p.getSurname() + ":" + a.toString());
            }
        }
        if (personComplete) {
            reportFailure("Person should not be completely loaded");
        } else {
            reportSuccess("Person is never completely loaded");
        }
        if (aggIsNull) {
            reportFailure("Aggregate object shouldn't be null");
        } else {
            reportSuccess("Aggregate object is never null");
        }
    }

    private void testAggregatePerson(SelectColumn selectColumnGivens, daotable table, SelectColumn selectColumnSurname) throws SQLException, DAOException {
        List> listAggregate
                = getDAO().selectAggregate("min",
                        selectColumnGivens,
                        table,
                        null,
                        new SelectColumn[]{selectColumnSurname},
                        null);
        boolean personComplete = false;
        boolean aggIsNull = false;
        for (AggregatedObject agg : listAggregate) {
            Person p = agg.getDatabaseObject();
            Object a = agg.getAggregate();
            personComplete |= p.isCompletelyLoaded();
            aggIsNull |= a == null;
        }
        if (personComplete) {
            reportFailure("Person should not be completely loaded");
        } else {
            reportSuccess("Person is never completely loaded");
        }
        if (aggIsNull) {
            reportFailure("Aggregate object shouldn't be null");
        } else {
            reportSuccess("Aggregate object is never null");
        }
    }

    private void testFilterFunctionSurname(daoproperty propertyGivens, daotable table, PropertyFilter filter) throws DAOException, SQLException {
        SelectColumn selectColumnGivensStart = new SelectColumn(propertyGivens);
        selectColumnGivensStart.setSelectFunction("substring", null, 1, 2);
        String minGivensFirst = (String) getDAO().selectAggregate("min", selectColumnGivensStart, table, filter);
        if (minGivensFirst == null) {
            reportFailure("Failed to min out " + TEST_SURNAME + " givens 2 char");
        } else {
            if (!minGivensFirst.equalsIgnoreCase(TEST_GIVENS.substring(0, 2))) {
                reportFailure("Wrong min out " + TEST_SURNAME + " givens 2 char");
            }
            reportSuccess("Successfully mined out " + TEST_SURNAME + " givens 2 char");
        }
    }

    private void testMinFilterSurname(SelectColumn selectColumnGivens, daotable table, PropertyFilter filter) throws SQLException, DAOException {
        String minGivens = (String) getDAO().selectAggregate("min", selectColumnGivens, table, filter);
        if (minGivens == null) {
            reportFailure("Failed to min out " + TEST_SURNAME + " givens");
        } else {
            if (!minGivens.equalsIgnoreCase(TEST_GIVENS)) {
                reportFailure("Wrong min out" + TEST_SURNAME + "givens");
            }
            reportSuccess("Successfully mined out" + TEST_SURNAME + "givens");
        }
    }

    private void testMaxFilterSurname(SelectColumn selectColumnGivens, daotable table, PropertyFilter filter) throws DAOException, SQLException {
        String maxGivens = (String) getDAO().selectAggregate("max", selectColumnGivens, table, filter);
        if (maxGivens == null) {
            reportFailure("Failed to max out " + TEST_SURNAME + " givens");
        } else {
            if (!maxGivens.equalsIgnoreCase(OTHER_TEST_GIVENS)) {
                reportFailure("Wrong min out " + TEST_SURNAME + " givens");
            }
            reportSuccess("Successfully maxed out " + TEST_SURNAME + " givens");
        }
    }

    private void testMinSurname(SelectColumn selectColumnSurname, daotable table) throws DAOException, SQLException {
        Object minSurname = getDAO().selectAggregate("min", selectColumnSurname, table, null);
        if (minSurname == null) {
            reportFailure("Failed to min out surname");
        } else {
            reportSuccess("Successfully mined out surname");
        }
    }

    private void testMaxSurname(SelectColumn selectColumnSurname, daotable table) throws SQLException, DAOException {
        Object maxSurname = getDAO().selectAggregate("max", selectColumnSurname, table, null);
        if (maxSurname == null) {
            reportFailure("Failed to max out surname");
        } else {
            reportSuccess("Successfully maxed out surname");
        }
    }

    private void testPersonDistinctCount(SelectColumn selectColumnSurname, daotable table, long count) throws DAOException, SQLException {
        Object personDistinctCount = getDAO().selectAggregate("count", selectColumnSurname, table, null);
        if (personDistinctCount == null || !(personDistinctCount instanceof Long)) {
            reportFailure("Failed to count distinct surname");
        } else {
            long distinctCount = (Long) personDistinctCount;
            if (distinctCount >= count) {
                reportFailure("Failed to count disitnct surnames " + count + " should be  > " + distinctCount);
            } else {
                reportSuccess("Successfully counted just distinct surnames");
            }
        }
    }

    private void testPersonSurnameCount(daotable table, PropertyFilter filter, long count) throws SQLException, DAOException {
        Object personSurnameCount = getDAO().selectAggregate("count", null, table, filter);
        if (personSurnameCount == null || !(personSurnameCount instanceof Long)) {
            reportFailure("Failed to count " + TEST_SURNAME);
        } else {
            long surnameCount = (Long) personSurnameCount;
            if (surnameCount >= count) {
                reportFailure("Failed to count just " + TEST_SURNAME + " " + count + " should be  > " + surnameCount);
            } else {
                reportSuccess("Successfully counted just " + TEST_SURNAME);
            }
        }
    }

    private long testPersonCount(daotable table) throws DAOException, SQLException {
        Object personCount = getDAO().selectAggregate("count", null, table, null);
        long count = 0;
        if (personCount == null || !(personCount instanceof Long)) {
            reportFailure("Failed to count person");
        } else {
            reportSuccess("Successfully counted person");
            count = (Long) personCount;
        }
        return count;
    }

    @Override
    public void runTestSuite() {
        testSelectClass();
        testSelectDAOTable();
        testSelectObject();
        testSelectObjectSort();
        testSelectTableSelect();
        testSelectClassSelect();
        testSelectTableSelectSort();
        testSelectClassSelectSort();
        testSelectTableSelectSorts();
        testSelectClassSelectSorts();
        testSelectTableSelectFilter();
        testSelectClassSelectFilter();
        testSelectTableSelectSortsFilter();
        testSelectClassSelectSortsFilter();
        testSelectTableSorts();
        testSelectClassSelectsMultiTable();
        testSelectClassSorts();
        testSelectTableFilter();
        testSelectClassFilter();
        testSelectTableFilterSorts();
        testSelectClassFilterSorts();
        testSelectByKeyObject();
        testSelectByKeyObjectCache();
        testSelectOneTableFilter();
        testSelectOneClassFilter();
        testSelectOneTableFilterSort();
        testSelectOneClassFilterSort();
        testSelectOneObject();
        testLike();
        testFunctions();
        testSelectColumnFunction();
        testAggregateFunction();
    }

}
Back to the Main Page