Source Code Cross Referenced for SequenceGeneratorTester.java in  » Portal » uPortal_rel-2-6-1-GA » org » jasig » portal » 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 » Portal » uPortal_rel 2 6 1 GA » org.jasig.portal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2002 The JA-SIG Collaborative.  All rights reserved.
002:         *  See license distributed with this file and
003:         *  available online at http://www.uportal.org/license.html
004:         */
005:
006:        package org.jasig.portal;
007:
008:        import junit.framework.*;
009:        import java.sql.*;
010:        import java.util.HashSet;
011:        import java.util.Set;
012:
013:        /**
014:         * Tests the sequence generator.
015:         * @author: Dan Ellentuck
016:         */
017:        public class SequenceGeneratorTester extends TestCase {
018:            private int numTestCounters;
019:            private String[] testCounterNames;
020:            private ReferenceSequenceGenerator generator;
021:
022:            protected class Tester implements  Runnable {
023:                protected ReferenceSequenceGenerator generator = null;
024:                protected int numTests = 0;
025:                protected String[] counterValues = null;
026:
027:                protected Tester(ReferenceSequenceGenerator gen, int tests) {
028:                    super ();
029:                    generator = gen;
030:                    numTests = tests;
031:                    counterValues = new String[numTests];
032:                }
033:
034:                public void run() {
035:                    for (int i = 0; i < numTests; i++) {
036:                        String ctrValue = null;
037:                        for (int j = 0; ctrValue == null && j < 10; j++) {
038:                            try {
039:                                ctrValue = generator
040:                                        .getNext(testCounterNames[0]);
041:                            } catch (Exception e) {
042:                                print("SequenceGeneratorTester$Tester: Caught Exception: "
043:                                        + e.getMessage());
044:                            }
045:                        }
046:                        counterValues[i] = ctrValue;
047:                    }
048:                }
049:            }
050:
051:            /**
052:             * EntityLockTester constructor comment.
053:             */
054:            public SequenceGeneratorTester(String name) {
055:                super (name);
056:            }
057:
058:            /**
059:             * @return org.jasig.portal.concurrency.locking.IEntityLockStore
060:             */
061:            private ReferenceSequenceGenerator getGenerator() {
062:                if (generator == null) {
063:                    generator = new ReferenceSequenceGenerator();
064:                }
065:                return generator;
066:            }
067:
068:            /**
069:             * Starts the application.
070:             * @param args an array of command-line arguments
071:             */
072:            public static void main(java.lang.String[] args) throws Exception {
073:                String[] mainArgs = { "org.jasig.portal.SequenceGeneratorTester" };
074:                print("START TESTING SEQUENCE GENERATOR");
075:                printBlankLine();
076:                junit.swingui.TestRunner.main(mainArgs);
077:                printBlankLine();
078:                print("END TESTING SEQUENCE GENERATOR");
079:
080:            }
081:
082:            /**
083:             * @param msg java.lang.String
084:             */
085:            private static void print(String msg) {
086:                java.sql.Timestamp ts = new java.sql.Timestamp(System
087:                        .currentTimeMillis());
088:                System.out.println(ts + " : " + msg);
089:            }
090:
091:            /**
092:             */
093:            private static void printBlankLine() {
094:                System.out.println("");
095:            }
096:
097:            /**
098:             */
099:            protected void setUp() {
100:                Connection conn = null;
101:                Statement stmt = null;
102:
103:                try {
104:                    String sql;
105:                    int idx;
106:                    numTestCounters = 5;
107:                    testCounterNames = new String[numTestCounters];
108:
109:                    print("Creating test counters.");
110:
111:                    for (idx = 0; idx < numTestCounters; idx++) {
112:                        testCounterNames[idx] = "TEST_COUNTER_" + idx;
113:                    }
114:
115:                    conn = RDBMServices.getConnection();
116:                    stmt = conn.createStatement();
117:
118:                    // Delete any left over counters that could interfere. 
119:                    for (idx = 0; idx < testCounterNames.length; idx++) {
120:                        sql = "DELETE FROM UP_SEQUENCE WHERE SEQUENCE_NAME = "
121:                                + "'" + testCounterNames[idx] + "'";
122:                        stmt.executeUpdate(sql);
123:                    }
124:                    // create some test counters:
125:                    for (idx = 0; idx < numTestCounters; idx++) {
126:                        sql = "INSERT INTO UP_SEQUENCE (SEQUENCE_NAME, SEQUENCE_VALUE) "
127:                                + "VALUES ("
128:                                + "'"
129:                                + testCounterNames[idx]
130:                                + "', 0)";
131:                        stmt.executeUpdate(sql);
132:                    }
133:                } catch (Exception ex) {
134:                    print("SequenceGeneratorTester.setUp(): " + ex.getMessage());
135:                } finally {
136:                    if (stmt != null) {
137:                        try {
138:                            stmt.close();
139:                        } catch (SQLException sqle) {
140:                        }
141:                    }
142:                    if (conn != null) {
143:                        RDBMServices.releaseConnection(conn);
144:                    }
145:                }
146:            }
147:
148:            /**
149:             * @return junit.framework.Test
150:             */
151:            public static junit.framework.Test suite() {
152:                TestSuite suite = new TestSuite();
153:
154:                suite.addTest(new SequenceGeneratorTester(
155:                        "testGetUniqueSequenceNumbers"));
156:                suite.addTest(new SequenceGeneratorTester(
157:                        "testCreateNewCounters"));
158:                suite.addTest(new SequenceGeneratorTester(
159:                        "testSetCounterValues"));
160:                suite.addTest(new SequenceGeneratorTester(
161:                        "testConcurrentAccess"));
162:
163:                //	Add more tests here.
164:                //  NB: Order of tests is not guaranteed.
165:
166:                return suite;
167:            }
168:
169:            /**
170:             */
171:            protected void tearDown() {
172:                Connection conn = null;
173:                Statement stmt = null;
174:                try {
175:                    // delete the test counters:
176:                    print("Deleting test counters.");
177:                    String sql;
178:                    int idx;
179:                    conn = RDBMServices.getConnection();
180:                    stmt = conn.createStatement();
181:                    for (idx = 0; idx < testCounterNames.length; idx++) {
182:                        sql = "DELETE FROM UP_SEQUENCE WHERE SEQUENCE_NAME = "
183:                                + "'" + testCounterNames[idx] + "'";
184:                        stmt.executeUpdate(sql);
185:                    }
186:                } catch (Exception ex) {
187:                    print("SequenceGeneratorTester.tearDown(): "
188:                            + ex.getMessage());
189:                } finally {
190:                    if (stmt != null) {
191:                        try {
192:                            stmt.close();
193:                        } catch (SQLException sqle) {
194:                        }
195:                    }
196:                    if (conn != null) {
197:                        RDBMServices.releaseConnection(conn);
198:                    }
199:                }
200:            }
201:
202:            /**
203:             */
204:            public void testConcurrentAccess() throws Exception {
205:                ReferenceSequenceGenerator gen = new ReferenceSequenceGenerator();
206:
207:                int numTests = 50;
208:                int numThreads = 5;
209:                String msg = null;
210:
211:                print("Setting up testing Threads.");
212:                Tester[] testers = new Tester[numThreads];
213:                for (int i = 0; i < numThreads; i++) {
214:                    testers[i] = new Tester(gen, numTests);
215:                    Thread thread = new Thread(testers[i]);
216:                    thread.start();
217:                }
218:
219:                long millis = numTests * numThreads * 100;
220:                print("Now sleeping for " + (millis / 1000) + " seconds.");
221:                Thread.sleep(millis);
222:
223:                msg = "Checking counter values for uniqueness.";
224:                print(msg);
225:
226:                String testValue;
227:                Set testValues = new HashSet();
228:                boolean testResult = false;
229:
230:                for (int testerIdx = 0; testerIdx < numThreads; testerIdx++) {
231:                    for (int valueIdx = 0; valueIdx < numTests; valueIdx++) {
232:                        testValue = testers[testerIdx].counterValues[valueIdx];
233:                        assertNotNull(msg, testValue);
234:                        assertTrue(msg, !testValues.contains(testValue));
235:                        testValues.add(testValue);
236:                    }
237:                }
238:
239:            }
240:
241:            /**
242:             */
243:            public void testCreateNewCounters() throws Exception {
244:                String msg = null;
245:                int numNewCounters = 5;
246:                int idx;
247:                int counterValue;
248:
249:                String[] counterNames = new String[numNewCounters];
250:                for (idx = 0; idx < numNewCounters; idx++) {
251:                    counterNames[idx] = "NEW_CTR_" + idx;
252:                }
253:
254:                print("Creating new counter(s).");
255:                for (idx = 0; idx < numNewCounters; idx++) {
256:                    getGenerator().createCounter(counterNames[idx]);
257:                }
258:
259:                msg = "Getting sequence value from new counter(s).";
260:                print(msg);
261:                for (idx = 0; idx < numNewCounters; idx++) {
262:                    counterValue = getGenerator().getNextInt(counterNames[idx]);
263:                    assertEquals(msg, 1, counterValue);
264:                }
265:
266:                print("Deleting new counter(s).");
267:                Connection conn = null;
268:                Statement stmt = null;
269:                try {
270:                    String sql;
271:                    conn = RDBMServices.getConnection();
272:                    stmt = conn.createStatement();
273:                    for (idx = 0; idx < counterNames.length; idx++) {
274:                        sql = "DELETE FROM UP_SEQUENCE WHERE SEQUENCE_NAME = "
275:                                + "'" + counterNames[idx] + "'";
276:                        stmt.executeUpdate(sql);
277:                    }
278:                } catch (Exception ex) {
279:                    print("SequenceGeneratorTester.testCreateNewCounters(): "
280:                            + ex.getMessage());
281:                } finally {
282:                    if (stmt != null) {
283:                        try {
284:                            stmt.close();
285:                        } catch (SQLException sqle) {
286:                        }
287:                    }
288:                    if (conn != null) {
289:                        RDBMServices.releaseConnection(conn);
290:                    }
291:                }
292:
293:            }
294:
295:            /**
296:             */
297:            public void testGetUniqueSequenceNumbers() throws Exception {
298:                String msg = null;
299:                int numTestValues = 10;
300:                int idx;
301:                String[][] testValues = new String[numTestCounters][numTestValues];
302:
303:                print("Getting sequence values.");
304:                for (idx = 0; idx < numTestCounters; idx++) {
305:                    for (int i = 0; i < numTestValues; i++) {
306:                        testValues[idx][i] = getGenerator().getNext(
307:                                testCounterNames[idx]);
308:                    }
309:
310:                }
311:
312:                msg = "Testing sequence values for uniqueness.";
313:                print(msg);
314:                boolean assertionValue;
315:                for (idx = 0; idx < numTestCounters; idx++) {
316:                    for (int i = 1; i < numTestValues; i++) {
317:                        assertionValue = testValues[idx][i - 1]
318:                                .equals(testValues[idx][i]);
319:                        assertTrue(msg, !assertionValue);
320:                    }
321:
322:                }
323:            }
324:
325:            /**
326:             */
327:            public void testSetCounterValues() throws Exception {
328:                int idx, testValue, nextCounterValue;
329:
330:                print("Setting sequence values.");
331:                for (idx = 0; idx < numTestCounters; idx++) {
332:                    testValue = idx * 999;
333:                    getGenerator().setCounter(testCounterNames[idx], testValue);
334:                    nextCounterValue = getGenerator().getNextInt(
335:                            testCounterNames[idx]);
336:                    nextCounterValue--;
337:                    assertEquals(testValue, nextCounterValue);
338:                }
339:
340:            }
341:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.