Source Code Cross Referenced for ObjectTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » lang » 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 » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:        package org.apache.harmony.luni.tests.java.lang;
018:
019:        public class ObjectTest extends junit.framework.TestCase {
020:
021:            /**
022:             * Test objects.
023:             */
024:            Object obj1 = new Object();
025:
026:            Object obj2 = new Object();
027:
028:            /**
029:             * Generic state indicator.
030:             */
031:            int status = 0;
032:
033:            int ready = 0;
034:
035:            /**
036:             * @tests java.lang.Object#Object()
037:             */
038:            public void test_Constructor() {
039:                // Test for method java.lang.Object()
040:                assertNotNull("Constructor failed !!!", new Object());
041:            }
042:
043:            /**
044:             * @tests java.lang.Object#equals(java.lang.Object)
045:             */
046:            public void test_equalsLjava_lang_Object() {
047:                // Test for method boolean java.lang.Object.equals(java.lang.Object)
048:                assertTrue("Same object should be equal", obj1.equals(obj1));
049:                assertTrue("Different objects should not be equal", !obj1
050:                        .equals(obj2));
051:            }
052:
053:            /**
054:             * @tests java.lang.Object#getClass()
055:             */
056:            public void test_getClass() throws Exception {
057:                // Test for method java.lang.Class java.lang.Object.getClass()
058:                String classNames[] = { "java.lang.Object",
059:                        "java.lang.Throwable", "java.lang.StringBuffer" };
060:                Class<?> classToTest = null;
061:                Object instanceToTest = null;
062:
063:                status = 0;
064:                for (int i = 0; i < classNames.length; ++i) {
065:                    classToTest = Class.forName(classNames[i]);
066:                    instanceToTest = classToTest.newInstance();
067:                    assertTrue("Instance didn't match creator class.",
068:                            instanceToTest.getClass() == classToTest);
069:                    assertTrue(
070:                            "Instance didn't match class with matching name.",
071:                            instanceToTest.getClass() == Class
072:                                    .forName(classNames[i]));
073:                }
074:            }
075:
076:            /**
077:             * @tests java.lang.Object#hashCode()
078:             */
079:            public void test_hashCode() {
080:                // Test for method int java.lang.Object.hashCode()
081:                assertTrue("Same object should have same hash.", obj1
082:                        .hashCode() == obj1.hashCode());
083:                assertTrue("Same object should have same hash.", obj2
084:                        .hashCode() == obj2.hashCode());
085:            }
086:
087:            /**
088:             * @tests java.lang.Object#notify()
089:             */
090:            public void test_notify() {
091:                // Test for method void java.lang.Object.notify()
092:
093:                // Inner class to run test thread.
094:                class TestThread implements  Runnable {
095:                    public void run() {
096:                        synchronized (obj1) {
097:                            try {
098:                                ready += 1;
099:                                obj1.wait();// Wait for ever.
100:                                status += 1;
101:                            } catch (InterruptedException ex) {
102:                                status = -1000;
103:                            }
104:                        }
105:                    }
106:                }
107:                ;
108:
109:                // Start of test code.
110:
111:                // Warning:
112:                // This code relies on each thread getting serviced within
113:                // 200 mSec of when it is notified. Although this
114:                // seems reasonable, it could lead to false-failures.
115:
116:                ready = 0;
117:                status = 0;
118:                final int readyWaitSecs = 3;
119:
120:                final int threadCount = 20;
121:                for (int i = 0; i < threadCount; ++i) {
122:                    new Thread(new TestThread()).start();
123:                }
124:                synchronized (obj1) {
125:                    try {
126:
127:                        // Wait up to readyWaitSeconds for all threads to be waiting on
128:                        // monitor
129:                        for (int i = 0; i < readyWaitSecs; i++) {
130:                            obj1.wait(1000, 0);
131:                            if (ready == threadCount) {
132:                                break;
133:                            }
134:                        }
135:
136:                        // Check pre-conditions of testing notifyAll
137:                        assertTrue(
138:                                "Not all launched threads are waiting. (ready = "
139:                                        + ready + ")", ready == threadCount);
140:                        assertTrue("Thread woke too early. (status = " + status
141:                                + ")", status == 0);
142:
143:                        for (int i = 1; i <= threadCount; ++i) {
144:                            obj1.notify();
145:                            obj1.wait(200, 0);
146:                            assertTrue("Out of sync. (expected " + i
147:                                    + " but got " + status + ")", status == i);
148:                        }
149:
150:                    } catch (InterruptedException ex) {
151:                        fail("Unexpectedly got an InterruptedException. (status = "
152:                                + status + ")");
153:                    }
154:                }
155:            }
156:
157:            /**
158:             * @tests java.lang.Object#notifyAll()
159:             */
160:            public void test_notifyAll() {
161:                // Test for method void java.lang.Object.notifyAll()
162:
163:                // Inner class to run test thread.
164:                class TestThread implements  Runnable {
165:                    public void run() {
166:                        synchronized (obj1) {
167:                            try {
168:                                ready += 1;
169:                                obj1.wait();// Wait for ever.
170:                                status += 1;
171:                            } catch (InterruptedException ex) {
172:                                status = -1000;
173:                            }
174:                        }
175:                    }
176:                }
177:                ;
178:
179:                // Start of test code.
180:
181:                // Warning:
182:                // This code relies on all threads getting serviced within
183:                // 5 seconds of when they are notified. Although this
184:                // seems reasonable, it could lead to false-failures.
185:
186:                status = 0;
187:                ready = 0;
188:                final int readyWaitSecs = 3;
189:                final int threadCount = 20;
190:                for (int i = 0; i < threadCount; ++i) {
191:                    new Thread(new TestThread()).start();
192:                }
193:
194:                synchronized (obj1) {
195:
196:                    try {
197:
198:                        // Wait up to readyWaitSeconds for all threads to be waiting on
199:                        // monitor
200:                        for (int i = 0; i < readyWaitSecs; i++) {
201:                            obj1.wait(1000, 0);
202:                            if (ready == threadCount) {
203:                                break;
204:                            }
205:                        }
206:
207:                        // Check pre-conditions of testing notifyAll
208:                        assertTrue(
209:                                "Not all launched threads are waiting. (ready = "
210:                                        + ready + ")", ready == threadCount);
211:                        assertTrue(
212:                                "At least one thread woke too early. (status = "
213:                                        + status + ")", status == 0);
214:
215:                        obj1.notifyAll();
216:
217:                        obj1.wait(5000, 0);
218:
219:                        assertTrue(
220:                                "At least one thread did not get notified. (status = "
221:                                        + status + ")", status == threadCount);
222:
223:                    } catch (InterruptedException ex) {
224:                        fail("Unexpectedly got an InterruptedException. (status = "
225:                                + status + ")");
226:                    }
227:
228:                }
229:            }
230:
231:            /**
232:             * @tests java.lang.Object#toString()
233:             */
234:            public void test_toString() {
235:                // Test for method java.lang.String java.lang.Object.toString()
236:                assertNotNull("Object toString returned null.", obj1.toString());
237:            }
238:
239:            /**
240:             * @tests java.lang.Object#wait()
241:             */
242:            public void test_wait() {
243:                // Test for method void java.lang.Object.wait()
244:
245:                // Inner class to run test thread.
246:                class TestThread implements  Runnable {
247:                    public void run() {
248:                        synchronized (obj1) {
249:                            try {
250:                                obj1.wait();// Wait for ever.
251:                                status = 1;
252:                            } catch (InterruptedException ex) {
253:                                status = -1;
254:                            }
255:                        }
256:                    }
257:                }
258:                ;
259:
260:                // Start of test code.
261:
262:                // Warning:
263:                // This code relies on threads getting serviced within
264:                // 1 second of when they are notified. Although this
265:                // seems reasonable, it could lead to false-failures.
266:
267:                status = 0;
268:                new Thread(new TestThread()).start();
269:                synchronized (obj1) {
270:                    try {
271:                        obj1.wait(1000, 0);
272:                        assertTrue("Thread woke too early. (status = " + status
273:                                + ")", status == 0);
274:                        obj1.notifyAll();
275:                        obj1.wait(1000, 0);
276:                        assertTrue("Thread did not get notified. (status = "
277:                                + status + ")", status == 1);
278:                    } catch (InterruptedException ex) {
279:                        fail("Unexpectedly got an InterruptedException. (status = "
280:                                + status + ")");
281:                    }
282:                }
283:            }
284:
285:            /**
286:             * @tests java.lang.Object#wait(long)
287:             */
288:            public void test_waitJ() {
289:                // Test for method void java.lang.Object.wait(long)
290:
291:                // Start of test code.
292:
293:                final int loopCount = 20;
294:                final int allowableError = 100; // millesconds
295:                final int delay = 200; // milliseconds
296:                synchronized (obj1) {
297:                    try {
298:                        int count = 0;
299:                        long[][] toLong = new long[3][3];
300:                        for (int i = 0; i < loopCount; ++i) {
301:                            long before = System.currentTimeMillis();
302:                            obj1.wait(delay, 0);
303:                            long after = System.currentTimeMillis();
304:                            long error = (after - before - delay);
305:                            if (error < 0)
306:                                error = -error;
307:                            if (i > 0 && error > allowableError) {
308:                                // Allow jit to warm up before testing
309:                                if (count < toLong.length) {
310:                                    toLong[count][0] = i;
311:                                    toLong[count][1] = before;
312:                                    toLong[count][2] = after;
313:                                    count++;
314:                                }
315:                                if (error > (1000 + delay)
316:                                        || count == toLong.length) {
317:                                    StringBuffer sb = new StringBuffer();
318:                                    for (int j = 0; j < count; j++) {
319:                                        sb
320:                                                .append("wakeup time too inaccurate, iteration ");
321:                                        sb.append(toLong[j][0]);
322:                                        sb.append(", before: ");
323:                                        sb.append(toLong[j][1]);
324:                                        sb.append(" after: ");
325:                                        sb.append(toLong[j][2]);
326:                                        sb.append(" diff: ");
327:                                        sb.append(toLong[j][2] - toLong[j][1]);
328:                                        sb.append("\n");
329:                                    }
330:                                    fail(sb.toString());
331:                                }
332:                            }
333:                        }
334:                    } catch (InterruptedException ex) {
335:                        fail("Unexpectedly got an InterruptedException. (status = "
336:                                + status + ")");
337:                    }
338:                }
339:            }
340:
341:            /**
342:             * @tests java.lang.Object#wait(long, int)
343:             */
344:            public void test_waitJI() {
345:                // Test for method void java.lang.Object.wait(long, int)
346:
347:                // Inner class to run test thread.
348:                class TestThread implements  Runnable {
349:                    public void run() {
350:                        synchronized (obj1) {
351:                            try {
352:                                obj1.wait(0, 1); // Don't wait very long.
353:                                status = 1;
354:                                obj1.wait(0, 0); // Wait for ever.
355:                                status = 2;
356:                            } catch (InterruptedException ex) {
357:                                status = -1;
358:                            }
359:                        }
360:                    }
361:                }
362:                ;
363:
364:                // Start of test code.
365:
366:                // Warning:
367:                // This code relies on threads getting serviced within
368:                // 1 second of when they are notified. Although this
369:                // seems reasonable, it could lead to false-failures.
370:
371:                status = 0;
372:                new Thread(new TestThread()).start();
373:                synchronized (obj1) {
374:                    try {
375:                        obj1.wait(1000, 0);
376:                        assertTrue("Thread did not wake after 1 ms. (status = "
377:                                + status + ")", status == 1);
378:                        obj1.notifyAll();
379:                        obj1.wait(1000, 0);
380:                        assertTrue("Thread did not get notified. (status = "
381:                                + status + ")", status == 2);
382:                    } catch (InterruptedException ex) {
383:                        fail("Unexpectedly got an InterruptedException. (status = "
384:                                + status + ")");
385:                    }
386:                }
387:
388:            }
389:        }
w_ww.__j___a___v__a___2___s._c_om__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.