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;
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
14 public class ProgressBarTest
17 private JPanel statusPanel;
19 private JLabel statusBar;
22 public void testConstructor_prematureInstantiation()
26 new ProgressBar(null, null);
27 Assert.fail("Expected exception");
28 } catch (NullPointerException e)
35 public void testConstructor_wrongLayout()
37 statusPanel = new JPanel();
38 statusPanel.setLayout(new FlowLayout());
41 new ProgressBar(statusPanel, null);
42 Assert.fail("expected exception");
43 } catch (IllegalArgumentException e)
50 public void testSetProgressBar()
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);
61 pb.setProgressBar("hello", 1L);
62 verifyProgress(layout, new String[]
68 pb.setProgressBar("world", 2L);
69 verifyProgress(layout, new String[]
70 { "hello", "world" });
73 * Remove 'hello' with no status bar update
75 pb.setProgressBar(null, 1L);
76 verifyProgress(layout, new String[]
78 assertEquals("nothing", statusBar.getText());
81 * Remove 'world' with status bar update
83 pb.setProgressBar("goodbye", 2L);
84 verifyProgress(layout, new String[]
86 assertEquals("goodbye", statusBar.getText());
90 * Verify the right number of progress bars containing the expected messages
96 private void verifyProgress(GridLayout layout, String[] msgs)
98 int msgCount = msgs.length;
99 assertEquals(1 + msgCount, layout.getRows());
100 assertEquals(msgCount, statusPanel.getComponentCount());
102 for (Component c : statusPanel.getComponents())
104 assertTrue(c instanceof JPanel);
105 assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
106 assertEquals(msgs[i++],
107 ((JLabel) ((JPanel) c).getComponent(0)).getText());