JFCUnit的一个例子(LoginScreen,补充JFCUnit的文档的例子)

LoginScreen.java 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author leiwei
 * @version 1.0
 */

public class LoginScreen extends JFrame{
  JPanel jPanel1 = new JPanel();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  JTextField LoginNameTextField = new JTextField();
  JButton EnterButton = new JButton();
  JButton ExitButton = new JButton();
  BorderLayout borderLayout1 = new BorderLayout();
  JPasswordField PasswordTextField = new JPasswordField();
  String title ="";
  private GridBagLayout gridBagLayout1 = new GridBagLayout();
  public LoginScreen(String ititle) {
    try {
      jbInit();
      super.setTitle(ititle);
    }
    catch(Exception e) {
      e.printStackTrace();
    }

  }
  public LoginScreen() {
     try {
       jbInit();
       super.setTitle("Demo");
     }
     catch(Exception e) {
       e.printStackTrace();
     }

   }
  private void jbInit() throws Exception {
    this.setName("loginScreen");
    this.setTitle(title);
    this.setBounds(300,300,300,300);
    jLabel2.setFont(new java.awt.Font("Dialog", 0, 13));
    jLabel2.setText("密 码:");
    jLabel1.setFont(new java.awt.Font("Dialog", 0, 13));
    jLabel1.setText("用户名:");
    jPanel1.setLayout(gridBagLayout1);
    this.getContentPane().setLayout(borderLayout1);
    EnterButton.setFont(new java.awt.Font("Dialog", 0, 12));
    EnterButton.setText("确定");
    EnterButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        EnterButton_actionPerformed(e);
      }
    });
    ExitButton.setFont(new java.awt.Font("Dialog", 0, 12));
    ExitButton.setText("取消");
    ExitButton.addActionListener(new java.awt.event.ActionListener(){
      public void actionPerformed(ActionEvent e) {
        ExitButton_actionPerformed(e);
      }
    });
    LoginNameTextField.setText("");
    PasswordTextField.setText("");
    jPanel1.add(jLabel1,  new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(52, 49, 0, 0), 0, 0));
    jPanel1.add(LoginNameTextField,    new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(52, 7, 0, 73), 214, 0));
    jPanel1.add(PasswordTextField,       new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(17, 7, 0, 73), 214, 0));
    jPanel1.add(jLabel2,   new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(19, 49, 0, 7), 0, 0));
    jPanel1.add(EnterButton,   new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(35, 88, 62, 0), 12, 0));
    jPanel1.add(ExitButton,   new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(38, 91, 62, 73), 13, 0));
    this.getContentPane().add(jPanel1, BorderLayout.CENTER,0);
    this.pack();
    ExitButton.setName("ExitButton");
    EnterButton.setName("EnterButton");
    LoginNameTextField.setName("LoginNameTextField");
    PasswordTextField.setName("PasswordTextField");
  }


  public void show(){
    super.show();
  }
  public static void main(String args[]){
    JFrame dialog = new LoginScreen("loginScreen");
    dialog.show();
  }

  void ExitButton_actionPerformed(ActionEvent e) {
    System.exit(0);
  }

  void EnterButton_actionPerformed(ActionEvent e) {
    if(this.PasswordTextField.getPassword().length == 0){
      javax.swing.JOptionPane.showMessageDialog(this,"Login Error","Login Error",JOptionPane.ERROR_MESSAGE);
    }
  }
}

===============================

LoginScreenTest.java

import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;

import junit.extensions.jfcunit.JFCTestCase;
import junit.extensions.jfcunit.JFCTestHelper;
import junit.extensions.jfcunit.TestHelper;
import junit.extensions.jfcunit.eventdata.MouseEventData;
import junit.extensions.jfcunit.finder.DialogFinder;
import junit.extensions.jfcunit.finder.NamedComponentFinder;
import junit.framework.Test;
import junit.framework.TestSuite;

import com.neusoft.qik.demo.LoginScreen;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author leiwei
 * @version 1.0
 */

public class LoginScreenTest
    extends JFCTestCase {

  private LoginScreen loginScreen = null;
  private TestHelper helper = null;

  public LoginScreenTest(String name) {
    super(name);
  }

  protected void setUp() throws Exception {
    super.setUp();
    helper = new JFCTestHelper();
    loginScreen = new LoginScreen("LoginScreenTest: " + getName());
    loginScreen.setVisible(true);
  }

  protected void tearDown() throws Exception {
    loginScreen = null;
    helper.cleanUp(this);
    super.tearDown();
  }


  public void testUI() {
    JDialog dialog;
    NamedComponentFinder finder = new NamedComponentFinder(JComponent.class, "ExitButton" );
    JButton exitButton = (JButton) finder.find(loginScreen,0);
    assertNotNull("Could not find the Exit button", exitButton);

    NamedComponentFinder finder1 = new NamedComponentFinder(JComponent.class, "EnterButton" );
    JButton enterButton = (JButton) finder1.find(loginScreen, 0);
    assertNotNull("Could not find the Enter button", enterButton);

    NamedComponentFinder finder2 = new NamedComponentFinder(JComponent.class, "LoginNameTextField" );
    JTextField userNameField = (JTextField) finder2.find(loginScreen, 0);
    assertNotNull("Could not find the userNameField", userNameField);

    assertEquals("Username field is empty", "", userNameField.getText());

    JTextField passwordField = (JTextField) helper.findNamedComponent(
        "PasswordTextField", loginScreen, 0);
    assertNotNull("Could not find the passwordField", passwordField);

    assertEquals("Password field is empty", "", passwordField.getText());

    try {
      helper.enterClickAndLeave(new MouseEventData(this, enterButton));
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

    DialogFinder dFinder = new DialogFinder("Login Error");

    List showingDialogs = dFinder.findAll(loginScreen);
//    assertEquals("Number of dialogs showing is wrong", 2, showingDialogs.size());

    dialog = (JDialog) showingDialogs.get(0);
    assertEquals("Wrong dialog showing up", "Login Error", dialog.getTitle());

    helper.disposeWindow(dialog, this);
  }

 

 

  public static Test suite() {
    return new TestSuite(LoginScreenTest.class);
  }


  public void TestUI(){}

  public static void main(String args[]) {
    junit.textui.TestRunner.run(suite());

  }

}

你可能感兴趣的:(exception,String,文档,dialog,import,login)