KickJava   Java API By Example, From Geeks To Geeks.

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


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.auth.*;
30 import com.methodhead.test.*;
31 import com.methodhead.sitecontext.*;
32 import com.methodhead.*;
33 import com.methodhead.util.*;
34 import servletunit.struts.*;
35 import org.apache.struts.action.*;
36
37 public class ModuleActionTest extends CactusStrutsTestCase {
38
39   private Page page1_ = null;
40   private Page page2_ = null;
41   private Page page3_ = null;
42   private HtmlFragment fragment1_ = null;
43
44   private Panel panel = null;
45   private NavModule navModule = null;
46   private TextModule textModule = null;
47   private IncludeModule includeModule = null;
48
49   static {
50     TestUtils.initLogger();
51     TestUtils.initDb();
52   }
53
54   public ModuleActionTest( String JavaDoc name ) {
55     super( name );
56   }
57
58   public void setUp() {
59     //setLogLevel( Level.DEBUG );
60
try {
61       super.setUp();
62
63       TestData.createWebappFiles( ServletUtils.getRealFile( request, "" ) );
64
65       //
66
// reset the database
67
//
68
ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
69
70       fragment1_ = new HtmlFragment();
71       fragment1_.setSiteContext( SiteContext.getDefaultContext() );
72       fragment1_.setString( "name", "Fragment1" );
73       fragment1_.setString( "value", "This is Fragment1" );
74       fragment1_.saveNew();
75
76       //
77
// create text test data
78
//
79
panel = new Panel();
80       panel.setName( "body" );
81       panel.setModuleClass( "com.methodhead.shim.TextModule" );
82
83       page1_ = new Page();
84       page1_.setString( "template", "template.jsp" );
85       page1_.addPanel( panel );
86       page1_.setSiteContext( SiteContext.getDefaultContext() );
87       page1_.saveNew();
88
89       textModule = new TextModule();
90       textModule.init( page1_, "body" );
91       textModule.create();
92
93       textModule.setString( "value", "Test text." );
94       textModule.save();
95
96       //
97
// create nav test data
98
//
99
panel = new Panel();
100       panel.setName( "body" );
101       panel.setModuleClass( "com.methodhead.shim.NavModule" );
102
103       page2_ = new Page();
104       page2_.setString( "template", "template.jsp" );
105       page2_.addPanel( panel );
106       page2_.setSiteContext( SiteContext.getDefaultContext() );
107       page2_.saveNew();
108
109       navModule = new NavModule();
110       navModule.init( page2_, "body" );
111       navModule.create();
112
113       navModule.setString( "type", NavModule.TYPE_TEXTSEPARATED );
114       navModule.setString( "separator", " | " );
115       navModule.save();
116
117       //
118
// create include test data
119
//
120
panel = new Panel();
121       panel.setName( "body" );
122       panel.setModuleClass( "com.methodhead.shim.IncludeModule" );
123
124       page3_ = new Page();
125       page3_.setString( "template", "template.jsp" );
126       page3_.addPanel( panel );
127       page3_.setSiteContext( SiteContext.getDefaultContext() );
128       page3_.saveNew();
129
130       includeModule = new IncludeModule();
131       includeModule.init( page3_, "body" );
132       includeModule.create();
133
134       includeModule.setString( "jsp", "jsp/test.jsp" );
135       includeModule.save();
136
137       //
138
// login an admin
139
//
140
TestData.createUsers();
141       AuthUtil.setUser( getRequest(), TestData.user1 );
142
143       //
144
// set site context
145
//
146
SiteContext.setContext( request, SiteContext.getDefaultContext() );
147     }
148     catch ( Exception JavaDoc e ) {
149       fail( e.getMessage() );
150     }
151   }
152
153   public void tearDown()
154   throws
155     Exception JavaDoc {
156     super.tearDown();
157   }
158
159   public void testConfigureTextModuleForm() throws Exception JavaDoc {
160     DynaActionForm form = null;
161
162     setRequestPathInfo( "/configureTextModuleForm" );
163     addRequestParameter( "pageid", "" + page1_.getInt( "id" ) );
164     addRequestParameter( "panel", "body" );
165     actionPerform();
166
167     verifyForward( "form" );
168
169     form = ( DynaActionForm )getActionForm();
170
171     assertEquals( "" + page1_.getInt( "id" ), form.get( "pageid" ) );
172     assertEquals( "body", form.get( "panel" ) );
173     assertEquals( "Test text.", form.get( "text" ) );
174     assertEquals( "", form.get( "usefragment" ) );
175   }
176
177   public void testConfigureTextModuleFormUsingFragment() throws Exception JavaDoc {
178     DynaActionForm form = null;
179
180     //
181
// using fragment
182
//
183
textModule.setInt( "htmlfragment_id", fragment1_.getInt( "id" ) );
184     textModule.save();
185
186     setRequestPathInfo( "/configureTextModuleForm" );
187     addRequestParameter( "pageid", "" + page1_.getInt( "id" ) );
188     addRequestParameter( "panel", "body" );
189     actionPerform();
190
191     verifyForward( "form" );
192
193     form = ( DynaActionForm )getActionForm();
194
195     assertEquals( "" + page1_.getInt( "id" ), form.get( "pageid" ) );
196     assertEquals( "body", form.get( "panel" ) );
197     assertEquals( "Test text.", form.get( "text" ) );
198     assertEquals( "yes", form.get( "usefragment" ) );
199     assertEquals( "1", form.get( "fragment" ) );
200   }
201
202   public void testConfigureTextModule() {
203     try {
204       TextModule textModule = null;
205
206       setRequestPathInfo( "/configureTextModule" );
207       addRequestParameter( "pageid", "" + page1_.getInt( "id" ) );
208       addRequestParameter( "panel", "body" );
209       addRequestParameter( "text", "Updated text." );
210       addRequestParameter( "usefragment", "" );
211       addRequestParameter( "fragment", "1" );
212       actionPerform();
213
214       verifyForwardPath( "/editPage.do?id=" + page1_.getInt( "id" ) );
215
216       textModule = new TextModule();
217       textModule.init( page1_, "body" );
218       textModule.load();
219
220       assertEquals( "Updated text.", textModule.getString( "value" ) );
221       assertEquals( 0, textModule.getInt( "htmlfragment_id" ) );
222     }
223     catch ( Exception JavaDoc e ) {
224       e.printStackTrace();
225       fail();
226     }
227   }
228
229   public void testConfigureTextModuleUseFragment() {
230     try {
231       TextModule textModule = null;
232
233       setRequestPathInfo( "/configureTextModule" );
234       addRequestParameter( "pageid", "" + page1_.getInt( "id" ) );
235       addRequestParameter( "panel", "body" );
236       addRequestParameter( "text", "Updated text." );
237       addRequestParameter( "usefragment", "yes" );
238       addRequestParameter( "fragment", "1" );
239       actionPerform();
240
241       verifyForwardPath( "/editPage.do?id=" + page1_.getInt( "id" ) );
242
243       textModule = new TextModule();
244       textModule.init( page1_, "body" );
245       textModule.load();
246
247       assertEquals( "Updated text.", textModule.getString( "value" ) );
248       assertEquals( fragment1_.getInt( "id" ), textModule.getInt( "htmlfragment_id" ) );
249     }
250     catch ( Exception JavaDoc e ) {
251       e.printStackTrace();
252       fail();
253     }
254   }
255
256   public void testConfigureNavModuleForm() {
257     try {
258       DynaActionForm form = null;
259
260       setRequestPathInfo( "/configureNavModuleForm" );
261       addRequestParameter( "pageid", "" + page2_.getInt( "id" ) );
262       addRequestParameter( "panel", "body" );
263       actionPerform();
264
265       verifyForward( "form" );
266
267       form = ( DynaActionForm )getActionForm();
268
269       assertEquals( "" + page2_.getInt( "id" ), form.get( "pageid" ) );
270       assertEquals( "body", form.get( "panel" ) );
271       assertEquals( "textseparated", form.get( "type" ) );
272       assertEquals( " | ", form.get( "separator" ) );
273       assertEquals( "", form.get( "hideroot" ) );
274       assertEquals( "1", form.get( "startlev" ) );
275       assertEquals( "1", form.get( "contextlev" ) );
276       assertEquals( "1", form.get( "depthlev" ) );
277       assertEquals( "1", form.get( "toplev" ) );
278       assertEquals( "", form.get( "header" ) );
279       assertEquals( "", form.get( "link" ) );
280       assertEquals( "", form.get( "curlink" ) );
281       assertEquals( "", form.get( "footer" ) );
282     }
283     catch ( Exception JavaDoc e ) {
284       e.printStackTrace();
285       fail();
286     }
287   }
288
289   public void testConfigureNavModule() throws Exception JavaDoc {
290     navModule = new NavModule();
291     navModule.init( page2_, "body" );
292     navModule.create();
293
294     //
295
// jsp
296
//
297
setRequestPathInfo( "/configureNavModule" );
298     addRequestParameter( "pageid", "" + page2_.getInt( "id" ) );
299     addRequestParameter( "panel", "body" );
300     addRequestParameter( "type", "jsp" );
301     addRequestParameter( "separator", "" );
302     addRequestParameter( "jsp", "jsp/nav.jsp" );
303     addRequestParameter( "header", "" );
304     addRequestParameter( "link", "" );
305     addRequestParameter( "curlink", "" );
306     addRequestParameter( "footer", "" );
307     actionPerform();
308
309     verifyNoActionErrors();
310     verifyForwardPath( "/editPage.do?id=" + page2_.getInt( "id" ) );
311
312     navModule = new NavModule();
313     navModule.init( page2_, "body" );
314     navModule.load();
315
316     assertEquals( NavModule.TYPE_JSP, navModule.getString( "type" ) );
317     assertEquals( "jsp/nav.jsp", navModule.getString( "jsp" ) );
318   }
319
320   public void testConfigureNavModuleTextSeparated() throws Exception JavaDoc {
321     //
322
// text separated
323
//
324
setRequestPathInfo( "/configureNavModule" );
325     addRequestParameter( "pageid", "" + page2_.getInt( "id" ) );
326     addRequestParameter( "panel", "body" );
327     addRequestParameter( "type", "textseparated" );
328     addRequestParameter( "separator", " - " );
329     addRequestParameter( "hideroot", "yes" );
330     addRequestParameter( "jsp", "" );
331     addRequestParameter( "header", "HEADER" );
332     addRequestParameter( "link", "LINK" );
333     addRequestParameter( "curlink", "CURLINK" );
334     addRequestParameter( "footer", "FOOTER" );
335     actionPerform();
336
337     verifyForwardPath( "/editPage.do?id=" + page2_.getInt( "id" ) );
338
339     navModule = new NavModule();
340     navModule.init( page2_, "body" );
341     navModule.load();
342
343     assertEquals( NavModule.TYPE_TEXTSEPARATED, navModule.getString( "type" ) );
344     assertEquals( " - ", navModule.getString( "separator" ) );
345     assertEquals( "HEADER", navModule.getString( "header" ) );
346     assertEquals( "FOOTER", navModule.getString( "footer" ) );
347     assertEquals( "LINK", navModule.getString( "link" ) );
348     assertEquals( "CURLINK", navModule.getString( "curlink" ) );
349     assertEquals( true, navModule.getBoolean( "hideroot" ) );
350   }
351
352   public void testConfigureNavModuleFolding() throws Exception JavaDoc {
353     //
354
// folding
355
//
356
clearRequestParameters();
357     setRequestPathInfo( "/configureNavModule" );
358     addRequestParameter( "pageid", "" + page2_.getInt( "id" ) );
359     addRequestParameter( "panel", "body" );
360     addRequestParameter( "type", "folding" );
361     addRequestParameter( "startlev", "2" );
362     addRequestParameter( "contextlev", "2" );
363     addRequestParameter( "depthlev", "2" );
364     addRequestParameter( "toplev", "2" );
365     addRequestParameter( "header", "HEADER" );
366     addRequestParameter( "link", "LINK" );
367     addRequestParameter( "curlink", "CURLINK" );
368     addRequestParameter( "footer", "FOOTER" );
369     actionPerform();
370
371     verifyForwardPath( "/editPage.do?id=" + page2_.getInt( "id" ) );
372
373     navModule = new NavModule();
374     navModule.init( page2_, "body" );
375     navModule.load();
376
377     assertEquals( NavModule.TYPE_FOLDING, navModule.getString( "type" ) );
378     assertEquals( 2, navModule.getInt( "startlev" ) );
379     assertEquals( 2, navModule.getInt( "contextlev" ) );
380     assertEquals( 2, navModule.getInt( "depthlev" ) );
381     assertEquals( 2, navModule.getInt( "toplev" ) );
382     assertEquals( "HEADER", navModule.getString( "header" ) );
383     assertEquals( "LINK", navModule.getString( "link" ) );
384     assertEquals( "CURLINK", navModule.getString( "curlink" ) );
385     assertEquals( "FOOTER", navModule.getString( "footer" ) );
386   }
387
388   public void testConfigureIncludeModuleForm() {
389     try {
390       DynaActionForm form = null;
391
392       setRequestPathInfo( "/configureIncludeModuleForm" );
393       addRequestParameter( "pageid", "" + page3_.getInt( "id" ) );
394       addRequestParameter( "panel", "body" );
395       actionPerform();
396
397       verifyForward( "form" );
398
399       form = ( DynaActionForm )getActionForm();
400
401       assertEquals( "" + page3_.getInt( "id" ), form.get( "pageid" ) );
402       assertEquals( "body", form.get( "panel" ) );
403       assertEquals( "jsp/test.jsp", form.get( "jsp" ) );
404     }
405     catch ( Exception JavaDoc e ) {
406       e.printStackTrace();
407       fail();
408     }
409   }
410
411   public void testConfigureIncludeModule() {
412     try {
413       IncludeModule includeModule = null;
414
415       setRequestPathInfo( "/configureIncludeModule" );
416       addRequestParameter( "pageid", "" + page3_.getInt( "id" ) );
417       addRequestParameter( "panel", "body" );
418       addRequestParameter( "jsp", "jsp/test2.jsp" );
419       actionPerform();
420
421       verifyForwardPath( "/editPage.do?id=" + page3_.getInt( "id" ) );
422
423       includeModule = new IncludeModule();
424       includeModule.init( page3_, "body" );
425       includeModule.load();
426
427       assertEquals( "jsp/test2.jsp", includeModule.getString( "jsp" ) );
428     }
429     catch ( Exception JavaDoc e ) {
430       e.printStackTrace();
431       fail();
432     }
433   }
434 }
435
Popular Tags