JUnit test class for the pureQuery nested beans example

The JUnit test class contains tests for the pureQuery nested beans.

In the class, the connection to the database is created in the method doBeforeClass. The method test_joinTest1 tests the nested beans generated from an implementation that contains an SQL query. The method test_joinTest2 tests the nested beans generated from an inline SQL query. Both methods generate beans that return data from the example SQL query

The tests generate the following pureQuery nested beans:
  • Dept1 bean is the top-level bean for the method test_joinTest1.
  • Dept2 bean is the top-level bean for the method test_joinTest2.
  • Emp bean is the child bean for both top-level beans.
The following code is for the example JUnit test class. For information about the test cases and the test output, see Tests for the pureQuery nested bean examples.
package mytest;

import java.util.List;
import java.sql.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.ibm.pdq.runtime.Data;
import com.ibm.pdq.runtime.factory.DataFactory;

/**
 * Basic tests nested beans with @JoinPoint and @JoinColumn annotations.
 */
public class NestedBeanJoinPointTest {
    private static Connection jdbcCon = null;

    @BeforeClass
    public static void doBeforeClass() throws SQLException,
            ClassNotFoundException {
        System.out.println("==start tests==");
        System.out.println(com.ibm.pdq.tools.DataVersion.getVersion());
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
            jdbcCon = DriverManager.getConnection(
                    "jdbc:db2://localhost:50000/GSDB:currentSchema=GOSALESHR;"
                            + "retrieveMessagesFromServerOnGetMessage=true;",
                    "testusr", "testpwd");
        } catch (SQLException e) {
            System.out.println(e);
        }
    }

    @Test
    public void test_joinTest1() throws Exception {
        System.out.println("==Test1 nested beans with interface==");
        SimpleQueryInterface data = null;
        data = DataFactory.getData(SimpleQueryInterface.class, jdbcCon);

        System.out.println("==Get Dept data ==");
        List<Dept1> depts = data.joinTest();

        System.out.println("Employees by Dept:");
        for (Dept1 d : depts) {
            System.out.println(d.getDeptCode() + " - " + d.getDeptNameEn());
            List<Emp> employees = d.getDeptEmployees();
            if (!employees.isEmpty()) {
                for (Emp e : employees) {
                    System.out.println("  " + e.getLastName() + ", "
                            + e.getFirstName() + ",  " + e.toString());
                }
            } else {
                System.out.println(" -- No Employees-- ");
            }
        }
    }


    @Test
    public void test_joinTest2() throws Exception {
        System.out.println("==Test2 nested bean with Inline SQL==");
        Data data = null;
        data = DataFactory.getData(jdbcCon);

        List<Dept2> depts = data
            queryList(
                "SELECT E.EMPLOYEE_CODE, E.FIRST_NAME, E.LAST_NAME, E.DATE_HIRED, "
                    + " ORG.ORGANIZATION_CODE, ORG.ORGANIZATION_NAME_EN"
                    + " FROM EMPLOYEE AS E, EMPLOYEE_HISTORY AS EH, ORGANIZATION AS ORG"
                    + " WHERE EH.EMPLOYEE_CODE = E.EMPLOYEE_CODE "
                    + "   AND EH.ORGANIZATION_CODE = ORG.ORGANIZATION_CODE"
                    + "   AND EH.RECORD_END_DATE IS NULL "
                    + " ORDER BY EH.ORGANIZATION_CODE, E.LAST_NAME ",
                Dept2.class);

        System.out.println("Employees by Dept:");
        for (Dept2 d : depts) {
            System.out.println(d.deptCode + " - " + d.deptNameEn);
            List<Emp> employees = d.deptEmployees;
            if (!employees.isEmpty()) {
                for (Emp e : employees) {
                    System.out.println("  " + e.getLastName() + ", "
                            + e.getFirstName() + ",  " + e.toString());
                }
            } else {
                System.out.println(" -- No Employees-- ");
            }
        }
    }

}

Feedback