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

開始之前,您必須先完成練習 1.3:建立 Java 方法

練習 1.4 會引導您建立 Java 測試類別來測試應用程式。在這個練習中,您將執行下列動作:

建立 Java 測試類別

  1. 展開 InOutArray 專案,展開 Java 資源區段,然後選取 sample.ims 套件。
  2. 按一下滑鼠右鍵,然後選取新建。選取新建 Java 類別圖示類別選項來建立新 Java 類別。
  3. 在 Java 類別名稱中,輸入 TestInOutProxy。請注意:所提供的 TestInOutProxy 類別只是範例;您可能需要配合您的 IMS 機器規格來變更交易碼。請向您的 IMS 管理者詢問交易碼。您可以在您要變更的程式碼中找到 input.setWs__trcd("SKS7 "); 這個陳述式。
  4. 確定來源資料夾欄位包含 InOutArray/JavaSource,且套件名稱欄位包含 sample.ims.data。
  5. 按一下完成
  6. 按兩下 TestInOutProxy,在 Java 編輯器中開啟該檔案。
  7. 複製下列所有程式碼,然後將它貼至 TestInOutProxy.java 類別中。取代編輯器中任何現有的程式碼。
  8. TestInOutProxy.java

    /*
     * 建立於 2004 年 10 月 4 日
     *
     * TODO 如果要變更這個所產生的檔案之範本,請前往
     * Window - 喜好設定   - Java - 程式碼樣式 - 程式碼範本
     */
    package sample.ims;
    import sample.ims.data.*;
    import com.ibm.connector2.ims.ico.IMSDFSMessageException;
    
    /**
     * @author ivyho
     *
     * TODO 如果要變更這個所產生的類型註解之範本,請前往
     * Window - 喜好設定   - Java - 程式碼樣式 - 程式碼範本
     */
    public class TestInOutProxy
    {
    	public static void main (String[] args)
    	{
    		try
    		{
    			// ---------------------------------------------------	
    			// 建立 formatHandler,然後從 formatHandler 建立輸入
    			// 訊息 Bean。
    			// ---------------------------------------------------	
    			INOUTMSG input = new INOUTMSG();
    
    			int sz = input.getSize();
    			System.out.println("\nInitial size of input message is: " + sz);
    
    			// ---------------------------------------------------		
    			// 請先不要設定長度 (LL) 欄位... 等
    			// 輸入訊息已調整為只反映
    			// 實際傳送的陣列元素數目時再設定。
    			// ---------------------------------------------------	
    			input.setWs__zz((short) 0);
    			input.setWs__trcd("SKS7 ");
    
    			// ---------------------------------------------------
    			// 建構陣列,
    			// 並將要傳送至 IMS 應用程式的元素移入陣列中。在這個範例中,
    			// 會傳送三個元素。
    			// ---------------------------------------------------	
    			Inoutmsg_ws__customer[] customers = new Inoutmsg_ws__customer[3];
    
    			Inoutmsg_ws__customer aCustomer1 = new Inoutmsg_ws__customer();
    			aCustomer1.setWs__cust__name("Cathy Tang");
    			aCustomer1.setWs__cust__number("CN001");
    			customers[0] = aCustomer1;
    
    			Inoutmsg_ws__customer aCustomer2 = new Inoutmsg_ws__customer();
    			aCustomer2.setWs__cust__name("Haley Fung");
    			aCustomer2.setWs__cust__number("CN002");
    			customers[1] = aCustomer2;
    
    			Inoutmsg_ws__customer aCustomer3 = new Inoutmsg_ws__customer();
    			aCustomer3.setWs__cust__name("Steve Kuo");
    			aCustomer3.setWs__cust__number("CN003");
    			customers[2] = aCustomer3;
    
    			// ---------------------------------------------------
    			// 設定輸入訊息上的陣列。
    			// ---------------------------------------------------
    			input.setWs__customer(customers);
    			input.setIndx((short) 3);
    			
    			System.out.println("\nInitial value of INDX is: " + input.getIndx());			
    
    			// ---------------------------------------------------			
    			// 將緩衝區重新配置為實際大小
    			// ---------------------------------------------------			
    			byte[] bytes = input.getBytes();
    			int size = input.getSize();
    			byte[] newBytes = new byte[size];
    			System.arraycopy(bytes, 0, newBytes, 0, size);
    
    			// ---------------------------------------------------			
    			// 將位元組設回格式處理常式中,並設定
    			// 輸入訊息的長度欄位,因為我們已經知道
    			// 實際大小了。
    			// ---------------------------------------------------			
    			input.setBytes(newBytes);
    			input.setWs__ll((short) size);
    			System.out.println("\nAdjusted size of input message is: " + size);
    			System.out.println("\nAdjusted size of INDX is: " + input.getIndx());				
    
    			// ---------------------------------------------------
    			// 將輸入訊息調整好之後,
    			// 請設定陣列後面的欄位。
    			// ---------------------------------------------------			 			
    			input.setWs__func__code("123456");
    
    			InOutImpl proxy = new InOutImpl();
    
    			INOUTMSG output = new sample.ims.data.INOUTMSG();
    			output = proxy.runInOut(input);
    
    			short outndx = output.getIndx();
    			System.out.println("\nOutput value of INDX is: " + outndx);
    
    			Inoutmsg_ws__customer outArray[] = output.getWs__customer();
    
    			for (int i = 0; i < outndx; i++)
    			{
    				System.out.println(
    					"\n"
    						+ outArray[i].getWs__cust__name()
    						+ outArray[i].getWs__cust__number());
    			}
    		}
    		catch (Exception e)
    		{
    			if (e instanceof IMSDFSMessageException)
    			{
    				System.out.println(
    					"\nIMS returned message: "
    						+ ((IMSDFSMessageException) e).getDFSMessage());
    			}
    			else
    			{
    				System.out.println(
    					"\nIMS Connector exception is: " + e);
    			}
    		}
    	}
    }
    
    
  9. 按下 Ctrl-S 來儲存變更

測試應用程式

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

    TestInOutProxy output

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

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

讀者意見

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