1 package net.sf.navigator.menu;
2
3 import java.util.ArrayList;
4
5 import junit.extensions.TestSetup;
6 import junit.framework.Test;
7 import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9
10 /**
11 * @author DEPeart
12 *
13 */
14 public class MenuRepositoryTest extends TestCase {
15
16 public MenuRepositoryTest(String name) {
17 super(name);
18 }
19
20 static MenuRepository repository;
21
22 public static Test suite() {
23 TestSuite suite = new TestSuite(MenuRepositoryTest.class);
24
25 return new TestSetup(suite) {
26 protected void setUp() throws Exception {
27 repository = buildRepository();
28 }
29
30 protected void tearDown() throws Exception {
31 }
32 };
33 }
34
35 public void setUp() throws Exception {
36 }
37
38 protected void tearDown() throws Exception {
39 }
40
41 private static MenuRepository buildRepository() {
42 MenuRepository rep = new MenuRepository();
43 MenuComponent parent = new MenuComponent();
44 parent.setDescription("parentMenuName");
45 parent.setName("parent");
46 MenuComponent child = new MenuComponent();
47 child.setDescription("childMenuName");
48 child.setName("child");
49 child.setParent(parent);
50 rep.addMenu(parent);
51 MenuComponent menu = new MenuComponent();
52 menu.setDescription("menu");
53 menu.setName("menu");
54 rep.addMenu(menu);
55 MenuComponent complex1 = new MenuComponent();
56 complex1.setName("complex1");
57 MenuComponent complex2 = new MenuComponent();
58 complex2.setName("complex1.1");
59 complex2.setParent(complex1);
60 MenuComponent complex3 = new MenuComponent();
61 complex3.setName("complex1.2");
62 complex3.setParent(complex1);
63 MenuComponent complex5 = new MenuComponent();
64 complex5.setName("complex1.2.1");
65 complex5.setParent(complex3);
66 MenuComponent complex4 = new MenuComponent();
67 complex4.setName("complex1.3");
68 complex4.setParent(complex1);
69 rep.addMenu(complex1);
70 return rep;
71 }
72
73 /**
74 * Ensure we return a child of a parent menu.
75 * @return Success
76 */
77 public void testGetMenu() {
78
79 MenuComponent menu = repository.getMenu("parent.child", ".");
80 assertEquals("childMenuName", menu.getDescription());
81 }
82
83 /**
84 * Ensure we return a parent menu only
85 * @return Success
86 */
87 public void testGetMenuParent() {
88
89 MenuComponent menu = repository.getMenu("parent", ".");
90 assertEquals("parentMenuName", menu.getDescription());
91 }
92
93 /**
94 * Test no menu found.
95 * @return null value. Failure
96 */
97 public void testGetMenuInvalidMenuName() {
98
99 MenuComponent menu = repository.getMenu("failure", ".");
100 assertNull(menu);
101 }
102
103 /**
104 * Test invalid delimiter, but first menu level requested and so will be found
105 * @return Sucess
106 */
107 public void testGetMenuInvalidDelimiterParentMenu() {
108
109 MenuComponent menu = repository.getMenu("parent", ",");
110 assertEquals("parentMenuName", menu.getDescription());
111 }
112
113 /**
114 * Test no menu found, because of invalid delimiter, when a child menu level
115 * @return null value. Failure
116 */
117 public void testGetMenuInvalidDelimiter() {
118
119 MenuComponent menu = repository.getMenu("parent.child", ",");
120 assertNull(menu);
121 }
122
123 /**
124 * Test for a null repository
125 * @return null value. Failure
126 */
127 public void testGetMenuNewRepository() {
128
129 MenuComponent menu = repository.getMenu("parent.child", ",");
130 assertNull(menu);
131 }
132
133 /**
134 * Test for a null delimiter
135 * @exception NullPointerException. Failure
136 */
137 public void testGetMenuNoDelimiter() {
138
139 try {
140 MenuComponent menu = repository.getMenu("parent.child", null);
141 fail("Should have thrown a NullPointerException");
142 } catch (NullPointerException e) {
143 }
144 }
145
146 /**
147 * Test for a null menuName
148 * @exception NullPointerException. Failure
149 */
150 public void testGetMenuNoMenuName() {
151
152 try {
153 MenuComponent menu = repository.getMenu(null, ".");
154 fail("Should have thrown a NullPointerException");
155 } catch (NullPointerException e) {
156 }
157 }
158
159 /**
160 * Test for an empty menuName
161 * @return null value. Failure
162 */
163 public void testGetMenuEmptyMenuName() {
164
165 MenuComponent menu = repository.getMenu("", ".");
166 assertNull(menu);
167 }
168
169 /**
170 * Test for an empty delimiter
171 * @return null value. Failure
172 */
173 public void testGetMenuEmptyDelimiter() {
174
175 MenuComponent menu = repository.getMenu("parent.child", "");
176 assertNull(menu);
177 }
178
179 /**
180 * Test for a null menuName
181 * @return -1
182 */
183 public void testGetMenuDepthNullMenuName() {
184
185 assertEquals(-1, repository.getMenuDepth(null));
186 }
187
188 /**
189 * Test for an invalid menuName
190 * @return -1
191 */
192 public void testGetMenuDepthNoMenuName() {
193
194 assertEquals(-1, repository.getMenuDepth("noMenu"));
195 }
196
197 /**
198 * Test for a depth of 1 menu level
199 * @return 1
200 */
201 public void testGetMenuDepthOf1() {
202
203 assertEquals(1, repository.getMenuDepth("menu"));
204 }
205
206 /**
207 * Test for a depth of 2 menu levels
208 * @return 2
209 */
210 public void testGetMenuDepthOf2() {
211
212 assertEquals(2, repository.getMenuDepth("parent"));
213 }
214
215 /**
216 * Test for a depth of 3 menu levels
217 * @return 3
218 */
219 public void testGetMenuDepthOf3() {
220
221 assertEquals(3, repository.getMenuDepth("complex1"));
222 }
223
224 /**
225 * Test for a depth of 3 menu levels
226 * @return 3
227 */
228 public void testGetMenuDepthAll() {
229
230 assertEquals(3, repository.getMenuDepth());
231 }
232
233 /**
234 * Test returning the top menus within an array
235 * @return 3
236 */
237 public void testGetTopMenusAsArray() {
238
239 MenuComponent[] menus = repository.getTopMenusAsArray();
240 assertEquals(3, menus.length);
241 }
242
243 /**
244 * Test removing all menus from the repository
245 * @return 0
246 */
247 public void testRemoveAllMenus() {
248
249 MenuRepository rep = buildRepository();
250 assertEquals(3, rep.getTopMenus().size());
251 rep.removeAllMenus();
252 assertEquals(0, rep.getTopMenus().size());
253 }
254
255 /**
256 * Test get a list of the top menus' names
257 * @return Success
258 */
259 public void testGetTopMenusNames() {
260 MenuRepository rep = buildRepository();
261 ArrayList names = (ArrayList) rep.getTopMenusNames();
262 assertEquals(3, names.size());
263 assertEquals("parent", ((String) names.get(0)));
264 }
265
266 /**
267 * Test building the breadcrumbs but no delimiter
268 * @return Success
269 */
270 public void testBuildBreadCrumbNullPointerException() {
271 MenuRepository rep = buildRepository();
272 try {
273 rep.buildBreadCrumbs();
274 fail("NullPointerException expected");
275 } catch (NullPointerException e) {
276 }
277 }
278
279 /**
280 * Test building the breadcrumbs passing the delimiter
281 * @return Success
282 */
283 public void testBuildBreadCrumbPassingDelimiter() {
284 MenuRepository rep = buildRepository();
285 rep.buildBreadCrumbs(":");
286 MenuComponent menu = rep.getMenu("parent");
287 assertEquals("parent",menu.getBreadCrumb());
288 MenuComponent[] menus = menu.getMenuComponents();
289 assertEquals("parent:child",menus[0].getBreadCrumb());
290 }
291
292 }