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.tests.java.lang; 19 20import java.io.ByteArrayInputStream; 21import java.io.ByteArrayOutputStream; 22import java.io.File; 23import java.io.InputStream; 24import java.io.PrintStream; 25import java.util.Map; 26import java.util.Properties; 27 28public class SystemTest extends junit.framework.TestCase { 29 30 static boolean flag = false; 31 32 static boolean ranFinalize = false; 33 34 /** 35 * java.lang.System#setIn(java.io.InputStream) 36 */ 37 public void test_setInLjava_io_InputStream() { 38 InputStream orgIn = System.in; 39 InputStream in = new ByteArrayInputStream(new byte[0]); 40 System.setIn(in); 41 assertTrue("in not set", System.in == in); 42 System.setIn(orgIn); 43 } 44 45 /** 46 * java.lang.System#setOut(java.io.PrintStream) 47 */ 48 public void test_setOutLjava_io_PrintStream() { 49 PrintStream orgOut = System.out; 50 PrintStream out = new PrintStream(new ByteArrayOutputStream()); 51 System.setOut(out); 52 assertTrue("out not set", System.out == out); 53 System.setOut(orgOut); 54 } 55 56 /** 57 * java.lang.System#setErr(java.io.PrintStream) 58 */ 59 public void test_setErrLjava_io_PrintStream() { 60 PrintStream orgErr = System.err; 61 PrintStream err = new PrintStream(new ByteArrayOutputStream()); 62 System.setErr(err); 63 assertTrue("err not set", System.err == err); 64 System.setErr(orgErr); 65 } 66 67 /** 68 * java.lang.System#arraycopy(java.lang.Object, int, 69 *java.lang.Object, int, int) 70 */ 71 public void test_arraycopyLjava_lang_ObjectILjava_lang_ObjectII() { 72 // Test for method void java.lang.System.arraycopy(java.lang.Object, 73 // int, java.lang.Object, int, int) 74 Integer a[] = new Integer[20]; 75 Integer b[] = new Integer[20]; 76 int i = 0; 77 while (i < a.length) { 78 a[i] = new Integer(i); 79 ++i; 80 } 81 System.arraycopy(a, 0, b, 0, a.length); 82 for (i = 0; i < a.length; i++) 83 assertTrue("Copied elements incorrectly", a[i].equals(b[i])); 84 85 /* Non primitive array types don't need to be identical */ 86 String[] source1 = new String[] { "element1" }; 87 Object[] dest1 = new Object[1]; 88 System.arraycopy(source1, 0, dest1, 0, dest1.length); 89 assertTrue("Invalid copy 1", dest1[0] == source1[0]); 90 91 char[][] source = new char[][] { { 'H', 'e', 'l', 'l', 'o' }, 92 { 'W', 'o', 'r', 'l', 'd' } }; 93 char[][] dest = new char[2][]; 94 System.arraycopy(source, 0, dest, 0, dest.length); 95 assertTrue("Invalid copy 2", dest[0] == source[0] 96 && dest[1] == source[1]); 97 } 98 99 /** 100 * 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 * 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 * 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 129 // Ensure spec'ed properties are non-null. See System.getProperties() 130 // spec. 131 String[] props = { "java.version", "java.vendor", "java.vendor.url", 132 "java.home", "java.vm.specification.version", 133 "java.vm.specification.vendor", "java.vm.specification.name", 134 "java.vm.version", "java.vm.vendor", "java.vm.name", 135 "java.specification.name", "java.specification.vendor", 136 "java.specification.name", "java.class.version", 137 "java.class.path", "java.ext.dirs", "os.name", "os.arch", 138 "os.version", "file.separator", "path.separator", 139 "line.separator", "user.name", "user.home", "user.dir", }; 140 for (int i = 0; i < props.length; i++) { 141 assertNotNull(props[i], System.getProperty(props[i])); 142 } 143 } 144 145 /** 146 * java.lang.System#getProperty(java.lang.String) 147 */ 148 public void test_getPropertyLjava_lang_String() { 149 // Test for method java.lang.String 150 // java.lang.System.getProperty(java.lang.String) 151 152 boolean is8859_1 = true; 153 String encoding = System.getProperty("file.encoding"); 154 byte[] bytes = new byte[128]; 155 char[] chars = new char[128]; 156 for (int i = 0; i < bytes.length; i++) { 157 bytes[i] = (byte) (i + 128); 158 chars[i] = (char) (i + 128); 159 } 160 String charResult = new String(bytes); 161 byte[] byteResult = new String(chars).getBytes(); 162 if (charResult.length() == 128 && byteResult.length == 128) { 163 for (int i = 0; i < bytes.length; i++) { 164 if (charResult.charAt(i) != (char) (i + 128) 165 || byteResult[i] != (byte) (i + 128)) 166 is8859_1 = false; 167 } 168 } else 169 is8859_1 = false; 170 String[] possibles = new String[] { "ISO8859_1", "8859_1", "ISO8859-1", 171 "ISO-8859-1", "ISO_8859-1", "ISO_8859-1:1978", "ISO-IR-100", 172 "LATIN1", "CSISOLATIN1" }; 173 boolean found8859_1 = false; 174 for (int i = 0; i < possibles.length; i++) { 175 if (possibles[i].equals(encoding)) { 176 found8859_1 = true; 177 break; 178 } 179 } 180 assertTrue("Wrong encoding: " + encoding, !is8859_1 || found8859_1); 181 } 182 183 /** 184 * java.lang.System#getProperty(java.lang.String) 185 * Tests that there are no extra path separator in boot class path. 186 * Regression test for HARMONY-3298 187 */ 188 public void test_getProperty_bootClassPath() { 189 String bootClassPath = System.getProperty("org.apache.harmony.boot.class.path"); 190 191 if (bootClassPath == null) { 192 bootClassPath = System.getProperty("sun.boot.class.path"); 193 } 194 195 if (bootClassPath != null 196 && (bootClassPath.indexOf(File.pathSeparator + File.pathSeparator) >= 0)) { 197 fail("Boot class path contains extra path separator: " + bootClassPath); 198 } 199 } 200 201 /** 202 * java.lang.System#getProperty(java.lang.String, java.lang.String) 203 */ 204 public void test_getPropertyLjava_lang_StringLjava_lang_String() { 205 // Test for method java.lang.String 206 // java.lang.System.getProperty(java.lang.String, java.lang.String) 207 assertTrue(!System.getProperty("java.version", "99999").equals("99999")); 208 assertEquals("Failed to return correct property value", "bogus", System 209 .getProperty("bogus.prop", "bogus")); 210 } 211 212 /** 213 * java.lang.System#setProperty(java.lang.String, java.lang.String) 214 */ 215 public void test_setPropertyLjava_lang_StringLjava_lang_String() { 216 // Test for method java.lang.String 217 // java.lang.System.setProperty(java.lang.String, java.lang.String) 218 219 assertNull("Failed to return null", System.setProperty("testing", 220 "value1")); 221 assertTrue("Failed to return old value", System.setProperty("testing", 222 "value2") == "value1"); 223 assertTrue("Failed to find value", 224 System.getProperty("testing") == "value2"); 225 226 boolean exception = false; 227 try { 228 System.setProperty("", "default"); 229 } catch (IllegalArgumentException e) { 230 exception = true; 231 } 232 assertTrue("Expected IllegalArgumentException", exception); 233 } 234 235 /** 236 * java.lang.System#getSecurityManager() 237 */ 238 public void test_getSecurityManager() { 239 // Test for method java.lang.SecurityManager 240 // java.lang.System.getSecurityManager() 241 assertNull("Returned incorrect SecurityManager", System 242 .getSecurityManager()); 243 } 244 245 /** 246 * java.lang.System#identityHashCode(java.lang.Object) 247 */ 248 public void test_identityHashCodeLjava_lang_Object() { 249 // Test for method int 250 // java.lang.System.identityHashCode(java.lang.Object) 251 Object o = new Object(); 252 String s = "Gabba"; 253 assertEquals("Nonzero returned for null", 254 0, System.identityHashCode(null)); 255 assertTrue("Nonequal has returned for Object", System 256 .identityHashCode(o) == o.hashCode()); 257 assertTrue("Same as usual hash returned for String", System 258 .identityHashCode(s) != s.hashCode()); 259 } 260 261 /** 262 * java.lang.System#runFinalization() 263 */ 264 public void test_runFinalization() { 265 // Test for method void java.lang.System.runFinalization() 266 267 flag = true; 268 createInstance(); 269 int count = 10; 270 // the gc below likely bogosifies the test, but will have to do for 271 // the moment 272 while (!ranFinalize && count-- > 0) { 273 System.gc(); 274 System.runFinalization(); 275 } 276 assertTrue("Failed to run finalization", ranFinalize); 277 } 278 279 /** 280 * java.lang.System#runFinalizersOnExit(boolean) 281 */ 282 @SuppressWarnings("deprecation") 283 public void test_runFinalizersOnExitZ() { 284 // Can we call the method at least? 285 System.runFinalizersOnExit(false); 286 } 287 288 /** 289 * java.lang.System#setProperties(java.util.Properties) 290 */ 291 public void test_setPropertiesLjava_util_Properties() { 292 // Test for method void 293 // java.lang.System.setProperties(java.util.Properties) 294 295 Properties orgProps = System.getProperties(); 296 java.util.Properties tProps = new java.util.Properties(); 297 tProps.put("test.prop", "this is a test property"); 298 tProps.put("bogus.prop", "bogus"); 299 System.setProperties(tProps); 300 try { 301 assertEquals("Failed to set properties", "this is a test property", System.getProperties() 302 .getProperty("test.prop")); 303 } finally { 304 // restore the original properties 305 System.setProperties(orgProps); 306 } 307 } 308 309 //Regression Test for Harmony-2356 310 public void testEnvUnmodifiable() { 311 Map map = System.getenv(); 312 try { 313 map.containsKey(null); 314 fail("Should throw NullPointerExcepiton."); 315 } catch (NullPointerException e) { 316 // expected 317 } 318 319 try { 320 map.containsKey(new Integer(10)); 321 fail("Should throw ClassCastException."); 322 } catch (ClassCastException e) { 323 // expected 324 } 325 326 try { 327 map.containsValue(null); 328 fail("Should throw NullPointerExcepiton."); 329 } catch (NullPointerException e) { 330 // expected 331 } 332 333 try { 334 map.containsValue(new Integer(10)); 335 fail("Should throw ClassCastException."); 336 } catch (ClassCastException e) { 337 // expected 338 } 339 340 try { 341 map.get(null); 342 fail("Should throw NullPointerExcepiton."); 343 } catch (NullPointerException e) { 344 // expected 345 } 346 347 try { 348 map.get(new Integer(10)); 349 fail("Should throw ClassCastException."); 350 } catch (ClassCastException e) { 351 // expected 352 } 353 354 try { 355 map.put(null, "AAA"); 356 fail("Should throw UnsupportedOperationExcepiton."); 357 } catch (UnsupportedOperationException e) { 358 // expected 359 } 360 361 try { 362 map.put("AAA", new Integer(10)); 363 fail("Should throw UnsupportedOperationException."); 364 } catch (UnsupportedOperationException e) { 365 // expected 366 } 367 368 try { 369 map.put("AAA", "BBB"); 370 fail("Should throw UnsupportedOperationException."); 371 } catch (UnsupportedOperationException e) { 372 // expected 373 } 374 375 try { 376 map.clear(); 377 fail("Should throw UnsupportedOperationException."); 378 } catch (UnsupportedOperationException e) { 379 // expected 380 } 381 382 try { 383 map.remove(null); 384 // Android isn't as strict about requiring this exception; no modification takes place anyway 385 // fail("Should throw UnsupportedOperationException."); 386 } catch (UnsupportedOperationException expected) { 387 } 388 389 } 390 391 @Override 392 protected void setUp() { 393 flag = false; 394 ranFinalize = false; 395 } 396 397 protected SystemTest createInstance() { 398 return new SystemTest("FT"); 399 } 400 401 @Override 402 protected void finalize() { 403 if (flag) 404 ranFinalize = true; 405 } 406 407 public SystemTest() { 408 } 409 410 public SystemTest(String name) { 411 super(name); 412 } 413} 414