3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertTrue;
6 import java.awt.Component;
7 import java.awt.FlowLayout;
8 import java.awt.GridLayout;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
13 import org.testng.Assert;
14 import org.testng.annotations.Test;
16 public class ProgressBarTest
19 private JPanel statusPanel;
21 private JLabel statusBar;
23 @Test(groups = { "Functional" })
24 public void testConstructor_prematureInstantiation()
28 new ProgressBar(null, null);
29 Assert.fail("Expected exception");
30 } catch (NullPointerException e)
36 @Test(groups = { "Functional" })
37 public void testConstructor_wrongLayout()
39 statusPanel = new JPanel();
40 statusPanel.setLayout(new FlowLayout());
43 new ProgressBar(statusPanel, null);
44 Assert.fail("expected exception");
45 } catch (IllegalArgumentException e)
51 @Test(groups = { "Functional" })
52 public void testSetProgressBar()
54 statusPanel = new JPanel();
55 GridLayout layout = new GridLayout(1, 1);
56 statusPanel.setLayout(layout);
57 statusBar = new JLabel("nothing");
58 ProgressBar pb = new ProgressBar(statusPanel, statusBar);
63 pb.setProgressBar("hello", 1L);
64 verifyProgress(layout, new String[] { "hello" });
69 pb.setProgressBar("world", 2L);
70 verifyProgress(layout, new String[] { "hello", "world" });
73 * Remove 'hello' with no status bar update
75 pb.setProgressBar(null, 1L);
76 verifyProgress(layout, new String[] { "world" });
77 assertEquals("nothing", statusBar.getText());
80 * Remove 'world' with status bar update
82 pb.setProgressBar("goodbye", 2L);
83 verifyProgress(layout, new String[] {});
84 assertEquals("goodbye", statusBar.getText());
88 * Verify the right number of progress bars containing the expected messages
94 private void verifyProgress(GridLayout layout, String[] msgs)
96 int msgCount = msgs.length;
97 assertEquals(1 + msgCount, layout.getRows());
98 assertEquals(msgCount, statusPanel.getComponentCount());
100 for (Component c : statusPanel.getComponents())
102 assertTrue(c instanceof JPanel);
103 assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
104 assertEquals(msgs[i++],
105 ((JLabel) ((JPanel) c).getComponent(0)).getText());