JAL-1753 Junit for new class
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 29 May 2015 13:07:56 +0000 (14:07 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 29 May 2015 13:07:56 +0000 (14:07 +0100)
test/jalview/gui/ProgressBarTest.java [new file with mode: 0644]

diff --git a/test/jalview/gui/ProgressBarTest.java b/test/jalview/gui/ProgressBarTest.java
new file mode 100644 (file)
index 0000000..2d3db68
--- /dev/null
@@ -0,0 +1,112 @@
+package jalview.gui;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.awt.Component;
+import java.awt.FlowLayout;
+import java.awt.GridLayout;
+
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
+import org.junit.Test;
+
+public class ProgressBarTest
+{
+
+  private JPanel statusPanel;
+
+  private JLabel statusBar;
+
+  @Test
+  public void testConstructor_prematureInstantiation()
+  {
+    try
+    {
+      new ProgressBar(null, null);
+      fail("Expected exception");
+    } catch (NullPointerException e)
+    {
+      // expected
+    }
+  }
+
+  @Test
+  public void testConstructor_wrongLayout()
+  {
+    statusPanel = new JPanel();
+    statusPanel.setLayout(new FlowLayout());
+    try
+    {
+      new ProgressBar(statusPanel, null);
+      fail("expected exception");
+    } catch (IllegalArgumentException e)
+    {
+      // expected
+    }
+  }
+
+  @Test
+  public void testSetProgressBar()
+  {
+    statusPanel = new JPanel();
+    GridLayout layout = new GridLayout(1, 1);
+    statusPanel.setLayout(layout);
+    statusBar = new JLabel("nothing");
+    ProgressBar pb = new ProgressBar(statusPanel, statusBar);
+
+    /*
+     * Add 'hello'
+     */
+    pb.setProgressBar("hello", 1L);
+    verifyProgress(layout, new String[]
+    { "hello" });
+
+    /*
+     * Add 'world'
+     */
+    pb.setProgressBar("world", 2L);
+    verifyProgress(layout, new String[]
+    { "hello", "world" });
+
+    /*
+     * Remove 'hello' with no status bar update
+     */
+    pb.setProgressBar(null, 1L);
+    verifyProgress(layout, new String[]
+    { "world" });
+    assertEquals("nothing", statusBar.getText());
+
+    /*
+     * Remove 'world' with status bar update
+     */
+    pb.setProgressBar("goodbye", 2L);
+    verifyProgress(layout, new String[]
+    {});
+    assertEquals("goodbye", statusBar.getText());
+  }
+
+  /**
+   * Verify the right number of progress bars containing the expected messages
+   * respectively
+   * 
+   * @param layout
+   * @param msgs
+   */
+  private void verifyProgress(GridLayout layout, String[] msgs)
+  {
+    int msgCount = msgs.length;
+    assertEquals(1 + msgCount, layout.getRows());
+    assertEquals(msgCount, statusPanel.getComponentCount());
+    int i = 0;
+    for (Component c : statusPanel.getComponents())
+    {
+      assertTrue(c instanceof JPanel);
+      assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
+      assertEquals(msgs[i++],
+              ((JLabel) ((JPanel) c).getComponent(0)).getText());
+    }
+  }
+}