KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
32 import com.methodhead.*;
33 import org.apache.cactus.*;
34 import org.apache.struts.action.*;
35 import org.apache.struts.config.*;
36 import org.apache.commons.beanutils.*;
37
38 public class IncludeModuleTest extends JspTestCase {
39
40   static {
41     TestUtils.initLogger();
42     TestUtils.initDb();
43   }
44
45   public IncludeModuleTest( String JavaDoc name ) {
46     super( name );
47   }
48
49   private Page page1_ = null;
50   private Page page2_ = null;
51   private Panel panel1_ = null;
52
53   private IncludeModule includeModule;
54   private Page page = null;
55   private Panel panel = null;
56   private DynaActionForm dynaActionForm = null;
57   private ActionForward forward = null;
58
59   ResultSet rs = null;
60
61   protected void setUp() {
62     //setLogLevel( Level.DEBUG );
63
try {
64       TestData.createWebappFiles( ServletUtils.getRealFile( request, "" ) );
65
66       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
67
68       panel1_ = new Panel();
69       panel1_.setName( "body" );
70       panel1_.setModuleClass( "com.methodhead.shim.IncludeModule" );
71
72       page1_ = new Page();
73       page1_.setSiteContext( SiteContext.getDefaultContext() );
74       page1_.setString( "title", "Page1" );
75       page1_.setString( "template", "template.jsp" );
76       page1_.addPanel( panel1_ );
77       page1_.saveNew();
78
79       page2_ = new Page();
80       page2_.setSiteContext( SiteContext.getDefaultContext() );
81       page2_.setString( "title", "Page2" );
82       page2_.setString( "template", "template.jsp" );
83       page2_.addPanel( panel1_ );
84       page2_.saveNew();
85
86       SiteContext.setContext( request, SiteContext.getDefaultContext() );
87     }
88     catch ( Exception JavaDoc e ) {
89       fail( e.getMessage() );
90     }
91   }
92
93   protected void tearDown() {
94   }
95
96   public void testLoad() {
97     try {
98       includeModule = new IncludeModule();
99       includeModule.setInt( "page_id", page1_.getInt( "id" ) );
100       includeModule.setString( "panel", "body" );
101       includeModule.setString( "jsp", "jsp/test.jsp" );
102       includeModule.saveNew();
103
104       includeModule = new IncludeModule();
105       includeModule.init( page1_, "body" );
106       includeModule.load();
107
108       assertEquals( "jsp/test.jsp", includeModule.getString( "jsp" ) );
109     }
110     catch ( Exception JavaDoc e ) {
111       e.printStackTrace();
112       fail();
113     }
114   }
115
116   public void testSave() {
117     try {
118       IncludeModule includeModule = new IncludeModule();
119
120       includeModule = new IncludeModule();
121       includeModule.init( page1_, "body" );
122       includeModule.create();
123
124       includeModule = new IncludeModule();
125       includeModule.init( page1_, "body" );
126       includeModule.setString( "jsp", "jsp/test.jsp" );
127       includeModule.save();
128
129       includeModule = new IncludeModule();
130       includeModule.init( page1_, "body" );
131       includeModule.load();
132
133       assertEquals( "jsp/test.jsp", includeModule.getString( "jsp" ) );
134     }
135     catch ( Exception JavaDoc e ) {
136       e.printStackTrace();
137       fail();
138     }
139   }
140
141   public void testCreate() {
142     try {
143
144       //
145
// typical create
146
//
147
includeModule = new IncludeModule();
148       includeModule.init( page1_, "body" );
149       includeModule.create();
150
151       try {
152         includeModule = new IncludeModule();
153         includeModule.load( "page_id=" + page1_.getInt( "id" ) + " AND panel='body'" );
154       }
155       catch( Exception JavaDoc e ) {
156         fail( "Couldn't load include module." );
157       }
158
159       //
160
// create again; create() should check for an existing record and there
161
// should be just one
162
//
163
includeModule = new IncludeModule();
164       includeModule.init( page1_, "body" );
165       includeModule.create();
166
167       rs = ConnectionSingleton.runQuery( "SELECT page_id FROM shim_include WHERE page_id=" + page1_.getInt( "id" ) + " AND panel='body'" );
168       assertTrue( rs.next() );
169       assertTrue( !rs.next() );
170     }
171     catch ( Exception JavaDoc e ) {
172       e.printStackTrace();
173       fail();
174     }
175   }
176
177   public void testDisplay() {
178     try {
179       //
180
// display without init'ing
181
//
182
try {
183         includeModule = new IncludeModule();
184         includeModule.display( request, response, out );
185         fail( "IncludeModule.display() didn't throw an exception." );
186       }
187       catch ( Exception JavaDoc e ) {
188         // success
189
}
190
191       panel = new Panel();
192       panel.setName( "body" );
193       panel.setModuleClass( "com.methodhead.shim.IncludeModule" );
194
195       page = new Page();
196       page.setString( "template", "template.jsp" );
197       page.addPanel( panel );
198       page.setSiteContext( SiteContext.getDefaultContext() );
199       page.saveNew();
200
201       includeModule = new IncludeModule();
202       includeModule.init( page, "body" );
203       includeModule.setString( "jsp", "jsp/test.jsp" );
204       includeModule.saveNew();
205
206       includeModule = new IncludeModule();
207       includeModule.init( page, "body" );
208       includeModule.display( request, response, out );
209     }
210     catch ( Exception JavaDoc e ) {
211       e.printStackTrace();
212       fail();
213     }
214   }
215
216   public void endDisplay( WebResponse response ) {
217     assertEquals( "Hello from test.jsp.\n", response.getText() );
218   }
219
220   public void testDisplayException() {
221     try {
222       //
223
// display without init'ing
224
//
225
panel = new Panel();
226       panel.setName( "body" );
227       panel.setModuleClass( "com.methodhead.shim.IncludeModule" );
228
229       page = new Page();
230       page.setString( "template", "template.jsp" );
231       page.addPanel( panel );
232       page.setSiteContext( SiteContext.getDefaultContext() );
233       page.saveNew();
234
235       includeModule = new IncludeModule();
236       includeModule.init( page, "body" );
237       includeModule.setString( "jsp", "jsp/invalid.jsp" );
238       includeModule.saveNew();
239
240       includeModule = new IncludeModule();
241       includeModule.init( page, "body" );
242       includeModule.display( request, response, out );
243     }
244     catch ( Exception JavaDoc e ) {
245       e.printStackTrace();
246       fail();
247     }
248   }
249
250   public void endDisplayException( WebResponse response ) {
251     assertTrue( response.getText().startsWith( "org.apache.jasper.JasperException" ) );
252   }
253
254   public void testConfigure() {
255     try {
256       dynaActionForm = new MockDynaActionForm();
257       dynaActionForm.set( "pageid", "666" );
258       dynaActionForm.set( "panel", "body" );
259
260       includeModule = new IncludeModule();
261       forward = includeModule.configure( new ActionMapping(), dynaActionForm, request, response );
262
263       assertEquals( "/configureIncludeModuleForm.do?pageid=666&panel=body", forward.getPath() );
264     }
265     catch ( Exception JavaDoc e ) {
266       e.printStackTrace();
267       fail();
268     }
269   }
270
271   public void testDestroy() {
272     try {
273       IncludeModule includeModule = new IncludeModule();
274
275       includeModule = new IncludeModule();
276       includeModule.init( page1_, "body" );
277       includeModule.create();
278
279       includeModule = new IncludeModule();
280       includeModule.init( page1_, "body" );
281       includeModule.destroy();
282
283       try {
284         includeModule.load( "page_id=" + page1_.getInt( "id" ) + " AND panel='body'" );
285         fail( "Module still in database." );
286       }
287       catch ( Exception JavaDoc e ) {
288         // success
289
}
290     }
291     catch ( Exception JavaDoc e ) {
292       e.printStackTrace();
293       fail();
294     }
295   }
296
297   public void testCopyTo() {
298     try {
299       //
300
// copy to a new page
301
//
302
includeModule = new IncludeModule();
303       includeModule.init( page1_, "body" );
304       includeModule.create();
305       includeModule.setString( "jsp", "test.jsp" );
306       includeModule.save();
307
308       includeModule = new IncludeModule();
309       includeModule.init( page1_, "body" );
310       includeModule.copyTo( page2_ );
311
312       includeModule = new IncludeModule();
313       includeModule.init( page2_, "body" );
314       includeModule.load();
315       
316       assertEquals( "test.jsp", includeModule.getString( "jsp" ) );
317
318       //
319
// copy to an existing page
320
//
321
includeModule.setString( "jsp", "newtest.jsp" );
322       includeModule.save();
323
324       includeModule = new IncludeModule();
325       includeModule.init( page2_, "body" );
326       includeModule.copyTo( page1_ );
327
328       includeModule = new IncludeModule();
329       includeModule.init( page1_, "body" );
330       includeModule.load();
331       
332       assertEquals( "newtest.jsp", includeModule.getString( "jsp" ) );
333     }
334     catch ( Exception JavaDoc e ) {
335       e.printStackTrace();
336       fail();
337     }
338   }
339 }
340
Popular Tags