KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > sitecontext > SiteContextAikpTest


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.sitecontext;
22
23 import java.util.*;
24 import java.sql.*;
25 import java.io.*;
26 import junit.framework.*;
27 import org.apache.log4j.*;
28 import com.methodhead.persistable.*;
29 import com.methodhead.aikp.*;
30 import com.methodhead.test.*;
31 import com.methodhead.*;
32 import org.apache.commons.beanutils.*;
33
34 public class SiteContextAikpTest extends TestCase {
35
36   SiteContextAikp siteContextAikp = null;
37   ResultSet rs = null;
38   List list = null;
39
40   static {
41     TestUtils.initLogger();
42     TestUtils.initDb();
43   }
44
45   public SiteContextAikpTest( String JavaDoc name ) {
46     super( name );
47   }
48
49   protected void setUp() {
50     try {
51       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
52     }
53     catch ( Exception JavaDoc e ) {
54       fail( e.getMessage() );
55     }
56   }
57
58   protected void tearDown() {
59   }
60
61   public void testProperties() {
62     try {
63       TestData.createDynaClassAndTable();
64
65       siteContextAikp = new SiteContextAikp( TestData.dynaClass );
66
67       //
68
// should throw an exception when site context has not been set
69
//
70
try {
71         siteContextAikp.getSiteContext();
72         fail( "No exception thrown" );
73       }
74       catch ( RuntimeException JavaDoc e ) {
75         // success
76
}
77
78       //
79
// should set and get as expected
80
//
81
siteContextAikp.setSiteContext( TestData.siteContext1 );
82       assertEquals( TestData.siteContext1, siteContextAikp.getSiteContext() );
83       assertEquals( 1, siteContextAikp.getInt( "sitecontext_id" ) );
84     }
85     catch ( Exception JavaDoc e ) {
86       e.printStackTrace();
87       fail();
88     }
89   }
90
91   public void testSaveNew() {
92     try {
93       TestData.createDynaClassAndTable();
94
95       //
96
// initialize the aikp
97
//
98
siteContextAikp = new SiteContextAikp( TestData.dynaClass );
99       siteContextAikp.setInt( "id", 0 );
100       siteContextAikp.setInt( "sitecontext_id", 0 );
101       siteContextAikp.setString( "field", "value" );
102
103       //
104
// should throw an exception when site context has not been set
105
//
106
try {
107         siteContextAikp.saveNew();
108         fail( "No exception thrown" );
109       }
110       catch ( RuntimeException JavaDoc e ) {
111         // success
112
}
113
114       //
115
// set site context and save new
116
//
117
siteContextAikp.setSiteContext( TestData.siteContext1 );
118       siteContextAikp.saveNew();
119
120       //
121
// should save site context id
122
//
123
rs = ConnectionSingleton.runQuery( "SELECT field FROM sitecontextaikp WHERE sitecontext_id=1" );
124       assertNotNull( rs );
125       assertTrue( rs.next() );
126       assertEquals( "value", rs.getString( "field" ) );
127       assertTrue( !rs.next() );
128
129       ConnectionSingleton.close( rs );
130     }
131     catch ( Exception JavaDoc e ) {
132       e.printStackTrace();
133       fail();
134     }
135   }
136
137   public void testSave() {
138     try {
139       TestData.createDynaClassAndTable();
140
141       //
142
// create a new testaikp
143
//
144
siteContextAikp = new SiteContextAikp( TestData.dynaClass );
145       siteContextAikp.setSiteContext( TestData.siteContext1 );
146       siteContextAikp.setInt( "id", 0 );
147       siteContextAikp.setInt( "sitecontext_id", 0 );
148       siteContextAikp.setString( "field", "value" );
149       siteContextAikp.saveNew();
150
151       //
152
// load it
153
//
154
siteContextAikp = new SiteContextAikp( TestData.dynaClass );
155       siteContextAikp.setSiteContext( TestData.siteContext1 );
156       siteContextAikp.load( new IntKey( "1" ) );
157
158       //
159
// force it's sitecontext_id to 0 (this happens in
160
// AikpAction.populatePersistable)
161
//
162
siteContextAikp.setInt( "sitecontext_id", 0 );
163       
164       siteContextAikp.save();
165
166       //
167
// should save site context id
168
//
169
rs = ConnectionSingleton.runQuery( "SELECT field FROM sitecontextaikp WHERE sitecontext_id=1" );
170       assertNotNull( rs );
171       assertTrue( rs.next() );
172       assertEquals( "value", rs.getString( "field" ) );
173       assertTrue( !rs.next() );
174
175       ConnectionSingleton.close( rs );
176     }
177     catch ( Exception JavaDoc e ) {
178       e.printStackTrace();
179       fail();
180     }
181   }
182
183   public void testLoad() {
184     try {
185       TestData.createSiteContextAikps();
186
187       siteContextAikp = new SiteContextAikp( TestData.dynaClass );
188
189       //
190
// should throw an exception when site context has not been set
191
//
192
try {
193         siteContextAikp.load( new IntKey( 1 ) );
194         fail( "No exception thrown" );
195       }
196       catch ( RuntimeException JavaDoc e ) {
197         // success
198
}
199
200       //
201
// set site context and load
202
//
203
siteContextAikp.setSiteContext( TestData.siteContext1 );
204       siteContextAikp.load( new IntKey( 1 ) );
205
206       //
207
// should save site context id
208
//
209
assertEquals( "siteContextAikp1", siteContextAikp.getString( "field" ) );
210
211       //
212
// set wrong site context and load
213
//
214
try {
215         siteContextAikp.setSiteContext( TestData.siteContext2 );
216         siteContextAikp.load( new IntKey( 1 ) );
217         fail( "No exception thrown" );
218       }
219       catch ( RuntimeException JavaDoc e ) {
220         // success
221
}
222     }
223     catch ( Exception JavaDoc e ) {
224       e.printStackTrace();
225       fail();
226     }
227   }
228
229   public void testLoadWhere() {
230     try {
231       TestData.createSiteContextAikps();
232
233       siteContextAikp = new SiteContextAikp( TestData.dynaClass );
234
235       //
236
// should throw an exception when site context has not been set
237
//
238
try {
239         siteContextAikp.load( "field='siteContextAikp1'" );
240         fail( "No exception thrown" );
241       }
242       catch ( RuntimeException JavaDoc e ) {
243         // success
244
}
245
246       //
247
// set site context and load
248
//
249
siteContextAikp.setSiteContext( TestData.siteContext1 );
250       siteContextAikp.load( "field='siteContextAikp1'" );
251
252       assertEquals( 1, siteContextAikp.getInt( "id" ) );
253
254       //
255
// set wrong site context and load
256
//
257
try {
258         siteContextAikp.setSiteContext( TestData.siteContext2 );
259         siteContextAikp.load( "field='siteContextAikp1'" );
260         fail( "No exception thrown" );
261       }
262       catch ( RuntimeException JavaDoc e ) {
263         // success
264
}
265     }
266     catch ( Exception JavaDoc e ) {
267       e.printStackTrace();
268       fail();
269     }
270   }
271
272   public void testLoadAll() {
273     try {
274       TestData.createSiteContextAikps();
275
276       siteContextAikp = new SiteContextAikp( TestData.dynaClass );
277
278       //
279
// should throw an exception when site context has not been set
280
//
281
try {
282         siteContextAikp.loadAll( null, "field" );
283         fail( "No exception thrown" );
284       }
285       catch ( RuntimeException JavaDoc e ) {
286         // success
287
}
288
289       //
290
// set site context and load all
291
//
292
siteContextAikp.setSiteContext( TestData.siteContext1 );
293       list = siteContextAikp.loadAll( null, "field" );
294
295       //
296
// should get site context 1 aikps; their sitecontext_id and site context
297
// should be set
298
//
299
assertEquals( 2, list.size() );
300
301       siteContextAikp = ( SiteContextAikp )list.get( 0 );
302       assertNotNull( siteContextAikp );
303       assertEquals( "siteContextAikp1", siteContextAikp.getString( "field" ) );
304       assertEquals( TestData.siteContext1, siteContextAikp.getSiteContext() );
305       assertEquals( 1, siteContextAikp.getInt( "sitecontext_id" ) );
306
307       siteContextAikp = ( SiteContextAikp )list.get( 1 );
308       assertNotNull( siteContextAikp );
309       assertEquals( "siteContextAikp2", siteContextAikp.getString( "field" ) );
310       assertEquals( TestData.siteContext1, siteContextAikp.getSiteContext() );
311       assertEquals( 1, siteContextAikp.getInt( "sitecontext_id" ) );
312     }
313     catch ( Exception JavaDoc e ) {
314       e.printStackTrace();
315       fail();
316     }
317   }
318 }
319
Popular Tags