KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > event > EventTest


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.event;
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.sitecontext.*;
30 import com.methodhead.test.*;
31
32 public class EventTest extends DbTestCase {
33
34   private Event event1_ = null;
35   private Event event2_ = null;
36   private Event event3_ = null;
37   private Event event4_ = null;
38   private Event event5_ = null;
39
40   Event event = null;
41   SiteContext siteContext1_ = null;
42   List list = null;
43
44   public EventTest( String JavaDoc name ) {
45     super( name );
46   }
47
48   protected void setUp() {
49     //setLogLevel( Level.DEBUG );
50
try {
51       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
52
53       siteContext1_ = new SiteContext();
54       siteContext1_.saveNew();
55     }
56     catch ( Exception JavaDoc e ) {
57       fail( e.getMessage() );
58     }
59   }
60
61   protected void tearDown() {
62   }
63
64   protected void createEvents()
65   throws
66     Exception JavaDoc {
67
68     GregorianCalendar cal = new GregorianCalendar();
69
70     cal.set( 2003, 8, 27, 12, 0, 0 );
71     event1_ = new Event();
72     event1_.setDate( "eventTime", cal.getTime() );
73     event1_.setString( "description", "event1" );
74     event1_.saveNew();
75
76     cal.set( 2003, 8, 27, 13, 0, 0 );
77     event2_ = new Event();
78     event2_.setDate( "eventTime", cal.getTime() );
79     event2_.setString( "description", "event2" );
80     event2_.saveNew();
81
82     cal.set( 2003, 8, 27, 14, 0, 0 );
83     event3_ = new Event();
84     event3_.setDate( "eventTime", cal.getTime() );
85     event3_.setString( "description", "event3" );
86     event3_.saveNew();
87
88     cal.set( 2003, 8, 27, 15, 0, 0 );
89     event4_ = new Event();
90     event4_.setDate( "eventTime", cal.getTime() );
91     event4_.setString( "description", "event4" );
92     event4_.saveNew();
93
94     cal.set( 2003, 8, 27, 16, 0, 0 );
95     event5_ = new Event();
96     event5_.setSiteContext( siteContext1_ );
97     event5_.setDate( "eventTime", cal.getTime() );
98     event5_.setString( "description", "event5" );
99     event5_.saveNew();
100   }
101
102   public void testLogStatic() {
103     ResultSet rs = null;
104     try {
105       java.util.Date JavaDoc now = new java.util.Date JavaDoc();
106       Event.log( siteContext1_, "userName", "source", "test" );
107
108       rs = ConnectionSingleton.runQuery( "SELECT eventTime, userName, source, description FROM mh_event WHERE sitecontext_id=1 ORDER BY eventTime" );
109
110       assertNotNull( rs );
111       assertTrue( rs.next() );
112       assertTrue( ( now.getTime() - rs.getTimestamp( "eventTime" ).getTime() ) < 1000 );
113       assertEquals( "userName", rs.getString( "userName" ) );
114       assertEquals( "source", rs.getString( "source" ) );
115       assertEquals( "test", rs.getString( "description" ) );
116       assertTrue( !rs.next() );
117
118       now = new java.util.Date JavaDoc();
119       Event.log( siteContext1_, "test" );
120
121       rs = ConnectionSingleton.runQuery( "SELECT eventTime, userName, source, description FROM mh_event WHERE sitecontext_id=1 ORDER BY userName DESC" );
122
123       assertNotNull( rs );
124       assertTrue( rs.next() );
125       assertTrue( rs.next() );
126       assertTrue( ( now.getTime() - rs.getTimestamp( "eventTime" ).getTime() ) < 1000 );
127       assertEquals( "", rs.getString( "userName" ) );
128       assertEquals( "", rs.getString( "source" ) );
129       assertEquals( "test", rs.getString( "description" ) );
130       assertTrue( !rs.next() );
131
132       ConnectionSingleton.close( rs );
133     }
134     catch ( Exception JavaDoc e ) {
135       if ( rs != null )
136         ConnectionSingleton.close( rs );
137
138       e.printStackTrace();
139       fail();
140     }
141   }
142
143   public void testLog() {
144     ResultSet rs = null;
145     try {
146       java.util.Date JavaDoc now = new java.util.Date JavaDoc();
147       event = new Event();
148       event.log( "userName", "source", "test" );
149
150       rs = ConnectionSingleton.runQuery( "SELECT eventTime, userName, source, description FROM mh_event WHERE sitecontext_id=0" );
151
152       assertNotNull( rs );
153       assertTrue( rs.next() );
154       assertTrue( ( now.getTime() - rs.getTimestamp( "eventTime" ).getTime() ) < 1000 );
155       assertEquals( "userName", rs.getString( "userName" ) );
156       assertEquals( "source", rs.getString( "source" ) );
157       assertEquals( "test", rs.getString( "description" ) );
158       assertTrue( !rs.next() );
159     }
160     catch ( Exception JavaDoc e ) {
161       if ( rs != null )
162         ConnectionSingleton.close( rs );
163
164       e.printStackTrace();
165       fail();
166     }
167   }
168
169   public void testLogSingleArg() {
170     ResultSet rs = null;
171     try {
172       java.util.Date JavaDoc now = new java.util.Date JavaDoc();
173       event = new Event();
174       event.log( "test" );
175
176       rs = ConnectionSingleton.runQuery( "SELECT eventTime, userName, source, description FROM mh_event WHERE sitecontext_id=0" );
177
178       assertNotNull( rs );
179       assertTrue( rs.next() );
180       assertTrue( ( now.getTime() - rs.getTimestamp( "eventTime" ).getTime() ) < 1000 );
181       assertEquals( "", rs.getString( "userName" ) );
182       assertEquals( "", rs.getString( "source" ) );
183       assertEquals( "test", rs.getString( "description" ) );
184       assertTrue( !rs.next() );
185
186       ConnectionSingleton.close( rs );
187     }
188     catch ( Exception JavaDoc e ) {
189       if ( rs != null )
190         ConnectionSingleton.close( rs );
191
192       e.printStackTrace();
193       fail();
194     }
195   }
196
197   public void testGetEvents() {
198     try {
199       GregorianCalendar from = new GregorianCalendar();
200       GregorianCalendar to = new GregorianCalendar();
201       createEvents();
202       List events = null;
203       Event event = null;
204       event = new Event();
205
206       //
207
// bounded on both sides
208
//
209
from.set( 2003, 8, 27, 12, 30, 0 );
210       to.set( 2003, 8, 27, 14, 30, 0 );
211       events = event.getEvents( from.getTime(), to.getTime() );
212
213       assertNotNull( events );
214       assertEquals( 2, events.size() );
215
216       event = ( Event )events.get( 0 );
217
218       assertEquals( event3_.getString( "description" ), event.getString( "description" ) );
219
220       event = ( Event )events.get( 1 );
221
222       assertEquals( event2_.getString( "description" ), event.getString( "description" ) );
223
224       //
225
// unbounded from
226
//
227
events = event.getEvents( null, to.getTime() );
228
229       assertNotNull( events );
230       assertEquals( 3, events.size() );
231
232       event = ( Event )events.get( 0 );
233
234       assertEquals( event3_.getString( "description" ), event.getString( "description" ) );
235
236       event = ( Event )events.get( 1 );
237
238       assertEquals( event2_.getString( "description" ), event.getString( "description" ) );
239
240       event = ( Event )events.get( 2 );
241
242       assertEquals( event1_.getString( "description" ), event.getString( "description" ) );
243
244       //
245
// unbounded to
246
//
247
events = event.getEvents( from.getTime(), null );
248
249       assertNotNull( events );
250       assertEquals( 3, events.size() );
251
252       event = ( Event )events.get( 0 );
253
254       assertEquals( event4_.getString( "description" ), event.getString( "description" ) );
255
256       event = ( Event )events.get( 1 );
257
258       assertEquals( event3_.getString( "description" ), event.getString( "description" ) );
259
260       event = ( Event )events.get( 2 );
261
262       assertEquals( event2_.getString( "description" ), event.getString( "description" ) );
263
264       //
265
// unbounded
266
//
267
events = event.getEvents( null, null );
268
269       assertNotNull( events );
270       assertEquals( 4, events.size() );
271
272       event = ( Event )events.get( 0 );
273
274       assertEquals( event4_.getString( "description" ), event.getString( "description" ) );
275
276       event = ( Event )events.get( 1 );
277
278       assertEquals( event3_.getString( "description" ), event.getString( "description" ) );
279
280       event = ( Event )events.get( 2 );
281
282       assertEquals( event2_.getString( "description" ), event.getString( "description" ) );
283
284       event = ( Event )events.get( 3 );
285
286       assertEquals( event1_.getString( "description" ), event.getString( "description" ) );
287     }
288     catch ( Exception JavaDoc e ) {
289       e.printStackTrace();
290       fail();
291     }
292   }
293
294   public void testDeleteBefore() {
295     try {
296       GregorianCalendar cal = new GregorianCalendar( 2003, 8, 27, 13, 30, 0 );
297       createEvents();
298       List events = null;
299       Event event = null;
300
301       event = new Event();
302       event.deleteBefore( cal.getTime() );
303       events = event.getEvents( null, null );
304   
305       assertEquals( 2, events.size() );
306
307       event = ( Event )events.get( 0 );
308
309       assertEquals( event4_.getString( "description" ), event.getString( "description" ) );
310
311       event = ( Event )events.get( 1 );
312
313       assertEquals( event3_.getString( "description" ), event.getString( "description" ) );
314     }
315     catch ( Exception JavaDoc e ) {
316       e.printStackTrace();
317       fail();
318     }
319   }
320
321   public void testDeleteAll() {
322     try {
323       createEvents();
324
325       event = new Event();
326       event.setSiteContext( siteContext1_ );
327       event.deleteAll();
328
329       event = new Event();
330
331       event.setSiteContext( siteContext1_ );
332       list = event.getEvents( null, null );
333       assertEquals( 0, list.size() );
334
335       event.setSiteContext( SiteContext.getDefaultContext() );
336       list = event.getEvents( null, null );
337       assertEquals( 4, list.size() );
338     }
339     catch ( Exception JavaDoc e ) {
340       e.printStackTrace();
341       fail();
342     }
343   }
344 }
345
Popular Tags