Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit 7a02d544bf221a4e8b03d666769c9d3338b61e42 1 parent 5fdf321
@leadVisionary authored
View
8 .classpath
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="test"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
View
17 .project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>Menu</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
View
18 src/com/visionarysoftwaresolutions/menu/Menu.java
@@ -0,0 +1,18 @@
+package com.visionarysoftwaresolutions.menu;
+
+import com.visionarysoftwaresolutions.menu.domain.MenuIterator;
+
+public interface Menu {
+
+ public abstract void add(Menu child);
+
+ public abstract void remove(Menu child);
+
+ public abstract boolean hasChildren();
+
+ public abstract String getText();
+
+ public abstract Menu getChild(String text);
+
+ public abstract MenuIterator createIterator();
+}
View
40 src/com/visionarysoftwaresolutions/menu/domain/MenuComponent.java
@@ -0,0 +1,40 @@
+package com.visionarysoftwaresolutions.menu.domain;
+
+import com.visionarysoftwaresolutions.menu.Menu;
+
+abstract class MenuComponent implements Menu {
+ protected String name;
+
+ public MenuComponent(String name){
+ NullChecker.checkNull(name);
+ this.name = name;
+ }
+ /* (non-Javadoc)
+ * @see com.visionarysoftwaresolutions.webui.treeview.Menu#add(com.visionarysoftwaresolutions.webui.treeview.Menu)
+ */
+ @Override
+ public abstract void add(Menu child);
+ /* (non-Javadoc)
+ * @see com.visionarysoftwaresolutions.webui.treeview.Menu#remove(com.visionarysoftwaresolutions.webui.treeview.Menu)
+ */
+ @Override
+ public abstract void remove(Menu child);
+ /* (non-Javadoc)
+ * @see com.visionarysoftwaresolutions.webui.treeview.Menu#hasChildren()
+ */
+ @Override
+ public abstract boolean hasChildren();
+ /* (non-Javadoc)
+ * @see com.visionarysoftwaresolutions.webui.treeview.Menu#getText()
+ */
+ @Override
+ public String getText()
+ {
+ return name;
+ }
+ /* (non-Javadoc)
+ * @see com.visionarysoftwaresolutions.webui.treeview.Menu#getChild(java.lang.String)
+ */
+ @Override
+ public abstract Menu getChild(String text);
+}
View
42 src/com/visionarysoftwaresolutions/menu/domain/MenuComposite.java
@@ -0,0 +1,42 @@
+package com.visionarysoftwaresolutions.menu.domain;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import com.visionarysoftwaresolutions.menu.Menu;
+
+class MenuComposite extends MenuComponent {
+ private Map<String, MenuComponent> components;
+
+ public MenuComposite(String name) {
+ super(name);
+ components = new LinkedHashMap<String, MenuComponent>();
+ }
+
+ @Override
+ public void add(Menu child) {
+ NullChecker.checkNull(child);
+ components.put(child.getText(), (MenuComponent) child);
+ }
+
+ @Override
+ public void remove(Menu child) {
+ NullChecker.checkNull(child);
+ components.remove(child.getText());
+ }
+
+ @Override
+ public boolean hasChildren() {
+ return !components.isEmpty();
+ }
+
+ @Override
+ public Menu getChild(String text) {
+ return components.get(text);
+ }
+
+ @Override
+ public MenuIterator createIterator() {
+ return new MenuIterator(components.values());
+ }
+}
View
41 src/com/visionarysoftwaresolutions/menu/domain/MenuFactory.java
@@ -0,0 +1,41 @@
+package com.visionarysoftwaresolutions.menu.domain;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import com.visionarysoftwaresolutions.menu.Menu;
+
+public class MenuFactory {
+
+ public static Menu createMenu(String name){
+ return new MenuComposite(name);
+ }
+
+ public static Menu addItemToMenu(Menu menu, String item){
+ NullChecker.checkNull(menu);
+ NullChecker.checkNull(item);
+ menu.add(new MenuLeaf(item));
+ return menu;
+ }
+
+ public static Menu addSubMenu(Menu menu, Menu newMenu) {
+ NullChecker.checkNull(menu);
+ NullChecker.checkNull(newMenu);
+ menu.add(newMenu);
+ return menu;
+ }
+
+ public static Set<Menu> getSubMenus(Menu menu){
+ NullChecker.checkNull(menu);
+ Set<Menu> result = new LinkedHashSet<Menu>();
+ MenuIterator iterator = menu.createIterator();
+ while(iterator.hasNext()){
+ Menu contained = iterator.next();
+ if(contained instanceof MenuComposite)
+ {
+ result.add(contained);
+ }
+ }
+ return result;
+ }
+}
View
43 src/com/visionarysoftwaresolutions/menu/domain/MenuIterator.java
@@ -0,0 +1,43 @@
+package com.visionarysoftwaresolutions.menu.domain;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import com.visionarysoftwaresolutions.menu.Menu;
+
+public class MenuIterator implements Iterator<Menu>{
+ Collection<MenuComponent> items = null;
+ Iterator<MenuComponent> current = null;
+
+ public MenuIterator(){
+
+ }
+
+ public MenuIterator(Collection<MenuComponent> collection){
+ if(collection != null){
+ this.items = collection;
+ current = items.iterator();
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+ return current != null && current.hasNext();
+ }
+
+ @Override
+ public Menu next() {
+ if(current == null){
+ throw new NoSuchElementException("No next element");
+ }
+ return (Menu) current.next();
+ }
+
+ @Override
+ public void remove() {
+ if(current !=null){
+ current.remove();
+ }
+ }
+}
View
35 src/com/visionarysoftwaresolutions/menu/domain/MenuLeaf.java
@@ -0,0 +1,35 @@
+package com.visionarysoftwaresolutions.menu.domain;
+
+import com.visionarysoftwaresolutions.menu.Menu;
+
+class MenuLeaf extends MenuComponent {
+
+ public MenuLeaf(String name) {
+ super(name);
+ }
+
+ @Override
+ public void add(Menu child) {
+ throw new UnsupportedOperationException("Menu Items shouldn't have sub menu items. Use a Menu instead!");
+ }
+
+ @Override
+ public void remove(Menu child) {
+ throw new UnsupportedOperationException("Menu Items don't have sub menu items. Use a Menu instead!");
+ }
+
+ @Override
+ public boolean hasChildren() {
+ return false;
+ }
+
+ @Override
+ public Menu getChild(String text) {
+ throw new UnsupportedOperationException("Menu Items don't have sub menu items. Use a Menu instead!");
+ }
+
+ @Override
+ public MenuIterator createIterator() {
+ return new MenuIterator();
+ }
+}
View
9 src/com/visionarysoftwaresolutions/menu/domain/NullChecker.java
@@ -0,0 +1,9 @@
+package com.visionarysoftwaresolutions.menu.domain;
+
+class NullChecker {
+ public static void checkNull(Object child) {
+ if(null == child){
+ throw new UnsupportedOperationException("This menu doesn't support nulls");
+ }
+ }
+}
View
80 test/com/visionarysoftwaresolutions/menu/domain/MenuCompositeTest.java
@@ -0,0 +1,80 @@
+package com.visionarysoftwaresolutions.menu.domain;
+
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.visionarysoftwaresolutions.menu.Menu;
+
+public class MenuCompositeTest {
+ private static final String MENU = "Menu";
+ MenuComponent toTest;
+
+ @Before
+ public void setup(){
+ toTest = new MenuComposite(MENU);
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotGiveMenuNullName() {
+ toTest = new MenuComposite(null);
+ }
+
+ @Test
+ public void testMenuNameProperlySetByConstructorAndRetrievable(){
+ assertEquals(MENU, toTest.getText());
+ }
+
+ @Test
+ public void testHasChildrenIsFalseWhenEmpty(){
+ assertFalse(toTest.hasChildren());
+ }
+
+ @Test
+ public void testAddLeafChild() {
+ toTest.add(new MenuLeaf("Leaf"));
+ assertTrue(toTest.hasChildren());
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testAddNullChild(){
+ toTest.add(null);
+ assertFalse(toTest.hasChildren());
+ }
+
+ @Test
+ public void testAddCompositeChild() {
+ MenuComponent subMenu = new MenuComposite("SubMenu");
+ toTest.add(subMenu);
+ assertTrue(toTest.hasChildren());
+ }
+
+ @Test
+ public void testGetChild(){
+ toTest.add(new MenuLeaf("Books"));
+ Menu books = toTest.getChild("Books");
+ assertNotNull(books);
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testRemoveNullChild(){
+ toTest.remove(null);
+ }
+
+ @Test
+ public void testRemoveNonExistingChild(){
+ toTest.add(new MenuLeaf("Books"));
+ toTest.remove(new MenuLeaf("Movies"));
+ assertTrue(toTest.hasChildren());
+ assertNotNull(toTest.getChild("Books"));
+ }
+
+ @Test
+ public void testRemoveLastChild(){
+ toTest.add(new MenuLeaf("Books"));
+ Menu books = toTest.getChild("Books");
+ toTest.remove(books);
+ assertFalse(toTest.hasChildren());
+ }
+}
View
89 test/com/visionarysoftwaresolutions/menu/domain/MenuFactoryTest.java
@@ -0,0 +1,89 @@
+package com.visionarysoftwaresolutions.menu.domain;
+
+import static org.junit.Assert.*;
+
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.visionarysoftwaresolutions.menu.Menu;
+import com.visionarysoftwaresolutions.menu.domain.MenuFactory;
+
+public class MenuFactoryTest {
+ Menu toTest;
+
+ @Before
+ public void setup(){
+ toTest = MenuFactory.createMenu("My menu");
+ }
+ @Test
+ public void testMenuCreation() {
+ assertEquals("My menu", toTest.getText());
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotCreateMenuWithNull(){
+ MenuFactory.createMenu(null);
+ }
+
+ @Test
+ public void testCreateMenuAddItem(){
+ Menu result = MenuFactory.addItemToMenu(toTest, "Booze");
+ assertEquals(result,toTest);
+ assertTrue(result.hasChildren());
+ assertNotNull(result.getChild("Booze"));
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotAddItemToNullMenu(){
+ MenuFactory.addItemToMenu(null, "Booze");
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotAddNullItemToMenu(){
+ MenuFactory.addItemToMenu(toTest, null);
+ }
+
+ @Test
+ public void testCreateMenuAddSubMenu(){
+ Menu newMenu = MenuFactory.createMenu("Wines");
+ newMenu = MenuFactory.addSubMenu(toTest, newMenu);
+ assertTrue(toTest.hasChildren());
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotAddNullSubMenu(){
+ MenuFactory.addSubMenu(toTest, null);
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotAddItemsToMenuItems(){
+ toTest = MenuFactory.addItemToMenu(new MenuLeaf("Boogie"), "Booze");
+ }
+
+ @Test
+ public void testGetSubMenus(){
+ Menu sandwiches = MenuFactory.createMenu("Sandwiches and More");
+ MenuFactory.addSubMenu(toTest, sandwiches);
+ MenuFactory.addItemToMenu(toTest, "Tomato Bisque");
+ assertTrue(toTest.hasChildren());
+ Set<Menu> subMenus =MenuFactory.getSubMenus(toTest);
+ assertEquals(1, subMenus.size());
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotGetSubMenusWhenNullPassedIn(){
+ MenuFactory.getSubMenus(null);
+ }
+
+ @Test
+ public void testAddMoreItemsToSubMenu(){
+ Menu sandwiches = MenuFactory.createMenu("Sandwiches and More");
+ assertFalse(sandwiches.hasChildren());
+ toTest = MenuFactory.addSubMenu(toTest, sandwiches);
+ MenuFactory.addItemToMenu(sandwiches, "Tomato Bisque");
+ assertTrue(toTest.hasChildren());
+ assertTrue(sandwiches.hasChildren());
+ }
+}
View
65 test/com/visionarysoftwaresolutions/menu/domain/MenuIteratorTest.java
@@ -0,0 +1,65 @@
+package com.visionarysoftwaresolutions.menu.domain;
+
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+import java.util.NoSuchElementException;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.visionarysoftwaresolutions.menu.domain.MenuIterator;
+
+public class MenuIteratorTest {
+ MenuIterator it;
+ @Before
+ public void setup(){
+ it = new MenuIterator();
+ }
+
+ @Test
+ public void testGracefullyHandleNull(){
+ it = new MenuIterator(null);
+ assertFalse(it.hasNext());
+ it.remove();
+ }
+
+ @Test
+ public void testCreateEmptyIteratorAndCheckIfMore() {
+ assertFalse(it.hasNext());
+ }
+
+ @Test(expected=NoSuchElementException.class)
+ public void testExceptionThrownWhenNoNext(){
+ it.next();
+ }
+
+ @Test
+ public void testCreateIteratorWithTwoItems(){
+ ArrayList<MenuComponent> items = new ArrayList<MenuComponent>();
+ MenuComponent item1 = new MenuLeaf("Root Item");
+ MenuComponent item2 = new MenuLeaf("Another Item");
+ items.add(item1);
+ items.add(item2);
+ it = new MenuIterator(items);
+ assertTrue(it.hasNext());
+ assertEquals(item1, it.next());
+ assertEquals(item2, it.next());
+ assertFalse(it.hasNext());
+ }
+
+ @Test
+ public void testRemoveItemFromIterator(){
+ ArrayList<MenuComponent> items = new ArrayList<MenuComponent>();
+ MenuComponent item1 = new MenuLeaf("Root Item");
+ MenuComponent item2 = new MenuLeaf("Another Item");
+ items.add(item1);
+ items.add(item2);
+ it = new MenuIterator(items);
+ it.next();
+ it.remove();
+ assertEquals(1, items.size());
+ assertEquals(item2, items.get(0));
+ }
+
+}
View
54 test/com/visionarysoftwaresolutions/menu/domain/MenuLeafTest.java
@@ -0,0 +1,54 @@
+package com.visionarysoftwaresolutions.menu.domain;
+
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.visionarysoftwaresolutions.menu.domain.MenuIterator;
+
+public class MenuLeafTest {
+ private static final String DUMB_LEAF = "Dumb Leaf";
+ MenuLeaf toTest;
+
+ @Before
+ public void setup(){
+ toTest = new MenuLeaf(DUMB_LEAF);
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotGiveItemNullName() {
+ toTest = new MenuLeaf(null);
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotAddChildrenToLeaf() {
+ toTest.add(new MenuLeaf("nein!"));
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotRemoveChildrenFromLeaf() {
+ toTest.add(new MenuLeaf("nein!"));
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testCannotGetChildrenFromLeaf() {
+ toTest.getChild("nein!");
+ }
+
+ @Test
+ public void testMenuLeafHasNoChildren(){
+ assertFalse(toTest.hasChildren());
+ }
+
+ @Test
+ public void testGetText(){
+ assertEquals(DUMB_LEAF, toTest.getText());
+ }
+
+ @Test
+ public void testIteratorShowsNoIteration(){
+ MenuIterator it = toTest.createIterator();
+ assertFalse(it.hasNext());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.