Menu

[r6]: / trunk / tests / functional / DiffStatsFuncTest.java  Maximize  Restore  History

Download this file

142 lines (101 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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);
}
}