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.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.File;
023: import java.io.InputStream;
024: import java.io.PrintStream;
025: import java.util.Map;
026: import java.util.Properties;
027:
028: public class SystemTest extends junit.framework.TestCase {
029:
030: static boolean flag = false;
031:
032: static boolean ranFinalize = false;
033:
034: /**
035: * @tests java.lang.System#setIn(java.io.InputStream)
036: */
037: public void test_setInLjava_io_InputStream() {
038: InputStream orgIn = System.in;
039: InputStream in = new ByteArrayInputStream(new byte[0]);
040: System.setIn(in);
041: assertTrue("in not set", System.in == in);
042: System.setIn(orgIn);
043: }
044:
045: /**
046: * @tests java.lang.System#setOut(java.io.PrintStream)
047: */
048: public void test_setOutLjava_io_PrintStream() {
049: PrintStream orgOut = System.out;
050: PrintStream out = new PrintStream(new ByteArrayOutputStream());
051: System.setOut(out);
052: assertTrue("out not set", System.out == out);
053: System.setOut(orgOut);
054: }
055:
056: /**
057: * @tests java.lang.System#setErr(java.io.PrintStream)
058: */
059: public void test_setErrLjava_io_PrintStream() {
060: PrintStream orgErr = System.err;
061: PrintStream err = new PrintStream(new ByteArrayOutputStream());
062: System.setErr(err);
063: assertTrue("err not set", System.err == err);
064: System.setErr(orgErr);
065: }
066:
067: /**
068: * @tests java.lang.System#arraycopy(java.lang.Object, int,
069: * java.lang.Object, int, int)
070: */
071: public void test_arraycopyLjava_lang_ObjectILjava_lang_ObjectII() {
072: // Test for method void java.lang.System.arraycopy(java.lang.Object,
073: // int, java.lang.Object, int, int)
074: Integer a[] = new Integer[20];
075: Integer b[] = new Integer[20];
076: int i = 0;
077: while (i < a.length) {
078: a[i] = new Integer(i);
079: ++i;
080: }
081: System.arraycopy(a, 0, b, 0, a.length);
082: for (i = 0; i < a.length; i++)
083: assertTrue("Copied elements incorrectly", a[i].equals(b[i]));
084:
085: /* Non primitive array types don't need to be identical */
086: String[] source1 = new String[] { "element1" };
087: Object[] dest1 = new Object[1];
088: System.arraycopy(source1, 0, dest1, 0, dest1.length);
089: assertTrue("Invalid copy 1", dest1[0] == source1[0]);
090:
091: char[][] source = new char[][] { { 'H', 'e', 'l', 'l', 'o' },
092: { 'W', 'o', 'r', 'l', 'd' } };
093: char[][] dest = new char[2][];
094: System.arraycopy(source, 0, dest, 0, dest.length);
095: assertTrue("Invalid copy 2", dest[0] == source[0]
096: && dest[1] == source[1]);
097: }
098:
099: /**
100: * @tests java.lang.System#currentTimeMillis()
101: */
102: public void test_currentTimeMillis() {
103: // Test for method long java.lang.System.currentTimeMillis()
104: long firstRead = System.currentTimeMillis();
105: try {
106: Thread.sleep(150);
107: } catch (InterruptedException e) {
108: }
109: long secondRead = System.currentTimeMillis();
110: assertTrue("Incorrect times returned: " + firstRead + ", "
111: + secondRead, firstRead < secondRead);
112: }
113:
114: /**
115: * @tests java.lang.System#exit(int)
116: */
117: public void test_exitI() {
118: // Test for method void java.lang.System.exit(int)
119: // Tested in destructive test: Test_System_Exit ???
120: }
121:
122: /**
123: * @tests java.lang.System#getProperties()
124: */
125: public void test_getProperties() {
126: // Test for method java.util.Properties java.lang.System.getProperties()
127: Properties p = System.getProperties();
128: assertTrue("Incorrect properties returned", p.getProperty(
129: "java.version").indexOf("1.", 0) >= 0);
130:
131: // Ensure spec'ed properties are non-null. See System.getProperties()
132: // spec.
133: String[] props = { "java.version", "java.vendor",
134: "java.vendor.url", "java.home",
135: "java.vm.specification.version",
136: "java.vm.specification.vendor",
137: "java.vm.specification.name", "java.vm.version",
138: "java.vm.vendor", "java.vm.name",
139: "java.specification.name", "java.specification.vendor",
140: "java.specification.name", "java.class.version",
141: "java.class.path", "java.ext.dirs", "os.name",
142: "os.arch", "os.version", "file.separator",
143: "path.separator", "line.separator", "user.name",
144: "user.home", "user.dir", };
145: for (int i = 0; i < props.length; i++) {
146: assertNotNull(props[i], System.getProperty(props[i]));
147: }
148: }
149:
150: /**
151: * @tests java.lang.System#getProperty(java.lang.String)
152: */
153: public void test_getPropertyLjava_lang_String() {
154: // Test for method java.lang.String
155: // java.lang.System.getProperty(java.lang.String)
156: assertTrue("Failed to return correct property value", System
157: .getProperty("java.version").indexOf("1.", 0) >= 0);
158:
159: boolean is8859_1 = true;
160: String encoding = System.getProperty("file.encoding");
161: byte[] bytes = new byte[128];
162: char[] chars = new char[128];
163: for (int i = 0; i < bytes.length; i++) {
164: bytes[i] = (byte) (i + 128);
165: chars[i] = (char) (i + 128);
166: }
167: String charResult = new String(bytes);
168: byte[] byteResult = new String(chars).getBytes();
169: if (charResult.length() == 128 && byteResult.length == 128) {
170: for (int i = 0; i < bytes.length; i++) {
171: if (charResult.charAt(i) != (char) (i + 128)
172: || byteResult[i] != (byte) (i + 128))
173: is8859_1 = false;
174: }
175: } else
176: is8859_1 = false;
177: String[] possibles = new String[] { "ISO8859_1", "8859_1",
178: "ISO8859-1", "ISO-8859-1", "ISO_8859-1",
179: "ISO_8859-1:1978", "ISO-IR-100", "LATIN1",
180: "CSISOLATIN1" };
181: boolean found8859_1 = false;
182: for (int i = 0; i < possibles.length; i++) {
183: if (possibles[i].equals(encoding)) {
184: found8859_1 = true;
185: break;
186: }
187: }
188: assertTrue("Wrong encoding: " + encoding, !is8859_1
189: || found8859_1);
190: }
191:
192: /**
193: * @tests java.lang.System#getProperty(java.lang.String)
194: * Tests that there are no extra path separator in boot class path.
195: * Regression test for HARMONY-3298
196: */
197: public void test_getProperty_bootClassPath() {
198: String bootClassPath = System
199: .getProperty("org.apache.harmony.boot.class.path");
200:
201: if (bootClassPath == null) {
202: bootClassPath = System.getProperty("sun.boot.class.path");
203: }
204:
205: if (bootClassPath != null
206: && (bootClassPath.indexOf(File.pathSeparator
207: + File.pathSeparator) >= 0)) {
208: fail("Boot class path contains extra path separator: "
209: + bootClassPath);
210: }
211: }
212:
213: /**
214: * @tests java.lang.System#getProperty(java.lang.String, java.lang.String)
215: */
216: public void test_getPropertyLjava_lang_StringLjava_lang_String() {
217: // Test for method java.lang.String
218: // java.lang.System.getProperty(java.lang.String, java.lang.String)
219: assertTrue("Failed to return correct property value: "
220: + System.getProperty("java.version", "99999"),
221: System.getProperty("java.version", "99999").indexOf(
222: "1.", 0) >= 0);
223: assertEquals("Failed to return correct property value",
224: "bogus", System.getProperty("bogus.prop", "bogus"));
225: }
226:
227: /**
228: * @tests java.lang.System#setProperty(java.lang.String, java.lang.String)
229: */
230: public void test_setPropertyLjava_lang_StringLjava_lang_String() {
231: // Test for method java.lang.String
232: // java.lang.System.setProperty(java.lang.String, java.lang.String)
233:
234: assertNull("Failed to return null", System.setProperty(
235: "testing", "value1"));
236: assertTrue("Failed to return old value", System.setProperty(
237: "testing", "value2") == "value1");
238: assertTrue("Failed to find value", System
239: .getProperty("testing") == "value2");
240:
241: boolean exception = false;
242: try {
243: System.setProperty("", "default");
244: } catch (IllegalArgumentException e) {
245: exception = true;
246: }
247: assertTrue("Expected IllegalArgumentException", exception);
248: }
249:
250: /**
251: * @tests java.lang.System#getSecurityManager()
252: */
253: public void test_getSecurityManager() {
254: // Test for method java.lang.SecurityManager
255: // java.lang.System.getSecurityManager()
256: assertNull("Returned incorrect SecurityManager", System
257: .getSecurityManager());
258: }
259:
260: /**
261: * @tests java.lang.System#identityHashCode(java.lang.Object)
262: */
263: public void test_identityHashCodeLjava_lang_Object() {
264: // Test for method int
265: // java.lang.System.identityHashCode(java.lang.Object)
266: Object o = new Object();
267: String s = "Gabba";
268: assertEquals("Nonzero returned for null", 0, System
269: .identityHashCode(null));
270: assertTrue("Nonequal has returned for Object", System
271: .identityHashCode(o) == o.hashCode());
272: assertTrue("Same as usual hash returned for String", System
273: .identityHashCode(s) != s.hashCode());
274: }
275:
276: /**
277: * @tests java.lang.System#runFinalization()
278: */
279: public void test_runFinalization() {
280: // Test for method void java.lang.System.runFinalization()
281:
282: flag = true;
283: createInstance();
284: int count = 10;
285: // the gc below likely bogosifies the test, but will have to do for
286: // the moment
287: while (!ranFinalize && count-- > 0) {
288: System.gc();
289: System.runFinalization();
290: }
291: assertTrue("Failed to run finalization", ranFinalize);
292: }
293:
294: /**
295: * @tests java.lang.System#runFinalizersOnExit(boolean)
296: */
297: @SuppressWarnings("deprecation")
298: public void test_runFinalizersOnExitZ() {
299: // Can we call the method at least?
300: System.runFinalizersOnExit(false);
301: }
302:
303: /**
304: * @tests java.lang.System#setProperties(java.util.Properties)
305: */
306: public void test_setPropertiesLjava_util_Properties() {
307: // Test for method void
308: // java.lang.System.setProperties(java.util.Properties)
309:
310: Properties orgProps = System.getProperties();
311: java.util.Properties tProps = new java.util.Properties();
312: tProps.put("test.prop", "this is a test property");
313: tProps.put("bogus.prop", "bogus");
314: System.setProperties(tProps);
315: try {
316: assertEquals("Failed to set properties",
317: "this is a test property", System.getProperties()
318: .getProperty("test.prop"));
319: } finally {
320: // restore the original properties
321: System.setProperties(orgProps);
322: }
323: }
324:
325: //Regression Test for Harmony-2356
326: public void testEnvUnmodifiable() {
327: Map map = System.getenv();
328: try {
329: map.containsKey(null);
330: fail("Should throw NullPointerExcepiton.");
331: } catch (NullPointerException e) {
332: // expected
333: }
334:
335: try {
336: map.containsKey(new Integer(10));
337: fail("Should throw ClassCastException.");
338: } catch (ClassCastException e) {
339: // expected
340: }
341:
342: try {
343: map.containsValue(null);
344: fail("Should throw NullPointerExcepiton.");
345: } catch (NullPointerException e) {
346: // expected
347: }
348:
349: try {
350: map.containsValue(new Integer(10));
351: fail("Should throw ClassCastException.");
352: } catch (ClassCastException e) {
353: // expected
354: }
355:
356: try {
357: map.get(null);
358: fail("Should throw NullPointerExcepiton.");
359: } catch (NullPointerException e) {
360: // expected
361: }
362:
363: try {
364: map.get(new Integer(10));
365: fail("Should throw ClassCastException.");
366: } catch (ClassCastException e) {
367: // expected
368: }
369:
370: try {
371: map.put(null, "AAA");
372: fail("Should throw UnsupportedOperationExcepiton.");
373: } catch (UnsupportedOperationException e) {
374: // expected
375: }
376:
377: try {
378: map.put("AAA", new Integer(10));
379: fail("Should throw UnsupportedOperationException.");
380: } catch (UnsupportedOperationException e) {
381: // expected
382: }
383:
384: try {
385: map.put("AAA", "BBB");
386: fail("Should throw UnsupportedOperationException.");
387: } catch (UnsupportedOperationException e) {
388: // expected
389: }
390:
391: try {
392: map.clear();
393: fail("Should throw UnsupportedOperationException.");
394: } catch (UnsupportedOperationException e) {
395: // expected
396: }
397:
398: try {
399: map.remove(null);
400: fail("Should throw UnsupportedOperationException.");
401: } catch (UnsupportedOperationException e) {
402: // expected
403: }
404:
405: }
406:
407: @Override
408: protected void setUp() {
409: flag = false;
410: ranFinalize = false;
411: }
412:
413: protected SystemTest createInstance() {
414: return new SystemTest("FT");
415: }
416:
417: @Override
418: protected void finalize() {
419: if (flag)
420: ranFinalize = true;
421: }
422:
423: public SystemTest() {
424: }
425:
426: public SystemTest(String name) {
427: super(name);
428: }
429: }
|