MenuService { private MenuItem constructMenu(MenuBar bar, Object parent, String key) { String caption = messageService.getMessageNoDefault(key + "." + DISPLAY_NAME, VaadinUtils.getLocale()); MenuItem menuItem = null; if (!StringUtils.isEmpty(caption)) { String destination = messageService.getMessageNoDefault(key + "." + DESTINATION, VaadinUtils.getLocale()); String tabIndex = messageService.getMessageNoDefault(key + "." + TAB_INDEX, VaadinUtils.getLocale()); String mode = messageService.getMessageNoDefault(key + "." + MODE, VaadinUtils.getLocale()); String description = messageService.getMessageNoDefault(key + "." + DESCRIPTION, VaadinUtils.getLocale()); NavigateCommand command = null; if (!StringUtils.isEmpty(destination)) { command = new NavigateCommand(this, bar, destination, tabIndex, mode); } if (parent instanceof MenuBar) { menuItem = ((MenuBar) parent).addItem(caption, command); } else { menuItem = ((SubMenu) parent).addItem(caption, command); } if (description != null) { VaadinUtils.setTooltip(menuItem, description); } parent = menuItem.getSubMenu(); if (!StringUtils.isEmpty(destination)) { destinationMap.put(destination + "#" + mode, menuItem); } int index = 1; String childKey = messageService.getMessageNoDefault(key + "." + index + "." + DISPLAY_NAME, VaadinUtils.getLocale()); while (childKey != null) { constructMenu(bar, parent, key + "." + index); index++; childKey = messageService.getMessageNoDefault(key + "." + index + "." + DISPLAY_NAME, VaadinUtils.getLocale()); } if (checker != null && !checker.isAccessAllowed(destination)) { menuItem.setVisible(false); } } return menuItem; }  MenuBar constructMenu(String rootName); void setLastVisited(MenuBar menuBar, String destination); String getLastVisited(); void setVisible(MenuBar menu, String destination, boolean visible); void setVisible(MenuBar menu, String destination, String mode, boolean visible); static final String DESCRIPTION; static final String DESTINATION; static final String DISPLAY_NAME; static final String MODE; static final String TAB_INDEX; }
@Test public void testEmpty() { MenuBar bar = menuService.constructMenu("ocs.not.here"); assertTrue(bar.getItems().isEmpty()); }
@Test public void testMenuStructure() { MenuBar bar = menuService.constructMenu("ocs.menu"); assertEquals(2, bar.getItems().size()); MenuItem first = bar.getItems().get(0); assertEquals("Menu 1", first.getText()); assertEquals(2, first.getSubMenu().getItems().size()); MenuItem firstSub = first.getSubMenu().getItems().get(0); assertEquals("Menu 1.1", firstSub.getText()); MenuItem second = bar.getItems().get(1); assertEquals("Menu 2", second.getText()); }
@Test public void testDisableItem() { MenuBar bar = menuService.constructMenu("ocs.menu"); MenuItem first = bar.getItems().get(0); MenuItem firstSub = (MenuItem) first.getSubMenu().getChildren().findFirst().orElse(null); assertFalse(firstSub.isVisible()); assertTrue(first.isVisible()); }
@Test public void testDisableItemAndParent() { MenuBar bar = menuService.constructMenu("ocs.menu2"); MenuItem first = bar.getItems().get(0); MenuItem firstSub = (MenuItem) first.getSubMenu().getChildren().findFirst().orElse(null); assertFalse(firstSub.isVisible()); assertFalse(first.isVisible()); }