Classe de test JUnit pour l'exemple de beans imbriqués pureQuery

La classe de test JUnit contient des tests pour les beans imbriqués pureQuery.

Dans la classe, la connexion à la base de données est créée dans la méthode doBeforeClass. La méthode test_joinTest1 teste les beans imbriqués générés à partir d'une implémentation qui contient une requête SQL. La méthode test_joinTest2 teste les beans imbriqués générés à partir d'une requête SQL intégrée. Les deux méthodes génèrent des beans qui renvoient des données à partir de l'exemple de requête SQL

Les tests génèrent les beans imbriqués pureQuery suivants :
  • Bean Dept1 est le bean de niveau supérieur pour la méthode test_joinTest1.
  • Bean Dept2 est le bean de niveau supérieur pour la méthode test_joinTest2.
  • Bean Emp est le bean enfant pour les deux beans de niveau supérieur.
Le code suivant s'applique à l'exemple de classe de test JUnit. Pour plus d'informations sur les scénarios et les résultats de test, voir Tests pour les exemples de beans imbriqués pureQuery.
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-- ");
            }
        }
    }

}

Commentaires