Source Code Cross Referenced for ObjectTest.java in  » Apache-Harmony-Java-SE » java-package » 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 » java package » 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:
018:        /**
019:         * @author Roman S. Bushmanov
020:         * @version $Revision$
021:         */package java.lang;
022:
023:        import junit.framework.TestCase;
024:        import java.util.*;
025:
026:        /**
027:         * Unit test for java.lang.Object class.
028:         */
029:
030:        public class ObjectTest extends TestCase {
031:
032:            private Object obj1 = new Object();
033:
034:            private Object obj2 = new Object();
035:
036:            private Object obj3 = obj1;
037:
038:            public void testGetClass1() {
039:                assertEquals("java.lang.Object", obj1.getClass().getName());
040:            }
041:
042:            public void testGetClass2() {
043:                Object o = new Boolean(true);
044:                assertEquals("java.lang.Boolean", o.getClass().getName());
045:            }
046:
047:            /**
048:             * Verify 1.5 getClass()
049:             */
050:            public void testGetClass3() {
051:                Object o = new Boolean(true);
052:                Class<? extends Object> c = o.getClass();
053:                assertEquals("java.lang.Boolean", c.getName());
054:            }
055:
056:            /**
057:             * Verify 1.5 getClass()
058:             */
059:            public void testGetClass4() {
060:                AbstractMap<Integer, String> hm = new HashMap<Integer, String>();
061:                Class<? extends AbstractMap> c = hm.getClass();
062:                assertEquals("java.util.HashMap", c.getName());
063:            }
064:
065:            public void testHashCode1() {
066:                assertEquals("Hash code for the same object must be the same!",
067:                        obj1.hashCode(), obj3.hashCode());
068:            }
069:
070:            public void testEquals1() {
071:                assertTrue("Equilvalence relation must be reflexive!", obj1
072:                        .equals(obj1));
073:            }
074:
075:            public void testEguals2() {
076:                assertFalse("Different objects must be not equal!", obj1
077:                        .equals(obj2));
078:            }
079:
080:            public void testClone1() {
081:                try {
082:                    ObjectDescendant o = new ObjectDescendant();
083:                    o.testClone();
084:                } catch (CloneNotSupportedException e) {
085:                    return;
086:                }
087:                fail("CloneNotSupported exception must be thrown if an object "
088:                        + "doesn't implement Cloneabe interface!");
089:            }
090:
091:            public void testClone2() {
092:                try {
093:                    CloneableObjectDescendant o = new CloneableObjectDescendant(
094:                            1, obj1);
095:                    CloneableObjectDescendant clone = (CloneableObjectDescendant) o
096:                            .testClone();
097:                    assertEquals("Assert 0:", 1, clone.getPrimitiveVal());
098:                    assertEquals("Assert 1:", obj1, clone.getReferenceVal());
099:                } catch (CloneNotSupportedException e) {
100:                    fail("Should not throw CloneNotSupported exception!");
101:                }
102:            }
103:
104:            public void testToString() {
105:                Object o = new int[0];
106:                assertEquals("[I@" + Integer.toHexString(o.hashCode()), o
107:                        .toString());
108:            }
109:
110:            public void testNotifyAll() {
111:                Object o = new Object();
112:                //create and start two test threads
113:                TestThread t1 = new TestThread1(o);
114:                TestThread t2 = new TestThread1(o);
115:                t1.start();
116:                t2.start();
117:                //wait until both threads are started
118:                for (int i = 0; !(t1.flag && t2.flag) && i < 60; i++) {
119:                    try {
120:                        Thread.sleep(50);
121:                    } catch (InterruptedException e) {
122:                        fail("The main thread was interrupted.");
123:                    }
124:                }
125:                assertTrue("Test threads did not start for a long time!",
126:                        t1.flag && t2.flag);
127:                //now both test threads in the wait set of object o
128:                //notify both waiting threads
129:                synchronized (o) {
130:                    o.notifyAll();
131:                }
132:                //wait until both threads are notified
133:                for (int i = 0; t1.flag || t2.flag && i < 60; i++) {
134:                    try {
135:                        Thread.sleep(50);
136:                    } catch (InterruptedException e) {
137:                        fail("The main thread was interrupted.");
138:                    }
139:                }
140:                assertFalse("At least one of two test threads is not notified "
141:                        + "for a long time!", t1.flag || t2.flag);
142:            }
143:
144:            public void testNotify1() {
145:                Object o = new Object();
146:                try {
147:                    o.notify();
148:                } catch (IllegalMonitorStateException e) {
149:                    return;
150:                }
151:                fail("An IllegalMonitorStateException should be thrown!");
152:            }
153:
154:            public void testNotify() {
155:                Object o = new Object();
156:                //create and start two test threads
157:                TestThread t1 = new TestThread1(o);
158:                TestThread t2 = new TestThread1(o);
159:                t1.start();
160:                t2.start();
161:                //wait until both threads are started
162:                for (int i = 0; !(t1.flag && t2.flag) && i < 60; i++) {
163:                    try {
164:                        Thread.sleep(50);
165:                    } catch (InterruptedException e) {
166:                        fail("The main thread was interrupted.");
167:                    }
168:                }
169:                assertTrue("Test threads did not start for a long time!",
170:                        t1.flag && t2.flag);
171:                //now both test threads wait for object o
172:                //notify one of two waiting threads
173:                synchronized (o) {
174:                    o.notify();
175:                }
176:                //wait until one of two threads is notified
177:                for (int i = 0; t1.flag && t2.flag && i < 60; i++) {
178:                    try {
179:                        Thread.sleep(50);
180:                    } catch (InterruptedException e) {
181:                        fail("The main thread was interrupted.");
182:                    }
183:                }
184:                for (int i = 0; t1.flag && t2.flag && i < 60; i++) {
185:                    try {
186:                        Thread.sleep(50);
187:                    } catch (InterruptedException e) {
188:                        fail("The main thread was interrupted.");
189:                    }
190:                }
191:                assertFalse(
192:                        "None of two test threads is notified for a long time!",
193:                        t1.flag && t2.flag);
194:                assertFalse("Both test threads were notified instead of one!",
195:                        !t1.flag && !t2.flag);
196:            }
197:
198:            public void testWaitlongint() {
199:                long millis = 500;
200:                int nanos = 500;
201:                long start = 0;
202:                long finish = 0;
203:                synchronized (obj1) {
204:                    try {
205:                        start = System.currentTimeMillis();
206:                        obj1.wait(millis, nanos);
207:                        finish = System.currentTimeMillis();
208:                    } catch (InterruptedException e) {
209:                        fail("The main thread was interrupted!");
210:                    }
211:                }
212:                long atLeastWait = millis - 40;
213:                long actualWait = finish - start;
214:                assertTrue("Current thread hasn't slept enough: "
215:                        + "expected at least " + atLeastWait + " but was "
216:                        + actualWait, actualWait >= atLeastWait);
217:            }
218:
219:            public void testWaitlongint1() {
220:                try {
221:                    obj1.wait(500, 1000000);
222:                } catch (IllegalArgumentException e) {
223:                    return;
224:                } catch (InterruptedException e) {
225:                    fail("The main thread was interrupted!");
226:                }
227:                fail("An IllegalArgumentException must be thrown!");
228:            }
229:
230:            public void testWaitlong() {
231:                long timeout = 500;
232:                long start = 0;
233:                long finish = 0;
234:                Object o = new Object();
235:                synchronized (o) {
236:                    try {
237:                        start = System.currentTimeMillis();
238:                        o.wait(timeout);
239:                        finish = System.currentTimeMillis();
240:                    } catch (InterruptedException e) {
241:                        fail("The main thread was interrupted!");
242:                    }
243:                }
244:                // the sleeping time is more or less equal to timeout
245:                // so we allow 10% less time (there was bug filed on this issue)
246:                assertTrue("Current thread hasn't slept enough!", finish
247:                        - start + 1 > timeout - timeout / 10);
248:            }
249:
250:            public void testWait() {
251:                TestThread t = new TestThread() {
252:                    public void run() {
253:                        try {
254:                            (new Object()).wait();
255:                        } catch (InterruptedException e) {
256:                        } catch (IllegalMonitorStateException e) {
257:                            flag = true;
258:                        }
259:                    }
260:                };
261:                t.start();
262:                int i = 0;
263:                while (t.isAlive() && i < 300) {
264:                    try {
265:                        t.join(10);
266:                        i++;
267:                    } catch (Exception e) {
268:                        fail("The main thread was interrupted!");
269:                    }
270:                }
271:                assertTrue("An IllegalMonitorStateException must be thrown "
272:                        + "in test thread!", t.flag);
273:                if (t.isAlive()) {
274:                    fail("thread has not finished!");
275:                }
276:            }
277:
278:            public void testWait1() {
279:                final Object o = new Object();
280:                TestThread t = new TestThread() {
281:                    public void run() {
282:                        try {
283:                            synchronized (o) {
284:                                threadStarted = true;
285:                                o.notify();
286:                                o.wait();
287:                            }
288:                        } catch (InterruptedException e) {
289:                            flag = true;
290:                        }
291:                    }
292:                };
293:                try {
294:                    synchronized (o) {
295:                        t.start();
296:                        while (!t.threadStarted) {
297:                            o.wait();
298:                        }
299:                    }
300:                } catch (InterruptedException e) {
301:                    fail("The main thread was interrupted!");
302:                }
303:                assertTrue("thread must be alive", t.isAlive());
304:                t.interrupt();
305:                for (int i = 0; !t.flag && i < 300; i++) {
306:                    try {
307:                        t.join(10);
308:                    } catch (Exception e) {
309:                        fail("The main thread was interrupted_2!");
310:                    }
311:                }
312:                assertTrue(
313:                        "An InterruptedException must be thrown in test thread!",
314:                        t.flag);
315:            }
316:
317:            private class ObjectDescendant {
318:                void testClone() throws CloneNotSupportedException {
319:                    clone();
320:                }
321:            }
322:
323:            private class CloneableObjectDescendant implements  Cloneable {
324:
325:                private int primitiveField = -1;
326:
327:                private Object objectField = null;
328:
329:                CloneableObjectDescendant(int primitiveVal, Object referenceVal) {
330:                    primitiveField = primitiveVal;
331:                    objectField = referenceVal;
332:                }
333:
334:                int getPrimitiveVal() {
335:                    return primitiveField;
336:                }
337:
338:                Object getReferenceVal() {
339:                    return objectField;
340:                }
341:
342:                Object testClone() throws CloneNotSupportedException {
343:                    return clone();
344:                }
345:            }
346:
347:            private class TestThread extends Thread {
348:                boolean flag = false;
349:                volatile boolean threadStarted = false;
350:            }
351:
352:            private class TestThread1 extends TestThread {
353:
354:                private Object lock;
355:
356:                TestThread1(Object lock) {
357:                    this .lock = lock;
358:                }
359:
360:                public void run() {
361:                    try {
362:                        synchronized (lock) {
363:                            flag = true;
364:                            lock.wait();
365:                        }
366:                        flag = false;
367:                    } catch (InterruptedException e) {
368:                    }
369:                }
370:            }
371:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.