001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.menu;
022:
023: import java.util.*;
024: import java.sql.*;
025: import junit.framework.*;
026: import org.apache.log4j.*;
027: import com.methodhead.persistable.*;
028: import com.methodhead.test.*;
029: import com.methodhead.tree.*;
030: import java.awt.*;
031:
032: public class MenuRendererTest extends TestCase {
033:
034: public MenuRendererTest(String name) {
035: super (name);
036: }
037:
038: FoldingTreeNode menu1_ = null;
039: FoldingTreeNode menu2_ = null;
040: FoldingTreeNode menu3_ = null;
041: String menu1Html_ = null;
042: String menu2Html_ = null;
043: String menu3Html_ = null;
044:
045: protected void setUp() {
046: //setLogLevel( Level.DEBUG );
047: try {
048: FoldingTreeNode item = null;
049: FoldingTreeNode subitem = null;
050: FoldingTreeNode subsubitem = null;
051:
052: //
053: // Menu ()
054: // MenuItem ("http://site.com")
055: //
056: item = new FoldingTreeNode();
057: item.setLabel("MenuItem");
058: item.setUrl("http://site.com");
059: menu1_ = new FoldingTreeNode();
060: menu1_.setLabel("Menu");
061: menu1_.setUrl("");
062: menu1_.add(item);
063:
064: menu1Html_ = "NoOffFirstLineMenus=1;\n"
065: + "Menu1=new Array(\"Menu\",\"\",\"\",1,20,40);\n"
066: + "Menu1_1=new Array(\"MenuItem\",\"http://site.com\",\"\",0,20,80);\n";
067:
068: //
069: // Menu ()
070: // MenuItem ("http://site.com")
071: // LongMenuItem ("http://LongMenuItem.com")
072: // Item ("http://Item.com")
073: //
074: menu2_ = new FoldingTreeNode();
075: menu2_.setLabel("Menu");
076: menu2_.setUrl("");
077: item = new FoldingTreeNode();
078: item.setLabel("MenuItem");
079: item.setUrl("http://site.com");
080: menu2_.add(item);
081: item = new FoldingTreeNode();
082: item.setLabel("LongMenuItem");
083: item.setUrl("http://LongMenuItem.com");
084: menu2_.add(item);
085: item = new FoldingTreeNode();
086: item.setLabel("Item");
087: item.setUrl("http://Item.com");
088: menu2_.add(item);
089:
090: menu2Html_ = "NoOffFirstLineMenus=1;\n"
091: + "Menu1=new Array(\"Menu\",\"\",\"\",3,20,40);\n"
092: + "Menu1_1=new Array(\"MenuItem\",\"http://site.com\",\"\",0,20,120);\n"
093: + "Menu1_2=new Array(\"LongMenuItem\",\"http://LongMenuItem.com\",\"\",0,20,120);\n"
094: + "Menu1_3=new Array(\"Item\",\"http://Item.com\",\"\",0,20,120);\n";
095:
096: //
097: // Menu1 ()
098: // Menu2 ("Menu2.com")
099: // Menu21 ("Menu21.com")
100: // Menu3 ()
101: // Menu31 ("Menu31.com")
102: // Menu32 ("Menu32.com")
103: // Menu321 ("Menu321.com")
104: // Menu33 ("Menu33")
105: //
106: menu3_ = new FoldingTreeNode();
107: item = new FoldingTreeNode();
108: item.setLabel("Menu1");
109: item.setUrl("");
110: menu3_.add(item);
111: item = new FoldingTreeNode();
112: item.setLabel("Menu2");
113: item.setUrl("Menu2.com");
114: subitem = new FoldingTreeNode();
115: subitem.setLabel("Menu21");
116: subitem.setUrl("Menu21.com");
117: item.add(subitem);
118: menu3_.add(item);
119: item = new FoldingTreeNode();
120: item.setLabel("Menu3");
121: item.setUrl("Menu3.com");
122: subitem = new FoldingTreeNode();
123: subitem.setLabel("Menu31");
124: subitem.setUrl("Menu31.com");
125: item.add(subitem);
126: subitem = new FoldingTreeNode();
127: subitem.setLabel("Menu32");
128: subitem.setUrl("Menu32.com");
129: subsubitem = new FoldingTreeNode();
130: subsubitem.setLabel("Menu321");
131: subsubitem.setUrl("Menu321.com");
132: subitem.add(subsubitem);
133: item.add(subitem);
134: subitem = new FoldingTreeNode();
135: subitem.setLabel("Menu33");
136: subitem.setUrl("Menu33.com");
137: item.add(subitem);
138: menu3_.add(item);
139:
140: menu3Html_ = "NoOffFirstLineMenus=3;\n"
141: + "Menu1=new Array(\"Menu1\",\"\",\"\",0,20,50);\n"
142: + "Menu2=new Array(\"Menu2\",\"Menu2.com\",\"\",1,20,50);\n"
143: + "Menu2_1=new Array(\"Menu21\",\"Menu21.com\",\"\",0,20,60);\n"
144: + "Menu3=new Array(\"Menu3\",\"Menu3.com\",\"\",3,20,50);\n"
145: + "Menu3_1=new Array(\"Menu31\",\"Menu31.com\",\"\",0,20,60);\n"
146: + "Menu3_2=new Array(\"Menu32\",\"Menu32.com\",\"\",1,20,60);\n"
147: + "Menu3_2_1=new Array(\"Menu321\",\"Menu321.com\",\"\",0,20,70);\n"
148: + "Menu3_3=new Array(\"Menu33\",\"Menu33.com\",\"\",0,20,60);\n";
149: } catch (Exception e) {
150: fail(e.getMessage());
151: }
152: }
153:
154: protected void tearDown() {
155: }
156:
157: public void testGetVarName() {
158: try {
159: assertEquals("Menu1", MenuRenderer
160: .getVarName(new int[] { 1 }));
161: assertEquals("Menu1_1", MenuRenderer.getVarName(new int[] {
162: 1, 1 }));
163: assertEquals("Menu2_1_3", MenuRenderer
164: .getVarName(new int[] { 2, 1, 3 }));
165: } catch (Exception e) {
166: e.printStackTrace();
167: fail();
168: }
169: }
170:
171: public void testGetLongestLabelLength() {
172: try {
173: assertEquals(8, MenuRenderer.getLongestLabelLength(menu1_));
174: assertEquals(12, MenuRenderer.getLongestLabelLength(menu2_));
175: } catch (Exception e) {
176: e.printStackTrace();
177: fail();
178: }
179: }
180:
181: public void testGetMenuHtml() {
182: try {
183: StringBuffer buf = null;
184: MenuRenderer renderer = null;
185: String html = null;
186:
187: //
188: // simple menu
189: //
190: html = "Menu1=new Array(\"Menu\",\"\",\"\",1,20,40);\n"
191: + "Menu1_1=new Array(\"MenuItem\",\"http://site.com\",\"\",0,20,80);\n";
192: buf = new StringBuffer();
193: renderer = new MenuRenderer();
194: renderer.getMenuHtml(buf, menu1_, new int[] { 1 }, 40, 20);
195:
196: assertEquals(html, buf.toString());
197:
198: //
199: // simple menu with baseurl
200: //
201: html = "Menu1=new Array(\"Menu\",\"\",\"\",1,20,40);\n"
202: + "Menu1_1=new Array(\"MenuItem\",\"BASEURLhttp://site.com\",\"\",0,20,80);\n";
203: buf = new StringBuffer();
204: renderer = new MenuRenderer();
205: renderer.setBaseUrl("BASEURL");
206: renderer.getMenuHtml(buf, menu1_, new int[] { 1 }, 40, 20);
207:
208: assertEquals(html, buf.toString());
209:
210: //
211: // more complicated menu
212: //
213: html = "Menu1=new Array(\"Menu\",\"\",\"\",3,20,40);\n"
214: + "Menu1_1=new Array(\"MenuItem\",\"http://site.com\",\"\",0,20,120);\n"
215: + "Menu1_2=new Array(\"LongMenuItem\",\"http://LongMenuItem.com\",\"\",0,20,120);\n"
216: + "Menu1_3=new Array(\"Item\",\"http://Item.com\",\"\",0,20,120);\n";
217: buf = new StringBuffer();
218: renderer = new MenuRenderer();
219: renderer.getMenuHtml(buf, menu2_, new int[] { 1 }, 40, 20);
220:
221: assertEquals(html, buf.toString());
222: } catch (Exception e) {
223: e.printStackTrace();
224: fail();
225: }
226: }
227:
228: public void testRenderMenu() {
229: try {
230: String html = null;
231: MenuRenderer renderer = null;
232:
233: renderer = new MenuRenderer();
234: html = renderer.renderMenu(menu3_);
235:
236: assertEquals(menu3Html_, html);
237: } catch (Exception e) {
238: e.printStackTrace();
239: fail();
240: }
241: }
242: }
|