1   package net.sf.navigator.menu;
2   
3   import junit.extensions.TestSetup;
4   import junit.framework.Test;
5   import junit.framework.TestCase;
6   import junit.framework.TestSuite;
7   
8   import java.util.List;
9   
10  /**
11   * @author DEPeart
12   *
13   */
14  public class MenuComponentTest extends TestCase {
15  
16      public MenuComponentTest(String name) {
17          super(name);
18      }
19  
20      static MenuComponent masterMenu;
21  
22      public static Test suite() {
23          TestSuite suite = new TestSuite(MenuComponentTest.class);
24  
25          return new TestSetup(suite) {
26              protected void setUp() throws Exception {
27                  masterMenu = buildMenu();
28              }
29  
30              protected void tearDown() throws Exception {
31              }
32          };
33      }
34  
35      private static MenuComponent buildMenu() {
36          MenuComponent complex1 = new MenuComponent();
37          complex1.setName("complex1");
38          MenuComponent complex2 = new MenuComponent();
39          complex2.setName("complex1.1");
40          complex2.setParent(complex1);
41          MenuComponent complex3 = new MenuComponent();
42          complex3.setName("complex1.2");
43          complex3.setParent(complex1);
44          MenuComponent complex5 = new MenuComponent();
45          complex5.setName("complex1.2.1");
46          complex5.setParent(complex3);
47          MenuComponent complex4 = new MenuComponent();
48          complex4.setName("complex1.3");
49          complex4.setParent(complex1);
50          return complex1;
51      }
52  
53      /**
54       * Ensure we remove the child menus for a parent menu
55       */
56      public void testRemoveChildren() {
57          MenuComponent parent = buildMenu();
58          assertEquals(3, parent.getComponents().size());
59          parent.removeChildren();
60          assertEquals(0, parent.getComponents().size());
61      }
62  
63      /**
64       * Get the depth of a menu
65       */
66      public void testGetMenuDepth() {
67          assertEquals(3,masterMenu.getMenuDepth());
68      }
69  
70      /**
71       * Build the breadcrumb name for a top menu item
72       */
73      public void testSetBreadCrumbTopMenu() {
74          MenuComponent menu = new MenuComponent();
75          menu.setName("parent");
76          menu.setBreadCrumb(".");
77          assertEquals("parent",menu.getBreadCrumb());
78      }
79  
80      /**
81       * Build the breadcrumb name for child menu
82       */
83      public void testSetBreadCrumbChildMenu() {
84          masterMenu.setBreadCrumb(":");
85          List menus = masterMenu.getComponents();
86          MenuComponent menu = (MenuComponent) menus.get(0);
87          menu.setBreadCrumb(":");
88          assertEquals("complex1:complex1.1", menu.getBreadCrumb());
89      }
90  }