KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
24 import java.util.*;
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.*;
32 import com.methodhead.util.*;
33 import org.apache.cactus.*;
34
35 public class TemplateTest extends ServletTestCase {
36
37   static {
38     TestUtils.initLogger();
39     TestUtils.initDb();
40   }
41
42   public TemplateTest( String JavaDoc name ) {
43     super( name );
44   }
45
46   File templateBase_ = null;
47   File template_ = null;
48   SiteContext siteContext1_ = null;
49   SiteContext siteContext2_ = null;
50
51   protected void setUp() {
52     //setLogLevel( Level.DEBUG );
53
try {
54       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
55
56       siteContext1_ = new SiteContext();
57       siteContext1_.setString( "path", "foo" );
58       siteContext1_.saveNew();
59
60       siteContext2_ = new SiteContext();
61       siteContext2_.setString( "path", "foo" );
62       siteContext2_.saveNew();
63     }
64     catch ( Exception JavaDoc e ) {
65       fail( e.getMessage() );
66     }
67   }
68
69   protected void tearDown() {
70   }
71
72   public void testGetSiteContext() {
73     try {
74       Template template = null;
75       SiteContext siteContext = null;
76
77       template = new Template();
78       siteContext = SiteContext.getDefaultContext();
79
80       //
81
// get site context
82
//
83
try {
84         template.getSiteContext();
85         fail( "Template.getSiteContext() did not throw an exception." );
86       }
87       catch ( ShimException e ) {
88         // success
89
}
90
91       template.setSiteContext( siteContext );
92
93       assertNotNull( template.getSiteContext() );
94       assertEquals( siteContext, template.getSiteContext() );
95     }
96     catch ( Exception JavaDoc e ) {
97       e.printStackTrace();
98       fail();
99     }
100   }
101
102   public void testGetTemplateList() throws Exception JavaDoc {
103     Template t = null;
104     List l = null;
105
106     //
107
// get the list for the default context
108
//
109
t = new Template();
110     t.setSiteContext( SiteContext.getDefaultContext() );
111     l = t.getTemplateList( request );
112
113     assertNotNull( l );
114     assertEquals( 4, l.size() );
115     assertEquals( "template.jsp", l.get( 0 ) );
116     assertEquals( "Template2.jsp", l.get( 1 ) );
117     assertEquals( "template3.jsp", l.get( 2 ) );
118     assertEquals( "template4.jsp", l.get( 3 ) );
119
120     //
121
// get the list for a specific context (should be id=1)
122
//
123
t = new Template();
124     t.setSiteContext( siteContext1_ );
125     l = t.getTemplateList( request );
126
127     assertNotNull( l );
128     assertEquals( 2, l.size() );
129     assertEquals( "template.jsp", l.get( 0 ) );
130
131     //
132
// get the list for a specific context with no templates (should be id=2)
133
//
134
t = new Template();
135     t.setSiteContext( siteContext2_ );
136     l = t.getTemplateList( request );
137
138     assertNotNull( l );
139     assertEquals( 0, l.size() );
140   }
141
142   public void testLoad() {
143     try {
144       Template t = null;
145       PanelConfig panelConfig = null;
146       Map map = null;
147
148       TestData.createWebappFiles( ServletUtils.getRealFile( request, "" ) );
149
150       //
151
// try to load with an invalid template
152
//
153
try {
154         t = new Template();
155         t.setSiteContext( SiteContext.getDefaultContext() );
156         t.load( request, null );
157         fail( "load() did not throw an exception." );
158       }
159       catch ( ShimException e ) {
160         // success
161
}
162
163       //
164
// try to load with an invalid template
165
//
166
try {
167         t = new Template();
168         t.setSiteContext( SiteContext.getDefaultContext() );
169         t.load( request, "" );
170         fail( "load() did not throw an exception." );
171       }
172       catch ( ShimException e ) {
173         // success
174
}
175
176       //
177
// load for the default context
178
//
179
t = new Template();
180       t.setSiteContext( SiteContext.getDefaultContext() );
181       t.load( request, "template.jsp" );
182
183       assertEquals( "template.jsp", t.getFileName() );
184       assertNull( request.getAttribute( ShimGlobals.PANELMAP_KEY) );
185
186       map = t.getPanelConfigs();
187
188       assertNotNull( map );
189       assertEquals( 1, map.keySet().size() );
190
191       panelConfig = ( PanelConfig )map.get( "body" );
192
193       assertNotNull( panelConfig );
194       assertEquals( ShimGlobals.DEFAULT_MODULE, panelConfig.getDefaultModule() );
195
196       //
197
// load for a particular context
198
//
199
t = new Template();
200       t.setSiteContext( siteContext1_ );
201       t.load( request, "template.jsp" );
202
203       assertEquals( "template.jsp", t.getFileName() );
204       assertNull( request.getAttribute( ShimGlobals.PANELMAP_KEY) );
205
206       map = t.getPanelConfigs();
207
208       assertNotNull( map );
209       assertEquals( 2, map.keySet().size() );
210
211       panelConfig = ( PanelConfig )map.get( "body" );
212
213       assertNotNull( panelConfig );
214       assertEquals( ShimGlobals.DEFAULT_MODULE, panelConfig.getDefaultModule() );
215
216       panelConfig = ( PanelConfig )map.get( "footer" );
217
218       assertNotNull( panelConfig );
219       assertEquals( "some.Module", panelConfig.getDefaultModule() );
220     }
221     catch ( Exception JavaDoc e ) {
222       e.printStackTrace();
223       fail();
224     }
225   }
226
227   public void testInclude() {
228     try {
229       Template template = null;
230       SiteContext siteContext = null;
231
232       //
233
// include a template from the default site context
234
//
235
template = new Template();
236       template.setSiteContext( SiteContext.getDefaultContext() );
237       template.include( request, response, "template3.jsp" );
238
239       //
240
// include a template from a specific site context
241
//
242
siteContext = new SiteContext();
243       siteContext.setInt( "id", 1 );
244       template.setSiteContext( siteContext );
245       template.include( request, response, "template2.jsp" );
246     }
247     catch ( Exception JavaDoc e ) {
248       e.printStackTrace();
249       fail();
250     }
251   }
252
253   public void endInclude( WebResponse response ) {
254     assertEquals( "This is a template.\nThis is also a template.\n", response.getText() );
255   }
256 }
257
Popular Tags