交互测试接口的简单实现 reflect(反射) + XStream

阅读更多
交互测试接口的简单实现
 
1.       问题
或者你已经习惯了写 Hardcode JUnit Test Case。但这里有问题:
l         你写的代码要经过 Java compiler 才可运行,但实际的环境下不一定允许这样
l         很难用于交互测试与开发期的错误跟踪。不能要求你的交互测试环境总有一个 Java IDE。
 
上述问题在 Java Application 的 RMI 接口测试和错误跟踪时就更明显。
 
 
2.       解决方法
要做交互测试与错误跟踪,要面对的情况是不可料的,故很难用 Hardcode 的静态语言 Java 去做。但我对动态语言(如 BeanShell )也不大了解。但相对比较了解 Java 的反射机制与 XStream(http://xstream.codehaus.org/) 的从 XML 文本到 Java Object 的应用
 
Java 的 reflect(反射) + XStream 的 文本到 Object instance :
 
 
public class MyMethodInfo implements Comparable {
    public Method method ;
 
    public MyMethodInfo(Method method) {
       this . method = method;
    }
 
    public String toString() {
       StringBuilder builder = new StringBuilder();
       builder.append( method .getName() ).append( " (" );
       Class[] params = method .getParameterTypes();
       for ( int i = 0 ; i < params. length ; i++ ) {
           builder.append( params[i].getSimpleName() );
           if ( i < params. length - 1 ) {
              builder.append( ", " );
           }
       }
       builder.append( ')' );
       return builder.toString();
    }
 
    public int compareTo(MyMethodInfo o) {
       return this . method .getName().compareTo( o. method .getName() );
    }
 
}
 
 
public class MethodArgPanel extends JPanel {
 
    private static final long serialVersionUID = 1L;
    private JScrollPane jScrollPane = null ;
    private JTextArea argTextArea = null ;
 
    /**
      * This is the default constructor
      */
    public MethodArgPanel() {
       super ();
       initialize();
    }
 
    /**
      * This method initializes this
      *
      * @return void
      */
    private void initialize() {
       this .setSize(246, 230);
       this .setLayout( new BorderLayout());
       this .add(getJScrollPane(), BorderLayout. CENTER );
    }
 
    /**
      * This method initializes jScrollPane
      *
      * @return javax.swing.JScrollPane
      */
    private JScrollPane getJScrollPane() {
       if ( jScrollPane == null ) {
           jScrollPane = new JScrollPane();
           jScrollPane .setViewportView(getArgTextArea());
       }
       return jScrollPane ;
    }
 
    /**
      * This method initializes argTextArea
      *
      * @return javax.swing.JTextArea   
      */
    private JTextArea getArgTextArea() {
       if ( argTextArea == null ) {
           argTextArea = new JTextArea();
       }
       return argTextArea ;
    }
   
    public String getText() {
       return argTextArea .getText();
    }
 
// @jve:decl-index=0:visual-constraint="10,10 "
 
 
public class MainFrame extends JFrame {
 
    private static final long serialVersionUID = 1L;
 
    private JPanel jContentPane = null ;
 
    private JSplitPane mainSplitPane = null ;
 
    private JSplitPane leftSplitPane = null ;
 
    private JPanel cmdPanel = null ;
 
    private JButton execButton = null ;
 
    private JScrollPane argsScrollPane = null ;
 
    private JPanel argsPanel = null ;
 
    private JScrollPane resultScrollPane = null ;
 
    private JTextArea resultTextArea = null ;
 
    private JScrollPane methodScrollPane = null ;
 
    private JList methodList = null ;
 
    private MethodArgPanel[] methodArgPanels ;
 
    private Object proxy ;
 
    /**
      * This method initializes mainSplitPane
      *
      * @return javax.swing.JSplitPane
      */
    private JSplitPane getMainSplitPane() {
       if ( mainSplitPane == null ) {
           mainSplitPane = new JSplitPane();
           mainSplitPane .setDividerLocation( 200 );
           mainSplitPane .setRightComponent( getLeftSplitPane() );
           mainSplitPane .setLeftComponent( getMethodScrollPane() );
       }
       return mainSplitPane ;
    }
 
    /**
      * This method initializes leftSplitPane
      *
      * @return javax.swing.JSplitPane
      */
    private JSplitPane getLeftSplitPane() {
       if ( leftSplitPane == null ) {
           leftSplitPane = new JSplitPane();
           leftSplitPane .setOrientation( JSplitPane. VERTICAL_SPLIT );
           leftSplitPane .setDividerSize( 8 );
           leftSplitPane .setDividerLocation( 400 );
           leftSplitPane .setBottomComponent( getResultScrollPane() );
           leftSplitPane .setTopComponent( getCmdPanel() );
       }
       return leftSplitPane ;

你可能感兴趣的:(Swing,Java,junit,IDE,XML)