< 上一课 | 下一课 >

课程 1.4:创建 Java 测试类以测试应用程序

课程 1.4 将指导您完成创建 Java™ 测试类以测试应用程序。

在开始之前,必须先完成课程 1.3。在本课程中,您将完成下列任务:
  1. 首先,将创建 Java 测试类:展开 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 类中。替换编辑器中的任何现有代码:
    注: 已经为英语语言环境创建了 TestInOutProxy.java Java 类,您可能必须根据其他语言环境对此代码进行修改。
    /*
      * Created on 4-Oct-2004
     *
      * TODO To change the template for this generated file go to
      * Window - Preferences - Java - Code Style - Code Templates
     */
    package sample.ims;
    import sample.ims.data.*;
    import com.ibm.connector2.ims.ico.IMSDFSMessageException;
    
    /**
      * @author ivyho
     *
      * TODO To change the template for this generated type comment go to
      * Window - Preferences - Java - Code Style - Code Templates
     */
    public class TestInOutProxy
    {
    	public static void main (String[] args)
    	{
    		try
    		{
    			// ---------------------------------------------------	
    			// Create the formatHandler, then create the input
    			// message bean from the formatHandler. 
    			// ---------------------------------------------------	
    			INOUTMSG input = new INOUTMSG();
    
    			int sz = input.getSize();
    			System.out.println("\nInitial size of input message is: " + sz);
    
    			// ---------------------------------------------------		
    			// Don't set the length (LL) field yet... wait until 
    			// input message has been adjusted to reflect only
    			// the number of array elements actually sent. 
    			// ---------------------------------------------------	
    			input.setWs__zz((short) 0);
    			input.setWs__trcd("SKS7 ");
    
    			// ---------------------------------------------------
    			// Construct an array and populate it with the elements
    			// to be sent to the IMS application program.  In this
    			// case three elements are sent.  
    			// ---------------------------------------------------	
    			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;
    
    			// ---------------------------------------------------
    			// Set the array on the input message.
    			// ---------------------------------------------------
    			input.setWs__customer(customers);
    			input.setIndx((short) 3);
    			
    			System.out.println("\nInitial value of INDX is: " + input.getIndx());			
    
    			// ---------------------------------------------------			
    			// Reallocate the buffer to the actual size 
    			// ---------------------------------------------------			
    			byte[] bytes = input.getBytes();
    			int size = input.getSize();
    			byte[] newBytes = new byte[size];
    			System.arraycopy(bytes, 0, newBytes, 0, size);
    
    			// ---------------------------------------------------			
    			// Set the bytes back into the format handler and set
    			// the length field of the input message, now that 
    			// we know the actual 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());				
    
    			// ---------------------------------------------------
    			// Set fields that follow the array after the input 
    			// message has been adjusted.  
    			// ---------------------------------------------------			 			
    			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);
    			}
    		}
    	}
    }
  8. Ctrl-S 键以保存更改。
  9. 接下来,将测试此应用程序:展开 InOutArray 项目和 sample.ims 包。
  10. 右键单击 TestInOutProxy.java 类并展开运行,然后选择运行方式 > Java 应用程序
  11. 您应该会在控制台上看到以下输出:
    测试输出
祝贺您!您已完成“输入输出数组”教程。
< 上一课 | 下一课 >