Advertisement
Guest User

TEST

a guest
Apr 1st, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.91 KB | None | 0 0
  1. package converter;
  2.  
  3. import java.awt.Component;
  4.  
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JMenuItem;
  9. import javax.swing.JTextField;
  10.  
  11. import junit.extensions.abbot.ComponentTestFixture;
  12. import junit.extensions.abbot.TestHelper;
  13. import abbot.finder.matchers.ClassMatcher;
  14. import abbot.finder.matchers.JMenuItemMatcher;
  15. import abbot.tester.JButtonTester;
  16. import abbot.tester.JTextComponentTester;
  17.  
  18. /**
  19.  * Demonstrates testing a simple Swing UI ({@link CelsiusConverter}) with Abbot.
  20.  *
  21.  * @author Satadip Dutta (original)
  22.  * @author Tom Roche (ported to new-style Abbot, added more tests)
  23.  * @version $Id: CelsiusConverterTest.java,v 1.5 2005/05/06 23:27:16 tlroche Exp
  24.  *          $
  25.  */
  26. public class CelsiusConverterTest extends ComponentTestFixture {
  27.  
  28.     // for precision testing
  29.     final static int DEFAULT_PRECISION = 0;
  30.     final static int PRECISION1 = 1;
  31.     final static int PRECISION2 = 2;
  32.     final static int PRECISION3 = 3;
  33.     final static int PRECISION4 = 4;
  34.  
  35.     private CelsiusConverter cc;
  36.     private JTextField tempCelsius;
  37.     private JButton convertButton;
  38.     private JLabel outputLabel;
  39.     private JLabel celsiusLabel;
  40.  
  41.     /*
  42.      * ,,,
  43.      */
  44.    
  45.     private JTextComponentTester tt;
  46.     private JButtonTester bt;
  47.  
  48.     /** For older versions of JUnit. */
  49.     public CelsiusConverterTest(String name) {
  50.         super(name);
  51.     }
  52.  
  53.     protected void setUp() throws Exception {
  54.         cc = new CelsiusConverter();
  55.         JFrame frame = new JFrame();
  56.         cc.enframe(frame);
  57.         // Display at the current frame's desired size (avoids packing)
  58.         showWindow(frame, null, false);
  59.  
  60.         // only one JTextField in our UI, so we can just class-match
  61.         tempCelsius = (JTextField) getFinder().find(
  62.                 new ClassMatcher(JTextField.class));
  63.         // ditto for the JButton
  64.         convertButton = (JButton) getFinder().find(
  65.                 new ClassMatcher(JButton.class));
  66.         // But there's 2 JLabel's in our UI, so we need to add more
  67.         // information
  68.         outputLabel = (JLabel) getFinder().find(new ClassMatcher(JLabel.class) {
  69.             public boolean matches(Component c) {
  70.                 String text = CelsiusConverter
  71.                         .lookupString("output.label.text");
  72.                 return super.matches(c) && ((JLabel) c).getText().equals(text);
  73.             }
  74.         });
  75.        
  76.         celsiusLabel = (JLabel) getFinder().find(new ClassMatcher(JLabel.class) {
  77.             public boolean matches(Component c) {
  78.                 String text = CelsiusConverter
  79.                         .lookupString("input.label.text");
  80.                 return super.matches(c) && ((JLabel) c).getText().equals(text);
  81.             }
  82.         });
  83.  
  84.         /*
  85.          * YOUR CODE GOES HERE...
  86.          */
  87.        
  88.         tt = new JTextComponentTester();
  89.         bt = new JButtonTester();
  90.     }
  91.    
  92.     public void testCelsiusValueCorrect() throws Exception {
  93.         tt.actionEnterText(tempCelsius, "-100.1");
  94.         bt.actionClick(convertButton);
  95.         assertEquals(CelsiusConverter.celsiusOutput(-100, DEFAULT_PRECISION),
  96.                 celsiusLabel.getText());
  97.     }
  98.    
  99.     public void testNegativeNumberClick() throws Exception {
  100.         tt.actionEnterText(tempCelsius, "-45"); //$NON-NLS-1$
  101.         bt.actionClick(convertButton);
  102.         assertEquals(CelsiusConverter.fahrenheitOutput(-49, DEFAULT_PRECISION),
  103.                 outputLabel.getText());
  104.     }
  105.    
  106.     public void testNegativeNumberReturn() throws Exception {
  107.         tt.actionEnterText(tempCelsius, "-45"); //$NON-NLS-1$
  108.         bt.actionKeyPress(java.awt.event.KeyEvent.VK_ENTER);
  109.         assertEquals(CelsiusConverter.fahrenheitOutput(-49, DEFAULT_PRECISION),
  110.                 outputLabel.getText());
  111.     }
  112.    
  113.     public void testAbsoluteZeroClick() throws Exception {
  114.         tt.actionEnterText(tempCelsius, "-274"); //$NON-NLS-1$
  115.         bt.actionClick(convertButton);
  116.         assertEquals(CelsiusConverter.lookupString("output.label.text") + "NA", outputLabel.getText());
  117.     }
  118.    
  119.     public void testAbsoluteZeroReturn() throws Exception {
  120.         tt.actionEnterText(tempCelsius, "-274"); //$NON-NLS-1$
  121.         bt.actionKeyPress(java.awt.event.KeyEvent.VK_ENTER);
  122.         assertEquals(CelsiusConverter.lookupString("output.label.text") + "NA", outputLabel.getText());
  123.     }
  124.    
  125.     public void testBadInputClick() throws Exception {
  126.         // get default text
  127.         String originalText = outputLabel.getText();
  128.         // nothing should change if the input is not parseable as a double
  129.         tt.actionEnterText(tempCelsius, " HELLO "); //$NON-NLS-1$
  130.         bt.actionClick(convertButton);
  131.         assertTrue("Output changed for bad input", outputLabel.getText()
  132.                 .equals(originalText)); //$NON-NLS-1$
  133.     }
  134.  
  135.     public void testBadInputReturn() throws Exception {
  136.         // get default text
  137.         String originalText = outputLabel.getText();
  138.         // nothing should change if the input is not parseable as a double
  139.         tt.actionEnterText(tempCelsius, " HELLO "); //$NON-NLS-1$
  140.         bt.actionKeyPress(java.awt.event.KeyEvent.VK_ENTER);
  141.         assertTrue("Output changed for bad input", outputLabel.getText()
  142.                 .equals(originalText)); //$NON-NLS-1$
  143.     }
  144.     public void testChangePrecision() throws Exception {
  145.         JMenuItem item2 = (JMenuItem) getFinder().find(
  146.                 new JMenuItemMatcher(String.valueOf(PRECISION2))); //$NON-NLS-1$
  147.  
  148.         // the output should update to reflect a higher precision after making
  149.         // a change, even with no new input
  150.  
  151.         // initial precision is 0
  152.         tt.actionEnterText(tempCelsius, "25.23"); //$NON-NLS-1$
  153.         bt.actionClick(convertButton);
  154.  
  155.         // now update precision and make sure output fields update too
  156.         tt.actionSelectMenuItem(item2);
  157.  
  158.         assertEquals(
  159.                 "Failed to reflect change in precision", //$NON-NLS-1$
  160.                 CelsiusConverter.fahrenheitOutput(77.41, PRECISION2),
  161.                 outputLabel.getText());
  162.     }
  163.  
  164.     public void testHighPrecision3() throws Exception {
  165.         JMenuItem item3 = (JMenuItem) getFinder().find(
  166.                 new JMenuItemMatcher(String.valueOf(PRECISION3))); //$NON-NLS-1$
  167.         tt.actionSelectMenuItem(item3);
  168.         tt.actionEnterText(tempCelsius, "-45.543"); //$NON-NLS-1$
  169.         bt.actionClick(convertButton);
  170.  
  171.         assertEquals(
  172.                 "Failed to show answer with proper precision", //$NON-NLS-1$
  173.                 CelsiusConverter.fahrenheitOutput(-49.977, PRECISION3),
  174.                 outputLabel.getText());
  175.     }
  176.    
  177.     public void testHighPrecision1() throws Exception {
  178.         JMenuItem item1 = (JMenuItem) getFinder().find(
  179.                 new JMenuItemMatcher(String.valueOf(PRECISION1))); //$NON-NLS-1$
  180.         tt.actionSelectMenuItem(item1);
  181.         tt.actionEnterText(tempCelsius, "-45.5"); //$NON-NLS-1$
  182.         bt.actionClick(convertButton);
  183.  
  184.         assertEquals(CelsiusConverter.celsiusOutput(-45.5, PRECISION1),
  185.                 celsiusLabel.getText());
  186.     }
  187.    
  188.     public void testHighPrecision4() throws Exception {
  189.         JMenuItem item4 = (JMenuItem) getFinder().find(
  190.                 new JMenuItemMatcher(String.valueOf(PRECISION4))); //$NON-NLS-1$
  191.         tt.actionSelectMenuItem(item4);
  192.         tt.actionEnterText(tempCelsius, "-45.5555"); //$NON-NLS-1$
  193.         bt.actionClick(convertButton);
  194.  
  195.         assertEquals(CelsiusConverter.celsiusOutput(-45.5555, PRECISION4),
  196.                 celsiusLabel.getText());
  197.     }
  198.  
  199.     /*
  200.      * YOUR TEST CASES
  201.      */
  202.  
  203.     public static void main(String[] args) {
  204.         /*
  205.          * junit.textui.TestRunner.main(new String[] {
  206.          * CelsiusConverterTest.class.getName() });
  207.          */
  208.         TestHelper.runTests(args, CelsiusConverterTest.class);
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement