以下の例では、Classics Java™ アプリケーションに対するテストを行います。
import resources.GetTreeDataExampleHelper; import com.rational.test.ft.*; import com.rational.test.ft.object.interfaces.*; import com.rational.test.ft.object.interfaces.SAP.*; import com.rational.test.ft.object.interfaces.siebel.*; import com.rational.test.ft.script.*; import com.rational.test.ft.value.*; import com.rational.test.ft.vp.*; /** * Description : Functional Test Script * @author Administrator */ public class GetTreeDataExample extends GetTreeDataExampleHelper { /** * Script Name : GetTreeDataExample * Generated : Jul 14, 2006 4:46:31 PM * Description : Functional Test Script * Original Host : WinNT Version 5.1 Build 2600 (S) * * @since 2006/07/14 * @author Administrator */ public void testMain(Object[] args) { //Start Classics Java Application startApp("ClassicsJavaA"); // Frame: ClassicsCD tree2().waitForExistence(); //Display available test data types available from tree System.out.println ("Available Tree Data Types: " + tree2().getTestDataTypes()); //Declare variables for tree ITestDataTree cdTree; ITestDataTreeNodes cdTreeNodes; ITestDataTreeNode[] cdTreeNode; //Variables to hold tree data cdTree = (ITestDataTree)tree2().getTestData("tree"); cdTreeNodes = cdTree.getTreeNodes(); cdTreeNode = cdTreeNodes.getRootNodes(); //Print out total number of nodes System.out.println ("Tree Total Node Count: " + cdTreeNodes.getNodeCount()); System.out.println ("Tree Root Node Count : " + cdTreeNodes.getRootNodeCount()); //Iterate through tree branches; this is a recursive method. for (int i = 0;i<cdTreeNode.length;++i) showTree(cdTreeNode[i], 0); //Shut down Classics Java Application classicsJava(ANY,MAY_EXIT).close(); } void showTree(ITestDataTreeNode node, int indent) { //Recursive method to print out tree nodes with proper indenting. //Determine number of tabs to use - to properly indent tree int tabCount = ( indent < tabs.length() ? indent : tabs.length() ); //Print out node name + number of children System.out.println(tabs.substring(0, tabCount) + node.getNode() + " (" + node.getChildCount() + "children)" ); //Determine if node has children; recursively call this same //method to print out child nodes. ITestDataTreeNode[] children = node.getChildren(); int childCount = ( children != null ? children.length : 0 ); for ( int i = 0; i < childCount; ++i ) showTree(children[i], indent+1); } //String of tabs used to indent tree view final String tabs = "¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t¥t"; }
このアプリケーションの最初の画面には Java Swing JTree コンポーネントがあり、 ここには 5 つのコンポーザーがリストされています。その下のレベルには、選択されたコンポーザーに使用可能な CD がリストされています。この例に示すコードは、ツリーのすべてのブランチから値を抽出して、 それらをコンソール・ウィンドウに表示します。
データを抽出するための最初のステップは、getTestData メソッドを使用してコントロールからデータを抽出することです。これは以下の構文を使用して行います。
ITestDataTree cdTree; cdTree = (ITestDataTree)tree2().getTestData("tree");
次のステップは、ツリー上のすべてのノードを含む配列を作成することです。これは以下のように行います。
ITestDataTreeNodes cdTreeNodes; ITestDataTreeNode[] cdTreeNode; cdTreeNodes = cdTree.getTreeNodes();//Encapsulates the root nodes. cdTreeNode = cdTreeNodes.getRootNodes();;//Extracts actual root nodes.
これは 2 つのステップから構成されるプロセスであることに注意してください。最初に、getTreeNodes メソッドを使用して TreeNodes オブジェクトを戻す必要があります。その後、getRootNodes メソッドを呼び出してツリーのルート・ノードの配列を抽出できます。
ツリー・ノードがあれば、再帰を使用して各ノードをウォークスルーしながら、その値とそこに含まれる直接の子の数を判別できます。 これは再帰的メソッド showTree で行います。 再帰的メソッドは、それ自体を呼び出すメソッドで、ツリー構造内をウォークスルーする効果的な方法です。 ノードの値を抽出するためには、getNode メソッドが使用されます。 ノードに含まれる子の数を抽出するには、getChildCount メソッドが使用されます。 この例でそれを実行するコードは、次のようになります。
System.out.println(tabs.substring(0, tabCount) + node.getNode()+" (" + node.getChildCount() + " children)");
なお、カスタム showTree メソッドで提供される追加のコーディングは、ツリーのインデントを示すタブを使用してフォーマットされた印刷を可能にするためのものです。