AnchorLayoutManagerTest.java

package edu.hawaii.ics.yucheng;

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JPanel;

import junit.framework.TestCase;

/**
 * Tests the AnchorLayoutManager class.
 */
public class AnchorLayoutManagerTest extends TestCase {

    /**
     * Tests the addLayoutComponent method.
     * 
     * @throws Exception
     */
    public void testAddLayoutComponent1() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        try {
            manager.addLayoutComponent(null, new JButton());
            fail();
        } catch (final NullPointerException e) {
            // This is expected.
        }
    }


    /**
     * Tests the addLayoutComponent method.
     * 
     * @throws Exception
     */
    public void testAddLayoutComponent2() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        try {
            manager.addLayoutComponent("T", null);
            fail();
        } catch (final NullPointerException e) {
            // This is expected.
        }
    }


    /**
     * Tests the addLayoutComponent method.
     * 
     * @throws Exception
     */
    public void testAddLayoutComponent3() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        manager.addLayoutComponent("T", new JButton());
    }


    /**
     * Tests the constructor.
     * 
     * @throws Exception
     */
    public void testConstructor1() throws Exception {
        new AnchorLayoutManager(1, 1);
    }


    /**
     * Tests the constructor.
     * 
     * @throws Exception
     */
    public void testConstructor2() throws Exception {
        try {
            new AnchorLayoutManager(0, 1);
            fail();
        } catch (final IllegalArgumentException e) {
            // This is expected.
        }
    }


    /**
     * Tests the constructor.
     * 
     * @throws Exception
     */
    public void testConstructor3() throws Exception {
        try {
            new AnchorLayoutManager(1, 0);
            fail();
        } catch (final IllegalArgumentException e) {
            // This is expected.
        }
    }


    /**
     * Tests the layoutContainer method.
     * 
     * @throws Exception
     */
    public void testLayoutContainer1() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        try {
            manager.layoutContainer(null);
            fail();
        } catch (final NullPointerException e) {
            // This is expected.
        }
    }


    /**
     * Tests the layoutContainer method.
     * 
     * @throws Exception
     */
    public void testLayoutContainer2() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        final JPanel panel = new JPanel();
        final JButton button = new JButton();
        button.setBounds(1, 1, 8, 8);
        manager.addLayoutComponent("", button);
        manager.addLayoutComponent("TL", button);
        manager.addLayoutComponent("BR", button);
        manager.addLayoutComponent("TLBR", button);
        manager.layoutContainer(panel);
    }


    /**
     * Tests the minimumLayoutSize method.
     * 
     * @throws Exception
     */
    public void testMinimumLayoutSize1() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        try {
            manager.minimumLayoutSize(null);
            fail();
        } catch (final NullPointerException e) {
            // This is expected.
        }
    }


    /**
     * Tests the minimumLayoutSize method.
     * 
     * @throws Exception
     */
    public void testMinimumLayoutSize2() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        final JPanel panel = new JPanel();
        final Dimension dimension = manager.minimumLayoutSize(panel);
        assertEquals(10, dimension.width);
        assertEquals(10, dimension.height);
    }


    /**
     * Tests the preferredLayoutSize method.
     * 
     * @throws Exception
     */
    public void testPreferredLayoutSize1() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        try {
            manager.preferredLayoutSize(null);
            fail();
        } catch (final NullPointerException e) {
            // This is expected.
        }
    }


    /**
     * Tests the preferredLayoutSize method.
     * 
     * @throws Exception
     */
    public void testPreferredLayoutSize2() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        final JPanel panel = new JPanel();
        panel.setBounds(0, 0, 20, 20);
        final Dimension dimension = manager.preferredLayoutSize(panel);
        assertEquals(20, dimension.width);
        assertEquals(20, dimension.height);
    }


    /**
     * Tests the removeLayoutComponent method.
     * 
     * @throws Exception
     */
    public void testRemoveLayoutComponent1() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        try {
            manager.removeLayoutComponent(null);
            fail();
        } catch (final NullPointerException e) {
            // This is expected.
        }
    }


    /**
     * Tests the removeLayoutComponent method.
     * 
     * @throws Exception
     */
    public void testRemoveLayoutComponent2() throws Exception {
        final AnchorLayoutManager manager = new AnchorLayoutManager(10, 10);
        manager.removeLayoutComponent(new JButton());
    }
}
Valid HTML 4.01 Valid CSS