如何使用JUnit测试void actionEvent内部的信息

huangapple go评论76阅读模式
英文:

How do i test with JUnit for information inside of a void actionEvent

问题

以下是您提供的代码的翻译部分:

import java.awt.Container;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.UnknownHostException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class frameIt extends JFrame implements ActionListener {

    Container cont = getContentPane();
    JLabel uL = new JLabel("USER");
    JLabel pL = new JLabel("PASS");
    JTextField userT1 = new JTextField();
    JPasswordField pass1 = new JPasswordField();
    JButton logButt = new JButton("LOGIN");
    JButton reButt = new JButton("RESET");

    frameIt() {

        cont.setLayout(null);

        uL.setBounds(20, 50, 50, 20);
        pL.setBounds(20, 80, 50, 20);
        userT1.setBounds(70, 50, 100, 30);
        pass1.setBounds(70, 80, 100, 30);
        logButt.setBounds(70, 100, 100, 30);
        reButt.setBounds(70, 130, 100, 30);

        cont.add(logButt);
        cont.add(reButt);
        cont.add(uL);
        cont.add(pL);
        cont.add(userT1);
        cont.add(pass1);

        logButt.addActionListener(this);
        reButt.addActionListener(this);
    }

    //auditing
    public String theLog(String user, String eventType) throws UnknownHostException {
        //UTC date and time string
        OffsetDateTime dateTimeUTC = OffsetDateTime.now(ZoneOffset.UTC);

        // create date and time string
        Date dateTimeLocal = Date.from(dateTimeUTC.toInstant());

        // create filewriter, bufferedwriter, and printwriter
        FileWriter FWrite = null;
        BufferedWriter buffWrite = null;
        PrintWriter printerWrite = null;

        // try block
        try {
            // create file
            File logFile = new File("Log.log");

            // check if file already exists
            // if file does not exist then create it
            if (!logFile.exists()) {
                logFile.createNewFile();
            }

            //filewriter, bufferedwriter, and printwriter
            FWrite = new FileWriter(logFile, true);
            buffWrite = new BufferedWriter(FWrite);
            printerWrite = new PrintWriter(buffWrite);

            //printwriter
           
            printerWrite.println("----------------------");
            printerWrite.println("UTC Date/Time: " + dateTimeUTC);
            printerWrite.println("Local Date/Time: " + dateTimeLocal);
            printerWrite.println("User name: " + user);
            printerWrite.println("Event type: " + eventType);
            printerWrite.println("----------------------");
            printerWrite.println("");

        } 
        // catch block 
        
        catch (IOException e) {
            System.out.println("Sorry, there was an error.");
        } finally {
           
            // printwriter
            printerWrite.close();
        }

        return null;
    }
    
    public void actionPerformed(ActionEvent aE)  {
        
        if (aE.getSource() == reButt) {
            userT1.setText("");
            pass1.setText("");
        }

        if (aE.getSource() == logButt) {
            String userText = userT1.getText();
            String pwdText = String.valueOf(pass1.getPassword());

            try {
                if (userText.equals("admin") && pwdText.equals("admin")) {
                    theLog(userText, "Good Login");
                    JOptionPane.showMessageDialog(this, "Login Correct");
                } else {
                    JOptionPane.showMessageDialog(this, "try again");
                    theLog(userText, "Failed Login");
                }
            } catch (IOException ex) {
                Logger.getLogger(frameIt.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String[] args) {
        //calling frameIt.java
        frameIt frame = new frameIt();
        //title
        frame.setTitle("Sign on Forever");
        //Want to see it? this is how!
        frame.setVisible(true);
        //Window size
        frame.setBounds(15, 15, 300, 300);
        //exits
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

请注意,我只提供了您给出的代码的翻译部分,不包括注释。如果您有任何进一步的问题或需要更多帮助,请随时提问。

英文:

I am trying to get information from inside a void actionEvent so I can use it for JUnit test ( first time trying to use JUnit ). But i am not sure how to do this. If anyone can throw some guidance, i would appreciate it.
Here is my code below. (this code it just to use with test with JUnit, i know its garbage sauce.

import java.awt.Container;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.UnknownHostException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class frameIt extends JFrame implements ActionListener {
Container cont = getContentPane();
JLabel uL = new JLabel("USER");
JLabel pL = new JLabel("PASS");
JTextField userT1 = new JTextField();
JPasswordField pass1 = new JPasswordField();
JButton logButt = new JButton("LOGIN");
JButton reButt = new JButton("RESET");
frameIt() {
cont.setLayout(null);
uL.setBounds(20, 50, 50, 20);
pL.setBounds(20, 80, 50, 20);
userT1.setBounds(70, 50, 100, 30);
pass1.setBounds(70, 80, 100, 30);
logButt.setBounds(70, 100, 100, 30);
reButt.setBounds(70, 130, 100, 30);
cont.add(logButt);
cont.add(reButt);
cont.add(uL);
cont.add(pL);
cont.add(userT1);
cont.add(pass1);
logButt.addActionListener(this);
reButt.addActionListener(this);
}
//auditing
public String theLog(String user, String eventType) throws UnknownHostException {
//UTC date and time string
OffsetDateTime dateTimeUTC = OffsetDateTime.now(ZoneOffset.UTC);
// create date and time string
Date dateTimeLocal = Date.from(dateTimeUTC.toInstant());
// create filewriter, bufferedwriter, and printwriter
FileWriter FWrite = null;
BufferedWriter buffWrite = null;
PrintWriter printerWrite = null;
// try block
try {
// create file
File logFile = new File("Log.log");
// check if file already exists
// if file does not exist then create it
if (!logFile.exists()) {
logFile.createNewFile();
}
//filewriter, bufferedwriter, and printwriter
FWrite = new FileWriter(logFile, true);
buffWrite = new BufferedWriter(FWrite);
printerWrite = new PrintWriter(buffWrite);
//printwriter
printerWrite.println("----------------------");
printerWrite.println("UTC Date/Time: " + dateTimeUTC);
printerWrite.println("Local Date/Time: " + dateTimeLocal);
printerWrite.println("User name: " + user);
printerWrite.println("Event type: " + eventType);
printerWrite.println("----------------------");
printerWrite.println("");
} 
// catch block 
catch (IOException e) {
System.out.println("Sorry, there was an error.");
} finally {
// printwriter
printerWrite.close();
}
return null;
}
public void actionPerformed(ActionEvent aE)  {
if (aE.getSource() == reButt) {
userT1.setText("");
pass1.setText("");
}
if (aE.getSource() == logButt) {
String userText = userT1.getText();
String pwdText = String.valueOf(pass1.getPassword());
try {
if (userText.equals("admin") && pwdText.equals("admin")) {
theLog(userText, "Good Login");
JOptionPane.showMessageDialog(this, "Login Correct");
} else {
JOptionPane.showMessageDialog(this, "try again");
theLog(userText, "Failed Login");
}
}
catch (IOException ex) {
Logger.getLogger(frameIt.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
//calling frameIt.java
frameIt frame = new frameIt();
//title
frame.setTitle("Sign on Forever");
//Want to see it? this is how!
frame.setVisible(true);
//Window size
frame.setBounds(15, 15, 300, 300);
//exits
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

答案1

得分: 1

我发现,与其尝试这样做,将ActionEvent从按钮点击转换为auth会更容易。就像这样。

btnLogout.setOnAction((ActionEvent e) -> {
    doLogin();
});

然后将其他所有内容放在doLogin中。
@Spindoctor的答案也不错,应该予以考虑。

英文:

I found instead of trying to do this, its easier to cast the ActionEvent's from the button click to the auth. Such as this.

btnLogout.setOnAction((ActionEvent e) -> {
doLogin();
});

and put everything else in the doLogin.
@Spindoctor s answer is good though and should be counted.

huangapple
  • 本文由 发表于 2020年8月31日 11:43:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63664482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定