JAL-1270 JUnit to TestNG refactoring
[jalview.git] / test / jalview / gui / ProgressBarTest.java
1 package jalview.gui;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertTrue;
5 import org.testng.annotations.Test;
6 import org.testng.Assert;
7 import java.awt.Component;
8 import java.awt.FlowLayout;
9 import java.awt.GridLayout;
10
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
13
14 public class ProgressBarTest
15 {
16
17   private JPanel statusPanel;
18
19   private JLabel statusBar;
20
21   @Test
22   public void testConstructor_prematureInstantiation()
23   {
24     try
25     {
26       new ProgressBar(null, null);
27       Assert.fail("Expected exception");
28     } catch (NullPointerException e)
29     {
30       // expected
31     }
32   }
33
34   @Test
35   public void testConstructor_wrongLayout()
36   {
37     statusPanel = new JPanel();
38     statusPanel.setLayout(new FlowLayout());
39     try
40     {
41       new ProgressBar(statusPanel, null);
42       Assert.fail("expected exception");
43     } catch (IllegalArgumentException e)
44     {
45       // expected
46     }
47   }
48
49   @Test
50   public void testSetProgressBar()
51   {
52     statusPanel = new JPanel();
53     GridLayout layout = new GridLayout(1, 1);
54     statusPanel.setLayout(layout);
55     statusBar = new JLabel("nothing");
56     ProgressBar pb = new ProgressBar(statusPanel, statusBar);
57
58     /*
59      * Add 'hello'
60      */
61     pb.setProgressBar("hello", 1L);
62     verifyProgress(layout, new String[]
63     { "hello" });
64
65     /*
66      * Add 'world'
67      */
68     pb.setProgressBar("world", 2L);
69     verifyProgress(layout, new String[]
70     { "hello", "world" });
71
72     /*
73      * Remove 'hello' with no status bar update
74      */
75     pb.setProgressBar(null, 1L);
76     verifyProgress(layout, new String[]
77     { "world" });
78     assertEquals("nothing", statusBar.getText());
79
80     /*
81      * Remove 'world' with status bar update
82      */
83     pb.setProgressBar("goodbye", 2L);
84     verifyProgress(layout, new String[]
85     {});
86     assertEquals("goodbye", statusBar.getText());
87   }
88
89   /**
90    * Verify the right number of progress bars containing the expected messages
91    * respectively
92    * 
93    * @param layout
94    * @param msgs
95    */
96   private void verifyProgress(GridLayout layout, String[] msgs)
97   {
98     int msgCount = msgs.length;
99     assertEquals(1 + msgCount, layout.getRows());
100     assertEquals(msgCount, statusPanel.getComponentCount());
101     int i = 0;
102     for (Component c : statusPanel.getComponents())
103     {
104       assertTrue(c instanceof JPanel);
105       assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
106       assertEquals(msgs[i++],
107               ((JLabel) ((JPanel) c).getComponent(0)).getText());
108     }
109   }
110 }