01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
18: /**
19: * @author Roman S. Bushmanov
20: * @version $Revision$
21: */package java.lang;
22:
23: import java.util.Map;
24:
25: import junit.framework.TestCase;
26:
27: public class SystemTest extends TestCase {
28:
29: /**
30: * Test for String getenv(String)
31: */
32: public void testGetenvString() {
33: assertNull("_NONEXISTENT_ENVIRONMENT_VARIABLE_", System
34: .getenv("_NONEXISTENT_ENVIRONMENT_VARIABLE_"));
35: try {
36: System.getenv(null);
37: fail("NPE is expected");
38: } catch (NullPointerException ok) {
39: }
40: }
41:
42: /**
43: * test for Map getenv()
44: */
45: public void testGetenv() {
46: Map<String, String> env = System.getenv();
47: for (String key : env.keySet()) {
48: assertEquals("Value of " + key, env.get(key), System
49: .getenv(key));
50: }
51: }
52:
53: public void testNanoTime() {
54: long start = System.nanoTime();
55: try {
56: Thread.sleep(100);
57: } catch (InterruptedException e) {
58: fail("Was interruptred");
59: }
60: long duration = System.nanoTime() - start;
61: assertTrue("Elapsed time should be >0", duration > 0);
62: }
63:
64: public void testNanoTime1() {
65: long startNano = System.nanoTime();
66: long startMilli = System.currentTimeMillis();
67: try {
68: Thread.sleep(1000);
69: } catch (InterruptedException e) {
70: fail("Was interruptred");
71: }
72: long durationNano = System.nanoTime() - startNano;
73: long durationMillis = System.currentTimeMillis() - startMilli;
74: assertTrue("Bad accuracy!", Math.abs(durationNano / 1000000
75: - durationMillis) < 50);
76: }
77:
78: public void testClearProperty() {
79: String name = "user.name";
80: String initialValue = System.getProperty(name);
81: String newValue = "Baba Yaga";
82: System.setProperty(name, newValue);
83: assertEquals("incorrect value", newValue, System
84: .getProperty(name));
85: System.clearProperty(name);
86: assertNull("user.name should not be null", System
87: .getProperty(name));
88: System.setProperties(null);
89: assertEquals("user.name has not been restored", initialValue,
90: System.getProperty(name));
91: }
92: }
|