KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.methodhead.sitecontext.*;
31 import com.methodhead.auth.*;
32 import com.methodhead.aikp.*;
33 import com.methodhead.*;
34 import servletunit.struts.*;
35 import org.apache.struts.action.*;
36 import org.apache.struts.util.*;
37 import org.apache.cactus.*;
38
39 public class HtmlFragmentActionTest extends CactusStrutsTestCase {
40
41   private HtmlFragment fragment = null;
42   private List list = null;
43   private LabelValueBean labelValue = null;
44   private DynaActionForm form = null;
45   private Panel panel = null;
46
47   private SiteContext siteContext1_ = null;
48   private HtmlFragment fragment1_ = null;
49   private HtmlFragment fragment2_ = null;
50   private Page page1_ = null;
51   private TextModule textModule1_ = null;
52
53   private void createData() {
54
55     fragment1_ = new HtmlFragment();
56     fragment1_.setSiteContext( siteContext1_ );
57     fragment1_.setString( "name", "Fragment1" );
58     fragment1_.setString( "value", "Value1" );
59     fragment1_.saveNew();
60
61     fragment2_ = new HtmlFragment();
62     fragment2_.setSiteContext( siteContext1_ );
63     fragment2_.setString( "name", "Fragment2" );
64     fragment2_.setString( "value", "Value2" );
65     fragment2_.saveNew();
66
67     panel = new Panel();
68     panel.setName( "body" );
69     panel.setModuleClass( "com.methodhead.shim.TextModule" );
70
71     page1_ = new Page();
72     page1_.set( "title", "Page1" );
73     page1_.set( "aliasname", "page1" );
74     page1_.setBoolean( "hidden", false );
75     page1_.setString( "template", "template.jsp" );
76     page1_.addPanel( panel );
77     page1_.setSiteContext( siteContext1_ );
78     page1_.saveNew();
79
80     textModule1_ = new TextModule();
81     textModule1_.init( page1_, "body" );
82     textModule1_.create();
83
84     textModule1_.setString( "value", "Test text." );
85     textModule1_.setInt( "htmlfragment_id", fragment1_.getInt( "id" ) );
86     textModule1_.save();
87   }
88
89   static {
90     TestUtils.initLogger();
91     TestUtils.initDb();
92   }
93
94   public HtmlFragmentActionTest( String JavaDoc name ) {
95     super( name );
96   }
97
98   public void setUp() {
99     try {
100       super.setUp();
101
102       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
103
104       siteContext1_ = new SiteContext();
105       siteContext1_.getDomains().add( "methodhead.com" );
106       siteContext1_.saveNew();
107
108       TestData.createUsers();
109       AuthUtil.setUser( request, TestData.user1 );
110       SiteContext.setContext( request, siteContext1_ );
111     }
112     catch ( Exception JavaDoc e ) {
113       fail( e.getMessage() );
114     }
115   }
116
117   public void tearDown()
118   throws
119     Exception JavaDoc {
120     super.tearDown();
121   }
122
123   public void testDoSaveNew() {
124     try {
125       createData();
126
127       setRequestPathInfo( "/htmlFragment" );
128       addRequestParameter( "action", "saveNew" );
129       addRequestParameter( "name", "Name" );
130       addRequestParameter( "value", "Value" );
131       actionPerform();
132
133       verifyForwardPath( "/htmlFragment.do?action=list" );
134   
135       fragment = new HtmlFragment();
136       fragment.load( new IntKey( 3 ) );
137       assertEquals( 1, fragment.getInt( "sitecontext_id" ) );
138       assertEquals( "Name", fragment.getString( "name" ) );
139       assertEquals( "Value", fragment.getString( "value" ) );
140     }
141     catch ( Exception JavaDoc e ) {
142       e.printStackTrace();
143       fail();
144     }
145   }
146
147   public void testDoList() {
148     try {
149       createData();
150
151       setRequestPathInfo( "/htmlFragment" );
152       addRequestParameter( "action", "list" );
153       actionPerform();
154
155       verifyForward( "list" );
156   
157       form = ( DynaActionForm )getActionForm();
158
159       assertEquals( "edit", form.get( "action" ) );
160
161       list = ( List )form.get( "list" );
162       assertEquals( 2, list.size() );
163
164       labelValue = ( LabelValueBean )list.get( 0 );
165       assertEquals( "Fragment1", labelValue.getLabel() );
166       assertEquals( "1", labelValue.getValue() );
167
168       labelValue = ( LabelValueBean )list.get( 1 );
169       assertEquals( "Fragment2", labelValue.getLabel() );
170       assertEquals( "2", labelValue.getValue() );
171     }
172     catch ( Exception JavaDoc e ) {
173       e.printStackTrace();
174       fail();
175     }
176   }
177
178   public void testDoCancel() {
179     try {
180       createData();
181
182       setRequestPathInfo( "/htmlFragment" );
183       addRequestParameter( "action", "save" );
184       addRequestParameter( "cancel", "Cancel" );
185       actionPerform();
186
187       verifyForwardPath( "/htmlFragment.do?action=list" );
188     }
189     catch ( Exception JavaDoc e ) {
190       e.printStackTrace();
191       fail();
192     }
193   }
194
195   public void testDoSave() {
196     try {
197       createData();
198
199       setRequestPathInfo( "/htmlFragment" );
200       addRequestParameter( "action", "save" );
201       addRequestParameter( "id", "1" );
202       addRequestParameter( "name", "Name" );
203       addRequestParameter( "value", "Value" );
204       actionPerform();
205
206       verifyForwardPath( "/htmlFragment.do?action=list" );
207     }
208     catch ( Exception JavaDoc e ) {
209       e.printStackTrace();
210       fail();
211     }
212   }
213
214   public void testDoConfirm() {
215     try {
216       createData();
217
218       setRequestPathInfo( "/htmlFragment" );
219       addRequestParameter( "action", "save" );
220       addRequestParameter( "delete", "Delete" );
221       addRequestParameter( "id", "1" );
222       actionPerform();
223
224       verifyInputForward();
225
226       verifyActionMessages( new String JavaDoc[] { "shim.htmlFragment.fragmentInUse" } );
227     }
228     catch ( Exception JavaDoc e ) {
229       e.printStackTrace();
230       fail();
231     }
232   }
233 }
234
Popular Tags