Source Code Cross Referenced for RuntimeTest.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 Serguei S.Zapreyev
020:         * @version $Revision$
021:         */package java.lang;
022:
023:        import java.io.File;
024:
025:        import junit.framework.TestCase;
026:
027:        /*
028:         * Created on January 5, 2005
029:         *
030:         * This RuntimeTest class is used to test the Core API Runtime class
031:         * 
032:         */
033:
034:        /**
035:         * ###############################################################################
036:         * ###############################################################################
037:         * REMINDER("XXX") LIST:
038:         * 1. [Jun 11, 2005] test_availableProcessors, test_freeMemory, test_gc, test_runFinalization, 
039:         *    test_runFinalizersOnExit fail on "ORP+our Runtime+CLASSPATH API" platform
040:         *    because the availableProcessors, freeMemory, runFinalization (runFinalizersOnExit?)
041:         *    methods aren't correctly supported yet in orp/drl_natives/src
042:         * 2. [Jun 11, 2005] test_maxMemory, test_totalMemory fail on "ORP+CLASSPATH API" platform
043:         *    because the maxMemory
044:         *    method isn't correctly supported yet in orp/drl_natives/src:
045:         *    (Exception: java.lang.UnsatisfiedLinkError: Error compiling method java/lang/Runtime.maxMemory()J)
046:         * 3. [Jun 11, 2005] test_availableProcessors fails on "ORP+CLASSPATH API" platform
047:         *    because the availableProcessors
048:         *    method isn't correctly supported yet in orp/drl_natives/src:
049:         *    (Exception: java.lang.UnsatisfiedLinkError: Error compiling method java/lang/Runtime.availableProcessors()I)
050:         * ###############################################################################
051:         * ###############################################################################
052:         **/
053:
054:        public class RuntimeTest extends TestCase {
055:
056:            protected void setUp() throws Exception {
057:            }
058:
059:            protected void tearDown() throws Exception {
060:            }
061:
062:            /**
063:             *  
064:             */
065:            public void test_availableProcessors() {
066:                /**/System.out.println("test_availableProcessors");
067:                int fR = Runtime.getRuntime().availableProcessors();
068:                int sR = Runtime.getRuntime().availableProcessors();
069:                assertEquals(
070:                        "Runtime.availableProcessors method should return the "
071:                                + "same value during this test running as a rule!",
072:                        fR, sR);
073:                assertTrue("Runtime.availableProcessors method should return "
074:                        + "a value greater than 0!(" + fR + "|"
075:                        + Runtime.getRuntime().availableProcessors() + ")",
076:                        fR > 0);
077:                //XXX: the next case may be to compare with the bringing via Runtime.exec(...).getOutputStream()...
078:            }
079:
080:            /**
081:             *  
082:             */
083:            public void test_freeMemory() {
084:                Runtime.getRuntime().gc();
085:                long r1 = Runtime.getRuntime().freeMemory();
086:                assertTrue("Runtime.freeMemory() returned negative value: "
087:                        + r1, r1 >= 0);
088:                if (r1 < 500000) {
089:                    // low memory condition, 
090:                    // avoid false alarm if indicator is too coarse-grained 
091:                    return;
092:                }
093:                int probe = 0;
094:                try {
095:                    String stmp = "";
096:                    for (int ind = 0; ind < 300; ind++) {
097:                        stmp += "0123456789";
098:                    }
099:                    String inc = stmp;
100:                    probe = stmp.length();
101:                    while (r1 <= Runtime.getRuntime().freeMemory()) {
102:                        stmp += inc;
103:                        probe = stmp.length();
104:                    }
105:                } catch (OutOfMemoryError e) {
106:                    fail("Runtime.freeMemory() failed to detect " + probe
107:                            + " memory reduction from " + r1);
108:                }
109:            }
110:
111:            /**
112:             *  
113:             */
114:            public void test_gc() {
115:                /**/System.out.println("test_gc");
116:                long r1 = Runtime.getRuntime().freeMemory();
117:                long r2;
118:                long r4;
119:                String[] sa = new String[(int) r1 / 50000];
120:                int ind1 = 0;
121:                try {
122:                    String stmp = "";
123:                    for (int ind2 = 0; ind2 < 100/* 1000 */; ind2++) {
124:                        stmp += "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
125:                                + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
126:                                + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
127:                                + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
128:                                + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
129:                                + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
130:                                + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
131:                                + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
132:                                + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
133:                                + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
134:                                + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
135:                    }
136:                    for (ind1 = 0; ind1 < (int) r1 / 50000; ind1++) {
137:                        sa[ind1] = "" + stmp;
138:                    }
139:
140:                    r2 = Runtime.getRuntime().freeMemory();
141:                    for (ind1 = 0; ind1 < (int) r1 / 50000; ind1++) {
142:                        sa[ind1] = null;
143:                        Runtime.getRuntime().gc();
144:                        try {
145:                            Thread.sleep(20);
146:                        } catch (Exception e) {
147:                        }
148:                    }
149:                    sa = null;
150:                    try {
151:                        Thread.sleep(1000);
152:                    } catch (Exception e) {
153:                    }
154:                    Runtime.getRuntime().gc();
155:                    try {
156:                        Thread.sleep(1000);
157:                    } catch (Exception e) {
158:                    }
159:                    r4 = Runtime.getRuntime().freeMemory();
160:
161:                    //assertTrue( "FAILED: gc.check001: Runtime.gc method should initiate garbage collecting!("+r4+">"+r2+"?)", r4>r2 /*r4-r2>49999*/);
162:                    if (r4 <= r2) {
163:                        System.out
164:                                .println("WARNNING: RuntimeTest.test_gc "
165:                                        + "check001: It would be better if Runtime.gc method"
166:                                        + " could initiate garbage collecting! "
167:                                        + "(Here we have " + r4 + " !> " + r2
168:                                        + " .)");
169:                    }
170:                } catch (OutOfMemoryError e) {
171:                    System.out
172:                            .println("WARNNING: test_gc did not check "
173:                                    + "Runtime.gc method due to the technical reason !");
174:                }
175:            }
176:
177:            /**
178:             *  
179:             */
180:            public void test_getLocalizedInputStream() {
181:                /**/System.out.println("test_getLocalizedInputStream");
182:                byte[] bt = new byte[9];
183:                int res = 0;
184:                java.io.InputStream is = Runtime
185:                        .getRuntime()
186:                        .getLocalizedInputStream(
187:                                new java.io.StringBufferInputStream(
188:                                        "\u005a\u0061\u0070\u0072\u0065\u0079\u0065\u0076"));
189:                try {
190:                    res = is.read(bt);
191:                } catch (Exception e) {
192:                    fail("Runtime.getLocalizedInputStream method should return "
193:                            + "correct input stream!");
194:                }
195:                assertEquals(
196:                        "Incorrect number of bytes returned by InputStream!",
197:                        8, res);
198:                //assertTrue("FAILED: getLocalizedInputStream.check003: Runtime.getLocalizedInputStream method should return correct input stream ("+new String(bt)+")!", new String(bt).indexOf("Zapreyev")==0);
199:            }
200:
201:            /**
202:             *  
203:             */
204:            public void test_getLocalizedOutputStream() {
205:                /**/System.out.println("test_getLocalizedOutputStream");
206:                byte[] bt1 = { 0x5a, 0x61, 0x70, 0x72, 0x65, 0x79, 0x65, 0x76 };
207:                byte[] bt2 = new byte[9];
208:                java.io.PipedInputStream pis = new java.io.PipedInputStream();
209:                java.io.OutputStream os = null;
210:                try {
211:                    os = Runtime.getRuntime().getLocalizedOutputStream(
212:                            new java.io.PipedOutputStream(pis));
213:                } catch (Exception e) {
214:                    fail("check001: unexpected exception " + e);
215:                }
216:                try {
217:                    os.write(bt1);
218:                } catch (Exception e) {
219:                    fail("check002: unexpected exception " + e);
220:                }
221:                try {
222:                    pis.read(bt2);
223:                } catch (Exception e) {
224:                    fail("check003: unexpected exception " + e);
225:                }
226:                assertTrue("Incorrect bytes written by outputStream: "
227:                        + new String(bt2) + ")!", new String(bt2)
228:                        .indexOf("Zapreyev") == 0);
229:            }
230:
231:            /**
232:             *  
233:             */
234:            public void test_getRuntime() {
235:                /**/System.out.println("test_getRuntime");
236:                Runtime r1 = Runtime.getRuntime();
237:                assertNotNull(
238:                        "Runtime.getRuntime method must not return null!", r1);
239:                for (int ind2 = 0; ind2 < 1000; ind2++) {
240:                    assertSame(
241:                            "Runtime.getRuntime() should always return the same value!",
242:                            r1, Runtime.getRuntime());
243:                }
244:            }
245:
246:            /**
247:             *  
248:             */
249:            public void test_load() {
250:                /**/System.out.println("test_load");
251:                String jLP = null;
252:                String jlp = System.getProperty("java.library.path");
253:                String vblp = System.getProperty("vm.boot.library.path");
254:                jLP = (jlp != null && jlp.length() != 0 ? jlp : "")
255:                        + (vblp != null && vblp.length() != 0 ? File.pathSeparator
256:                                + vblp
257:                                : "");
258:                if (jLP.length() == 0) {
259:                    fail("empty java.library.path!");
260:                }
261:                String[] paths = jLP.split(File.pathSeparator);
262:                String ext = (System.getProperty("os.name").indexOf("indows") != -1) ? ".dll"
263:                        : ".so";
264:                int ind1;
265:                int ind2;
266:                File[] asf = null;
267:                for (ind1 = 0; ind1 < paths.length; ind1++) {
268:                    asf = new java.io.File(paths[ind1]).listFiles();
269:                    if (asf != null) {
270:                        for (ind2 = 0; ind2 < asf.length; ind2++) {
271:                            if (asf[ind2].getName().indexOf(ext) != -1) {
272:                                try {
273:                                    Runtime.getRuntime().load(
274:                                            asf[ind2].getCanonicalPath());
275:                                    return;
276:                                } catch (UnsatisfiedLinkError e) {
277:                                    continue;
278:                                } catch (Throwable e) {
279:                                    continue;
280:                                }
281:                            }
282:                        }
283:                    }
284:                }
285:                fail("Runtime.loadLibrary method has not loaded a dynamic library!");
286:            }
287:
288:            /**
289:             *  
290:             */
291:            public void test_loadLibrary() {
292:                /**/System.out.println("test_loadLibrary");
293:                String jLP = null;
294:                String jlp = System.getProperty("java.library.path");
295:                String vblp = System.getProperty("vm.boot.library.path");
296:                jLP = (jlp != null && jlp.length() != 0 ? jlp : "")
297:                        + (vblp != null && vblp.length() != 0 ? File.pathSeparator
298:                                + vblp
299:                                : "");
300:                if (jLP.length() == 0) {
301:                    fail("empty java.library.path!");
302:                }
303:                String[] paths = jLP.split(File.pathSeparator);
304:                String ext = (System.getProperty("os.name").indexOf("indows") != -1 ? ".dll"
305:                        : ".so");
306:                int ind1;
307:                int ind2;
308:                File[] asf = null;
309:                for (ind1 = 0; ind1 < paths.length; ind1++) {
310:                    if (paths[ind1] == null) {
311:                        continue;
312:                    }
313:                    asf = new java.io.File(paths[ind1]).listFiles();
314:                    if (asf != null) {
315:                        for (ind2 = 0; ind2 < asf.length; ind2++) {
316:                            if (asf[ind2].getName().indexOf(ext) != -1) {
317:                                String libName = asf[ind2].getName();
318:                                if (ext.equals(".dll")) {
319:                                    libName = libName.substring(0, libName
320:                                            .length() - 4);
321:                                } else {
322:                                    libName = libName.substring(3, libName
323:                                            .length() - 3);
324:                                }
325:                                try {
326:                                    Runtime.getRuntime().loadLibrary(libName);
327:                                    return;
328:                                } catch (UnsatisfiedLinkError e) {
329:                                    continue;
330:                                } catch (Throwable e) {
331:                                    continue;
332:                                }
333:
334:                            }
335:                        }
336:                    }
337:                }
338:                fail("Runtime.loadLibrary method has not loaded a dynamic library!");
339:            }
340:
341:            /**
342:             *  
343:             */
344:            public void test_maxMemory() {
345:                /**/System.out.println("test_maxMemory");
346:                long r1 = Runtime.getRuntime().freeMemory();
347:                long r2 = Runtime.getRuntime().maxMemory();
348:                assertTrue(
349:                        "Runtime.maxMemory method must not return negative value!",
350:                        r2 >= 0);
351:                assertTrue(
352:                        "Runtime.maxMemory must be greater than Runtime.freeMemory!",
353:                        r2 >= r1);
354:                for (int ind2 = 0; ind2 < 1000; ind2++) {
355:                    //assertSame("Runtime.maxMemory() must always return the same value!",
356:                    //        r2, Runtime.getRuntime().maxMemory());
357:                    assertTrue(
358:                            "FAILED: test_maxMemory: Runtime.maxMemory method should return same value each time!",
359:                            r2 == Runtime.getRuntime().maxMemory());
360:                }
361:            }
362:
363:            /**
364:             * 
365:             */
366:            public void test_totalMemory() {
367:                /**/System.out.println("test_totalMemory");
368:                long r1 = Runtime.getRuntime().freeMemory();
369:                long r2 = Runtime.getRuntime().maxMemory();
370:                long r3 = Runtime.getRuntime().totalMemory();
371:                assertTrue(
372:                        "Runtime.totalMemory() should not return negative value!",
373:                        r3 >= 0);
374:                assertTrue("Runtime.totalMemory() should be greater than "
375:                        + "Runtime.freeMemory()!", r3 >= r1);
376:                assertTrue("Runtime.totalMemory() should be smaller than "
377:                        + "Runtime.maxMemory()!", r2 >= r3);
378:                for (int ind2 = 0; ind2 < 1000; ind2++) {
379:                    assertTrue(
380:                            "Runtime.totalMemory, Runtime.freeMemory, "
381:                                    + "Runtime.maxMemory should correlate correctly!",
382:                            Runtime.getRuntime().freeMemory() <= Runtime
383:                                    .getRuntime().totalMemory()
384:                                    && Runtime.getRuntime().maxMemory() >= Runtime
385:                                            .getRuntime().totalMemory());
386:                }
387:            }
388:
389:            /**
390:             *  
391:             */
392:            public void test_traceInstructions() {
393:                /**/System.out.println("test_traceInstructions");
394:                java.util.Random r = new java.util.Random();
395:                try {
396:                    for (int ind2 = 0; ind2 < 1000; ind2++) {
397:                        Runtime.getRuntime().traceInstructions(
398:                                (r.nextInt(10) % 2) == 0);
399:                        Integer.toString(ind2);
400:                    }
401:                } catch (Throwable e) {
402:                    fail("Unexpected exception: " + e);
403:                }
404:            }
405:
406:            /**
407:             *  
408:             */
409:            public void test_traceMethodCalls() {
410:                /**/System.out.println("test_traceMethodCalls");
411:                java.util.Random r = new java.util.Random();
412:                try {
413:                    for (int ind2 = 0; ind2 < 1000; ind2++) {
414:                        Runtime.getRuntime().traceMethodCalls(
415:                                (r.nextInt(10) % 2) == 0);
416:                        Math.pow((long) ind2, (long) ind2);
417:                        Math.IEEEremainder((double) ind2, (double) ind2);
418:                    }
419:                } catch (Throwable e) {
420:                    fail("Unexpected exception: " + e);
421:                }
422:            }
423:
424:            /**
425:             *  
426:             */
427:            public void test_halt() {
428:                //System.out.println("test_halt");
429:                //Runtime.getRuntime().halt(777);
430:                //fail("what's wrong  ;) ?");
431:            }
432:
433:            /**
434:             *  
435:             */
436:            public void test_exit() {
437:                //System.out.println("test_exit");
438:                //Runtime.getRuntime().exit(777);
439:                //fail("what's wrong  ;) ?");
440:            }
441:
442:            /**
443:             * Regression test for HARMONY-690 
444:             */
445:            public void test_addShutdownHook() {
446:                // Test for method long java.lang.Runtime.addShutdownHook()
447:                boolean exception = false;
448:                try {
449:                    Runtime.getRuntime().addShutdownHook(null);
450:                } catch (NullPointerException npe) {
451:                    exception = true;
452:                }
453:                assertTrue("NullPointerException expected!", exception);
454:            }
455:
456:            /**
457:             * Regression test for HARMONY-690 
458:             */
459:            public void test_removeShutdownHook() {
460:                // Test for method long java.lang.Runtime.removeShutdownHook()
461:                boolean exception = false;
462:                try {
463:                    Runtime.getRuntime().removeShutdownHook(null);
464:                } catch (NullPointerException npe) {
465:                    exception = true;
466:                }
467:                assertTrue("NullPointerException expected!", exception);
468:            }
469:
470:            /**
471:             * Regression test for HARMONY-920 
472:             */
473:            public void test_execStrStr() throws Exception {
474:                try {
475:                    String[] cmd = new String[] { null, "gcc" };
476:                    String[] env = new String[] { "aaa", "bbb" };
477:                    Runtime.getRuntime().exec(cmd, env);
478:                    fail("1: exception expcected");
479:                } catch (NullPointerException npe) {
480:                    //expected
481:                }
482:                try {
483:                    String[] cmd = new String[] { "gcc", "-m" };
484:                    String[] env = new String[] { "aaa", null };
485:                    Runtime.getRuntime().exec(cmd, env);
486:                    fail("2: exception expcected");
487:                } catch (NullPointerException npe) {
488:                    //expected
489:                }
490:            }
491:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.