|
Added
Link Here
|
| 1 |
<!DOCTYPE html |
| 2 |
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 |
<html lang="en-us" xml:lang="en-us"> |
| 4 |
<!-- |
| 5 |
/********************************************************************** |
| 6 |
* Copyright (c) 2009 IBM Corporation. |
| 7 |
* All rights reserved. This content is made available under |
| 8 |
* the terms of the Eclipse Public License v1.0 |
| 9 |
* which accompanies this distribution, and is available at |
| 10 |
* http://www.eclipse.org/legal/epl-v10.html |
| 11 |
* $Id$ |
| 12 |
* |
| 13 |
* Contributors: |
| 14 |
* IBM - Initial contribution |
| 15 |
**********************************************************************/ |
| 16 |
--> |
| 17 |
|
| 18 |
<head> |
| 19 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 20 |
<meta name="security" content="public" /> |
| 21 |
<meta name="Robots" content="index,follow" /> |
| 22 |
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' /> |
| 23 |
<meta name="DC.Type" content="task" /> |
| 24 |
<meta name="DC.Title" content="Accessing a datapool from a Hyades test" /> |
| 25 |
<meta name="abstract" content="You can use a datapool from a Hyades test by making calls to the datapool API from a test's generated Java code." /> |
| 26 |
<meta name="description" content="You can use a datapool from a Hyades test by making calls to the datapool API from a test's generated Java code." /> |
| 27 |
<meta name="DC.Relation" scheme="URI" content="../tasks/tdatapool.dita" /> |
| 28 |
<meta name="DC.Format" content="XHTML" /> |
| 29 |
<meta name="DC.Identifier" content="taccessdp" /> |
| 30 |
<meta name="DC.Language" content="en-us" /> |
| 31 |
<link rel="stylesheet" type="text/css" href="../css/ibmdita.css" /> |
| 32 |
<link rel="stylesheet" type="text/css" href="../css/swg_info_common.css" /> |
| 33 |
<title>Tutorial: Accessing a datapool from a TPTP JUnit test</title> |
| 34 |
</head> |
| 35 |
<body> |
| 36 |
|
| 37 |
<h1 class="topictitle1">Tutorial: Accessing a datapool from a TPTP JUnit test</h1> |
| 38 |
<div> |
| 39 |
<h2><br /> |
| 40 |
Objectives:</h2> |
| 41 |
<p>To create and access a datapool from an example test application using the Eclipse Test and |
| 42 |
Performance Tools Platform (TPTP). </p><h2> |
| 43 |
Time required</h2> |
| 44 |
<p>1 hour</p> |
| 45 |
<h2><strong><a name="BeforeYouBegin">Before you begin</a></strong></h2> |
| 46 |
<p><span>Before you start this tutorial, |
| 47 |
you need to <span> </span></span></p> |
| 48 |
<ol> |
| 49 |
<li> |
| 50 |
<span><span>Install Eclipse and the Eclipse Test and Performance Tools |
| 51 |
Platform (TPTP).</span></span></li> |
| 52 |
<li>Have a basic understanding of JUnit testing. For more information about JUnit testing, see <a |
| 53 |
href="http://www.junit.org">www.junit.org</a>.</li> |
| 54 |
<li>Configure and run the Agent Controller which corresponds to your TPTP version. For more information, see <a |
| 55 |
href="http://www.eclipse.org/tptp/home/downloads/installguide/HyadesDataCollection.html"> |
| 56 |
Getting started with the Agent Controller</a>.<br /> |
| 57 |
</li></ol> |
| 58 |
<h2><br /> |
| 59 |
<strong>Description</strong></h2> |
| 60 |
<p><span>In this tutorial, |
| 61 |
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.<br /></span><br /> |
| 62 |
This tutorial guides you through the following procedures:</p> |
| 63 |
<ol> |
| 64 |
<li><a href="#CreateUserApp">Creating a sample user application</a></li> |
| 65 |
<li><a href="#SetupTestApp" target="_self">Setting up a TPTP test |
| 66 |
suite</a></li> |
| 67 |
<li><a href="#CreateDatapool">Creating a datapool and initializing test |
| 68 |
data</a></li> |
| 69 |
<li><a href="#ModifyTestCasesDP">Modifying test cases to reference a |
| 70 |
datapool</a><br /> |
| 71 |
</li> |
| 72 |
</ol> |
| 73 |
|
| 74 |
|
| 75 |
<h2><a name="CreateUserApp">Creating a sample user application</a></h2><br /> |
| 76 |
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.<br /><ol> |
| 77 |
<li>Create a new Eclipse Java Project. <ul> |
| 78 |
<li>From the File menu choose <b>File > New > Project...</b> The |
| 79 |
New Project dialog appears.</li> |
| 80 |
<li>In the Wizards list, select <b>Java Project</b> and click <b>Next</b>. |
| 81 |
The Create a Java project page appears.</li> |
| 82 |
<li>Type <tt>datapoolExample</tt> for the Project name and click <b>Finish</b>. |
| 83 |
The datapoolExample project appears in the Navigator view.<br /> |
| 84 |
</li> |
| 85 |
</ul> |
| 86 |
</li> |
| 87 |
<li>Create the MyShoppingCart class.<ul> |
| 88 |
<li>In the Navigator view, right-click the <b>datapoolExample</b> |
| 89 |
project and choose <b>New > Class</b>. The New Java Class dialog |
| 90 |
appears.</li> |
| 91 |
<li>Type <tt>MyShoppingCart</tt> in the <b>Name</b> field.</li> |
| 92 |
<li>Clear the option to create the main method <b>public static void |
| 93 |
main(String[] args)</b>.</li> |
| 94 |
<li>Click Finish. The MyShoppingCart.java file appears in the Java |
| 95 |
editor.</li> |
| 96 |
<li>Type the following for the MyShoppingCart source:<pre> |
| 97 |
import java.util.Hashtable; |
| 98 |
|
| 99 |
public class MyShoppingCart { |
| 100 |
|
| 101 |
public Hashtable myFlowers; |
| 102 |
|
| 103 |
public MyShoppingCart() { |
| 104 |
|
| 105 |
myFlowers = new Hashtable(); |
| 106 |
myFlowers.put("Orchid", new Double(5.99)); |
| 107 |
myFlowers.put("Tulip", new Double(1.99)); |
| 108 |
myFlowers.put("Yellow Carnation", new Double(6.99)); |
| 109 |
myFlowers.put("White Rose", new Double(9.99)); |
| 110 |
myFlowers.put("Geraniums", new Double(4.99)); |
| 111 |
} |
| 112 |
|
| 113 |
public double itemCost(String itemName, int itemQuantity) |
| 114 |
{ |
| 115 |
Double price = (Double)myFlowers.get(itemName); |
| 116 |
|
| 117 |
if (price != null) { |
| 118 |
return price.doubleValue()*itemQuantity; |
| 119 |
} |
| 120 |
|
| 121 |
return -1; |
| 122 |
} |
| 123 |
}</pre></li> |
| 124 |
<li>Save MyShoppingCart.java. From the File menu choose <b>File > |
| 125 |
Save</b>.</li> |
| 126 |
</ul> |
| 127 |
</li> |
| 128 |
</ol> |
| 129 |
<h2><a name="SetupTestApp">Setting up a TPTP test suite</a></h2> |
| 130 |
<br /> 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 |
| 131 |
test for the itemCost method.<br /><ol> |
| 132 |
<li>Open the Test Perspective. |
| 133 |
<ul> |
| 134 |
<li>From the File menu, choose <b>Window > Open Perspective > |
| 135 |
Other</b>.</li> |
| 136 |
<li>Select <b>Test</b> and click <b>OK</b>. The <a href='javascript:executeCommand("org.eclipse.ui.views.showView(org.eclipse.ui.views.showView.viewId=org.eclipse.hyades.test.ui.TestNavigator)")'>Test Navigator</a> of the <a href='javascript:executeCommand("org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.hyades.ui.perspective.TestPerspective)")'>Test Perspective</a> appears.</li> |
| 137 |
</ul> |
| 138 |
</li> |
| 139 |
<li>Create a new TPTP JUnit Test.<ul> |
| 140 |
<li>In the <a href='javascript:executeCommand("org.eclipse.ui.views.showView(org.eclipse.ui.views.showView.viewId=org.eclipse.hyades.test.ui.TestNavigator)")'>Test Navigator</a> of the <a href='javascript:executeCommand("org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.hyades.ui.perspective.TestPerspective)")'>Test Perspective</a>, right-click the datapoolExample project and choose <b>New |
| 141 |
> Test Element...</b></li> |
| 142 |
<li>In the Test Element dialog, select <b>TPTP JUnit test</b> and click <b>Next</b>. The New Test Case dialog appears, prompting you to add JUnit libraries. Click <b>Yes</b> to add JUnit libraries.</li> |
| 143 |
<li> In the New JUnit Test Source Code page, type <b>MyShoppingCartTest</b> in the <b>Name</b> field. In the <b>Select how the test behavior is edited</b> section, choose the edit <b>In |
| 144 |
the test editor</b> option (the default).</li> |
| 145 |
<li>Click<b> Finish</b>. The TPTP JUnit Test editor appears, showing the |
| 146 |
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 <b>Implement |
| 147 |
Test Behavior as code</b> option in the <b>Source Information</b> section should be cleared. For more information, see |
| 148 |
<a href="../concepts/cJUnitTestEditor.htm">JUnit Test Suite Editor</a>.</li> |
| 149 |
</ul> |
| 150 |
|
| 151 |
</li> |
| 152 |
<li>Add the testItemCost and testMyShoppingCartConstructor methods. |
| 153 |
<ul> |
| 154 |
<li>In the Test Methods tab, click Add. A default name appears for your test.</li> |
| 155 |
<li>Add the testItemCost method. In the Name field, type <b>testItemCost</b> for the new test name. In the Description field, type <b>Test |
| 156 |
for the MyShoppingCart.itemCost(String, int) method</b>.</li> |
| 157 |
<li>Add the testMyShoppingCartConstructor method. In the Test Methods tab, click Add. In the Name field, type <b>testMyShoppingCartConstructor</b> for the new test name. In the Description field, type <b>Test |
| 158 |
for the MyShoppingCart constructor</b>.</li> |
| 159 |
</ul> |
| 160 |
</li> |
| 161 |
<li>Configure the test execution behavior. |
| 162 |
<ul> |
| 163 |
<li>Add a test execution loop. In the Behavior tab, click <b>Add.. |
| 164 |
> Loop</b>. In the Name field, type <b>Loop_MyShoppingCart</b>. In |
| 165 |
the Number of Iterations field, type <b>1</b> (the default value).</li> |
| 166 |
<li>Add a testMyShoppingCartConstructor invocation. Select |
| 167 |
Loop_MyShoppingCart and click <b>Add... > invocation</b>. The Test Invocation |
| 168 |
dialog appears. Select Select <b>testMyShoppingCartConstructor</b> and |
| 169 |
click OK.</li> |
| 170 |
<li>Add a testItemCost invocation. Select |
| 171 |
Loop_MyShoppingCart and click <b>Add... > invocation</b>. The Test |
| 172 |
Invocation dialog appears. Select <b>testItemCost</b> and click OK.</li> |
| 173 |
<li>Save the test suite. From the File menu, choose <b>File > Save</b>. |
| 174 |
<br /> |
| 175 |
<img border="0" src="../images/testTutorial_Behavior.gif" width="534" |
| 176 |
height="407" alt=""/> <br /> |
| 177 |
Note: the Save command causes the TPTP JUnit Test editor to create |
| 178 |
test method stubs in <i>MyShoppingCartTest.java</i>.<br /> |
| 179 |
</li> |
| 180 |
</ul> |
| 181 |
</li> |
| 182 |
<li> |
| 183 |
Enter code for the generated JUnit test methods testMyShoppingCartConstructor and testItemCost.<ul> |
| 184 |
<li>Open the Java Perspective. From the file menu choose <b>Window |
| 185 |
> Open Perspective > Other... > Java</b>.</li> |
| 186 |
<li>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.</li> |
| 187 |
<li>Type the following code for the testMyShoppingCartConstructor method.<pre>public void testMyShoppingCartConstructor() |
| 188 |
throws Exception |
| 189 |
{ |
| 190 |
MyShoppingCart cart = new MyShoppingCart(); |
| 191 |
Double priceOrchid = (Double)cart.myFlowers.get("Orchid"); |
| 192 |
Double priceTulip = (Double)cart.myFlowers.get("Tulip"); |
| 193 |
|
| 194 |
assertTrue(priceOrchid.doubleValue() == 5.99); |
| 195 |
assertTrue(priceTulip.doubleValue() == 1.99); |
| 196 |
}</pre></li> |
| 197 |
<li>Type the following code for the testItemCost method. |
| 198 |
<pre>public void testItemCost() |
| 199 |
throws Exception |
| 200 |
{ |
| 201 |
MyShoppingCart cart = new MyShoppingCart(); |
| 202 |
double priceForTwo = cart.itemCost("Orchid", 2); |
| 203 |
|
| 204 |
assertTrue(priceForTwo == 11.98); |
| 205 |
}</pre> |
| 206 |
</li><li>Save MyShoppingCartTest.java. From the file menu, choose <b>File |
| 207 |
> Save</b>.</li></ul></li><li>Open the Test Perspective and create a test deployment. For an example, see <a href="../tasks/tdeploymentintro.htm">Creating a |
| 208 |
Test Deployment</a>.<br/></li> |
| 209 |
<li> |
| 210 |
Run the test using your custom deployment.<ul> |
| 211 |
<li>In the <a href='javascript:executeCommand("org.eclipse.ui.views.showView(org.eclipse.ui.views.showView.viewId=org.eclipse.hyades.test.ui.TestNavigator)")'>Test Navigator</a> of the <a href='javascript:executeCommand("org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.hyades.ui.perspective.TestPerspective)")'>Test Perspective</a>, right-click the MyShoppingCartTest test suite and choose <b>Run |
| 212 |
As > Run...</b>. The Run configuration dialog appears.</li> |
| 213 |
<li>In the Configurations pane, select <b>Test</b> and then click <b>New</b>.</li> |
| 214 |
<li>In the left pane of the Run configuration (<b>Select test to run</b>), expand datapoolExample and choose the MyShoppingCartTest test suite.</li> |
| 215 |
<li>In the right pane of the Run configuration, select the deployment you created section. </li> |
| 216 |
<li>In the Test Logs tab, clear the <b>Use defaults</b> option and |
| 217 |
select the datapoolExample project folder for the location.</li> |
| 218 |
<li>Click <b>Run</b> to launch the test.</li> |
| 219 |
</ul></li> |
| 220 |
<li>Double-click the MyShoppingCartTest test log, which appears in the <a href='javascript:executeCommand("org.eclipse.ui.views.showView(org.eclipse.ui.views.showView.viewId=org.eclipse.hyades.test.ui.TestNavigator)")'>Test Navigator</a> of the <a href='javascript:executeCommand("org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.hyades.ui.perspective.TestPerspective)")'>Test Perspective</a>. 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.<br /> |
| 221 |
<img border="0" src="../images/testTutorial_JUnitSuccess.gif" |
| 222 |
width="666" height="420" alt=""/></li></ol><br /> |
| 223 |
<h2><a name="CreateDatapool">Creating a datapool and initializing test |
| 224 |
data</a></h2> |
| 225 |
<br />In this procedure, you create a simple datapool to store test data. Using the datapool editor, you define a datapool's structural |
| 226 |
elements, including variables (columns), records (rows), and |
| 227 |
equivalence classes (groups of related records).<br /><ol> |
| 228 |
<li>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. |
| 229 |
<ul> |
| 230 |
<li>Type the following in a text editor. <pre> |
| 231 |
,ModelNumber::String,Description::String,Price::Double |
| 232 |
flowers,F0001,Orchid,5.99 |
| 233 |
flowers,F0002,Tulip,1.99 |
| 234 |
flowers,F0003,Yellow Carnation,6.99 |
| 235 |
flowers,F0004,White Rose,9.99 |
| 236 |
flowers,F0005,Geraniums,4.99</pre> |
| 237 |
</li><li>Save the file as flowerData.csv in a temporary external location.</li> |
| 238 |
</ul> |
| 239 |
</li> |
| 240 |
|
| 241 |
<li class="skipspace"><span>In Eclipse, open the Test perspective.</span> |
| 242 |
<ul> |
| 243 |
<li>From the File menu, choose <b>Window > Open Perspective > |
| 244 |
Other</b>.</li> |
| 245 |
<li>Select <b>Test</b> and click <b>OK</b>. The <a href='javascript:executeCommand("org.eclipse.ui.views.showView(org.eclipse.ui.views.showView.viewId=org.eclipse.hyades.test.ui.TestNavigator)")'>Test Navigator</a> of the <a href='javascript:executeCommand("org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.hyades.ui.perspective.TestPerspective)")'>Test Perspective</a> |
| 246 |
appears.</li> |
| 247 |
</ul> |
| 248 |
</li> |
| 249 |
<li class="skipspace"><span>In the <a href='javascript:executeCommand("org.eclipse.ui.views.showView(org.eclipse.ui.views.showView.viewId=org.eclipse.hyades.test.ui.TestNavigator)")'>Test Navigator</a> of the <a href='javascript:executeCommand("org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.hyades.ui.perspective.TestPerspective)")'>Test Perspective</a>, right-click a |
| 250 |
project and select <span class="uicontrol">New > Test Elemen<b>t</b></span><b>...</b> |
| 251 |
The <span class="uicontrol">New Test Element </span> dialog appears.</span></li> |
| 252 |
<li class="skipspace"><span>In the Wizards list box, expand the <span |
| 253 |
class="uicontrol">Test Assets</span> folder and select <span |
| 254 |
class="uicontrol">Datapool</span>.</span></li> |
| 255 |
<li class="skipspace"><span>Click <b>Next</b>. The <span |
| 256 |
class="uicontrol">New Datapool</span> dialog appears.</span></li> |
| 257 |
<li class="skipspace"><span>Choose the datapool project folder and |
| 258 |
datapool name. In the list of existing projects, select the |
| 259 |
<b>datapoolExample</b> project. In the <span class="uicontrol">Name</span> |
| 260 |
field, type shoppingCartDatapool.</span> Click <b>Next</b> to |
| 261 |
continue and then <b>Next</b> again to open the CSV (comma delimited) file import page.</li> |
| 262 |
<li class="skipspace">Import the |
| 263 |
CSV file flowerData.csv.<ul> |
| 264 |
<li><span>In the <span class="uicontrol">CSV File</span> field, click <span class="uicontrol">Browse</span> |
| 265 |
and navigate to flowerData.csv.</span></li> |
| 266 |
<li><span>Check the <span class="uicontrol">First row |
| 267 |
contains variable names and suggested types</span></span> option. The first row of flowerData.csv contains column headings and types.</li> |
| 268 |
<li>Check the <span><span class="uicontrol">First column contains |
| 269 |
equivalence class names</span></span> option. Equivalence classes group related data. The first column of flowerData.csv specifies a single equivalence class called flowers.</li></ul> |
| 270 |
</li> |
| 271 |
<li class="skipspace"><span>Click <span class="uicontrol">Finish</span>.</span> If you specified initial datapool dimensions, which may conflict with the CSV file dimensions, a dialog appears. Click <b>Yes</b> to use the dimensions of the CSV file. Click <b>No</b> to use the specified initial dimensions (and possibly truncate the CSV file data).<span> |
| 272 |
</span>The Datapool editor appears, showing the data contained in the flowers equivalence class. <br /> |
| 273 |
<img |
| 274 |
border="0" src="../images/testTutorial_datapoolAppears.gif" |
| 275 |
width="354" height="416" alt=""/> |
| 276 |
<br />For detailed information about creating a datapool, see <a |
| 277 |
href="../tasks/tcreatedatapool.htm">Creating a datapool</a>.</li> |
| 278 |
</ol> |
| 279 |
<h2><a name="ModifyTestCasesDP"><br /> |
| 280 |
Modifying test cases to reference a |
| 281 |
datapool</a></h2> |
| 282 |
<br />In this procedure, you use the datapool API to replace hard-coded test values with |
| 283 |
references to datapool columns. |
| 284 |
|
| 285 |
<h3>Adding libraries required by the datapool API</h3>The datapool API requires various TPTP and Eclipse Modeling Framework (EMF) libraries. In this procedure, you add the libraries to your Java build path.<br /> |
| 286 |
<ol> |
| 287 |
<li>Open the Java Perspective. From the file menu choose <b>Window > |
| 288 |
Open Perspective > Java</b>.</li> |
| 289 |
<li>Open the <b>datapoolExample</b> project properties. In the Navigator or Package Explorer window, right-click the <b>datapoolExample</b> project folder and choose <b>Properties</b>. The Properties for <b>datapoolExample</b> dialog appears.</li> |
| 290 |
<li>Add the following library JARs to the Java build path of the <b>datapoolExample</b> project: |
| 291 |
|
| 292 |
<p> |
| 293 |
<ul> |
| 294 |
<li><b><i>org.eclipse.tptp.platform.models_<version>/tptp-models.jar</i></b></li> |
| 295 |
<li><b><i>org.eclipse.tptp.platform.models.hierarchy_<version>/tptp-models-hierarchy.jar</i></b></li> |
| 296 |
<li><b><i>org.eclipse.emf.common_<version>.jar</i></b></li> |
| 297 |
<li><b><i>org.eclipse.emf.ecore_<version>.jar</i></b></li> |
| 298 |
<li><b><i>org.eclipse.emf.ecore.xmi_<version>.jar</i></b></li> |
| 299 |
<li><b><i>org.eclipse.equinox.common_<version>.jar</i></b></li> |
| 300 |
</ul> |
| 301 |
</p> |
| 302 |
|
| 303 |
<p>where <b><i><version></i></b> is the current version of the library JAR.</p> |
| 304 |
|
| 305 |
<p>Steps:</p> |
| 306 |
|
| 307 |
<ul> |
| 308 |
<li>In the left pane, select <b>Java Build Path</b>. </li> |
| 309 |
<li>In the right pane, select the <b>Libraries</b> tab. </li> |
| 310 |
<li>For each of the library JARs, complete the following steps: |
| 311 |
|
| 312 |
<ul> |
| 313 |
<li>If the library JAR is on the Java build path of the <b>datapoolExample</b> project, continue to the next library JAR. Otherwise, continue to the next step.</li> |
| 314 |
<li>Click <b>Add External JARs...</b>.</li> |
| 315 |
<li>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 <b>TPTP Libraries</b> or <b>JUnit 3</b> classpath container for the <b>datapoolExample</b> project. </li> |
| 316 |
<li>Click <b>Open</b>.</li> |
| 317 |
</ul> |
| 318 |
</li> |
| 319 |
</ul> |
| 320 |
</li> |
| 321 |
<li>Click <b>OK</b> to close the Properties dialog.</li> |
| 322 |
</ol><h3><br /> |
| 323 |
Using the datapool API</h3>In this procedure, you modify the MyShoppingCartTest class and testMyShoppingCartConstructor method to utilize the <span> |
| 324 |
shoppingCartDatapool</span>. |
| 325 |
<br /> |
| 326 |
<ol> |
| 327 |
<li>If required, open the Java Perspective. From the file menu choose <b>Window > |
| 328 |
Open Perspective > Java</b>.</li> |
| 329 |
<li>Open MyShoppingCartTest.java. In the Navigator, open the |
| 330 |
datapoolExample project folder and double-click |
| 331 |
MyShoppingCartTest.java. The MyShoppingCartTest.java contents |
| 332 |
appear in the Java editor.</li> |
| 333 |
<li>Add the following import statements to the MyShoppingCartTest class.<pre>import org.eclipse.hyades.execution.runtime.datapool.IDatapool; |
| 334 |
import org.eclipse.hyades.execution.runtime.datapool.IDatapoolFactory; |
| 335 |
import org.eclipse.hyades.execution.runtime.datapool.IDatapoolIterator; |
| 336 |
import org.eclipse.hyades.models.common.datapool.impl.Common_DatapoolFactoryImpl;</pre></li> |
| 337 |
<li>Declare an IDatapoolIterator class instance. This class instance will be initialized in your set up code and used in test methods.<pre>public class MyShoppingCartTest extends HyadesTestCase { |
| 338 |
|
| 339 |
IDatapoolIterator dpIterator; |
| 340 |
|
| 341 |
//...</pre></li> |
| 342 |
<li>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. <b>Note:</b> specify your fully qualified workspace path in place of <b><workspace></b> in the java.io.File constructor.<pre>protected void setUp() throws Exception { |
| 343 |
// Initialize the datapool factory |
| 344 |
IDatapoolFactory dpFactory = new Common_DatapoolFactoryImpl(); |
| 345 |
|
| 346 |
// Load the shoppingCartDatapool datapool |
| 347 |
IDatapool datapool = dpFactory.load( |
| 348 |
new java.io.File("<b><workspace></b>\\datapoolExample\\shoppingCartDatapool.datapool"), |
| 349 |
false); |
| 350 |
|
| 351 |
// Create an iterator to traverse the datapool |
| 352 |
dpIterator = dpFactory.open(datapool,"org.eclipse.hyades.datapool.iterator.DatapoolIteratorSequentialPrivate"); |
| 353 |
|
| 354 |
// Initialize the datapool to traverse the first equivalence class. |
| 355 |
dpIterator.dpInitialize(datapool,0); |
| 356 |
}</pre><b>Notes: </b> |
| 357 |
<ul> |
| 358 |
<li>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.</li> |
| 359 |
<li>If you want to programmatically edit a datapool import org.eclipse.hyades.edit.datapool.</li> |
| 360 |
<li>The second parameter in the IDatapoolFactory open method represents the type of iterator. For shared datapools use DatapoolIteratorSequentialPrivate.</li> |
| 361 |
</ul> |
| 362 |
</li><li>Modify the testMyShoppingCartConstructor method to utilize the <span> |
| 363 |
shoppingCartDatapool</span>. |
| 364 |
<pre>public void testMyShoppingCartConstructor() |
| 365 |
throws Exception |
| 366 |
{ |
| 367 |
MyShoppingCart cart = new MyShoppingCart(); |
| 368 |
|
| 369 |
// Traverse through datapool... |
| 370 |
// Test if constructor initializes each flower record appropriately |
| 371 |
while(!dpIterator.dpDone()) |
| 372 |
{ |
| 373 |
String Description = dpIterator.dpCurrent().getCell("Description").getStringValue(); |
| 374 |
double datapoolPrice = dpIterator.dpCurrent().getCell("Price").getDoubleValue(); |
| 375 |
|
| 376 |
// Test if the flower is accounted for in the application |
| 377 |
Double applicationPrice; |
| 378 |
assertNotNull(applicationPrice = (Double)cart.myFlowers.get(Description)); |
| 379 |
|
| 380 |
// Test that the application price equals the price in the datapool |
| 381 |
assertTrue(applicationPrice.doubleValue() == datapoolPrice); |
| 382 |
|
| 383 |
dpIterator.dpNext(); |
| 384 |
} |
| 385 |
}</pre></li><li>Save MyShoppingCartTest.java. From the file menu, choose <b>File |
| 386 |
> Save</b>.</li> |
| 387 |
<li>Add the datapool to your testArtifact. |
| 388 |
<ul> |
| 389 |
<li>Open the Test Perspective. In the <a href='javascript:executeCommand("org.eclipse.ui.views.showView(org.eclipse.ui.views.showView.viewId=org.eclipse.hyades.test.ui.TestNavigator)")'>Test Navigator</a> of the <a href='javascript:executeCommand("org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.hyades.ui.perspective.TestPerspective)")'>Test Perspective</a>, double-click testDeployment to open it in the editor.</li> |
| 390 |
<li>In the <b>Pairs</b> tab, select testAsset and click <b>Open...</b></li> |
| 391 |
<li>In the <b>Test Assets</b> tab, click <b>Add...</b>. Select the datapool and click <b>OK</b>. Save the asset.</li> |
| 392 |
</ul> |
| 393 |
</li> |
| 394 |
<li>Run the test using your custom deployment.<ul> |
| 395 |
<li>Open the Test Perspective. In the <a href='javascript:executeCommand("org.eclipse.ui.views.showView(org.eclipse.ui.views.showView.viewId=org.eclipse.hyades.test.ui.TestNavigator)")'>Test Navigator</a> of the <a href='javascript:executeCommand("org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.hyades.ui.perspective.TestPerspective)")'>Test Perspective</a>, right-click the MyShoppingCartTest test suite and choose <b>Run |
| 396 |
As > Run...</b>. The Run configuration dialog appears.</li> |
| 397 |
<li>In the left pane of the Run configuration, choose the MyShoppingCartTest test suite.</li> |
| 398 |
<li>In the right pane of the Run configuration, select the deployment you created in the <a |
| 399 |
href="#BeforeYouBegin">Before You Begin</a> section. </li> |
| 400 |
<li>In the Test Logs tab, clear the <b>Use defaults option</b> and |
| 401 |
select the datapoolExample project folder for the location.</li> |
| 402 |
<li>Click <b>Run</b> to launch the test.</li> |
| 403 |
</ul></li> |
| 404 |
<b>Notes: </b> |
| 405 |
<ul> |
| 406 |
<li>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.</li> |
| 407 |
</ul> |
| 408 |
<li>Double-click the new MyShoppingCartTest test log, which appears in the <a href='javascript:executeCommand("org.eclipse.ui.views.showView(org.eclipse.ui.views.showView.viewId=org.eclipse.hyades.test.ui.TestNavigator)")'>Test Navigator</a> of the <a href='javascript:executeCommand("org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.hyades.ui.perspective.TestPerspective)")'>Test Perspective</a>. The MyShoppingCartTest test log appears. Select the Events tab to view the test result details.</li></ol> |
| 409 |
</div> |
| 410 |
|
| 411 |
<p> |
| 412 |
<b>Related tasks</b><br/> |
| 413 |
<a href="../concepts/cjunitintro.htm">Testing with JUnit</a><br /> |
| 414 |
<a href="../tasks/tdatapoolsintro.htm">Providing tests with variable data</a><br /> |
| 415 |
<a href="../tasks/tdeploymentintro.htm">Creating a test deployment</a><br /> |
| 416 |
</p> |
| 417 |
|
| 418 |
</body> |
| 419 |
</html> |