Accessing a datapool from a Hyades test

You can use a datapool from a Hyades test by making calls to the datapool API from a test's generated Java code.

The datapool you want to use must already exist.

The Java code for the test from which you want to use a datapool must exist. Generate code by right-clicking the test and selecting Generate.

Add these jars to the Java build path. [ECLIPSE_HOME] is the Eclipse installation directory:
  • [ECLIPSE_HOME]/plugins/org.eclipse.hyades.models.common_3.0.0/common_model.jar
  • [ECLIPSE_HOME]/plugins/org.eclipse.hyades.test.datapool_3.0.0/datapool_api.jar
  • [ECLIPSE_HOME]/plugins/org.eclipse.emf.ecore_2.0.0/runtime/ecore.jar
  • [ECLIPSE_HOME]/plugins/org.eclipse.emf.common_2.0.0/runtime/common.jar
The steps for using a datapool from a test's Java code are listed below. An example immediately follows the steps.
  1. Open the test in your Java editor. The test's source code is not visible in the test perspective. To see the test, open the Java perspective and browse to the source folder (src/test by default).
  2. To the beginning of the test, add import statements for the datapool API classes.
  3. In the body of the test, locate the post class containing a value that you want to substitute with values from a datapool column.
  4. To the beginning of the post class, add code that creates an instance reference to the datapool and starts an iteration loop through it.
  5. Locate the line containing the value to be substituted and add code that replaces the value with a reference to the appropriate datapool column.
  6. At the end of the post class, just before the closing brace, close your iteration loop through the datapool.

The following excerpt, illustrating step 2, shows the import statements appearing at the beginning of a generated Hyades test. The lines in bold at the end are the datapool import statements that you must add to your test.

package test;
import java.util.Random;
import junit.extensions.RepeatedTest;
import junit.framework.Test;
import org.eclipse.hyades.test.common.junit.DefaultTestArbiter;
import org.eclipse.hyades.test.common.junit.HyadesTestCase;
import org.eclipse.hyades.test.common.junit.HyadesTestSuite;
import org.eclipse.hyades.test.http.runner.HttpCookieCache;
import org.eclipse.hyades.test.http.runner.HttpExecutor;
import org.eclipse.hyades.test.http.runner.HttpHeader;
import org.eclipse.hyades.test.http.runner.HttpRequest;
import org.eclipse.hyades.test.http.runner.HttpResponse;
import org.eclipse.hyades.test.http.runner.internal.util.HttpTestUtil;
import org.eclipse.hyades.models.common.datapool.impl.Common_DatapoolFactoryImpl;
import org.eclipse.hyades.execution.runtime.datapool.*;

The following excerpt, illustrating step 3, is taken from a generated test in which the tester went to www.amazon.com and searched for a book named atonement.

	public void c8postwww_amazon_com() throws Exception {
		HttpRequest request = new HttpRequest();
		request.setMethod("POST");
		request.setVersion("1.1");
		request.setHost("www.amazon.com");
		request.setPort(80);
		request
				.setAbsolutePath("/exec/obidos/search-handle-form/102-5005957-7048952");
		request
				.setBody("url=index%3Dstripbooks=atonement=10=6");

In the following excerpt, illustrating steps 4 and 5, the lines in bold show how to modify the generated test code such that a column named title in a datapool named books replaces the value atonement. In place of "pathname of books.datapool" in the second line, put the fully-qualified pathname of the books datapool.

	public void c8postwww_amazon_com() throws Exception {

  IDatapoolFactory dpFactory = new Common_DatapoolFactoryImpl();
     IDatapool datapool = dpFactory.load(new File("pathname of books.datapool"), false); //false - nonshared
     IDatapoolIterator iter = dpFactory.open(datapool, 
                 "org.eclipse.hyades.datapool.DatapoolIteratorSequentialPrivate");
     iter.dpInitialize(datapool, -1);  //-1 - go through all ECs


     while (!iter.dpDone())
     {

		   HttpRequest request = new HttpRequest();
		   request.setMethod("POST");
		   request.setVersion("1.1");
		   request.setHost("www.amazon.com");
		   request.setPort(80);
		   request
			.setAbsolutePath("/exec/obidos/search-handle-form/102-5005957-7048952");
	// request
		//	.setBody("url=index%3Dstripbooks=atonement=10=6");

    //new 
        String title = iter.dpCurrent().getCell("title").getStringValue();
        iter.dpNext();
        String body = "url=index%3Dstripbooks=" +  title + " =10=6";
        request.setBody(body);		
        //end new
        // Lines from test ommitted ...
        }// Close loop through datapool

Parent topic: Providing tests with variable data

(C) Copyright IBM Corporation 2000, 2004. All Rights Reserved.