Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Eclipse Application Testing Guide
Previous Page Home Next Page

Tutorial: Accessing a datapool from a TPTP JUnit test


Objectives:

To create and access a datapool from an example test application using the Eclipse Test and Performance Tools Platform (TPTP).

Time required

1 hour

Before you begin

Before you start this tutorial, you need to

  1. Install Eclipse and the Eclipse Test and Performance Tools Platform (TPTP).
  2. Have a basic understanding of JUnit testing. For more information about JUnit testing, see www.junit.org.
  3. Configure and run the Agent Controller which corresponds to your TPTP version. For more information, see Getting started with the Agent Controller.


Description

In this tutorial, you create and test an application called MyShoppingCart. Using the Eclipse Test and Performance Tools Platform (TPTP), you develop JUnit tests and create a datapool to provide data to the test environment.

This tutorial guides you through the following procedures:

  1. Creating a sample user application
  2. Setting up a TPTP test suite
  3. Creating a datapool and initializing test data
  4. Modifying test cases to reference a datapool

Creating a sample user application


In this procedure, you develop the MyShoppingCart class. In subsequent procedures, you will use the Eclipse Test and Performance Tools Platform to develop a corresponding test environment.
  1. Create a new Eclipse Java Project.
    • From the File menu choose File > New > Project... The New Project dialog appears.
    • In the Wizards list, select Java Project and click Next. The Create a Java project page appears.
    • Type datapoolExample for the Project name and click Finish. The datapoolExample project appears in the Navigator view.
  2. Create the MyShoppingCart class.
    • In the Navigator view, right-click the datapoolExample project and choose New > Class. The New Java Class dialog appears.
    • Type MyShoppingCart in the Name field.
    • Clear the option to create the main method public static void main(String[] args).
    • Click Finish. The MyShoppingCart.java file appears in the Java editor.
    • Type the following for the MyShoppingCart source:
      import java.util.Hashtable;
      
      public class MyShoppingCart {
      
      	public Hashtable myFlowers;
      	
      	public MyShoppingCart() {
      	    
      		myFlowers = new Hashtable();
      		myFlowers.put("Orchid", new Double(5.99));
      		myFlowers.put("Tulip", new Double(1.99));
      		myFlowers.put("Yellow Carnation", new Double(6.99));
      		myFlowers.put("White Rose", new Double(9.99));
      		myFlowers.put("Geraniums", new Double(4.99));		
      	}
      	
      	public double itemCost(String itemName, int itemQuantity)
      	{
      	     Double price = (Double)myFlowers.get(itemName);
      		
      	     if (price != null) {
      	    	 return price.doubleValue()*itemQuantity;
      	     }
      	     
      	     return -1;	    	 	    	 	     		
      	}
      }
    • Save MyShoppingCart.java. From the File menu choose File > Save.

Setting up a TPTP test suite


In this procedure, you develop a test suite for the the MyShoppingCart class. Using the Eclipse Test and Performance Tools Platform, you develop a JUnit test for the itemCost method.
  1. Open the Test Perspective.
    • From the File menu, choose Window > Open Perspective > Other.
    • Select Test and click OK. The Test Navigator of the Test Perspective appears.
  2. Create a new TPTP JUnit Test.
    • In the Test Navigator of the Test Perspective, right-click the datapoolExample project and choose New > Test Element...
    • In the Test Element dialog, select TPTP JUnit test and click Next. The New Test Case dialog appears, prompting you to add JUnit libraries. Click Yes to add JUnit libraries.
    • In the New JUnit Test Source Code page, type MyShoppingCartTest in the Name field. In the Select how the test behavior is edited section, choose the edit In the test editor option (the default).
    • Click Finish. The TPTP JUnit Test editor appears, showing the MyShoppingCartTest test suite. The Overview tab includes a test description, Source Information, and a Test Methods listing. Currently, no test methods are defined. For this tutorial, the TPTP JUnit Test editor generates method stubs so the Implement Test Behavior as code option in the Source Information section should be cleared. For more information, see JUnit Test Suite Editor.
  3. Add the testItemCost and testMyShoppingCartConstructor methods.
    • In the Test Methods tab, click Add. A default name appears for your test.
    • Add the testItemCost method. In the Name field, type testItemCost for the new test name. In the Description field, type Test for the MyShoppingCart.itemCost(String, int) method.
    • Add the testMyShoppingCartConstructor method. In the Test Methods tab, click Add. In the Name field, type testMyShoppingCartConstructor for the new test name. In the Description field, type Test for the MyShoppingCart constructor.
  4. Configure the test execution behavior.
    • Add a test execution loop. In the Behavior tab, click Add.. > Loop. In the Name field, type Loop_MyShoppingCart. In the Number of Iterations field, type 1 (the default value).
    • Add a testMyShoppingCartConstructor invocation. Select Loop_MyShoppingCart and click Add... > invocation. The Test Invocation dialog appears. Select Select testMyShoppingCartConstructor and click OK.
    • Add a testItemCost invocation. Select Loop_MyShoppingCart and click Add... > invocation. The Test Invocation dialog appears. Select testItemCost and click OK.
    • Save the test suite. From the File menu, choose File > Save.
      Note: the Save command causes the TPTP JUnit Test editor to create test method stubs in MyShoppingCartTest.java.
  5. Enter code for the generated JUnit test methods testMyShoppingCartConstructor and testItemCost.
    • Open the Java Perspective. From the file menu choose Window > Open Perspective > Other... > Java.
    • Open MyShoppingCartTest.java. In the Navigator, open the datapoolExample project folder and double-click MyShoppingCartTest.java. The MyShoppingCartTest.java contents appear in the Java editor, including code to setup and execute the test suite, and stub methods for testMyShoppingCartConstructor and testItemCost.
    • Type the following code for the testMyShoppingCartConstructor method.
      public void testMyShoppingCartConstructor()
      throws Exception
      {
      	MyShoppingCart cart = new MyShoppingCart();
      	Double priceOrchid = (Double)cart.myFlowers.get("Orchid");
      	Double priceTulip = (Double)cart.myFlowers.get("Tulip");
      	
      	assertTrue(priceOrchid.doubleValue() == 5.99);
      	assertTrue(priceTulip.doubleValue() == 1.99);
      }
    • Type the following code for the testItemCost method.
      public void testItemCost()
      throws Exception
      {
      	MyShoppingCart cart = new MyShoppingCart();
      	double priceForTwo = cart.itemCost("Orchid", 2);
      	
      	assertTrue(priceForTwo == 11.98);
      }
    • Save MyShoppingCartTest.java. From the file menu, choose File > Save.
  6. Open the Test Perspective and create a test deployment. For an example, see Creating a Test Deployment.
  7. Run the test using your custom deployment.
    • In the Test Navigator of the Test Perspective, right-click the MyShoppingCartTest test suite and choose Run As > Run.... The Run configuration dialog appears.
    • In the Configurations pane, select Test and then click New.
    • In the left pane of the Run configuration (Select test to run), expand datapoolExample and choose the MyShoppingCartTest test suite.
    • In the right pane of the Run configuration, select the deployment you created section.
    • In the Test Logs tab, clear the Use defaults option and select the datapoolExample project folder for the location.
    • Click Run to launch the test.
  8. Double-click the MyShoppingCartTest test log, which appears in the Test Navigator of the Test Perspective. The MyShoppingCartTest test log appears. Select the Events tab to view the test details. You should see the following events: start of test suite, start of Loop_MyShoppingCart, test start, test verdict, test stop, second test start, second test verdict, second test stop, loop stop, test suite verdict, and test suite stop.

Creating a datapool and initializing test data


In this procedure, you create a simple datapool to store test data. Using the datapool editor, you define a datapool's structural elements, including variables (columns), records (rows), and equivalence classes (groups of related records).
  1. Create a CSV (comma delimited) file including your test data. Typically, you can export data in a spreadsheet application or database to CSV format. In this tutorial, however, you type the data in a text editor.
    • Type the following in a text editor.
      ,ModelNumber::String,Description::String,Price::Double
      flowers,F0001,Orchid,5.99
      flowers,F0002,Tulip,1.99
      flowers,F0003,Yellow Carnation,6.99
      flowers,F0004,White Rose,9.99
      flowers,F0005,Geraniums,4.99
    • Save the file as flowerData.csv in a temporary external location.
  2. In Eclipse, open the Test perspective.
    • From the File menu, choose Window > Open Perspective > Other.
    • Select Test and click OK. The Test Navigator of the Test Perspective appears.
  3. In the Test Navigator of the Test Perspective, right-click a project and select New > Test Element ... The New Test Element dialog appears.
  4. In the Wizards list box, expand the Test Assets folder and select Datapool.
  5. Click Next. The New Datapool dialog appears.
  6. Choose the datapool project folder and datapool name. In the list of existing projects, select the datapoolExample project. In the Name field, type shoppingCartDatapool. Click Next to continue and then Next again to open the CSV (comma delimited) file import page.
  7. Import the CSV file flowerData.csv.
    • In the CSV File field, click Browse and navigate to flowerData.csv.
    • Check the First row contains variable names and suggested types option. The first row of flowerData.csv contains column headings and types.
    • Check the First column contains equivalence class names option. Equivalence classes group related data. The first column of flowerData.csv specifies a single equivalence class called flowers.
  8. Click Finish. If you specified initial datapool dimensions, which may conflict with the CSV file dimensions, a dialog appears. Click Yes to use the dimensions of the CSV file. Click No to use the specified initial dimensions (and possibly truncate the CSV file data). The Datapool editor appears, showing the data contained in the flowers equivalence class.
    For detailed information about creating a datapool, see Creating a datapool.


Modifying test cases to reference a datapool


In this procedure, you use the datapool API to replace hard-coded test values with references to datapool columns.

Adding libraries required by the datapool API

The datapool API requires various TPTP and Eclipse Modeling Framework (EMF) libraries. In this procedure, you add the libraries to your Java build path.
  1. Open the Java Perspective. From the file menu choose Window > Open Perspective > Java.
  2. Open the datapoolExample project properties. In the Navigator or Package Explorer window, right-click the datapoolExample project folder and choose Properties. The Properties for datapoolExample dialog appears.
  3. Add the following library JARs to the Java build path of the datapoolExample project:

    • org.eclipse.tptp.platform.models_<version>/tptp-models.jar
    • org.eclipse.tptp.platform.models.hierarchy_<version>/tptp-models-hierarchy.jar
    • org.eclipse.emf.common_<version>.jar
    • org.eclipse.emf.ecore_<version>.jar
    • org.eclipse.emf.ecore.xmi_<version>.jar
    • org.eclipse.equinox.common_<version>.jar

    where <version> is the current version of the library JAR.

    Steps:

    • In the left pane, select Java Build Path.
    • In the right pane, select the Libraries tab.
    • For each of the library JARs, complete the following steps:
      • If the library JAR is on the Java build path of the datapoolExample project, continue to the next library JAR. Otherwise, continue to the next step.
      • Click Add External JARs....
      • Navigate to the directory containing the library JAR and select the JAR file. The library JAR is contained in the same directory as the dependencies under the TPTP Libraries or JUnit 3 classpath container for the datapoolExample project.
      • Click Open.
  4. Click OK to close the Properties dialog.


Using the datapool API

In this procedure, you modify the MyShoppingCartTest class and testMyShoppingCartConstructor method to utilize the shoppingCartDatapool.
  1. If required, open the Java Perspective. From the file menu choose Window > Open Perspective > Java.
  2. Open MyShoppingCartTest.java. In the Navigator, open the datapoolExample project folder and double-click MyShoppingCartTest.java. The MyShoppingCartTest.java contents appear in the Java editor.
  3. Add the following import statements to the MyShoppingCartTest class.
    import org.eclipse.hyades.execution.runtime.datapool.IDatapool;
    import org.eclipse.hyades.execution.runtime.datapool.IDatapoolFactory;
    import org.eclipse.hyades.execution.runtime.datapool.IDatapoolIterator;
    import org.eclipse.hyades.models.common.datapool.impl.Common_DatapoolFactoryImpl;
  4. Declare an IDatapoolIterator class instance. This class instance will be initialized in your set up code and used in test methods.
    public class MyShoppingCartTest extends HyadesTestCase {
    	
    	IDatapoolIterator dpIterator;
    	
    	//...
  5. Modify the setUp method to initialize your datapool. In a TPTP JUnit test environment, the setUp method provides a common fixture. You can use setUp to initialize common test variables. Note: specify your fully qualified workspace path in place of <workspace> in the java.io.File constructor.
    protected void setUp() throws Exception {
    	// Initialize the datapool factory
    	IDatapoolFactory dpFactory = new Common_DatapoolFactoryImpl();
    		
    	// Load the shoppingCartDatapool datapool 
    	IDatapool datapool = dpFactory.load(
    			new java.io.File("<workspace>\\datapoolExample\\shoppingCartDatapool.datapool"),
    			false);
    		   
    	// Create an iterator to traverse the datapool
    	dpIterator = dpFactory.open(datapool,"org.eclipse.hyades.datapool.iterator.DatapoolIteratorSequentialPrivate");
    	
    	// Initialize the datapool to traverse the first equivalence class.
    	dpIterator.dpInitialize(datapool,0);
    }
    Notes:
    • The second parameter in the IDatapoolFactory load method indicates if the datapool instance is shared. If true the datapool cache is checked for an existing copy of the datapool. If false, the datapool is a private instance. Modifying a shared datapool instance may affect other users, and is only recommended for confined environments.
    • If you want to programmatically edit a datapool import org.eclipse.hyades.edit.datapool.
    • The second parameter in the IDatapoolFactory open method represents the type of iterator. For shared datapools use DatapoolIteratorSequentialPrivate.
  6. Modify the testMyShoppingCartConstructor method to utilize the shoppingCartDatapool.
    public void testMyShoppingCartConstructor()
    throws Exception
    {
    	MyShoppingCart cart = new MyShoppingCart();
    		
    	// Traverse through datapool...
    	// Test if constructor initializes each flower record appropriately
    	while(!dpIterator.dpDone())
    	{
    		String Description = dpIterator.dpCurrent().getCell("Description").getStringValue();
    		double datapoolPrice = dpIterator.dpCurrent().getCell("Price").getDoubleValue();
    		
    		// Test if the flower is accounted for in the application
    		Double applicationPrice;
    		assertNotNull(applicationPrice = (Double)cart.myFlowers.get(Description));
    		
    		// Test that the application price equals the price in the datapool 
    		assertTrue(applicationPrice.doubleValue() == datapoolPrice);
    		
    		dpIterator.dpNext();
    	}
    }
  7. Save MyShoppingCartTest.java. From the file menu, choose File > Save.
  8. Add the datapool to your testArtifact.
    • Open the Test Perspective. In the Test Navigator of the Test Perspective, double-click testDeployment to open it in the editor.
    • In the Pairs tab, select testAsset and click Open...
    • In the Test Assets tab, click Add.... Select the datapool and click OK. Save the asset.
  9. Run the test using your custom deployment.
    • Open the Test Perspective. In the Test Navigator of the Test Perspective, right-click the MyShoppingCartTest test suite and choose Run As > Run.... The Run configuration dialog appears.
    • In the left pane of the Run configuration, choose the MyShoppingCartTest test suite.
    • In the right pane of the Run configuration, select the deployment you created in the Before You Begin section.
    • In the Test Logs tab, clear the Use defaults option and select the datapoolExample project folder for the location.
    • Click Run to launch the test.
  10. Notes:
    • If the deployment you select contains any encrypted datapools, a dialog will be shown after you have launched the test to require the password for each encrypted datapool that contained in the deployment.
  11. Double-click the new MyShoppingCartTest test log, which appears in the Test Navigator of the Test Perspective. The MyShoppingCartTest test log appears. Select the Events tab to view the test result details.

Related tasks
Testing with JUnit
Providing tests with variable data
Creating a test deployment


 
 
  Published under the terms of the Eclipse Public License Version 1.0 ("EPL") Design by Interspire