1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.lang;
19
20import java.io.IOException;
21import java.io.InputStream;
22import java.util.Vector;
23
24public class RuntimeTest extends junit.framework.TestCase {
25
26	Runtime r = Runtime.getRuntime();
27
28	InputStream is;
29
30	String s;
31
32	static boolean flag = false;
33
34	static boolean ranFinalize = false;
35
36	class HasFinalizer {
37		String internalString;
38
39		HasFinalizer(String s) {
40			internalString = s;
41		}
42
43		@Override
44        protected void finalize() {
45			internalString = "hit";
46		}
47	}
48
49	@Override
50    protected void finalize() {
51		if (flag)
52			ranFinalize = true;
53	}
54
55	protected RuntimeTest createInstance() {
56		return new RuntimeTest("FT");
57	}
58
59	/**
60	 * @tests java.lang.Runtime#exit(int)
61	 */
62	public void test_exitI() {
63		// Test for method void java.lang.Runtime.exit(int)
64		assertTrue("Can't really test this", true);
65	}
66
67	/**
68	 * @tests java.lang.Runtime#exec(java.lang.String)
69	 */
70	public void test_exec() {
71		boolean success = false;
72
73		/* successful exec's are tested by java.lang.Process */
74		try {
75			Runtime.getRuntime().exec("AnInexistentProgram");
76		} catch (IOException e) {
77			success = true;
78		}
79		assertTrue(
80				"failed to throw IOException when exec'ed inexistent program",
81				success);
82	}
83
84	/**
85	 * @tests java.lang.Runtime#freeMemory()
86	 */
87	public void test_freeMemory() {
88		// Test for method long java.lang.Runtime.freeMemory()
89		assertTrue("freeMemory returned nonsense value", r.freeMemory() > 0);
90	}
91
92	/**
93	 * @tests java.lang.Runtime#gc()
94	 */
95	public void test_gc() {
96		// Test for method void java.lang.Runtime.gc()
97		try {
98			r.gc(); // ensure all garbage objects have been collected
99			r.gc(); // two GCs force collection phase to complete
100			long firstRead = r.totalMemory() - r.freeMemory();
101			Vector<StringBuffer> v = new Vector<StringBuffer>();
102			for (int i = 1; i < 10; i++)
103				v.addElement(new StringBuffer(10000));
104			long secondRead = r.totalMemory() - r.freeMemory();
105			v = null;
106			r.gc();
107			r.gc();
108			assertTrue("object memory did not grow", secondRead > firstRead);
109			assertTrue("space was not reclaimed", (r.totalMemory() - r
110					.freeMemory()) < secondRead);
111		} catch (OutOfMemoryError oome ) {
112			System.out.println("Out of memory during freeMemory test");
113			r.gc();
114			r.gc();
115		}
116	}
117
118	/**
119	 * @tests java.lang.Runtime#getRuntime()
120	 */
121	public void test_getRuntime() {
122		// Test for method java.lang.Runtime java.lang.Runtime.getRuntime()
123		assertTrue("Used to test", true);
124	}
125
126	/**
127	 * @tests java.lang.Runtime#runFinalization()
128	 */
129	public void test_runFinalization() {
130		// Test for method void java.lang.Runtime.runFinalization()
131
132		flag = true;
133		createInstance();
134		int count = 10;
135		// the gc below likely bogosifies the test, but will have to do for
136		// the moment
137		while (!ranFinalize && count-- > 0) {
138			r.gc();
139			r.runFinalization();
140		}
141		assertTrue("Failed to run finalization", ranFinalize);
142	}
143
144	/**
145	 * @tests java.lang.Runtime#totalMemory()
146	 */
147	public void test_totalMemory() {
148		// Test for method long java.lang.Runtime.totalMemory()
149		assertTrue("totalMemory returned nonsense value", r.totalMemory() >= r
150				.freeMemory());
151	}
152
153	public RuntimeTest() {
154	}
155
156	public RuntimeTest(String name) {
157		super(name);
158	}
159}
160