Source Code Cross Referenced for StorageTestThreaded.java in  » Database-DBMS » perst » org » garret » perst » 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 DBMS » perst » org.garret.perst 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $URL: StorageTestThreaded.java $ 
003:         * $Rev: 3582 $ 
004:         * $Date: 2007-11-25 14:29:06 +0300 (Ð’Ñ?, 25 ноÑ? 2007) $
005:         *
006:         * Copyright 2005 Netup, Inc. All rights reserved.
007:         * URL:    http://www.netup.biz
008:         * e-mail: info@netup.biz
009:         */
010:
011:        package org.garret.perst;
012:
013:        import static org.garret.perst.Storage.INFINITE_PAGE_POOL;
014:        import junit.framework.*;
015:
016:        /**
017:         * These tests verifies an implementation of the <code>Storage</code> interface. <br />
018:         * he implementation is created by the following way : 
019:         * <pre>
020:         *   storage = org.garret.perst.StorageFactory.getInstance().createStorage()
021:         * </pre>
022:         * The tested features:
023:         * <ul>
024:         * <li>Synchronisation in multithreaded applications.</li>
025:         * </ul>
026:         * <p>
027:         * In test are used simple <CODE>Persistent</CODE> class <CODE>Root</CODE>:
028:         * <pre>
029:         *   class Root extends Persistent {
030:         *       int i;
031:         *   }
032:         * </pre>
033:         */
034:        public class StorageTestThreaded extends TestCase {
035:
036:            Storage storage;
037:
038:            public StorageTestThreaded(String testName) {
039:                super (testName);
040:            }
041:
042:            public static junit.framework.Test suite() {
043:                junit.framework.TestSuite suite = new junit.framework.TestSuite(
044:                        StorageTestThreaded.class);
045:
046:                return suite;
047:            }
048:
049:            protected void setUp() throws java.lang.Exception {
050:                storage = StorageFactory.getInstance().createStorage();
051:            }
052:
053:            protected void tearDown() throws java.lang.Exception {
054:                if (storage.isOpened())
055:                    storage.close();
056:            }
057:
058:            /**
059:             * <B>Goal:</B> To prove correct synchronisation in <code>EXCLUSIVE_TRANSACTION</code>.
060:             * <P>
061:             * <B>Conditions:</B>
062:             * <ul>
063:             * <li>Two threads started: <br />
064:             *   Each thread  accessed storage in <code>EXCLUSIVE_TRANSACTION</code>.
065:             * </li>
066:             * </ul>
067:             * <P>
068:             * <B>Result:</B>
069:             * <ul>
070:             * <li>Storage provided correct synchronisation.</li>
071:             * </ul>
072:             */
073:            public void testThreadExclusive() {
074:                storage.open(new NullFile(), INFINITE_PAGE_POOL);
075:                storage.setRoot(new Root());
076:                storage.commit();
077:                new TestThreadExclusive(storage);
078:                new TestThreadExclusive(storage);
079:
080:                while (TestThread.cnt > 0) {
081:                    try {
082:                        Thread.sleep(100);
083:                    } catch (InterruptedException ex) {
084:                    }
085:                }
086:                if (TestThread.failed != "") {
087:                    fail(TestThread.failed);
088:                }
089:            }
090:
091:            /**
092:             * <B>Goal:</B> To prove correct synchronisation in <code>SERIALIZABLE_TRANSACTION</code>.
093:             * <P>
094:             * <B>Conditions:</B>
095:             * <ul>
096:             * <li>Two threads started: <br />
097:             * Each thread  accessed storage in <code>SERIALIZABLE_TRANSACTION</code>.
098:             * </li>
099:             * </ul>
100:             * <P>
101:             * <B>Result:</B>
102:             * <ul>
103:             * <li>Storage provided correct synchronisation.</li>
104:             * </ul>
105:             */
106:            public void testThreadSerializable() {
107:                storage.open(new NullFile(), INFINITE_PAGE_POOL);
108:                storage.setRoot(new Root());
109:                storage.commit();
110:                new TestThreadSerializable(storage);
111:                new TestThreadSerializable(storage);
112:
113:                while (TestThread.cnt > 0) {
114:                    try {
115:                        Thread.sleep(100);
116:                    } catch (InterruptedException ex) {
117:                    }
118:                }
119:                if (TestThread.failed != "") {
120:                    fail(TestThread.failed);
121:                }
122:            }
123:
124:            /**
125:             * Internal class.
126:             */
127:            private static class Root extends PersistentResource {
128:                int i;
129:
130:                public Root(int i) {
131:                    this .i = i;
132:                }
133:
134:                public Root() {
135:                }
136:
137:                public String toString() {
138:                    return "Root{" + i + "}";
139:                }
140:            }
141:
142:            private static class TestThread extends Thread {
143:                static Integer lock = new Integer(5);
144:                static int max_id = 0;
145:                static int cnt = 0;
146:                Storage storage;
147:                static String failed = "";
148:                int myID = ++max_id;
149:
150:                TestThread(Storage storage) {
151:                    cnt++;
152:                    this .storage = storage;
153:                    this .start();
154:                }
155:
156:                protected void print(String s) {
157:                    //System.out.println("Thread "+myID+": "+s+".");
158:                }
159:
160:            }
161:
162:            private static class TestThreadExclusive extends TestThread {
163:                TestThreadExclusive(Storage storage) {
164:                    super (storage);
165:                }
166:
167:                public void run() {
168:                    print("ready");
169:                    storage
170:                            .beginThreadTransaction(Storage.EXCLUSIVE_TRANSACTION);
171:                    print("start");
172:                    Root root = (Root) storage.getRoot();
173:                    if (0 != root.i)
174:                        failed = "Test failed.";
175:                    root.i = 10;
176:                    try {
177:                        sleep(100 + myID * 50);
178:                    } catch (InterruptedException e) {
179:                    }
180:                    if (10 != root.i)
181:                        failed = "Test failed.";
182:                    root.i = 0;
183:                    print("end");
184:                    storage.rollbackThreadTransaction();
185:                    cnt--;
186:                }
187:            }
188:
189:            private static class TestThreadSerializable extends TestThread {
190:                TestThreadSerializable(Storage storage) {
191:                    super (storage);
192:                }
193:
194:                public void run() {
195:                    storage
196:                            .beginThreadTransaction(Storage.SERIALIZABLE_TRANSACTION);
197:                    Root root = (Root) storage.getRoot();
198:                    print("ready");
199:                    root.exclusiveLock();
200:                    print("start");
201:                    if (0 != root.i)
202:                        failed = "Test failed.";
203:                    root.i = 10;
204:                    try {
205:                        sleep(100);
206:                    } catch (InterruptedException e) {
207:                    }
208:                    if (10 != root.i)
209:                        failed = "Test failed.";
210:                    root.i = 0;
211:                    print("end");
212:                    root.unlock();
213:                    storage.rollbackThreadTransaction();
214:                    cnt--;
215:                }
216:            }
217:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.