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.shim;
022:
023: import java.util.*;
024: import java.sql.*;
025: import java.io.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.test.*;
030: import servletunit.struts.*;
031: import org.apache.struts.action.*;
032: import org.apache.cactus.*;
033: import com.methodhead.auth.*;
034: import com.methodhead.sitecontext.*;
035: import org.apache.struts.util.*;
036: import com.methodhead.tree.*;
037: import com.methodhead.*;
038:
039: public class EditorActionTest extends CactusStrutsTestCase {
040:
041: Thumbnail thumb = null;
042: FileInfo info = null;
043: List list = null;
044: DynaActionForm dynaForm = null;
045: LabelValueBean option = null;
046: FoldingTreeNode node = null;
047: FoldingTreeNode node2 = null;
048:
049: private Link link1_ = null;
050: private Link link2_ = null;
051: private Link link3_ = null;
052: private Link link4_ = null;
053: private Link link5_ = null;
054:
055: private SiteMap siteMap1_ = null;
056:
057: private SiteMapTree siteMapTree1_ = null;
058:
059: private TestSite site = null;
060:
061: private void createData() {
062: site = new TestSite();
063: site.createPages();
064: site.createLinks();
065: site.createSiteMap();
066: site.createSiteMapTree();
067:
068: // Node1
069: // Node2
070: // Node3
071: // Node4
072: // Node5
073: /*
074: link1_ = new Link();
075: link1_.setPageId( 1 );
076: link1_.setTitle( "Page1" );
077: link2_ = new Link();
078: link2_.setPageId( 2 );
079: link2_.setTitle( "Page2" );
080: link3_ = new Link();
081: link3_.setPageId( 3 );
082: link3_.setTitle( "Page3" );
083: link4_ = new Link();
084: link4_.setPageId( 4 );
085: link4_.setTitle( "Page4" );
086: link5_ = new Link();
087: link5_.setPageId( 5 );
088: link5_.setTitle( "Page5" );
089:
090: link1_.add( link2_ );
091: link2_.add( link3_ );
092: link1_.add( link4_ );
093: link1_.add( link5_ );
094:
095: siteMap1_ = new SiteMap();
096: siteMap1_.setRoot( link1_ );
097: siteMap1_.setSiteContext( SiteContext.getDefaultContext() );
098: siteMap1_.save();
099:
100: siteMapTree1_ = new SiteMapTree();
101: siteMapTree1_.build( siteMap1_ );
102:
103: node = siteMapTree1_.find( 1 );
104: node.setOpened( true );
105: node = siteMapTree1_.find( 2 );
106: node.setOpened( false );
107: node = siteMapTree1_.find( 3 );
108: node.setOpened( false );
109: node = siteMapTree1_.find( 4 );
110: node.setOpened( false );
111: node = siteMapTree1_.find( 5 );
112: node.setOpened( false );
113: */
114: node = site.siteMapTree_.find(1);
115: node.setOpened(true);
116:
117: session.getServletContext().removeAttribute(
118: ShimGlobals.SITEMAPMAP_KEY);
119: session.removeAttribute(ShimGlobals.SITEMAPTREE_KEY);
120: session.removeAttribute(ShimGlobals.MODE_KEY);
121:
122: request.getSession().setAttribute(ShimGlobals.SITEMAPTREE_KEY,
123: site.siteMapTree_);
124: SiteContext
125: .setContext(request, SiteContext.getDefaultContext());
126: }
127:
128: static {
129: TestUtils.initLogger();
130: TestUtils.initDb();
131: }
132:
133: public EditorActionTest(String name) {
134: super (name);
135: }
136:
137: public void setUp() {
138: //setLogLevel( Level.DEBUG );
139: try {
140: super .setUp();
141: ConnectionSingleton.runBatchUpdate(new FileReader(
142: "webapp/WEB-INF/db/transfer-reset.sql"));
143:
144: TestData.createUsers();
145: AuthUtil.setUser(request, TestData.user1);
146: SiteContext.setContext(request, SiteContext
147: .getDefaultContext());
148: } catch (Exception e) {
149: fail(e.getMessage());
150: }
151: }
152:
153: public void tearDown() throws Exception {
154: super .tearDown();
155: }
156:
157: public void testListImagesBlankPath() {
158: //
159: // no or blank path
160: //
161: setRequestPathInfo("/listImages");
162: addRequestParameter("path", "");
163: actionPerform();
164:
165: verifyForward("form");
166:
167: dynaForm = (DynaActionForm) getActionForm();
168:
169: list = (List) dynaForm.get("images");
170: assertNotNull(list);
171: assertEquals(0, list.size());
172: }
173:
174: public void testListImagesInvalidPath() {
175: //
176: // invalid path
177: //
178: setRequestPathInfo("/listImages");
179: addRequestParameter("path", "../etc");
180: actionPerform();
181:
182: verifyForward("form");
183:
184: dynaForm = (DynaActionForm) getActionForm();
185:
186: list = (List) dynaForm.get("images");
187: assertNotNull(list);
188: assertEquals(0, list.size());
189: }
190:
191: public void testListImagesImagesDirectory() {
192: //
193: // images directory
194: //
195: setRequestPathInfo("/listImages");
196: addRequestParameter("path", "images");
197: actionPerform();
198:
199: verifyForward("form");
200:
201: dynaForm = (DynaActionForm) getActionForm();
202:
203: list = (List) dynaForm.get("images");
204: assertNotNull(list);
205: assertEquals(5, list.size());
206:
207: thumb = (Thumbnail) list.get(0);
208: assertEquals("badimage.jpg", thumb.getName());
209: assertNull(thumb.getThumbPath());
210: assertEquals("images/badimage.jpg", thumb.getImagePath());
211: assertEquals(21, thumb.getSize());
212:
213: thumb = (Thumbnail) list.get(1);
214: assertEquals("test200x300.jpg", thumb.getName());
215: assertEquals("thumbs/images/test200x300.jpg.jpg", thumb
216: .getThumbPath());
217: assertEquals("images/test200x300.jpg", thumb.getImagePath());
218: assertEquals(4306, thumb.getSize());
219:
220: thumb = (Thumbnail) list.get(2);
221: assertEquals("test300x200.gif", thumb.getName());
222: assertEquals("thumbs/images/test300x200.gif.jpg", thumb
223: .getThumbPath());
224: assertEquals("images/test300x200.gif", thumb.getImagePath());
225: assertEquals(1637, thumb.getSize());
226:
227: thumb = (Thumbnail) list.get(3);
228: assertEquals("test300x200.jpg", thumb.getName());
229: assertEquals("thumbs/images/test300x200.jpg.jpg", thumb
230: .getThumbPath());
231: assertEquals("images/test300x200.jpg", thumb.getImagePath());
232: assertEquals(4443, thumb.getSize());
233:
234: thumb = (Thumbnail) list.get(4);
235: assertEquals("test300x200.png", thumb.getName());
236: assertEquals("thumbs/images/test300x200.png.jpg", thumb
237: .getThumbPath());
238: assertEquals("images/test300x200.png", thumb.getImagePath());
239: assertEquals(3489, thumb.getSize());
240: }
241:
242: public void testPickImage() {
243: try {
244: setRequestPathInfo("/pickImage");
245: actionPerform();
246:
247: verifyForward("form");
248:
249: dynaForm = (DynaActionForm) getActionForm();
250:
251: assertEquals("", dynaForm.get("path"));
252:
253: list = (List) dynaForm.get("paths");
254: assertNotNull(list);
255: assertEquals(3, list.size());
256:
257: option = (LabelValueBean) list.get(0);
258: assertEquals("/", option.getLabel());
259: assertEquals("", option.getValue());
260:
261: option = (LabelValueBean) list.get(1);
262: assertEquals("/images", option.getLabel());
263: assertEquals("images", option.getValue());
264:
265: option = (LabelValueBean) list.get(2);
266: assertEquals("/images/moreimages", option.getLabel());
267: assertEquals("images/moreimages", option.getValue());
268: } catch (Exception e) {
269: e.printStackTrace();
270: fail();
271: }
272: }
273:
274: public void testDisplayFiles() {
275: try {
276: //
277: // no or blank path
278: //
279: setRequestPathInfo("/displayFiles");
280: addRequestParameter("path", "");
281: actionPerform();
282:
283: verifyForward("form");
284:
285: dynaForm = (DynaActionForm) getActionForm();
286:
287: list = (List) dynaForm.get("files");
288: assertNotNull(list);
289: assertEquals(0, list.size());
290:
291: //
292: // invalid path
293: //
294: setRequestPathInfo("/displayFiles");
295: addRequestParameter("path", "../etc");
296: actionPerform();
297:
298: verifyForward("form");
299:
300: dynaForm = (DynaActionForm) getActionForm();
301:
302: list = (List) dynaForm.get("files");
303: assertNotNull(list);
304: assertEquals(0, list.size());
305:
306: //
307: // images directory
308: //
309: setRequestPathInfo("/displayFiles");
310: addRequestParameter("path", "images");
311: actionPerform();
312:
313: verifyForward("form");
314:
315: dynaForm = (DynaActionForm) getActionForm();
316:
317: list = (List) dynaForm.get("files");
318: assertNotNull(list);
319: assertEquals(5, list.size());
320:
321: info = (FileInfo) list.get(0);
322: assertEquals("badimage.jpg", info.getName());
323: assertEquals("images/badimage.jpg", info.getPath());
324: assertEquals("IMG", info.getIconHint());
325: assertEquals(21, info.getSize());
326:
327: info = (FileInfo) list.get(1);
328: assertEquals("test200x300.jpg", info.getName());
329: assertEquals("images/test200x300.jpg", info.getPath());
330: assertEquals("IMG", info.getIconHint());
331: assertEquals(4306, info.getSize());
332:
333: info = (FileInfo) list.get(2);
334: assertEquals("test300x200.gif", info.getName());
335: assertEquals("images/test300x200.gif", info.getPath());
336: assertEquals("IMG", info.getIconHint());
337: assertEquals(1637, info.getSize());
338:
339: info = (FileInfo) list.get(3);
340: assertEquals("test300x200.jpg", info.getName());
341: assertEquals("images/test300x200.jpg", info.getPath());
342: assertEquals("IMG", info.getIconHint());
343: assertEquals(4443, info.getSize());
344:
345: info = (FileInfo) list.get(4);
346: assertEquals("test300x200.png", info.getName());
347: assertEquals("images/test300x200.png", info.getPath());
348: assertEquals("IMG", info.getIconHint());
349: assertEquals(3489, info.getSize());
350: } catch (Exception e) {
351: e.printStackTrace();
352: fail();
353: }
354: }
355:
356: public void testPickFile() {
357: try {
358: setRequestPathInfo("/pickFile");
359: actionPerform();
360:
361: verifyForward("form");
362:
363: dynaForm = (DynaActionForm) getActionForm();
364:
365: assertEquals("", dynaForm.get("path"));
366: } catch (Exception e) {
367: e.printStackTrace();
368: fail();
369: }
370: }
371:
372: public void testListPages() {
373: try {
374: createData();
375: setRequestPathInfo("/listPages");
376: actionPerform();
377:
378: verifyForward("form");
379:
380: dynaForm = (DynaActionForm) getActionForm();
381: node = (FoldingTreeNode) dynaForm.get("node");
382:
383: assertNotNull(node);
384: assertEquals("Page1", node.getLabel());
385: assertEquals("javascript:onPageClick(1,'page1.shtml')",
386: node.getUrl());
387: assertEquals(true, node.getOpened());
388: assertEquals(3, node.getChildCount());
389:
390: node2 = (FoldingTreeNode) node.getChildAt(0);
391: assertNotNull(node2);
392: assertEquals("Page2", node2.getLabel());
393: assertEquals("javascript:onPageClick(2,'page2.shtml')",
394: node2.getUrl());
395: assertEquals(false, node2.getOpened());
396: assertEquals(1, node2.getChildCount());
397:
398: node2 = (FoldingTreeNode) node.getChildAt(1);
399: assertNotNull(node2);
400: assertEquals("Page4", node2.getLabel());
401: assertEquals("javascript:onPageClick(4,'page4.shtml')",
402: node2.getUrl());
403: assertEquals(false, node2.getOpened());
404: assertEquals(0, node2.getChildCount());
405:
406: node2 = (FoldingTreeNode) node.getChildAt(2);
407: assertNotNull(node2);
408: assertEquals("Page5", node2.getLabel());
409: assertEquals("javascript:onPageClick(5,'page5.shtml')",
410: node2.getUrl());
411: assertEquals(false, node2.getOpened());
412: assertEquals(1, node2.getChildCount());
413: } catch (Exception e) {
414: e.printStackTrace();
415: fail();
416: }
417: }
418:
419: public void testPickPage() {
420: try {
421: createData();
422: setRequestPathInfo("/pickPage");
423: actionPerform();
424:
425: verifyForward("form");
426: } catch (Exception e) {
427: e.printStackTrace();
428: fail();
429: }
430: }
431: }
|