KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.methodhead.sitecontext.*;
30 import com.methodhead.aikp.*;
31 import org.apache.cactus.*;
32 import org.apache.struts.action.*;
33 import org.apache.struts.config.*;
34 import org.apache.commons.beanutils.*;
35 import java.io.*;
36
37 public class TextModuleTest extends JspTestCase {
38
39   TextModule textModule = null;
40   Page page = null;
41   Panel panel = null;
42   HtmlFragment fragment = null;
43   List list = null;
44
45   private Page page1_ = null;
46   private Page page2_ = null;
47   private Panel panel1_ = null;
48   private HtmlFragment fragment1_ = null;
49   private HtmlFragment fragment2_ = null;
50   SiteContext siteContext1_ = null;
51
52   static {
53     TestUtils.initLogger();
54     TestUtils.initDb();
55   }
56
57   public TextModuleTest( String JavaDoc name ) {
58     super( name );
59   }
60
61   protected void setUp() {
62     //setLogLevel( Level.DEBUG );
63
try {
64       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
65
66       siteContext1_ = new SiteContext();
67       siteContext1_.getDomains().add( "methodhead.com" );
68       siteContext1_.setString( "path", "path" );
69       siteContext1_.saveNew();
70
71       panel1_ = new Panel();
72       panel1_.setName( "body" );
73       panel1_.setModuleClass( "com.methodhead.shim.TextModule" );
74
75       page1_ = new Page();
76       page1_.setSiteContext( SiteContext.getDefaultContext() );
77       page1_.setString( "title", "Page1" );
78       page1_.setString( "template", "template.jsp" );
79       page1_.addPanel( panel1_ );
80       page1_.saveNew();
81
82       page2_ = new Page();
83       page2_.setSiteContext( siteContext1_ );
84       page2_.setString( "title", "Page2" );
85       page2_.setString( "template", "template.jsp" );
86       page2_.addPanel( panel1_ );
87       page2_.saveNew();
88
89       fragment1_ = new HtmlFragment();
90       fragment1_.setSiteContext( SiteContext.getDefaultContext() );
91       fragment1_.setString( "name", "Fragment1" );
92       fragment1_.setString( "value", "This is Fragment1" );
93       fragment1_.saveNew();
94
95       fragment2_ = new HtmlFragment();
96       fragment2_.setSiteContext( SiteContext.getDefaultContext() );
97       fragment2_.setString( "name", "Fragment2" );
98       fragment2_.setString( "value", "This is Fragment2" );
99       fragment2_.saveNew();
100     }
101     catch ( Exception JavaDoc e ) {
102       fail( e.getMessage() );
103     }
104   }
105
106   protected void tearDown() {
107   }
108
109   public void testLoad() {
110     try {
111       TextModule textModule = new TextModule();
112
113       textModule = new TextModule();
114       textModule.setInt( "page_id", page1_.getInt( "id" ) );
115       textModule.setString( "panel", "body" );
116       textModule.setString( "value", "Test text." );
117       textModule.saveNew();
118       textModule = new TextModule();
119       textModule.init( page1_, "body" );
120       textModule.load();
121
122       assertEquals( "Test text.", textModule.getString( "value" ) );
123       assertEquals( 0, textModule.getInt( "htmlfragment_id" ) );
124     }
125     catch ( Exception JavaDoc e ) {
126       e.printStackTrace();
127       fail();
128     }
129   }
130
131   public void testSave() {
132     try {
133       TextModule textModule = new TextModule();
134
135       textModule = new TextModule();
136       textModule.init( page1_, "body" );
137       textModule.create();
138       textModule = new TextModule();
139       textModule.init( page1_, "body" );
140       textModule.setString( "value", "Updated text." );
141       textModule.save();
142       textModule = new TextModule();
143       textModule.init( page1_, "body" );
144       textModule.load();
145
146       assertEquals( "Updated text.", textModule.getString( "value" ) );
147     }
148     catch ( Exception JavaDoc e ) {
149       e.printStackTrace();
150       fail();
151     }
152   }
153
154   public void testCreate() {
155     try {
156       TextModule textModule = null;
157       ResultSet rs = null;
158
159       //
160
// typical create
161
//
162
textModule = new TextModule();
163       textModule.init( page1_, "body" );
164       textModule.create();
165       rs = ConnectionSingleton.runQuery( "SELECT value,htmlfragment_id FROM shim_text WHERE page_id=" + page1_.getInt( "id" ) + " AND panel='body'" );
166
167       assertTrue( rs.next() );
168       assertEquals( "Insert your text here...", rs.getString( "value" ) );
169       assertEquals( 0, rs.getInt( "htmlfragment_id" ) );
170       assertTrue( !rs.next() );
171
172       ConnectionSingleton.close( rs );
173
174       //
175
// create for an existing page/panel
176
//
177
textModule.setString( "value", "Updated text." );
178       textModule.save();
179
180       textModule = new TextModule();
181       textModule.init( page1_, "body" );
182       textModule.create();
183       rs = ConnectionSingleton.runQuery( "SELECT value,htmlfragment_id FROM shim_text WHERE page_id=" + page1_.getInt( "id" ) + " AND panel='body'" );
184
185       assertTrue( rs.next() );
186       assertEquals( "Updated text.", rs.getString( "value" ) );
187       assertEquals( 0, rs.getInt( "htmlfragment_id" ) );
188       assertTrue( !rs.next() );
189
190       ConnectionSingleton.close( rs );
191     }
192     catch ( Exception JavaDoc e ) {
193       e.printStackTrace();
194       fail();
195     }
196   }
197
198   public void testDisplay() {
199     try {
200       TextModule textModule = null;
201       Page page = null;
202       Panel panel = null;
203
204       //
205
// display without init'ing
206
//
207
try {
208         textModule = new TextModule();
209         textModule.display( request, response, out );
210         fail( "TextModule.display() didn't throw an exception." );
211       }
212       catch ( Exception JavaDoc e ) {
213         // success
214
}
215
216       panel = new Panel();
217       panel.setName( "body" );
218       panel.setModuleClass( "com.methodhead.shim.TextModule" );
219       page = new Page();
220       page.setString( "template", "template.jsp" );
221       page.addPanel( panel );
222       page.setSiteContext( SiteContext.getDefaultContext() );
223       page.saveNew();
224       textModule = new TextModule();
225       textModule.setInt( "page_id", page.getInt( "id" ) );
226       textModule.setString( "panel", "body" );
227       textModule.setString( "value", "Test text." );
228       textModule.saveNew();
229       textModule = new TextModule();
230       textModule.init( page, "body" );
231       textModule.display( request, response, out );
232     }
233     catch ( Exception JavaDoc e ) {
234       e.printStackTrace();
235       fail();
236     }
237   }
238
239   public void endDisplay( WebResponse response ) {
240     assertEquals( "Test text.\n", response.getText() );
241   }
242
243   public void testDisplayFragment() {
244     try {
245
246       panel = new Panel();
247       panel.setName( "body" );
248       panel.setModuleClass( "com.methodhead.shim.TextModule" );
249
250       page = new Page();
251       page.setString( "template", "template.jsp" );
252       page.addPanel( panel );
253       page.setSiteContext( SiteContext.getDefaultContext() );
254       page.saveNew();
255
256       textModule = new TextModule();
257       textModule.setInt( "page_id", page.getInt( "id" ) );
258       textModule.setString( "panel", "body" );
259       textModule.setString( "value", "Test text." );
260       textModule.setInt( "htmlfragment_id", fragment1_.getInt( "id" ) );
261       textModule.saveNew();
262
263       textModule = new TextModule();
264       textModule.init( page, "body" );
265       textModule.display( request, response, out );
266     }
267     catch ( Exception JavaDoc e ) {
268       e.printStackTrace();
269       fail();
270     }
271   }
272
273   public void endDisplayFragment( WebResponse response ) {
274     assertEquals( "This is Fragment1\n", response.getText() );
275   }
276
277   public void testConfigure() {
278     try {
279       DynaActionForm dynaActionForm = null;
280       ActionForward forward = null;
281       TextModule textModule = null;
282
283       dynaActionForm = new MockDynaActionForm();
284       dynaActionForm.set( "pageid", "666" );
285       dynaActionForm.set( "panel", "body" );
286
287       textModule = new TextModule();
288       forward = textModule.configure( new ActionMapping(), dynaActionForm, request, response );
289
290       assertEquals( "/configureTextModuleForm.do?pageid=666&panel=body", forward.getPath() );
291     }
292     catch ( Exception JavaDoc e ) {
293       e.printStackTrace();
294       fail();
295     }
296   }
297
298   public void testUpdate() {
299     try {
300       TextModule textModule = new TextModule();
301
302       textModule = new TextModule();
303       textModule.init( page1_, "body" );
304       textModule.create();
305       textModule.update( "Updated text. And a <a HREF=\"pg/test\">link</a>" );
306       textModule = new TextModule();
307       textModule.init( page1_, "body" );
308       textModule.load();
309
310       assertEquals( "Updated text. And a <a HREF=\"pg/test\">link</a>", textModule.getString( "value" ) );
311     }
312     catch ( Exception JavaDoc e ) {
313       e.printStackTrace();
314       fail();
315     }
316   }
317
318   public void testUpdateFragment() {
319     try {
320       TextModule textModule = new TextModule();
321
322       textModule = new TextModule();
323       textModule.init( page1_, "body" );
324       textModule.setInt( "htmlfragment_id", fragment1_.getInt( "id" ) );
325       textModule.create();
326
327       textModule = new TextModule();
328       textModule.init( page1_, "body" );
329       textModule.update( "Updated text." );
330
331       fragment = new HtmlFragment();
332       fragment.load( new IntKey( fragment1_.getInt( "id" ) ) );
333       assertEquals( "Updated text.", fragment.getString( "value" ) );
334     }
335     catch ( Exception JavaDoc e ) {
336       e.printStackTrace();
337       fail();
338     }
339   }
340
341   public void testDestroy() {
342     try {
343       TextModule textModule = new TextModule();
344
345       textModule = new TextModule();
346       textModule.init( page1_, "body" );
347       textModule.create();
348
349       textModule = new TextModule();
350       textModule.init( page1_, "body" );
351       textModule.destroy();
352
353       try {
354         textModule.load( "page_id=" + page1_.getInt( "id" ) + " AND panel='body'" );
355         fail( "Text module still in database." );
356       }
357       catch ( Exception JavaDoc e ) {
358         // success
359
}
360     }
361     catch ( Exception JavaDoc e ) {
362       e.printStackTrace();
363       fail();
364     }
365   }
366
367   public void testCopyTo() {
368     try {
369       //
370
// copy to a new page
371
//
372
textModule = new TextModule();
373       textModule.init( page1_, "body" );
374       textModule.create();
375       textModule.setString( "value", "test" );
376       textModule.setInt( "htmlfragment_id", fragment1_.getInt( "id" ) );
377       textModule.save();
378
379       textModule = new TextModule();
380       textModule.init( page1_, "body" );
381       textModule.copyTo( page2_ );
382
383       textModule = new TextModule();
384       textModule.init( page2_, "body" );
385       textModule.load();
386       
387       assertEquals( "test", textModule.getString( "value" ) );
388       assertEquals( fragment1_.getInt( "id" ), textModule.getInt( "htmlfragment_id" ) );
389
390       //
391
// copy to a page with an existing text module
392
//
393
textModule.setString( "value", "updatedtest" );
394       textModule.setInt( "htmlfragment_id", fragment2_.getInt( "id" ) );
395       textModule.save();
396
397       textModule = new TextModule();
398       textModule.init( page2_, "body" );
399       textModule.copyTo( page1_ );
400
401       textModule = new TextModule();
402       textModule.init( page1_, "body" );
403       textModule.load();
404       
405       assertEquals( "updatedtest", textModule.getString( "value" ) );
406       assertEquals( fragment2_.getInt( "id" ), textModule.getInt( "htmlfragment_id" ) );
407     }
408     catch ( Exception JavaDoc e ) {
409       e.printStackTrace();
410       fail();
411     }
412   }
413
414   public void testGetDependentPages() {
415     try {
416       //
417
// copy to a new page
418
//
419
textModule = new TextModule();
420       textModule.init( page1_, "body" );
421       textModule.create();
422       textModule.setInt( "htmlfragment_id", fragment1_.getInt( "id" ) );
423       textModule.save();
424       
425       textModule = new TextModule();
426       list = textModule.getDependentPages( fragment1_ );
427       assertNotNull( list );
428       assertEquals( 1, list.size() );
429
430       page = ( Page )list.get( 0 );
431       assertEquals( page1_.getInt( "id" ), page.getInt( "id" ) );
432     }
433     catch ( Exception JavaDoc e ) {
434       e.printStackTrace();
435       fail();
436     }
437   }
438 }
439
Popular Tags