001: /**
002: * Copyright (C) 2007 NetMind Consulting Bt.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 3 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package hu.netmind.persistence;
018:
019: import java.util.*;
020: import hu.netmind.persistence.event.*;
021:
022: /**
023: * Tests of all events and of the event dispatcher.
024: * @author Brautigam Robert
025: * @version Revision: $Revision$
026: */
027: public class EventTests extends AbstractPersistenceTest {
028: public EventTests(String name) throws Exception {
029: super (name);
030: }
031:
032: public void testCreateObject() throws Exception {
033: dropTables("book");
034: // Create book
035: Book book = new Book();
036: book.setTitle("No worries left.");
037: book.setIsbn("1-2-3-4");
038: // Insert listener and save in store
039: MemorizerListener listener = new MemorizerListener();
040: store.getEventDispatcher().registerListener(listener);
041: store.save(book);
042: // Check
043: assertEquals(1, getCount(listener.getEvents(),
044: CreateObjectEvent.class));
045: CreateObjectEvent event = (CreateObjectEvent) listener
046: .getEvents().get(0);
047: assertEquals(book, event.getObject());
048: }
049:
050: public void testModifyObject() throws Exception {
051: dropTables("book");
052: // Create book
053: Book book = new Book();
054: book.setTitle("No worries left.");
055: book.setIsbn("1-2-3-4");
056: store.save(book);
057: book.setTitle("Altered title.");
058: // Insert listener and save in store
059: MemorizerListener listener = new MemorizerListener();
060: store.getEventDispatcher().registerListener(listener);
061: store.save(book);
062: // Check
063: assertEquals(1, getCount(listener.getEvents(),
064: ModifyObjectEvent.class));
065: ModifyObjectEvent event = (ModifyObjectEvent) listener
066: .getEvents().get(0);
067: assertEquals(book, event.getObject());
068: }
069:
070: public void testDeleteObject() throws Exception {
071: dropTables("book");
072: // Create book
073: Book book = new Book();
074: book.setTitle("No worries left.");
075: book.setIsbn("1-2-3-4");
076: store.save(book);
077: // Insert listener and save in store
078: MemorizerListener listener = new MemorizerListener();
079: store.getEventDispatcher().registerListener(listener);
080: store.remove(book);
081: // Check
082: assertEquals(1, getCount(listener.getEvents(),
083: DeleteObjectEvent.class));
084: DeleteObjectEvent event = (DeleteObjectEvent) listener
085: .getEvents().get(0);
086: assertEquals(book, event.getObject());
087: }
088:
089: public void testMapClearedEvent() throws Exception {
090: dropTables("mapholder");
091: // Create mapholder
092: MapHolder holder = new MapHolder();
093: holder.setMeta(new HashMap());
094: holder.getMeta().put("Ni", new Referrer(1));
095: holder.getMeta().put("Ni2", new Referrer(2));
096: store.save(holder);
097: // Select
098: holder = (MapHolder) store.findSingle("find mapholder");
099: // Register
100: MemorizerListener listener = new MemorizerListener();
101: store.getEventDispatcher().registerListener(listener);
102: // Do
103: holder.getMeta().clear();
104: store.save(holder);
105: // Check
106: assertEquals(1, getCount(listener.getEvents(),
107: ClearedContainerEvent.class));
108: ClearedContainerEvent event = (ClearedContainerEvent) listener
109: .getEvents().get(0);
110: assertEquals(holder, event.getObject());
111: assertEquals("meta", event.getAttributeName());
112: }
113:
114: public void testListClearedEvent() throws Exception {
115: dropTables("listholder");
116: // Create listholder
117: ListHolder holder = new ListHolder();
118: holder.setList(new Vector());
119: holder.getList().add(new Referrer(1));
120: holder.getList().add(new Referrer(2));
121: store.save(holder);
122: // Select
123: holder = (ListHolder) store.findSingle("find listholder");
124: // Register
125: MemorizerListener listener = new MemorizerListener();
126: store.getEventDispatcher().registerListener(listener);
127: // Do
128: holder.getList().clear();
129: store.save(holder);
130: // Check
131: assertEquals(1, getCount(listener.getEvents(),
132: ClearedContainerEvent.class));
133: ClearedContainerEvent event = (ClearedContainerEvent) listener
134: .getEvents().get(0);
135: assertEquals(holder, event.getObject());
136: assertEquals("list", event.getAttributeName());
137: }
138:
139: public void testSetClearedEvent() throws Exception {
140: dropTables("setholder");
141: // Create setholder
142: SetHolder holder = new SetHolder();
143: holder.setSet(new HashSet());
144: holder.getSet().add(new Referrer(1));
145: holder.getSet().add(new Referrer(2));
146: store.save(holder);
147: // Select
148: holder = (SetHolder) store.findSingle("find setholder");
149: // Register
150: MemorizerListener listener = new MemorizerListener();
151: store.getEventDispatcher().registerListener(listener);
152: // Do
153: holder.getSet().clear();
154: store.save(holder);
155: // Check
156: assertEquals(1, getCount(listener.getEvents(),
157: ClearedContainerEvent.class));
158: ClearedContainerEvent event = (ClearedContainerEvent) listener
159: .getEvents().get(0);
160: assertEquals(holder, event.getObject());
161: assertEquals("set", event.getAttributeName());
162: }
163:
164: public void testMapAddItemEvent() throws Exception {
165: dropTables("mapholder");
166: // Create mapholder
167: MapHolder holder = new MapHolder();
168: holder.setMeta(new HashMap());
169: holder.getMeta().put("Ni", new Referrer(1));
170: holder.getMeta().put("Ni2", new Referrer(2));
171: store.save(holder);
172: // Select
173: holder = (MapHolder) store.findSingle("find mapholder");
174: // Register
175: MemorizerListener listener = new MemorizerListener();
176: store.getEventDispatcher().registerListener(listener);
177: // Do
178: Referrer ref = new Referrer(3);
179: holder.getMeta().put("Ni3", ref);
180: store.save(holder);
181: // Check
182: assertEquals(1, getCount(listener.getEvents(),
183: AddedItemEvent.class));
184: AddedItemEvent event = (AddedItemEvent) listener.getEvents()
185: .get(0);
186: assertEquals(holder, event.getObject());
187: assertEquals("meta", event.getAttributeName());
188: assertEquals("Ni3", ((Map.Entry) event.getItem()).getKey());
189: assertEquals(ref, ((Map.Entry) event.getItem()).getValue());
190: }
191:
192: public void testListAddItemEvent() throws Exception {
193: dropTables("listholder");
194: // Create listholder
195: ListHolder holder = new ListHolder();
196: holder.setList(new Vector());
197: holder.getList().add(new Referrer(1));
198: holder.getList().add(new Referrer(2));
199: store.save(holder);
200: // Select
201: holder = (ListHolder) store.findSingle("find listholder");
202: // Register
203: MemorizerListener listener = new MemorizerListener();
204: store.getEventDispatcher().registerListener(listener);
205: // Do
206: Referrer ref = new Referrer(3);
207: holder.getList().add(ref);
208: store.save(holder);
209: // Check
210: assertEquals(1, getCount(listener.getEvents(),
211: AddedItemEvent.class));
212: AddedItemEvent event = (AddedItemEvent) listener.getEvents()
213: .get(0);
214: assertEquals(holder, event.getObject());
215: assertEquals("list", event.getAttributeName());
216: assertEquals(ref, event.getItem());
217: }
218:
219: public void testSetAddedItemEvent() throws Exception {
220: dropTables("setholder");
221: // Create setholder
222: SetHolder holder = new SetHolder();
223: holder.setSet(new HashSet());
224: holder.getSet().add(new Referrer(1));
225: holder.getSet().add(new Referrer(2));
226: store.save(holder);
227: // Select
228: holder = (SetHolder) store.findSingle("find setholder");
229: // Register
230: MemorizerListener listener = new MemorizerListener();
231: store.getEventDispatcher().registerListener(listener);
232: // Do
233: Referrer ref = new Referrer(3);
234: holder.getSet().add(ref);
235: store.save(holder);
236: // Check
237: assertEquals(1, getCount(listener.getEvents(),
238: AddedItemEvent.class));
239: AddedItemEvent event = (AddedItemEvent) listener.getEvents()
240: .get(0);
241: assertEquals(holder, event.getObject());
242: assertEquals("set", event.getAttributeName());
243: assertEquals(ref, event.getItem());
244: }
245:
246: public void testMapRemoveItemEvent() throws Exception {
247: dropTables("mapholder");
248: // Create mapholder
249: MapHolder holder = new MapHolder();
250: holder.setMeta(new HashMap());
251: holder.getMeta().put("Ni", new Referrer(1));
252: holder.getMeta().put("Ni2", new Referrer(2));
253: Referrer ref = new Referrer(3);
254: holder.getMeta().put("Ni3", ref);
255: store.save(holder);
256: // Select
257: holder = (MapHolder) store.findSingle("find mapholder");
258: // Register
259: MemorizerListener listener = new MemorizerListener();
260: store.getEventDispatcher().registerListener(listener);
261: // Do
262: holder.getMeta().remove("Ni3");
263: store.save(holder);
264: // Check
265: assertEquals(1, getCount(listener.getEvents(),
266: RemovedItemEvent.class));
267: RemovedItemEvent event = (RemovedItemEvent) listener
268: .getEvents().get(0);
269: assertEquals(holder, event.getObject());
270: assertEquals("meta", event.getAttributeName());
271: assertEquals("Ni3", ((Map.Entry) event.getItem()).getKey());
272: assertEquals(ref, ((Map.Entry) event.getItem()).getValue());
273: }
274:
275: public void testListRemoveItemEvent() throws Exception {
276: dropTables("listholder");
277: // Create listholder
278: ListHolder holder = new ListHolder();
279: holder.setList(new Vector());
280: holder.getList().add(new Referrer(1));
281: holder.getList().add(new Referrer(2));
282: Referrer ref = new Referrer(3);
283: holder.getList().add(ref);
284: store.save(holder);
285: // Select
286: holder = (ListHolder) store.findSingle("find listholder");
287: // Register
288: MemorizerListener listener = new MemorizerListener();
289: store.getEventDispatcher().registerListener(listener);
290: // Do
291: holder.getList().remove(ref);
292: store.save(holder);
293: // Check
294: assertEquals(1, getCount(listener.getEvents(),
295: RemovedItemEvent.class));
296: RemovedItemEvent event = (RemovedItemEvent) listener
297: .getEvents().get(0);
298: assertEquals(holder, event.getObject());
299: assertEquals("list", event.getAttributeName());
300: assertEquals(ref, event.getItem());
301: }
302:
303: public void testSetRemovedItemEvent() throws Exception {
304: dropTables("setholder");
305: // Create setholder
306: SetHolder holder = new SetHolder();
307: holder.setSet(new HashSet());
308: holder.getSet().add(new Referrer(1));
309: holder.getSet().add(new Referrer(2));
310: Referrer ref = new Referrer(3);
311: holder.getSet().add(ref);
312: store.save(holder);
313: // Select
314: holder = (SetHolder) store.findSingle("find setholder");
315: // Register
316: MemorizerListener listener = new MemorizerListener();
317: store.getEventDispatcher().registerListener(listener);
318: // Do
319: holder.getSet().remove(ref);
320: store.save(holder);
321: // Check
322: assertEquals(1, getCount(listener.getEvents(),
323: RemovedItemEvent.class));
324: RemovedItemEvent event = (RemovedItemEvent) listener
325: .getEvents().get(0);
326: assertEquals(holder, event.getObject());
327: assertEquals("set", event.getAttributeName());
328: assertEquals(ref, event.getItem());
329: }
330:
331: public class MemorizerListener implements PersistenceEventListener {
332: private List events = new Vector();
333:
334: public List getEvents() {
335: return events;
336: }
337:
338: public void handle(PersistenceEvent event) {
339: events.add(event);
340: }
341: }
342: }
|