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: package org.apache.harmony.luni.tests.java.lang;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.Vector;
023:
024: public class RuntimeTest extends junit.framework.TestCase {
025:
026: Runtime r = Runtime.getRuntime();
027:
028: InputStream is;
029:
030: String s;
031:
032: static boolean flag = false;
033:
034: static boolean ranFinalize = false;
035:
036: class HasFinalizer {
037: String internalString;
038:
039: HasFinalizer(String s) {
040: internalString = s;
041: }
042:
043: @Override
044: protected void finalize() {
045: internalString = "hit";
046: }
047: }
048:
049: @Override
050: protected void finalize() {
051: if (flag)
052: ranFinalize = true;
053: }
054:
055: protected RuntimeTest createInstance() {
056: return new RuntimeTest("FT");
057: }
058:
059: /**
060: * @tests java.lang.Runtime#exit(int)
061: */
062: public void test_exitI() {
063: // Test for method void java.lang.Runtime.exit(int)
064: assertTrue("Can't really test this", true);
065: }
066:
067: /**
068: * @tests java.lang.Runtime#exec(java.lang.String)
069: */
070: public void test_exec() {
071: boolean success = false;
072:
073: /* successful exec's are tested by java.lang.Process */
074: try {
075: Runtime.getRuntime().exec("AnInexistentProgram");
076: } catch (IOException e) {
077: success = true;
078: }
079: assertTrue(
080: "failed to throw IOException when exec'ed inexistent program",
081: success);
082: }
083:
084: /**
085: * @tests java.lang.Runtime#freeMemory()
086: */
087: public void test_freeMemory() {
088: // Test for method long java.lang.Runtime.freeMemory()
089: assertTrue("freeMemory returned nonsense value",
090: r.freeMemory() > 0);
091: }
092:
093: /**
094: * @tests java.lang.Runtime#gc()
095: */
096: public void test_gc() {
097: // Test for method void java.lang.Runtime.gc()
098: try {
099: r.gc(); // ensure all garbage objects have been collected
100: r.gc(); // two GCs force collection phase to complete
101: long firstRead = r.totalMemory() - r.freeMemory();
102: Vector<StringBuffer> v = new Vector<StringBuffer>();
103: for (int i = 1; i < 10; i++)
104: v.addElement(new StringBuffer(10000));
105: long secondRead = r.totalMemory() - r.freeMemory();
106: v = null;
107: r.gc();
108: r.gc();
109: assertTrue("object memory did not grow",
110: secondRead > firstRead);
111: assertTrue("space was not reclaimed", (r.totalMemory() - r
112: .freeMemory()) < secondRead);
113: } catch (OutOfMemoryError oome) {
114: System.out.println("Out of memory during freeMemory test");
115: r.gc();
116: r.gc();
117: }
118: }
119:
120: /**
121: * @tests java.lang.Runtime#getRuntime()
122: */
123: public void test_getRuntime() {
124: // Test for method java.lang.Runtime java.lang.Runtime.getRuntime()
125: assertTrue("Used to test", true);
126: }
127:
128: /**
129: * @tests java.lang.Runtime#runFinalization()
130: */
131: public void test_runFinalization() {
132: // Test for method void java.lang.Runtime.runFinalization()
133:
134: flag = true;
135: createInstance();
136: int count = 10;
137: // the gc below likely bogosifies the test, but will have to do for
138: // the moment
139: while (!ranFinalize && count-- > 0) {
140: r.gc();
141: r.runFinalization();
142: }
143: assertTrue("Failed to run finalization", ranFinalize);
144: }
145:
146: /**
147: * @tests java.lang.Runtime#totalMemory()
148: */
149: public void test_totalMemory() {
150: // Test for method long java.lang.Runtime.totalMemory()
151: assertTrue("totalMemory returned nonsense value", r
152: .totalMemory() >= r.freeMemory());
153: }
154:
155: public RuntimeTest() {
156: }
157:
158: public RuntimeTest(String name) {
159: super(name);
160: }
161: }
|