練習 1.4:建立 Java 測試類別來測試應用程式

開始之前,您必須先完成練習 1.3:建立訊息緩衝區類別

建立 Java 測試類別

  1. 展開 MultiSegOutput 專案,展開 Java 資源區段,然後選取 sample.ims 套件。
  2. 按一下滑鼠右鍵,然後選取新建。選取新建 Java 類別圖示類別選項來建立新 Java 類別。
  3. Java 類別名稱欄位中,輸入 TestMultiSeg。請注意:所提供的 TestMultiSeg.java 類別只是範例;您可能需要配合您的 IMS 機器規格來變更交易碼。請向您的 IMS 管理者詢問交易碼。您可以在 TestMultiSeg.java 類別中找到陳述式 input.setIn__trcd("SKS6 "),並作修改。
  4. 確定來源資料夾包含 MultiSegOutput/JavaSource,並確定套件名稱包含 sample.ims
  5. 按一下完成
  6. 在 Java 類別編輯器中開啟 TestMultiSeg.java
  7. 在編輯器中,將所有程式碼更改為以下各行:

TestMultiSeg.java

/*******************************************************************************
 * 授權資料 - IBM 財產
 *  
 * com.ibm.j2c.cheatsheet.content
 *  
 *Copyright IBM Corporation 2004. All Rights Reserved. 
 * 
 * Note to U.S. Government Users Restricted Rights:  Use, duplication or disclosure restricted by GSA ADP  Schedule Contract with IBM Corp. 
 *******************************************************************************/
package sample.ims;

import com.ibm.etools.marshall.util.MarshallIntegerUtils;
import sample.ims.data.*;

public class TestMultiSeg
{
	public static void main (String[] args)
	{
		byte[] segBytes = null;
		int srcPos = 0;
		int dstPos = 0;
		int totalLen = 0;
		int remainLen = 0;
		byte[] buff;
		short LL = 0;
		short ZZ = 0;

		try
		{
			// ---------------------------------------------------			
			// 將資料移入 IMS 交易輸入訊息。
			// 利用輸入訊息格式處理常式方法
			// getSize() 來設定輸入訊息的 LL 欄位。
			// ---------------------------------------------------					
			InputMsg input = new InputMsg();
			input.setIn__ll((short) input.getSize());
			input.setIn__zz((short) 0);
			//-----------------------------------------------------------
			// 找出 IMS 管理者的交易碼
			//----------------------------------------------------------
			input.setIn__trcd("SKS6 ");
			input.setIn__data1("M2 SI1");
			input.setIn__data2("M3 SI1");

			// ---------------------------------------------------			
			// 執行 IMS 交易。會傳回多重區段輸出
			// 訊息。
			// ---------------------------------------------------				
			MSOImpl proxy = new MSOImpl();
			
			sample.ims.CCIBuffer output = proxy.runMultiSegOutput(input);

			// ---------------------------------------------------	
			// 擷取多重區段輸出訊息,
			// 作為一個使用輸出訊息格式
			// 處理常式方法 getBytes() 的位元組陣列。
			// ---------------------------------------------------			 			
			System.out.println(
				"\nSize of output message is: " + output.getSize());
			segBytes = output.getBytes();

			srcPos = 0;
			dstPos = 0;
			totalLen = segBytes.length;
			remainLen = totalLen;

			// ---------------------------------------------------	
			// 從緩衝區移入第一個區段物件。
			// ---------------------------------------------------			             
			buff = null;
			// 取得區段長度。
			LL =
				MarshallIntegerUtils.unmarshallTwoByteIntegerFromBuffer(
					segBytes,
					srcPos,
					true,
					MarshallIntegerUtils.SIGN_CODING_TWOS_COMPLEMENT);

			// 將區段放置在位元組陣列中。
			buff = new byte[LL];
			System.arraycopy(segBytes, srcPos, buff, dstPos, LL);
			remainLen -= LL;

			// 從位元組陣列建立及移入區段物件。
			OutputSeg1 S1 = new OutputSeg1();
			S1.setBytes(buff);
			System.out.println(
				"\nOutSeg1 LL is:    "
					+ S1.getOut__ll()
					+ "\nOutSeg1 ZZ is:    "
					+ S1.getOut__zz()
					+ "\nOutSeg1_DATA1 is: "
					+ S1.getOut__data1());

			// ---------------------------------------------------	
			// 從緩衝區移入第二個區段物件。
			// ---------------------------------------------------	
			srcPos += LL;
			buff = null;
			// 取得區段長度。
			LL =
				MarshallIntegerUtils.unmarshallTwoByteIntegerFromBuffer(
					segBytes,
					srcPos,
					true,
					MarshallIntegerUtils.SIGN_CODING_TWOS_COMPLEMENT);

			// 將區段放置在位元組陣列中。
			buff = new byte[LL];
			System.arraycopy(segBytes, srcPos, buff, dstPos, LL);
			remainLen -= LL;

			// 從位元組陣列建立及移入區段物件。
			
			OutputSeg2 S2 = new OutputSeg2();
			S2.setBytes(buff);
			System.out.println(
				"\nOutSeg2 LL is:    "
					+ S2.getOut__ll()
					+ "\nOutSeg2 ZZ is:    "
					+ S2.getOut__zz()
					+ "\nOutSeg2_DATA1 is: "
					+ S2.getOut__data1()
					+ "\nOutSeg2_DATA2 is: "
					+ S2.getOut__data2());
			// ---------------------------------------------------				
			// 從緩衝區移入第三個區段物件。
			// ---------------------------------------------------	
			srcPos += LL;
			buff = null;
			// 取得區段長度。
			LL =
				MarshallIntegerUtils.unmarshallTwoByteIntegerFromBuffer(
					segBytes,
					srcPos,
					true,
					MarshallIntegerUtils.SIGN_CODING_TWOS_COMPLEMENT);

			// 將區段放置在位元組陣列中。
			buff = new byte[LL];
			System.arraycopy(segBytes, srcPos, buff, dstPos, LL);
			remainLen -= LL;

			// 從位元組陣列建立及移入區段物件。
			OutputSeg3 S3 = new OutputSeg3();
			S3.setBytes(buff);
			System.out.println(
				"\nOutSeg3 LL is:    "
					+ S3.getOut__ll()
					+ "\nOutSeg3 ZZ is:    "
					+ S3.getOut__zz()
					+ "\nOutSeg3_DATA1 is: "
					+ S3.getOut__data1()
					+ "\nOutSeg3_DATA2 is: "
					+ S3.getOut__data2()
					+ "\nOutSeg3_DATA3 is: "
					+ S3.getOut__data3());
		}
		catch (Exception e)
		{
			System.out.println("\nCaught exception is: " + e);
		}
	}
}

測試應用程式

  1. 展開 MultiSegOutput 專案和 sample.ims 套件。
  2. 用滑鼠右鍵按一下 TestMultiSeg.java 類別,然後選取執行。選取執行身分 > Java 應用程式
  3. 您應該會在主控台上看到下列輸出:

    TestMSOProxy 輸出

恭喜!您已完成「MultiSegment 輸出指導教學」。

請檢視摘要中的資料來完成您的指導教學。

使用條款 | 讀者意見

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