KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
25 import java.sql.*;
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.struts.util.*;
33 import org.apache.cactus.*;
34 import com.methodhead.sitecontext.*;
35 import com.methodhead.auth.*;
36 import com.methodhead.*;
37 import com.methodhead.util.*;
38 import org.apache.commons.lang.*;
39
40 public class ModuleFormTest extends CactusStrutsTestCase {
41
42   String JavaDoc s = null;
43   LabelValueBean labelValue = null;
44   List list = null;
45   DynaActionForm form = null;
46   Panel panel = null;
47
48   Page page1_ = null;
49   TextModule textModule1_ = null;
50   HtmlFragment fragment1_ = null;
51   HtmlFragment fragment2_ = null;
52
53   private void createData() {
54     panel = new Panel();
55     panel.setName( "body" );
56     panel.setModuleClass( "com.methodhead.shim.TextModule" );
57
58     page1_ = new Page();
59     page1_.set( "title", "Page1" );
60     page1_.set( "aliasname", "page1" );
61     page1_.setBoolean( "hidden", false );
62     page1_.setString( "template", "template.jsp" );
63     page1_.addPanel( panel );
64     page1_.setSiteContext( SiteContext.getDefaultContext() );
65     page1_.saveNew();
66
67     textModule1_ = new TextModule();
68     textModule1_.init( page1_, "body" );
69     textModule1_.create();
70
71     fragment1_ = new HtmlFragment();
72     fragment1_.setSiteContext( SiteContext.getDefaultContext() );
73     fragment1_.setString( "name", "Fragment2" );
74     fragment1_.setString( "value", "This is Fragment2" );
75     fragment1_.saveNew();
76
77     fragment2_ = new HtmlFragment();
78     fragment2_.setSiteContext( SiteContext.getDefaultContext() );
79     fragment2_.setString( "name", "Fragment1" );
80     fragment2_.setString( "value", "This is Fragment1" );
81     fragment2_.saveNew();
82   }
83
84   static {
85     TestUtils.initLogger();
86     TestUtils.initDb();
87   }
88
89   public ModuleFormTest( String JavaDoc name ) {
90     super( name );
91   }
92
93   public void setUp() {
94     try {
95       super.setUp();
96
97       TestData.createWebappFiles( ServletUtils.getRealFile( request, "" ) );
98
99       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
100
101       TestData.createUsers();
102       AuthUtil.setUser( request, TestData.user1 );
103       SiteContext.setContext( request, SiteContext.getDefaultContext() );
104     }
105     catch ( Exception JavaDoc e ) {
106       fail( e.getMessage() );
107     }
108   }
109
110   public void tearDown()
111   throws
112     Exception JavaDoc {
113     super.tearDown();
114   }
115
116   public void testReset() {
117     try {
118       createData();
119
120       //
121
// missing jsp
122
//
123
setRequestPathInfo( "/configureTextModuleForm" );
124       addRequestParameter( "pageid", "1" );
125       addRequestParameter( "panel", "body" );
126       actionPerform();
127
128       form = ( DynaActionForm )getActionForm();
129       list = ( List )form.get( "fragments" );
130       assertEquals( 2, list.size() );
131
132       labelValue = ( LabelValueBean )list.get( 0 );
133       assertEquals( "Fragment1", labelValue.getLabel() );
134       assertEquals( "2", labelValue.getValue() );
135
136       labelValue = ( LabelValueBean )list.get( 1 );
137       assertEquals( "Fragment2", labelValue.getLabel() );
138       assertEquals( "1", labelValue.getValue() );
139     }
140     catch ( Exception JavaDoc e ) {
141       e.printStackTrace();
142       fail();
143     }
144   }
145
146   public void testValidateConfigureNavModuleMissingJsp() throws Exception JavaDoc {
147     createData();
148
149     //
150
// missing jsp
151
//
152
setRequestPathInfo( "/configureNavModule" );
153     addRequestParameter( "pageid", "1" );
154     addRequestParameter( "type", "jsp" );
155     actionPerform();
156
157     verifyInputForward();
158     verifyActionErrors( new String JavaDoc[] { "shim.moduleForm.missingJsp" } );
159   }
160
161   public void testValidateConfigureNavModuleInvalidJsp() throws Exception JavaDoc {
162     //
163
// invalid jsp
164
//
165
createData();
166     setRequestPathInfo( "/configureNavModule" );
167     addRequestParameter( "pageid", "1" );
168     addRequestParameter( "type", "jsp" );
169     addRequestParameter( "jsp", "test.txt" );
170     actionPerform();
171
172     verifyInputForward();
173     verifyActionErrors( new String JavaDoc[] { "shim.moduleForm.invalidJsp" } );
174   }
175
176   public void testValidateConfigureNavModuleNonExistantJsp() throws Exception JavaDoc {
177     //
178
// non-existant jsp
179
//
180
createData();
181     setRequestPathInfo( "/configureNavModule" );
182     addRequestParameter( "pageid", "1" );
183     addRequestParameter( "type", "jsp" );
184     addRequestParameter( "jsp", "invalid.jsp" );
185     actionPerform();
186
187     verifyInputForward();
188     verifyActionErrors( new String JavaDoc[] { "shim.moduleForm.jspDoesNotExist" } );
189   }
190
191   public void testValidateConfigureNavModuleBadLevels() throws Exception JavaDoc {
192     //
193
// bad levels
194
//
195
createData();
196     setRequestPathInfo( "/configureNavModule" );
197     addRequestParameter( "pageid", "1" );
198     addRequestParameter( "type", "jsp" );
199     addRequestParameter( "pageid", "1" );
200     addRequestParameter( "panel", "body" );
201     addRequestParameter( "type", "folding" );
202     addRequestParameter( "startlev", "x" );
203     addRequestParameter( "contextlev", "y" );
204     addRequestParameter( "depthlev", "z" );
205     addRequestParameter( "toplev", "a" );
206     addRequestParameter( "header", "header" );
207     addRequestParameter( "link", "link" );
208     addRequestParameter( "curlink", "curlink" );
209     addRequestParameter( "footer", "footer" );
210     actionPerform();
211
212     verifyInputForward();
213     verifyActionErrors( new String JavaDoc[] { "shim.moduleForm.invalidStartLev", "shim.moduleForm.invalidContextLev", "shim.moduleForm.invalidDepthLev", "shim.moduleForm.invalidTopLev" } );
214   }
215
216   public void testValidateConfigureNavModuleHtmlTooLong() throws Exception JavaDoc {
217     //
218
// html too long
219
//
220
s = StringUtils.repeat( "a", 513 );
221
222     createData();
223     setRequestPathInfo( "/configureNavModule" );
224     addRequestParameter( "pageid", "1" );
225     addRequestParameter( "type", "jsp" );
226     addRequestParameter( "pageid", "1" );
227     addRequestParameter( "panel", "body" );
228     addRequestParameter( "type", "folding" );
229     addRequestParameter( "startlev", "1" );
230     addRequestParameter( "contextlev", "1" );
231     addRequestParameter( "depthlev", "1" );
232     addRequestParameter( "toplev", "1" );
233     addRequestParameter( "header", s );
234     addRequestParameter( "link", s );
235     addRequestParameter( "curlink", s );
236     addRequestParameter( "footer", s );
237     actionPerform();
238
239     verifyInputForward();
240     verifyActionErrors( new String JavaDoc[] { "shim.moduleForm.headerTooLong", "shim.moduleForm.linkTooLong", "shim.moduleForm.curlinkTooLong", "shim.moduleForm.footerTooLong" } );
241   }
242
243   public void testValidateConfigureIncludeModuleMissingJsp() throws Exception JavaDoc {
244     //
245
// missing jsp
246
//
247
setRequestPathInfo( "/configureIncludeModule" );
248     addRequestParameter( "pageid", "1" );
249     addRequestParameter( "panel", "body" );
250     actionPerform();
251
252     verifyInputForward();
253     verifyActionErrors( new String JavaDoc[] { "shim.moduleForm.missingJsp" } );
254   }
255
256   public void testValidateConfigureIncludeModuleInvalidJsp() throws Exception JavaDoc {
257     //
258
// invalid jsp
259
//
260
setRequestPathInfo( "/configureIncludeModule" );
261     addRequestParameter( "pageid", "1" );
262     addRequestParameter( "panel", "body" );
263     addRequestParameter( "jsp", "test.txt" );
264     actionPerform();
265
266     verifyInputForward();
267     verifyActionErrors( new String JavaDoc[] { "shim.moduleForm.invalidJsp" } );
268   }
269
270   public void testValidateConfigureIncludeModuleNonExistantJsp() throws Exception JavaDoc {
271     //
272
// non-existant jsp
273
//
274
setRequestPathInfo( "/configureIncludeModule" );
275     addRequestParameter( "pageid", "1" );
276     addRequestParameter( "panel", "body" );
277     addRequestParameter( "jsp", "invalid.jsp" );
278     actionPerform();
279
280     verifyInputForward();
281     verifyActionErrors( new String JavaDoc[] { "shim.moduleForm.jspDoesNotExist" } );
282   }
283 }
284
Popular Tags