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[]
70 pb.setProgressBar("world", 2L);
71 verifyProgress(layout, new String[]
72 { "hello", "world" });
75 * Remove 'hello' with no status bar update
77 pb.setProgressBar(null, 1L);
78 verifyProgress(layout, new String[]
80 assertEquals("nothing", statusBar.getText());
83 * Remove 'world' with status bar update
85 pb.setProgressBar("goodbye", 2L);
86 verifyProgress(layout, new String[]
88 assertEquals("goodbye", statusBar.getText());
92 * Verify the right number of progress bars containing the expected messages
98 private void verifyProgress(GridLayout layout, String[] msgs)
100 int msgCount = msgs.length;
101 assertEquals(1 + msgCount, layout.getRows());
102 assertEquals(msgCount, statusPanel.getComponentCount());
104 for (Component c : statusPanel.getComponents())
106 assertTrue(c instanceof JPanel);
107 assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
108 assertEquals(msgs[i++],
109 ((JLabel) ((JPanel) c).getComponent(0)).getText());