Source Code Cross Referenced for TestSequence.java in  » Database-ORM » Speedo_1.4.5 » org » objectweb » speedo » runtime » sequence » basic » 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 » Database ORM » Speedo_1.4.5 » org.objectweb.speedo.runtime.sequence.basic 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Speedo: an implementation of JDO compliant personality on top of JORM generic
003:         * I/O sub-system.
004:         * Copyright (C) 2001-2004 France Telecom R&D
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2 of the License, or (at your option) any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019:         *
020:         *
021:         *
022:         * Contact: speedo@objectweb.org
023:         *
024:         */package org.objectweb.speedo.runtime.sequence.basic;
025:
026:        import java.util.ArrayList;
027:        import java.util.Collection;
028:        import java.util.Iterator;
029:
030:        import javax.jdo.JDODataStoreException;
031:        import javax.jdo.JDOException;
032:        import javax.jdo.PersistenceManager;
033:        import javax.jdo.Query;
034:        import javax.jdo.datastore.Sequence;
035:
036:        import junit.framework.Assert;
037:
038:        import org.objectweb.speedo.SpeedoTestHelper;
039:        import org.objectweb.speedo.api.ExceptionHelper;
040:        import org.objectweb.speedo.pobjects.sequence.basic.Cow;
041:        import org.objectweb.util.monolog.api.BasicLevel;
042:
043:        /**
044:         * 
045:         * @author Y.Bersihand
046:         */
047:        public class TestSequence extends SpeedoTestHelper {
048:
049:            public TestSequence(String s) {
050:                super (s);
051:            }
052:
053:            protected String getLoggerName() {
054:                return LOG_NAME + ".rt.sequence.TestSequence";
055:            }
056:
057:            public static final String COW_SEQ = "org.objectweb.speedo.pobjects.sequence.basic.cowseq";
058:
059:            public static final String COW_SEQ_TRANSACTIONAL = "org.objectweb.speedo.pobjects.sequence.basic.cowseq_transactional";
060:
061:            public static final String COW_SEQ_DATASTORE = "org.objectweb.speedo.pobjects.sequence.basic.cowseq_datastore";
062:
063:            public static final int ADDITIONAL = 5;
064:
065:            public static final int COMPARE_NUMBER = 100;
066:
067:            /**
068:             * Get a sequence and use it to make objects persistent.
069:             */
070:            public void testSequence() {
071:                logger.log(BasicLevel.DEBUG,
072:                        "***************testSequence*****************");
073:                PersistenceManager pm = pmf.getPersistenceManager();
074:                pm.getObjectIdClass(Cow.class);
075:                //get the sequence
076:                Sequence s = pm.getSequence(COW_SEQ);
077:                assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
078:                Cow c1 = new Cow("cowSeq1", ((Long) s.next()).longValue());
079:                Cow c2 = new Cow("cowSeq2", ((Long) s.next()).longValue());
080:                Cow c3 = new Cow("cowSeq3", ((Long) s.next()).longValue());
081:                assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
082:                assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
083:                Collection c = new ArrayList();
084:                c.add(c1);
085:                c.add(c2);
086:                c.add(c3);
087:                //make persistent
088:                pm.currentTransaction().begin();
089:                pm.makePersistentAll(c);
090:                pm.currentTransaction().commit();
091:                logger.log(BasicLevel.DEBUG, "c1: " + c1.getNbOfLegs());
092:                logger.log(BasicLevel.DEBUG, "c2: " + c2.getNbOfLegs());
093:                logger.log(BasicLevel.DEBUG, "c3: " + c3.getNbOfLegs());
094:                pm.close();
095:            }
096:
097:            /**
098:             * Test the allocate method.
099:             */
100:            public void testAllocate() {
101:                logger.log(BasicLevel.DEBUG,
102:                        "***************testAllocate*****************");
103:                PersistenceManager pm = pmf.getPersistenceManager();
104:                pm.getObjectIdClass(Cow.class);
105:                //get the sequence
106:                Sequence s = pm.getSequence(COW_SEQ);
107:                assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
108:                long beforeAllocate = ((Long) s.current()).longValue();
109:                Collection col = new ArrayList();
110:                Cow cow = null;
111:                for (int i = 1; i <= ADDITIONAL; i++) {
112:                    cow = new Cow("cowAllocate" + i, ((Long) s.next())
113:                            .longValue());
114:                    col.add(cow);
115:                }
116:                assertEquals(beforeAllocate + ADDITIONAL, ((Long) s.current())
117:                        .longValue());
118:                cow = new Cow("cowAfterAllocate", ((Long) s.next()).longValue());
119:                col.add(cow);
120:                pm.currentTransaction().begin();
121:                //make persistent
122:                pm.makePersistentAll(col);
123:                s.current();
124:                pm.currentTransaction().commit();
125:                Iterator it = col.iterator();
126:                while (it.hasNext()) {
127:                    Cow c = (Cow) it.next();
128:                    logger.log(BasicLevel.DEBUG, "cow " + c.getName() + ": "
129:                            + c.getNbOfLegs());
130:                }
131:                pm.close();
132:            }
133:
134:            /**
135:             * Compare the allocate method to the next mathod in terms of time elapsed.
136:             * The allocate should be faster than next.
137:             *
138:             */
139:            public void testCompareNextToAllocate() {
140:                logger
141:                        .log(BasicLevel.DEBUG,
142:                                "***************testCompareNextToAllocate*****************");
143:                PersistenceManager pm = pmf.getPersistenceManager();
144:                pm.getObjectIdClass(Cow.class);
145:                //get the sequence
146:                Sequence s = pm.getSequence(COW_SEQ);
147:                assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
148:                long timeToGetManyNext = System.currentTimeMillis();
149:                for (int i = 0; i < COMPARE_NUMBER; i++) {
150:                    s.next();
151:                }
152:                timeToGetManyNext = System.currentTimeMillis()
153:                        - timeToGetManyNext;
154:                long timeToGetManyAllocate = System.currentTimeMillis();
155:                s.allocate(COMPARE_NUMBER);
156:                for (int i = 0; i < COMPARE_NUMBER; i++) {
157:                    s.next();
158:                }
159:                timeToGetManyAllocate = System.currentTimeMillis()
160:                        - timeToGetManyAllocate;
161:                logger.log(BasicLevel.DEBUG, COMPARE_NUMBER
162:                        + " ids generated:\nwith allocate: "
163:                        + timeToGetManyAllocate + "\nwith next: "
164:                        + timeToGetManyNext);
165:                assertTrue(
166:                        "The time to get many ids with an allocate should be inferior to the time to get many ids with next()",
167:                        timeToGetManyAllocate < timeToGetManyNext);
168:            }
169:
170:            /**
171:             * Get a transactional sequence and use it outside a transaction.
172:             */
173:            public void testSequenceTransactional1() {
174:                logger
175:                        .log(BasicLevel.DEBUG,
176:                                "***************testSequenceTransactional1*****************");
177:                PersistenceManager pm = pmf.getPersistenceManager();
178:                pm.getObjectIdClass(Cow.class);
179:                //get the sequence
180:                Sequence s = pm.getSequence(COW_SEQ_TRANSACTIONAL);
181:                assertNotNull("Sequence " + COW_SEQ_TRANSACTIONAL
182:                        + " should not be null.", s);
183:                try {
184:                    //use the sequence outside a tx
185:                    Cow c1 = new Cow("cowSeqNC1", ((Long) s.next()).longValue());
186:                    fail("An exception should be caught: a transactional sequence is used outside a transaction.");
187:                } catch (Exception e) {
188:                    if (!JDODataStoreException.class.equals(e.getClass())) {
189:                        logger.log(BasicLevel.ERROR, "Found exception: ", e);
190:                    }
191:                    assertEquals(
192:                            "Should be a JDODatastoreException due to the fact the  transactional"
193:                                    + "sequence has been used outside of a transaction. "
194:                                    + e.getMessage(),
195:                            JDODataStoreException.class, e.getClass());
196:                } finally {
197:                    if (pm.currentTransaction().isActive())
198:                        pm.currentTransaction().rollback();
199:                    pm.close();
200:                }
201:            }
202:
203:            /**
204:             * Get a sequence and use it to make objects persistent.
205:             */
206:            public void testSequenceTransactional2() {
207:                logger
208:                        .log(BasicLevel.DEBUG,
209:                                "***************testSequenceTransactional2*****************");
210:                PersistenceManager pm = pmf.getPersistenceManager();
211:                pm.getObjectIdClass(Cow.class);
212:                //get the sequence
213:                Sequence s = pm.getSequence(COW_SEQ_TRANSACTIONAL);
214:                assertNotNull("Sequence " + COW_SEQ_TRANSACTIONAL
215:                        + " should not be null.", s);
216:                //begin the tx
217:                pm.currentTransaction().begin();
218:                Cow c1 = new Cow("cowSeqNC1", ((Long) s.next()).longValue());
219:                Cow c2 = new Cow("cowSeqNC2", ((Long) s.next()).longValue());
220:                Cow c3 = new Cow("cowSeqNC3", ((Long) s.next()).longValue());
221:                assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
222:                assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
223:                Collection c = new ArrayList();
224:                c.add(c1);
225:                c.add(c2);
226:                c.add(c3);
227:                //make persistent
228:                pm.makePersistentAll(c);
229:                pm.currentTransaction().commit();
230:                //begin a tx
231:                pm.currentTransaction().begin();
232:                Cow c4 = new Cow("cowSeqNC4", ((Long) s.next()).longValue());
233:                //keep the number
234:                long index = c4.getNbOfLegs();
235:                pm.makePersistent(c4);
236:                //rollback
237:                pm.currentTransaction().rollback();
238:                //begin a tx
239:                pm.currentTransaction().begin();
240:                //check the next number generated is not the one used for the rollback : gap
241:                assertTrue(((Long) s.next()).longValue() != index);
242:                pm.currentTransaction().commit();
243:                pm.close();
244:            }
245:
246:            /**
247:             * Get a sequence linked to a datastore and use it to make objects persistent.
248:             */
249:            public void testSequenceDatastore() {
250:                logger
251:                        .log(BasicLevel.DEBUG,
252:                                "***************testSequenceDatastore*****************");
253:                PersistenceManager pm = pmf.getPersistenceManager();
254:                pm.getObjectIdClass(Cow.class);
255:                //get the sequence
256:                Sequence s = pm.getSequence(COW_SEQ_DATASTORE);
257:                assertNotNull("Sequence " + COW_SEQ_DATASTORE
258:                        + " should not be null.", s);
259:                //begin the tx
260:                pm.currentTransaction().begin();
261:                Cow c1 = new Cow("cowSeqDS1", ((Long) s.next()).longValue());
262:                Cow c2 = new Cow("cowSeqDS2", ((Long) s.next()).longValue());
263:                Cow c3 = new Cow("cowSeqDS3", ((Long) s.next()).longValue());
264:                assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
265:                assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
266:                Collection c = new ArrayList();
267:                c.add(c1);
268:                c.add(c2);
269:                c.add(c3);
270:                //make persistent
271:                pm.makePersistentAll(c);
272:                pm.currentTransaction().commit();
273:                pm.close();
274:            }
275:
276:            /**
277:             * Remove all the persistence instances.
278:             */
279:            public void testRemovingOfPersistentObject() {
280:                PersistenceManager pm = pmf.getPersistenceManager();
281:                try {
282:                    Class[] cs = new Class[] { Cow.class };
283:                    pm.currentTransaction().begin();
284:                    for (int i = 0; i < cs.length; i++) {
285:                        Query query = pm.newQuery(cs[i]);
286:                        Collection col = (Collection) query.execute();
287:                        Iterator it = col.iterator();
288:                        while (it.hasNext()) {
289:                            Object o = it.next();
290:                            Assert.assertNotNull(
291:                                    "null object in the query result"
292:                                            + cs[i].getName(), o);
293:                            pm.deletePersistent(o);
294:
295:                        }
296:                        query.close(col);
297:                    }
298:                    pm.currentTransaction().commit();
299:                } catch (JDOException e) {
300:                    Exception ie = ExceptionHelper.getNested(e);
301:                    logger.log(BasicLevel.ERROR, "", ie);
302:                    fail(ie.getMessage());
303:                } finally {
304:                    pm.close();
305:                }
306:            }
307:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.