01: package org.mockejb;
02:
03: import java.util.*;
04:
05: import org.apache.commons.logging.*;
06:
07: /**
08: * Provides rudimentary in-memory entity database implementation.
09: * Entities can be searched only by the primary key.
10: * Users should populate EntityDatabase with the mock entities for their test.
11: * MockEJB searches EntityDatabase:
12: * 1) During the call to CMP findByPrimaryKey.
13: * 2) After a BMP finder returns a PK or collection of PKs.
14: * MockEJB automatically add an entity to this "database" if
15: * its ejbCreate method returns a PK.
16: *
17: *
18: * @author Alexander Ananiev
19: */
20: class EntityDatabaseImpl implements EntityDatabase {
21:
22: // logger for this class
23: private static Log logger = LogFactory.getLog(EntityDatabase.class
24: .getName());
25:
26: EntityDatabaseImpl() {
27:
28: }
29:
30: private Map entityTypes = Collections
31: .synchronizedMap(new HashMap());
32:
33: public void add(Class homeIfaceClass, Object pk, Object entity) {
34:
35: logger.debug("Adding entity for home "
36: + homeIfaceClass.getName() + " with PK " + pk
37: + " to entity storage");
38:
39: Map entities = (Map) entityTypes.get(homeIfaceClass.getName());
40: if (entities == null) {
41: entities = Collections.synchronizedMap(new HashMap());
42: entityTypes.put(homeIfaceClass.getName(), entities);
43: }
44: entities.put(pk, entity);
45: }
46:
47: public Object find(Class homeIfaceClass, Object pk) {
48: Object entity = null;
49:
50: Map entities = (Map) entityTypes.get(homeIfaceClass.getName());
51: if (entities != null) {
52: entity = entities.get(pk);
53: }
54:
55: return entity;
56: }
57:
58: public void clear() {
59: entityTypes.clear();
60: }
61: }
|