Source Code Cross Referenced for PoJoFixture.java in  » Testing » Ejb3Unit » com » bm » testsuite » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » Ejb3Unit » com.bm.testsuite 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.bm.testsuite;
002:
003:        import java.util.ArrayList;
004:        import java.util.List;
005:
006:        import javax.persistence.EntityManager;
007:        import javax.persistence.EntityTransaction;
008:        import javax.persistence.Query;
009:        import javax.sql.DataSource;
010:
011:        import com.bm.cfg.Ejb3UnitCfg;
012:        import com.bm.jndi.Ejb3UnitJndiBinder;
013:        import com.bm.testsuite.dataloader.EntityInitialDataSet;
014:        import com.bm.testsuite.dataloader.InitialDataSet;
015:        import com.bm.utils.BasicDataSource;
016:
017:        /**
018:         * Supports entity manager and flat file db injection for pojos.
019:         * 
020:         * @author Daniel Wiese
021:         * @param <T> -
022:         *            the type of the service
023:         * @since 12.11.2005
024:         */
025:        public abstract class PoJoFixture extends BaseTest {
026:
027:            private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
028:                    .getLogger(PoJoFixture.class);
029:
030:            private final Ejb3UnitJndiBinder jndiBinder;
031:
032:            private InitialDataSet[] initalDataSet = null;
033:
034:            private final Ejb3UnitCfg configuration;
035:
036:            private EntityManager entityManager;
037:
038:            /**
039:             * Constructor.
040:             * 
041:             * @param usedEntityBeans -
042:             *            the used entity bens
043:             */
044:            public PoJoFixture(Class[] usedEntityBeans) {
045:                super ();
046:                this .jndiBinder = new Ejb3UnitJndiBinder(usedEntityBeans);
047:                final List<Class<? extends Object>> usedEntityBeansC = new ArrayList<Class<? extends Object>>();
048:
049:                for (Class<? extends Object> akt : usedEntityBeans) {
050:                    usedEntityBeansC.add(akt);
051:                }
052:                Ejb3UnitCfg.addEntytiesToTest(usedEntityBeansC);
053:                this .configuration = Ejb3UnitCfg.getConfiguration();
054:
055:            }
056:
057:            /**
058:             * Constructor.
059:             * 
060:             * @param usedEntityBeans -
061:             *            the used entity beans
062:             * @param initialData -
063:             *            the inital data to create in the db
064:             */
065:            public PoJoFixture(Class[] usedEntityBeans,
066:                    InitialDataSet... initialData) {
067:                this (usedEntityBeans);
068:                this .initalDataSet = initialData;
069:            }
070:
071:            /**
072:             * @author Daniel Wiese
073:             * @since 16.10.2005
074:             * @see junit.framework.TestCase#setUp()
075:             */
076:            @Override
077:            protected void setUp() throws Exception {
078:                super .setUp();
079:                this .jndiBinder.bind();
080:                log.info("Creating entity manager instance for POJO test");
081:                entityManager = this .configuration.getEntityManagerFactory()
082:                        .createEntityManager();
083:
084:                if (this .initalDataSet != null) {
085:                    EntityManager em = this .getEntityManager();
086:
087:                    for (InitialDataSet current : this .initalDataSet) {
088:                        // insert entity manager
089:                        if (current instanceof  EntityInitialDataSet) {
090:                            EntityInitialDataSet curentEntDs = (EntityInitialDataSet) current;
091:                            curentEntDs.setEntityManager(em);
092:                            EntityTransaction tx = em.getTransaction();
093:                            tx.begin();
094:                            current.create();
095:                            tx.commit();
096:                        } else {
097:                            current.create();
098:                        }
099:                    }
100:                }
101:            }
102:
103:            /**
104:             * Find all rows of the given class in the database.
105:             * 
106:             * @param <T>
107:             *            the tyme of the persistent object
108:             * @param clazz
109:             *            the class of of the persistent object
110:             * @return all DB instances
111:             */
112:            @SuppressWarnings("unchecked")
113:            protected <T> List<T> findAll(Class<T> clazz) {
114:                EntityManager manager = this .getEntityManager();
115:                Query query = manager.createQuery("select c from "
116:                        + clazz.getName() + " c");
117:                return (List<T>) query.getResultList();
118:            }
119:
120:            /**
121:             * Deletes all rows of the given class in the database.
122:             * 
123:             * @param <T>
124:             *            the tyme of the persistent object
125:             * @param clazz
126:             *            the class of of the persistent object
127:             * @return all DB instances
128:             */
129:            @SuppressWarnings("unchecked")
130:            protected <T> void deleteAll(Class<T> clazz) {
131:                EntityManager manager = this .getEntityManager();
132:                EntityTransaction tx = manager.getTransaction();
133:                tx.begin();
134:                Query query = manager.createQuery("delete from "
135:                        + clazz.getName());
136:                query.executeUpdate();
137:                tx.commit();
138:            }
139:
140:            /**
141:             * Persists all objects in the database.
142:             * 
143:             * @param <T>
144:             *            the tyme of the persistent object
145:             * @param complexObjectGraph
146:             *            th egraph to persist
147:             * @return the persisted objects
148:             */
149:            protected <T> List<T> persist(List<T> complexObjectGraph) {
150:                EntityManager manager = this .getEntityManager();
151:                EntityTransaction tx = manager.getTransaction();
152:                tx.begin();
153:                final List<T> persited = new ArrayList<T>();
154:                try {
155:                    for (T order : complexObjectGraph) {
156:                        persited.add(manager.merge(order));
157:                    }
158:                } finally {
159:                    tx.commit();
160:                }
161:
162:                return persited;
163:            }
164:
165:            /**
166:             * @author Daniel Wiese
167:             * @since 16.10.2005
168:             * @see junit.framework.TestCase#tearDown()
169:             */
170:            @Override
171:            protected void tearDown() throws Exception {
172:                if (this .initalDataSet != null) {
173:                    for (InitialDataSet current : this .initalDataSet) {
174:                        current.cleanup(entityManager);
175:                    }
176:                }
177:                this .entityManager = null;
178:                super .tearDown();
179:
180:            }
181:
182:            /**
183:             * Liefert die datasource.
184:             * 
185:             * @return die data source.
186:             */
187:            public DataSource getDataSource() {
188:                return new BasicDataSource(this .configuration);
189:            }
190:
191:            /**
192:             * Returns a isntance of a EntityManager.
193:             * 
194:             * @author Daniel Wiese
195:             * @since 12.11.2005
196:             * @return - a instance of an entity manager
197:             */
198:            public EntityManager getEntityManager() {
199:                return this.entityManager;
200:            }
201:
202:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.