Source Code Cross Referenced for TestSequenceId.java in  » Database-ORM » Speedo_1.4.5 » org » objectweb » speedo » runtime » sequence » id » 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.id 
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.id;
025:
026:        import java.util.Collection;
027:        import java.util.Iterator;
028:
029:        import javax.jdo.JDOException;
030:        import javax.jdo.PersistenceManager;
031:        import javax.jdo.Query;
032:        import javax.jdo.datastore.Sequence;
033:
034:        import junit.framework.Assert;
035:
036:        import org.objectweb.speedo.SpeedoTestHelper;
037:        import org.objectweb.speedo.api.ExceptionHelper;
038:        import org.objectweb.speedo.pobjects.sequence.id.CompactDisc;
039:        import org.objectweb.speedo.pobjects.sequence.id.Phone;
040:        import org.objectweb.speedo.pobjects.sequence.id.Product;
041:        import org.objectweb.speedo.pobjects.sequence.id.Suitcase;
042:        import org.objectweb.util.monolog.api.BasicLevel;
043:
044:        /**
045:         * @author Y.Bersihand
046:         */
047:        public class TestSequenceId extends SpeedoTestHelper {
048:
049:            public static final int ADDITIONAL = 100;
050:
051:            public TestSequenceId(String s) {
052:                super (s);
053:            }
054:
055:            protected String getLoggerName() {
056:                return LOG_NAME + ".rt.sequence.TestSequence";
057:            }
058:
059:            public static final String PRODUCT_SEQ = "org.objectweb.speedo.pobjects.sequence.id.product_seq";
060:            public static final String PHONE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.phone_seq";
061:            public static final String SUITCASE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.suitcase_seq";
062:            public static final String CD_SEQ = "org.objectweb.speedo.pobjects.sequence.id.cd_seq";
063:
064:            /**
065:             * Get a sequence and use it to make objects persistent.
066:             * A field has a value-strategy set to sequence.
067:             */
068:            public void testSequenceId1() {
069:                logger.log(BasicLevel.DEBUG,
070:                        "***************testSequenceId1*****************");
071:                PersistenceManager pm = pmf.getPersistenceManager();
072:                pm.getObjectIdClass(Product.class);
073:                //get the sequence
074:                Sequence s = pm.getSequence(PRODUCT_SEQ);
075:                assertNotNull("Sequence " + PRODUCT_SEQ
076:                        + " should not be null.", s);
077:                Product p1 = new Product();
078:                p1.setName("product 1");
079:                Product p2 = new Product();
080:                p2.setName("product 2");
081:                //make persistent
082:                pm.currentTransaction().begin();
083:                pm.makePersistent(p1);
084:                pm.makePersistent(p2);
085:                pm.currentTransaction().commit();
086:                assertTrue(p1.getReference() < p2.getReference());
087:                pm.close();
088:            }
089:
090:            /**
091:             * Get a sequence and use it to make objects persistent.
092:             * A datastore-identity has a strategy set to sequence.
093:             */
094:            public void testSequenceId2() {
095:                logger.log(BasicLevel.DEBUG,
096:                        "***************testSequenceId2*****************");
097:                PersistenceManager pm = pmf.getPersistenceManager();
098:                pm.getObjectIdClass(Phone.class);
099:                //get the sequence
100:                Sequence s = pm.getSequence(PHONE_SEQ);
101:                assertNotNull("Sequence " + PHONE_SEQ + " should not be null.",
102:                        s);
103:                Phone p1 = new Phone();
104:                p1.setName("phone 1");
105:                Phone p2 = new Phone();
106:                p2.setName("phone 2");
107:                //make persistent
108:                pm.currentTransaction().begin();
109:                pm.makePersistent(p1);
110:                pm.makePersistent(p2);
111:                pm.currentTransaction().commit();
112:                pm.close();
113:            }
114:
115:            public void testAllocateRdbSequence() {
116:                logger
117:                        .log(BasicLevel.DEBUG,
118:                                "***************testAllocateRdbSequence*****************");
119:                PersistenceManager pm = pmf.getPersistenceManager();
120:                try {
121:                    pm.getObjectIdClass(Phone.class);
122:                    //get the sequence
123:                    Sequence s = pm.getSequence(PHONE_SEQ);
124:                    assertNotNull("Sequence " + PHONE_SEQ
125:                            + " should not be null.", s);
126:                    Phone[] phones = new Phone[ADDITIONAL];
127:                    long timeAllocate = System.currentTimeMillis();
128:                    pm.currentTransaction().begin();
129:                    //allocate
130:                    s.allocate(ADDITIONAL);
131:                    for (int i = 0; i < ADDITIONAL; i++) {
132:                        phones[i] = new Phone();
133:                        phones[i].setName("phone " + i);
134:                    }
135:                    //make persistent
136:                    pm.makePersistentAll(phones);
137:                    pm.currentTransaction().commit();
138:                    timeAllocate = System.currentTimeMillis() - timeAllocate;
139:                    long timeNext = System.currentTimeMillis();
140:                    pm.currentTransaction().begin();
141:                    for (int i = ADDITIONAL; i < ADDITIONAL * 2; i++) {
142:                        phones[i - ADDITIONAL] = new Phone();
143:                        phones[i - ADDITIONAL].setName("phone " + i);
144:                    }
145:                    //make persistent
146:                    pm.makePersistentAll(phones);
147:                    pm.currentTransaction().commit();
148:                    timeNext = System.currentTimeMillis() - timeNext;
149:                    assertTrue(
150:                            "Time with allocate should be <= to time with next. \nAllocate: "
151:                                    + timeAllocate + "\nNext: " + timeNext,
152:                            timeAllocate <= timeNext);
153:                } catch (Exception e) {
154:                    fail(e.getMessage());
155:                } finally {
156:                    if (pm.currentTransaction().isActive())
157:                        pm.currentTransaction().rollback();
158:                    pm.close();
159:                }
160:            }
161:
162:            /**
163:             * Test a sequence which is not rdb.
164:             * The id of the class is application.
165:             *
166:             */
167:            public void testApplicationId1() {
168:                logger.log(BasicLevel.DEBUG,
169:                        "***************testApplicationId1*****************");
170:                PersistenceManager pm = pmf.getPersistenceManager();
171:                pm.getObjectIdClass(Suitcase.class);
172:                //get the sequence
173:                Sequence s = pm.getSequence(SUITCASE_SEQ);
174:                assertNotNull("Sequence " + SUITCASE_SEQ
175:                        + " should not be null.", s);
176:                Suitcase s1 = new Suitcase();
177:                s1.setColor("black");
178:                Suitcase s2 = new Suitcase();
179:                s2.setColor("grey");
180:                logger.log(BasicLevel.DEBUG, "Value is " + s.currentValue());
181:                //make persistent
182:                pm.currentTransaction().begin();
183:                pm.makePersistent(s1);
184:                pm.makePersistent(s2);
185:                pm.currentTransaction().commit();
186:                assertTrue(s1.getId() < s2.getId());
187:                logger.log(BasicLevel.DEBUG, "After commit, value is "
188:                        + s.currentValue());
189:                pm.close();
190:            }
191:
192:            /**
193:             * Get a non-rdb sequence and use it to make objects persistent.
194:             * A datastore-identity has a strategy set to sequence.
195:             */
196:            public void testApplicationId2() {
197:                logger.log(BasicLevel.DEBUG,
198:                        "***************testApplicationId2*****************");
199:                PersistenceManager pm = pmf.getPersistenceManager();
200:                pm.getObjectIdClass(CompactDisc.class);
201:                //get the sequence
202:                Sequence s = pm.getSequence(CD_SEQ);
203:                assertNotNull("Sequence " + CD_SEQ + " should not be null.", s);
204:                CompactDisc cd1 = new CompactDisc();
205:                cd1.setTitle("cd1");
206:                CompactDisc cd2 = new CompactDisc();
207:                cd1.setTitle("cd2");
208:                //make persistent
209:                pm.currentTransaction().begin();
210:                pm.makePersistent(cd1);
211:                pm.makePersistent(cd2);
212:                pm.currentTransaction().commit();
213:                pm.close();
214:            }
215:
216:            /**
217:             * Remove all the persistent instances.
218:             */
219:            public void testRemovingOfPersistentObject() {
220:                PersistenceManager pm = pmf.getPersistenceManager();
221:                try {
222:                    Class[] cs = new Class[] { Product.class, Phone.class,
223:                            Suitcase.class, CompactDisc.class };
224:                    pm.currentTransaction().begin();
225:                    for (int i = 0; i < cs.length; i++) {
226:                        Query query = pm.newQuery(cs[i]);
227:                        Collection col = (Collection) query.execute();
228:                        Iterator it = col.iterator();
229:                        while (it.hasNext()) {
230:                            Object o = it.next();
231:                            Assert.assertNotNull(
232:                                    "null object in the query result"
233:                                            + cs[i].getName(), o);
234:                            pm.deletePersistent(o);
235:
236:                        }
237:                        query.close(col);
238:                    }
239:                    pm.currentTransaction().commit();
240:                } catch (JDOException e) {
241:                    Exception ie = ExceptionHelper.getNested(e);
242:                    logger.log(BasicLevel.ERROR, "", ie);
243:                    fail(ie.getMessage());
244:                } finally {
245:                    pm.close();
246:                }
247:            }
248:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.