package functional;
import java.awt.Component;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.junit.Test;
import junit.extensions.abbot.ComponentTestFixture;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.Matcher;
import abbot.finder.MultipleComponentsFoundException;
import abbot.finder.matchers.ClassMatcher;
import abbot.tester.JButtonTester;
import abbot.tester.JTextFieldTester;
import diffstats.DiffFrame;
import diffstats.Launcher;
import diffstats.TextPanel;
public class DiffStatsFuncTest extends ComponentTestFixture {
DiffFrame diffFrame;
/** An Abbot button tester */
protected JButtonTester buttonTester = new JButtonTester();
/** An Abbot text field tester */
protected JTextFieldTester textTester = new JTextFieldTester();
public String SAMPLE_FILE = System.getProperty("user.dir") + File.separator +"tests" +
File.separator + "resources" + File.separator + "sample.udiff";
@Test
public void testFileChooserNull()throws Exception {
diffFrame = getDiffFrame(null);
clickButton("Browse");
selectFileInChooser(SAMPLE_FILE);
assertResultsText();
clickButton("Cancel");
}
@Test
public void testFileChooserEmptyArray()throws Exception {
diffFrame = getDiffFrame(new String[0]);
clickButton("Browse");
selectFileInChooser(SAMPLE_FILE);
assertResultsText();
clickButton("Cancel");
}
@Test
public void testCommandLineParameter()throws Exception {
String[] filePath = new String[1];
filePath[0] = SAMPLE_FILE;
diffFrame = getDiffFrame(filePath);
assertResultsText();
clickButton("Cancel");
}
@Test
public void testAboutDialog()throws Exception {
diffFrame = getDiffFrame(null);
clickButton("About");
JDialog aboutDialog = (JDialog)getFinder().find(new Matcher() {
public boolean matches(Component c) {
return c instanceof JDialog && ((JDialog)c).getTitle().equals("About DiffStats");
}
});
assertTrue(aboutDialog.isActive());
buttonTester.actionClick(((JButton)aboutDialog.getContentPane().getComponent(1)));
assertTrue(diffFrame.isActive());
}
private DiffFrame getDiffFrame(String[] filePath) throws ComponentNotFoundException,
MultipleComponentsFoundException {
Launcher.main(filePath);
return (DiffFrame) getFinder().find(new ClassMatcher(DiffFrame.class));
}
private void clickButton(final String buttonText) throws ComponentNotFoundException,
MultipleComponentsFoundException {
JButton button = (JButton)getFinder().find(new Matcher() {
public boolean matches(Component c) {
return c instanceof JButton && ((JButton)c).getText().equals(buttonText);
}
});
buttonTester.actionClick(button);
}
private void selectFileInChooser(String filePath) throws ComponentNotFoundException,
MultipleComponentsFoundException {
JFileChooser directoryChooser = (JFileChooser) getFinder().find(new ClassMatcher(JFileChooser.class));
JTextField textField = (JTextField) getFinder().find(directoryChooser, new ClassMatcher(JTextField.class));
textTester.actionEnterText(textField, filePath);
JButton chooserButton = (JButton) getFinder().find(directoryChooser, new ClassMatcher(JButton.class){
public boolean matches(Component component) {
return super .matches(component)
&& "Open".equals(((JButton) component).getText());
}});
buttonTester.actionClick(chooserButton);
}
private void assertResultsText() {
assertEquals("sample.udiff", diffFrame.getTextPanel().getFileField().getText());
TextPanel textPanel = diffFrame.getTextPanel();
assertEquals("121", ((JLabel)((JPanel) textPanel.getComponent(1)).getComponent(1)).getText());
assertEquals("100", ((JLabel)((JPanel) textPanel.getComponent(1)).getComponent(3)).getText());
assertEquals("192", ((JLabel)((JPanel) textPanel.getComponent(1)).getComponent(5)).getText());
assertEquals("413", ((JLabel)((JPanel) textPanel.getComponent(2)).getComponent(1)).getText());
assertEquals(46.5, Double.parseDouble(((JLabel)((JPanel) textPanel.getComponent(2)).getComponent(3)).getText()), .1);
}
}