public void test() {
   ExpressionBuilder eb = new ExpressionBuilder();
   ReportQuery rq = new ReportQuery(Employee.class, eb);
   rq.addAttribute("firstName");
   rq.addAttribute("lastName");
   Expression exp = eb.getFunction("dbms_random.value");
   exp.setSelectIfOrderedBy(false);
   rq.addOrdering(exp.ascending());
   rq.setSelectionCriteria(eb.anyOf("projects").get("teamLeader").isNull());
   results = (Vector) getSession().executeQuery(rq);
 }
 public ReadQuery getQueryForTest() {
   ReportQuery testQuery = new ReportQuery();
   testQuery.setReferenceClass(Employee.class);
   ExpressionBuilder employees = new ExpressionBuilder();
   Expression exp = employees.get("firstName").like("B%");
   testQuery.setSelectionCriteria(exp);
   testQuery.cacheQueryResults();
   testQuery.addAttribute("firstName");
   return testQuery;
 }
  public void setup() {
    Employee emp = (Employee) getSomeEmployees().firstElement();

    PhoneNumber phone = (PhoneNumber) emp.getPhoneNumbers().firstElement();
    String areaCode = phone.getAreaCode();
    String firstName = emp.getFirstName();

    setReferenceClass(Employee.class);

    ExpressionBuilder employeeBuilder = new ExpressionBuilder();
    Expression phones = employeeBuilder.anyOf("phoneNumbers");
    Expression whereClause =
        phones
            .get("owner")
            .get("firstName")
            .equal(firstName)
            .and(phones.get("areaCode").equal(areaCode));

    ReportQuery rq = new ReportQuery();
    rq.setSelectionCriteria(whereClause);
    rq.addAttribute("number", phones.get("number"));
    rq.setReferenceClass(Employee.class);

    setOriginalOject(getAttributeFromAll("number", (Vector) getSession().executeQuery(rq)));
    getSession().getIdentityMapAccessor().initializeAllIdentityMaps();

    String ejbqlString;
    ejbqlString =
        "SELECT phone.number FROM Employee employee, IN(employee.phoneNumbers) phone "
            + "WHERE phone.owner.firstName = \""
            + firstName
            + "\" AND phone.areaCode = \""
            + areaCode
            + "\"";

    useReportQuery();
    setEjbqlString(ejbqlString);
    super.setup();
  }
  /**
   * Load the last test result from the test result database. Find only the results run on the same
   * machine and database. Compare the current test run result with the previous results do
   * determine if the test passes or fails.
   */
  public static void verify(PerformanceRegressionTest test) {
    // Ensures all tests pass to reset baseline,
    // Required when tests or environment change to be slower to avoid failures.
    if (reset) {
      throw new TestWarningException("Reseting baseline.");
    }
    Session session = LoadBuildSystem.getSystem().getSession();

    // Query all previous successful test results for this test on the same machine and database.
    // Find only the baseline version, or last version run different than current version.
    // If you need to compare results against the current version you must change the TopLink
    // version string.
    ReadAllQuery query = new ReadAllQuery(TestResult.class);
    ExpressionBuilder result = new ExpressionBuilder();
    query.setSelectionCriteria(
        result
            .get("name")
            .equal(test.getName())
            .and(
                result
                    .get("loadBuildSummary")
                    .get("machine")
                    .equal(LoadBuildSystem.getSummary().getMachine()))
            .and(
                result
                    .get("loadBuildSummary")
                    .get("loginChoice")
                    .equal(LoadBuildSystem.getSummary().getLoginChoice())));
    // Allow comparing to a set version through a system property.
    String currentVersion = LoadBuildSystem.getSummary().getToplinkVersion();
    String baselineVersion = null;
    if (System.getProperties().containsKey("toplink.loadbuild.baseline-version")) {
      baselineVersion = System.getProperties().getProperty("toplink.loadbuild.baseline-version");
      // System properties cannot store spaces so need to replace them from \b.
      baselineVersion = baselineVersion.replace('_', ' ');
      ((PerformanceComparisonTestResult) ((TestCase) test).getTestResult()).baselineVersion =
          baselineVersion;
    } else {
      // Compare against the last successful version.
      ReportQuery reportQuery = new ReportQuery(TestResult.class, query.getExpressionBuilder());
      reportQuery.useDistinct();
      reportQuery.returnSingleValue();
      reportQuery.addAttribute("version", result.get("loadBuildSummary").get("toplinkVersion"));
      reportQuery.setSelectionCriteria(
          query
              .getSelectionCriteria()
              .and(
                  (result.get("outcome").equal(TestResult.PASSED))
                      .or(result.get("outcome").equal(TestResult.WARNING)))
              .and(result.get("loadBuildSummary").get("toplinkVersion").notEqual(currentVersion)));
      reportQuery.addOrdering(result.get("loadBuildSummary").get("timestamp").descending());
      baselineVersion = (String) session.executeQuery(reportQuery);
    }
    query.setSelectionCriteria(
        query
            .getSelectionCriteria()
            .and(result.get("loadBuildSummary").get("toplinkVersion").equal(baselineVersion)));
    query.addOrdering(result.get("loadBuildSummary").get("timestamp").descending());
    query.setMaxRows(10);
    query.useCursoredStream(1, 1);
    CursoredStream stream = (CursoredStream) session.executeQuery(query);
    if (!stream.hasMoreElements()) {
      throw new TestWarningException("No previous test result to compare performance with.");
    }
    TestResult lastResult = (TestResult) stream.nextElement();
    double lastCount = lastResult.getTestTime();
    PerformanceComparisonTestResult testResult =
        (PerformanceComparisonTestResult) ((TestCase) test).getTestResult();
    testResult.getBaselineVersionResults().add(new Double(lastCount));
    // Average last 5 runs.
    int numberOfRuns = 0;
    while (stream.hasMoreElements() && (numberOfRuns < 4)) {
      TestResult nextResult = (TestResult) stream.nextElement();
      testResult.getBaselineVersionResults().add(new Double(nextResult.getTestTime()));
      numberOfRuns++;
    }
    stream.close();
    double baselineAverage =
        PerformanceComparisonTestResult.averageResults(testResult.getBaselineVersionResults());
    double currentCount = ((TestCase) test).getTestResult().getTestTime();
    testResult.baselineVersion = lastResult.getLoadBuildSummary().getToplinkVersion();
    testResult.percentageDifferenceLastRun =
        PerformanceComparisonTestResult.percentageDifference(currentCount, lastCount);

    // Query the current version last 5 runs for averaging.
    query = new ReadAllQuery(TestResult.class);
    result = new ExpressionBuilder();
    query.setSelectionCriteria(
        result
            .get("name")
            .equal(((TestCase) test).getName())
            .and(
                result
                    .get("loadBuildSummary")
                    .get("machine")
                    .equal(LoadBuildSystem.getSummary().getMachine()))
            .and(
                result
                    .get("loadBuildSummary")
                    .get("loginChoice")
                    .equal(LoadBuildSystem.getSummary().getLoginChoice()))
            .and(result.get("loadBuildSummary").get("toplinkVersion").equal(currentVersion)));
    query.addOrdering(result.get("loadBuildSummary").get("timestamp").descending());
    query.useCursoredStream(1, 1);
    stream = (CursoredStream) session.executeQuery(query);
    // Average last 5 runs.
    testResult.getCurrentVersionResults().add(new Double(currentCount));
    numberOfRuns = 0;
    while (stream.hasMoreElements() && (numberOfRuns < 4)) {
      TestResult nextResult = (TestResult) stream.nextElement();
      testResult.getCurrentVersionResults().add(new Double(nextResult.getTestTime()));
      numberOfRuns++;
    }
    stream.close();
    double currentAverage =
        PerformanceComparisonTestResult.averageResults(testResult.getCurrentVersionResults());
    testResult.percentageDifferenceAverage =
        PerformanceComparisonTestResult.percentageDifference(currentAverage, baselineAverage);

    testResult.baselineStandardDeviation =
        PerformanceComparisonTestResult.standardDeviationResults(
            testResult.getBaselineVersionResults());
    testResult.currentStandardDeviation =
        PerformanceComparisonTestResult.standardDeviationResults(
            testResult.getCurrentVersionResults());

    if (testResult.percentageDifferenceAverage < test.getAllowableDecrease()) {
      throw new TestErrorException(
          "Test is "
              + ((long) testResult.percentageDifferenceAverage)
              + "% slower than last successful execution.");
    }
  }