练习 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

/*******************************************************************************
 * Licensed Materials - Property of 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
		{
			// ---------------------------------------------------			
			// Populate the IMS transaction input message with
			// data.  Use the input message format handler method 
			// getSize() to set the LL field of the input message.
			// ---------------------------------------------------					
			InputMsg input = new InputMsg();
			input.setIn__ll((short) input.getSize());
			input.setIn__zz((short) 0);
			//-----------------------------------------------------------
			// find out the transaction code from your IMS administrator
			//----------------------------------------------------------
			input.setIn__trcd("SKS6 ");
			input.setIn__data1("M2 SI1");
			input.setIn__data2("M3 SI1");

			// ---------------------------------------------------			
			// Run the IMS transaction.  The multi-segment output 
			// message is returned.
			// ---------------------------------------------------				
			MSOImpl proxy = new MSOImpl();
			
			sample.ims.CCIBuffer output = proxy.runMultiSegOutput(input);

			// ---------------------------------------------------	
			// Retrieve the multi-segment output message as a 
			// byte array using the output message format
			// handler method getBytes().
			// ---------------------------------------------------			 			
			System.out.println(
				"\nSize of output message is: " + output.getSize());
			segBytes = output.getBytes();

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

			// ---------------------------------------------------	
			// Populate first segment object from buffer. 
			// ---------------------------------------------------			             
			buff = null;
			// Get length of segment.
			LL =
				MarshallIntegerUtils.unmarshallTwoByteIntegerFromBuffer(
					segBytes,
					srcPos,
					true,
					MarshallIntegerUtils.SIGN_CODING_TWOS_COMPLEMENT);

			// Put segment in byte array.
			buff = new byte[LL];
			System.arraycopy(segBytes, srcPos, buff, dstPos, LL);
			remainLen -= LL;

			// Create and populate segment object from byte array.
			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());

			// ---------------------------------------------------	
			// Populate second segment object from buffer. 
			// ---------------------------------------------------	
			srcPos += LL;
			buff = null;
			// Get length of segment.
			LL =
				MarshallIntegerUtils.unmarshallTwoByteIntegerFromBuffer(
					segBytes,
					srcPos,
					true,
					MarshallIntegerUtils.SIGN_CODING_TWOS_COMPLEMENT);

			// Put segment in byte array.
			buff = new byte[LL];
			System.arraycopy(segBytes, srcPos, buff, dstPos, LL);
			remainLen -= LL;

			// Create and populate segment object from byte array.
			
			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());
			// ---------------------------------------------------				
			// Populate third segment object from buffer. 
			// ---------------------------------------------------	
			srcPos += LL;
			buff = null;
			// Get length of segment.
			LL =
				MarshallIntegerUtils.unmarshallTwoByteIntegerFromBuffer(
					segBytes,
					srcPos,
					true,
					MarshallIntegerUtils.SIGN_CODING_TWOS_COMPLEMENT);

			// Put segment in byte array.
			buff = new byte[LL];
			System.arraycopy(segBytes, srcPos, buff, dstPos, LL);
			remainLen -= LL;

			// Create and populate segment object from byte array.
			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. 右键单击 TestMSOProxy.java 类并选择运行。选择运行方式 > Java 应用程序
  3. 您应该会在控制台上看到以下输出:

    TestMSOProxy 输出

祝贺您!您已完成此多段输出教程。

查看总结中的内容之后,您就完成了整个教程。

使用条款 | 反馈

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