001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.event;
022:
023: import java.util.*;
024: import java.io.*;
025: import java.sql.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.sitecontext.*;
030: import com.methodhead.test.*;
031:
032: public class EventTest extends DbTestCase {
033:
034: private Event event1_ = null;
035: private Event event2_ = null;
036: private Event event3_ = null;
037: private Event event4_ = null;
038: private Event event5_ = null;
039:
040: Event event = null;
041: SiteContext siteContext1_ = null;
042: List list = null;
043:
044: public EventTest(String name) {
045: super (name);
046: }
047:
048: protected void setUp() {
049: //setLogLevel( Level.DEBUG );
050: try {
051: ConnectionSingleton.runBatchUpdate(new FileReader(
052: "webapp/WEB-INF/db/transfer-reset.sql"));
053:
054: siteContext1_ = new SiteContext();
055: siteContext1_.saveNew();
056: } catch (Exception e) {
057: fail(e.getMessage());
058: }
059: }
060:
061: protected void tearDown() {
062: }
063:
064: protected void createEvents() throws Exception {
065:
066: GregorianCalendar cal = new GregorianCalendar();
067:
068: cal.set(2003, 8, 27, 12, 0, 0);
069: event1_ = new Event();
070: event1_.setDate("eventTime", cal.getTime());
071: event1_.setString("description", "event1");
072: event1_.saveNew();
073:
074: cal.set(2003, 8, 27, 13, 0, 0);
075: event2_ = new Event();
076: event2_.setDate("eventTime", cal.getTime());
077: event2_.setString("description", "event2");
078: event2_.saveNew();
079:
080: cal.set(2003, 8, 27, 14, 0, 0);
081: event3_ = new Event();
082: event3_.setDate("eventTime", cal.getTime());
083: event3_.setString("description", "event3");
084: event3_.saveNew();
085:
086: cal.set(2003, 8, 27, 15, 0, 0);
087: event4_ = new Event();
088: event4_.setDate("eventTime", cal.getTime());
089: event4_.setString("description", "event4");
090: event4_.saveNew();
091:
092: cal.set(2003, 8, 27, 16, 0, 0);
093: event5_ = new Event();
094: event5_.setSiteContext(siteContext1_);
095: event5_.setDate("eventTime", cal.getTime());
096: event5_.setString("description", "event5");
097: event5_.saveNew();
098: }
099:
100: public void testLogStatic() {
101: ResultSet rs = null;
102: try {
103: java.util.Date now = new java.util.Date();
104: Event.log(siteContext1_, "userName", "source", "test");
105:
106: rs = ConnectionSingleton
107: .runQuery("SELECT eventTime, userName, source, description FROM mh_event WHERE sitecontext_id=1 ORDER BY eventTime");
108:
109: assertNotNull(rs);
110: assertTrue(rs.next());
111: assertTrue((now.getTime() - rs.getTimestamp("eventTime")
112: .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();
119: Event.log(siteContext1_, "test");
120:
121: rs = ConnectionSingleton
122: .runQuery("SELECT eventTime, userName, source, description FROM mh_event WHERE sitecontext_id=1 ORDER BY userName DESC");
123:
124: assertNotNull(rs);
125: assertTrue(rs.next());
126: assertTrue(rs.next());
127: assertTrue((now.getTime() - rs.getTimestamp("eventTime")
128: .getTime()) < 1000);
129: assertEquals("", rs.getString("userName"));
130: assertEquals("", rs.getString("source"));
131: assertEquals("test", rs.getString("description"));
132: assertTrue(!rs.next());
133:
134: ConnectionSingleton.close(rs);
135: } catch (Exception e) {
136: if (rs != null)
137: ConnectionSingleton.close(rs);
138:
139: e.printStackTrace();
140: fail();
141: }
142: }
143:
144: public void testLog() {
145: ResultSet rs = null;
146: try {
147: java.util.Date now = new java.util.Date();
148: event = new Event();
149: event.log("userName", "source", "test");
150:
151: rs = ConnectionSingleton
152: .runQuery("SELECT eventTime, userName, source, description FROM mh_event WHERE sitecontext_id=0");
153:
154: assertNotNull(rs);
155: assertTrue(rs.next());
156: assertTrue((now.getTime() - rs.getTimestamp("eventTime")
157: .getTime()) < 1000);
158: assertEquals("userName", rs.getString("userName"));
159: assertEquals("source", rs.getString("source"));
160: assertEquals("test", rs.getString("description"));
161: assertTrue(!rs.next());
162: } catch (Exception e) {
163: if (rs != null)
164: ConnectionSingleton.close(rs);
165:
166: e.printStackTrace();
167: fail();
168: }
169: }
170:
171: public void testLogSingleArg() {
172: ResultSet rs = null;
173: try {
174: java.util.Date now = new java.util.Date();
175: event = new Event();
176: event.log("test");
177:
178: rs = ConnectionSingleton
179: .runQuery("SELECT eventTime, userName, source, description FROM mh_event WHERE sitecontext_id=0");
180:
181: assertNotNull(rs);
182: assertTrue(rs.next());
183: assertTrue((now.getTime() - rs.getTimestamp("eventTime")
184: .getTime()) < 1000);
185: assertEquals("", rs.getString("userName"));
186: assertEquals("", rs.getString("source"));
187: assertEquals("test", rs.getString("description"));
188: assertTrue(!rs.next());
189:
190: ConnectionSingleton.close(rs);
191: } catch (Exception e) {
192: if (rs != null)
193: ConnectionSingleton.close(rs);
194:
195: e.printStackTrace();
196: fail();
197: }
198: }
199:
200: public void testGetEvents() {
201: try {
202: GregorianCalendar from = new GregorianCalendar();
203: GregorianCalendar to = new GregorianCalendar();
204: createEvents();
205: List events = null;
206: Event event = null;
207: event = new Event();
208:
209: //
210: // bounded on both sides
211: //
212: from.set(2003, 8, 27, 12, 30, 0);
213: to.set(2003, 8, 27, 14, 30, 0);
214: events = event.getEvents(from.getTime(), to.getTime());
215:
216: assertNotNull(events);
217: assertEquals(2, events.size());
218:
219: event = (Event) events.get(0);
220:
221: assertEquals(event3_.getString("description"), event
222: .getString("description"));
223:
224: event = (Event) events.get(1);
225:
226: assertEquals(event2_.getString("description"), event
227: .getString("description"));
228:
229: //
230: // unbounded from
231: //
232: events = event.getEvents(null, to.getTime());
233:
234: assertNotNull(events);
235: assertEquals(3, events.size());
236:
237: event = (Event) events.get(0);
238:
239: assertEquals(event3_.getString("description"), event
240: .getString("description"));
241:
242: event = (Event) events.get(1);
243:
244: assertEquals(event2_.getString("description"), event
245: .getString("description"));
246:
247: event = (Event) events.get(2);
248:
249: assertEquals(event1_.getString("description"), event
250: .getString("description"));
251:
252: //
253: // unbounded to
254: //
255: events = event.getEvents(from.getTime(), null);
256:
257: assertNotNull(events);
258: assertEquals(3, events.size());
259:
260: event = (Event) events.get(0);
261:
262: assertEquals(event4_.getString("description"), event
263: .getString("description"));
264:
265: event = (Event) events.get(1);
266:
267: assertEquals(event3_.getString("description"), event
268: .getString("description"));
269:
270: event = (Event) events.get(2);
271:
272: assertEquals(event2_.getString("description"), event
273: .getString("description"));
274:
275: //
276: // unbounded
277: //
278: events = event.getEvents(null, null);
279:
280: assertNotNull(events);
281: assertEquals(4, events.size());
282:
283: event = (Event) events.get(0);
284:
285: assertEquals(event4_.getString("description"), event
286: .getString("description"));
287:
288: event = (Event) events.get(1);
289:
290: assertEquals(event3_.getString("description"), event
291: .getString("description"));
292:
293: event = (Event) events.get(2);
294:
295: assertEquals(event2_.getString("description"), event
296: .getString("description"));
297:
298: event = (Event) events.get(3);
299:
300: assertEquals(event1_.getString("description"), event
301: .getString("description"));
302: } catch (Exception e) {
303: e.printStackTrace();
304: fail();
305: }
306: }
307:
308: public void testDeleteBefore() {
309: try {
310: GregorianCalendar cal = new GregorianCalendar(2003, 8, 27,
311: 13, 30, 0);
312: createEvents();
313: List events = null;
314: Event event = null;
315:
316: event = new Event();
317: event.deleteBefore(cal.getTime());
318: events = event.getEvents(null, null);
319:
320: assertEquals(2, events.size());
321:
322: event = (Event) events.get(0);
323:
324: assertEquals(event4_.getString("description"), event
325: .getString("description"));
326:
327: event = (Event) events.get(1);
328:
329: assertEquals(event3_.getString("description"), event
330: .getString("description"));
331: } catch (Exception e) {
332: e.printStackTrace();
333: fail();
334: }
335: }
336:
337: public void testDeleteAll() {
338: try {
339: createEvents();
340:
341: event = new Event();
342: event.setSiteContext(siteContext1_);
343: event.deleteAll();
344:
345: event = new Event();
346:
347: event.setSiteContext(siteContext1_);
348: list = event.getEvents(null, null);
349: assertEquals(0, list.size());
350:
351: event.setSiteContext(SiteContext.getDefaultContext());
352: list = event.getEvents(null, null);
353: assertEquals(4, list.size());
354: } catch (Exception e) {
355: e.printStackTrace();
356: fail();
357: }
358: }
359: }
|