KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.*;
26 import org.apache.log4j.*;
27 import com.methodhead.persistable.*;
28 import com.methodhead.test.*;
29 import servletunit.struts.*;
30 import org.apache.struts.action.*;
31 import org.apache.cactus.*;
32 import com.methodhead.sitecontext.*;
33 import com.methodhead.auth.*;
34 import com.methodhead.*;
35 import com.methodhead.util.*;
36 import java.io.*;
37 import org.apache.struts.util.*;
38
39 public class PageFormTest extends CactusStrutsTestCase {
40
41   private Panel panel = null;
42
43   private Page page1_ = null;
44   private Page page2_ = null;
45   private Link link1_ = null;
46   private Link link2_ = null;
47
48   DynaActionForm dynaForm = null;
49   List list = null;
50   LabelValueBean labelValueBean = null;
51
52   static {
53     TestUtils.initLogger();
54     TestUtils.initDb();
55   }
56
57   public PageFormTest( String JavaDoc name ) {
58     super( name );
59   }
60
61   public void setUp() {
62     try {
63       super.setUp();
64
65       TestData.createWebappFiles( ServletUtils.getRealFile( request, "" ) );
66
67       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
68
69       SiteContext.setContext( request, SiteContext.getDefaultContext() );
70
71       panel = new Panel();
72       panel.setName( "body" );
73       panel.setModuleClass( "com.methodhead.shim.MockModule" );
74
75       page1_ = new Page();
76       page1_.set( "title", "Page1" );
77       page1_.set( "aliasname", "page1" );
78       page1_.set( "template", "template.jsp" );
79       page1_.addPanel( panel );
80       page1_.setSiteContext( SiteContext.getDefaultContext() );
81       page1_.saveNew();
82
83       page2_ = new Page();
84       page2_.set( "title", "Page2" );
85       page2_.set( "aliasname", "page2" );
86       page2_.set( "template", "template.jsp" );
87       page2_.addPanel( panel );
88       page2_.setSiteContext( SiteContext.getDefaultContext() );
89       page2_.saveNew();
90
91       TestData.createUsers();
92       AuthUtil.setUser( request, TestData.user1 );
93
94       getSession().setAttribute( SiteContext.SITECONTEXT_KEY, SiteContext.getDefaultContext() );
95       getSession().setAttribute( ShimGlobals.SITEMAPTREE_KEY, new SiteMapTree() );
96
97       link1_ = new Link();
98       link1_.setPageId( 1 );
99       link1_.setTitle( "Page1" );
100
101       link2_ = new Link();
102       link2_.setPageId( 2 );
103       link2_.setTitle( "Page2" );
104
105       link1_.add( link2_ );
106
107       SiteMap siteMap = ShimUtils.getSiteMap( request );
108       siteMap.setRoot( link1_ );
109       siteMap.save();
110
111       SiteMapTree tree = ( SiteMapTree )getSession().getAttribute( ShimGlobals.SITEMAPTREE_KEY );
112       tree.build( siteMap );
113     }
114     catch ( Exception JavaDoc e ) {
115       fail( e.getMessage() );
116     }
117   }
118
119   public void tearDown()
120   throws
121     Exception JavaDoc {
122     super.tearDown();
123   }
124
125   public void testReset() {
126     //
127
// the /newPage action should be enough to test with
128
//
129
setRequestPathInfo( "/newPageForm" );
130     actionPerform();
131
132     dynaForm = ( DynaActionForm )getActionForm();
133     list = ( List )dynaForm.get( "templates" );
134
135     assertNotNull( list );
136     assertEquals( 5, list.size() );
137
138     //
139
// labels should be upcased, values should be the file names
140
//
141
labelValueBean = ( LabelValueBean )list.get( 0 );
142     assertEquals( "Select...", labelValueBean.getLabel() );
143     assertEquals( "", labelValueBean.getValue() );
144
145     labelValueBean = ( LabelValueBean )list.get( 1 );
146     assertEquals( "Template", labelValueBean.getLabel() );
147     assertEquals( "template.jsp", labelValueBean.getValue() );
148
149     labelValueBean = ( LabelValueBean )list.get( 2 );
150     assertEquals( "Template2", labelValueBean.getLabel() );
151     assertEquals( "Template2.jsp", labelValueBean.getValue() );
152
153     labelValueBean = ( LabelValueBean )list.get( 3 );
154     assertEquals( "Template3", labelValueBean.getLabel() );
155     assertEquals( "template3.jsp", labelValueBean.getValue() );
156
157     labelValueBean = ( LabelValueBean )list.get( 4 );
158     assertEquals( "Template4", labelValueBean.getLabel() );
159     assertEquals( "template4.jsp", labelValueBean.getValue() );
160   }
161
162   public void testValidateInvalidAlias() throws Exception JavaDoc {
163     //
164
// save a new page with an invalid alias
165
//
166
setRequestPathInfo( "/configurePage" );
167     addRequestParameter( "id", "" );
168     addRequestParameter( "title", "Test" );
169     addRequestParameter( "alias", "Test Alias" );
170     addRequestParameter( "template", "template.jsp" );
171     actionPerform();
172
173     verifyInputForward();
174     verifyActionErrors( new String JavaDoc[] { "shim.pageForm.invalidAlias" } );
175   }
176
177   public void testValidateAliasUsed() throws Exception JavaDoc {
178     //
179
// save a new page with an existing alias
180
//
181
setRequestPathInfo( "/configurePage" );
182     addRequestParameter( "id", "" );
183     addRequestParameter( "title", "Test" );
184     addRequestParameter( "alias", "page1" );
185     addRequestParameter( "template", "template.jsp" );
186     actionPerform();
187
188     verifyInputForward();
189     verifyActionErrors( new String JavaDoc[] { "shim.pageForm.aliasUsed" } );
190   }
191
192   public void testValidateAliasUsed2() throws Exception JavaDoc {
193     //
194
// save a new page with another page's alias
195
//
196
setRequestPathInfo( "/configurePage" );
197     addRequestParameter( "id", "" + page1_.getInt( "id" ) );
198     addRequestParameter( "title", "Test" );
199     addRequestParameter( "alias", "page2" );
200     addRequestParameter( "template", "template.jsp" );
201     actionPerform();
202
203     verifyInputForward();
204     verifyActionErrors( new String JavaDoc[] { "shim.pageForm.aliasUsed" } );
205   }
206
207   public void testValidateMissingTemplateOrCopy() throws Exception JavaDoc {
208     //
209
// save a new page without a template or page to copy from
210
//
211
setRequestPathInfo( "/configurePage" );
212     addRequestParameter( "id", "" );
213     addRequestParameter( "title", "Test" );
214     addRequestParameter( "alias", "test" );
215     addRequestParameter( "template", "" );
216     addRequestParameter( "copyfrom", "" );
217     actionPerform();
218
219     verifyInputForward();
220     verifyActionErrors( new String JavaDoc[] { "shim.pageForm.missingTemplateOrCopy" } );
221   }
222
223   public void testValidateQuotesInMetaKeywords() throws Exception JavaDoc {
224     //
225
// save a new page with quotes in the meta tags
226
//
227
setRequestPathInfo( "/configurePage" );
228     addRequestParameter( "id", "" );
229     addRequestParameter( "title", "Test" );
230     addRequestParameter( "alias", "test" );
231     addRequestParameter( "template", "template.jsp" );
232     addRequestParameter( "metadescription", "test\"test" );
233     addRequestParameter( "metakeywords", "test\"test" );
234     actionPerform();
235
236     verifyInputForward();
237     verifyActionErrors( new String JavaDoc[] { "shim.pageForm.quotesInMetaDescription", "shim.pageForm.quotesInMetaKeywords" } );
238   }
239
240   public void testValidate() throws Exception JavaDoc {
241     //
242
// a valid save
243
//
244
setRequestPathInfo( "/configurePage" );
245     addRequestParameter( "id", "" + page1_.getInt( "id" ) );
246     addRequestParameter( "title", "Test" );
247     addRequestParameter( "alias", "page1alias" );
248     addRequestParameter( "metadescription", "" );
249     addRequestParameter( "metakeywords", "" );
250     addRequestParameter( "template", "template.jsp" );
251     actionPerform();
252
253     verifyNoActionErrors();
254   }
255 }
256
Popular Tags