KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > EditorActionTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import java.util.*;
24 import java.sql.*;
25 import java.io.*;
26 import junit.framework.*;
27 import org.apache.log4j.*;
28 import com.methodhead.persistable.*;
29 import com.methodhead.test.*;
30 import servletunit.struts.*;
31 import org.apache.struts.action.*;
32 import org.apache.cactus.*;
33 import com.methodhead.auth.*;
34 import com.methodhead.sitecontext.*;
35 import org.apache.struts.util.*;
36 import com.methodhead.tree.*;
37 import com.methodhead.*;
38
39 public class EditorActionTest extends CactusStrutsTestCase {
40
41   Thumbnail thumb = null;
42   FileInfo info = null;
43   List list = null;
44   DynaActionForm dynaForm = null;
45   LabelValueBean option = null;
46   FoldingTreeNode node = null;
47   FoldingTreeNode node2 = null;
48
49   private Link link1_ = null;
50   private Link link2_ = null;
51   private Link link3_ = null;
52   private Link link4_ = null;
53   private Link link5_ = null;
54
55   private SiteMap siteMap1_ = null;
56
57   private SiteMapTree siteMapTree1_ = null;
58
59   private TestSite site = null;
60
61   private void createData() {
62     site = new TestSite();
63     site.createPages();
64     site.createLinks();
65     site.createSiteMap();
66     site.createSiteMapTree();
67
68     // Node1
69
// Node2
70
// Node3
71
// Node4
72
// Node5
73
/*
74     link1_ = new Link();
75     link1_.setPageId( 1 );
76     link1_.setTitle( "Page1" );
77     link2_ = new Link();
78     link2_.setPageId( 2 );
79     link2_.setTitle( "Page2" );
80     link3_ = new Link();
81     link3_.setPageId( 3 );
82     link3_.setTitle( "Page3" );
83     link4_ = new Link();
84     link4_.setPageId( 4 );
85     link4_.setTitle( "Page4" );
86     link5_ = new Link();
87     link5_.setPageId( 5 );
88     link5_.setTitle( "Page5" );
89
90     link1_.add( link2_ );
91     link2_.add( link3_ );
92     link1_.add( link4_ );
93     link1_.add( link5_ );
94
95     siteMap1_ = new SiteMap();
96     siteMap1_.setRoot( link1_ );
97     siteMap1_.setSiteContext( SiteContext.getDefaultContext() );
98     siteMap1_.save();
99
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( ShimGlobals.SITEMAPMAP_KEY );
118     session.removeAttribute( ShimGlobals.SITEMAPTREE_KEY );
119     session.removeAttribute( ShimGlobals.MODE_KEY );
120
121     request.getSession().setAttribute( ShimGlobals.SITEMAPTREE_KEY, site.siteMapTree_ );
122     SiteContext.setContext( request, SiteContext.getDefaultContext() );
123   }
124
125   static {
126     TestUtils.initLogger();
127     TestUtils.initDb();
128   }
129
130   public EditorActionTest( String JavaDoc name ) {
131     super( name );
132   }
133
134   public void setUp() {
135     //setLogLevel( Level.DEBUG );
136
try {
137       super.setUp();
138       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
139
140       TestData.createUsers();
141       AuthUtil.setUser( request, TestData.user1 );
142       SiteContext.setContext( request, SiteContext.getDefaultContext() );
143     }
144     catch ( Exception JavaDoc e ) {
145       fail( e.getMessage() );
146     }
147   }
148
149   public void tearDown()
150   throws
151     Exception JavaDoc {
152     super.tearDown();
153   }
154
155   public void testListImagesBlankPath() {
156     //
157
// no or blank path
158
//
159
setRequestPathInfo( "/listImages" );
160     addRequestParameter( "path", "" );
161     actionPerform();
162
163     verifyForward( "form" );
164
165     dynaForm = ( DynaActionForm )getActionForm();
166
167     list = ( List )dynaForm.get( "images" );
168     assertNotNull( list );
169     assertEquals( 0, list.size() );
170   }
171
172   public void testListImagesInvalidPath() {
173     //
174
// invalid path
175
//
176
setRequestPathInfo( "/listImages" );
177     addRequestParameter( "path", "../etc" );
178     actionPerform();
179
180     verifyForward( "form" );
181
182     dynaForm = ( DynaActionForm )getActionForm();
183
184     list = ( List )dynaForm.get( "images" );
185     assertNotNull( list );
186     assertEquals( 0, list.size() );
187   }
188
189   public void testListImagesImagesDirectory() {
190     //
191
// images directory
192
//
193
setRequestPathInfo( "/listImages" );
194     addRequestParameter( "path", "images" );
195     actionPerform();
196
197     verifyForward( "form" );
198
199     dynaForm = ( DynaActionForm )getActionForm();
200
201     list = ( List )dynaForm.get( "images" );
202     assertNotNull( list );
203     assertEquals( 5, list.size() );
204
205     thumb = ( Thumbnail )list.get( 0 );
206     assertEquals( "badimage.jpg", thumb.getName() );
207     assertNull( thumb.getThumbPath() );
208     assertEquals( "images/badimage.jpg", thumb.getImagePath() );
209     assertEquals( 21, thumb.getSize() );
210
211     thumb = ( Thumbnail )list.get( 1 );
212     assertEquals( "test200x300.jpg", thumb.getName() );
213     assertEquals( "thumbs/images/test200x300.jpg.jpg", thumb.getThumbPath() );
214     assertEquals( "images/test200x300.jpg", thumb.getImagePath() );
215     assertEquals( 4306, thumb.getSize() );
216
217     thumb = ( Thumbnail )list.get( 2 );
218     assertEquals( "test300x200.gif", thumb.getName() );
219     assertEquals( "thumbs/images/test300x200.gif.jpg", thumb.getThumbPath() );
220     assertEquals( "images/test300x200.gif", thumb.getImagePath() );
221     assertEquals( 1637, thumb.getSize() );
222
223     thumb = ( Thumbnail )list.get( 3 );
224     assertEquals( "test300x200.jpg", thumb.getName() );
225     assertEquals( "thumbs/images/test300x200.jpg.jpg", thumb.getThumbPath() );
226     assertEquals( "images/test300x200.jpg", thumb.getImagePath() );
227     assertEquals( 4443, thumb.getSize() );
228
229     thumb = ( Thumbnail )list.get( 4 );
230     assertEquals( "test300x200.png", thumb.getName() );
231     assertEquals( "thumbs/images/test300x200.png.jpg", thumb.getThumbPath() );
232     assertEquals( "images/test300x200.png", thumb.getImagePath() );
233     assertEquals( 3489, thumb.getSize() );
234   }
235
236   public void testPickImage() {
237     try {
238       setRequestPathInfo( "/pickImage" );
239       actionPerform();
240
241       verifyForward( "form" );
242
243       dynaForm = ( DynaActionForm )getActionForm();
244
245       assertEquals( "", dynaForm.get( "path" ) );
246
247       list = ( List )dynaForm.get( "paths" );
248       assertNotNull( list );
249       assertEquals( 3, list.size() );
250
251       option = ( LabelValueBean )list.get( 0 );
252       assertEquals( "/", option.getLabel() );
253       assertEquals( "", option.getValue() );
254
255       option = ( LabelValueBean )list.get( 1 );
256       assertEquals( "/images", option.getLabel() );
257       assertEquals( "images", option.getValue() );
258
259       option = ( LabelValueBean )list.get( 2 );
260       assertEquals( "/images/moreimages", option.getLabel() );
261       assertEquals( "images/moreimages", option.getValue() );
262     }
263     catch ( Exception JavaDoc e ) {
264       e.printStackTrace();
265       fail();
266     }
267   }
268
269   public void testDisplayFiles() {
270     try {
271       //
272
// no or blank path
273
//
274
setRequestPathInfo( "/displayFiles" );
275       addRequestParameter( "path", "" );
276       actionPerform();
277
278       verifyForward( "form" );
279
280       dynaForm = ( DynaActionForm )getActionForm();
281
282       list = ( List )dynaForm.get( "files" );
283       assertNotNull( list );
284       assertEquals( 0, list.size() );
285
286       //
287
// invalid path
288
//
289
setRequestPathInfo( "/displayFiles" );
290       addRequestParameter( "path", "../etc" );
291       actionPerform();
292
293       verifyForward( "form" );
294
295       dynaForm = ( DynaActionForm )getActionForm();
296
297       list = ( List )dynaForm.get( "files" );
298       assertNotNull( list );
299       assertEquals( 0, list.size() );
300
301       //
302
// images directory
303
//
304
setRequestPathInfo( "/displayFiles" );
305       addRequestParameter( "path", "images" );
306       actionPerform();
307
308       verifyForward( "form" );
309
310       dynaForm = ( DynaActionForm )getActionForm();
311
312       list = ( List )dynaForm.get( "files" );
313       assertNotNull( list );
314       assertEquals( 5, list.size() );
315
316       info = ( FileInfo )list.get( 0 );
317       assertEquals( "badimage.jpg", info.getName() );
318       assertEquals( "images/badimage.jpg", info.getPath() );
319       assertEquals( "IMG", info.getIconHint() );
320       assertEquals( 21, info.getSize() );
321
322       info = ( FileInfo )list.get( 1 );
323       assertEquals( "test200x300.jpg", info.getName() );
324       assertEquals( "images/test200x300.jpg", info.getPath() );
325       assertEquals( "IMG", info.getIconHint() );
326       assertEquals( 4306, info.getSize() );
327
328       info = ( FileInfo )list.get( 2 );
329       assertEquals( "test300x200.gif", info.getName() );
330       assertEquals( "images/test300x200.gif", info.getPath() );
331       assertEquals( "IMG", info.getIconHint() );
332       assertEquals( 1637, info.getSize() );
333
334       info = ( FileInfo )list.get( 3 );
335       assertEquals( "test300x200.jpg", info.getName() );
336       assertEquals( "images/test300x200.jpg", info.getPath() );
337       assertEquals( "IMG", info.getIconHint() );
338       assertEquals( 4443, info.getSize() );
339
340       info = ( FileInfo )list.get( 4 );
341       assertEquals( "test300x200.png", info.getName() );
342       assertEquals( "images/test300x200.png", info.getPath() );
343       assertEquals( "IMG", info.getIconHint() );
344       assertEquals( 3489, info.getSize() );
345     }
346     catch ( Exception JavaDoc e ) {
347       e.printStackTrace();
348       fail();
349     }
350   }
351
352   public void testPickFile() {
353     try {
354       setRequestPathInfo( "/pickFile" );
355       actionPerform();
356
357       verifyForward( "form" );
358
359       dynaForm = ( DynaActionForm )getActionForm();
360
361       assertEquals( "", dynaForm.get( "path" ) );
362     }
363     catch ( Exception JavaDoc e ) {
364       e.printStackTrace();
365       fail();
366     }
367   }
368
369   public void testListPages() {
370     try {
371       createData();
372       setRequestPathInfo( "/listPages" );
373       actionPerform();
374
375       verifyForward( "form" );
376
377       dynaForm = ( DynaActionForm )getActionForm();
378       node = ( FoldingTreeNode )dynaForm.get( "node" );
379
380       assertNotNull( node );
381       assertEquals( "Page1", node.getLabel() );
382       assertEquals( "javascript:onPageClick(1,'page1.shtml')", node.getUrl() );
383       assertEquals( true, node.getOpened() );
384       assertEquals( 3, node.getChildCount() );
385
386       node2 = ( FoldingTreeNode )node.getChildAt( 0 );
387       assertNotNull( node2 );
388       assertEquals( "Page2", node2.getLabel() );
389       assertEquals( "javascript:onPageClick(2,'page2.shtml')", node2.getUrl() );
390       assertEquals( false, node2.getOpened() );
391       assertEquals( 1, node2.getChildCount() );
392
393       node2 = ( FoldingTreeNode )node.getChildAt( 1 );
394       assertNotNull( node2 );
395       assertEquals( "Page4", node2.getLabel() );
396       assertEquals( "javascript:onPageClick(4,'page4.shtml')", node2.getUrl() );
397       assertEquals( false, node2.getOpened() );
398       assertEquals( 0, node2.getChildCount() );
399
400       node2 = ( FoldingTreeNode )node.getChildAt( 2 );
401       assertNotNull( node2 );
402       assertEquals( "Page5", node2.getLabel() );
403       assertEquals( "javascript:onPageClick(5,'page5.shtml')", node2.getUrl() );
404       assertEquals( false, node2.getOpened() );
405       assertEquals( 1, node2.getChildCount() );
406     }
407     catch ( Exception JavaDoc e ) {
408       e.printStackTrace();
409       fail();
410     }
411   }
412
413   public void testPickPage() {
414     try {
415       createData();
416       setRequestPathInfo( "/pickPage" );
417       actionPerform();
418
419       verifyForward( "form" );
420     }
421     catch ( Exception JavaDoc e ) {
422       e.printStackTrace();
423       fail();
424     }
425   }
426 }
427
Popular Tags