Path: blob/master/test/jdk/java/lang/System/PropertyTest.java
41149 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.System;24import java.util.Arrays;25import java.util.List;26import java.util.Objects;27import java.util.Properties;28import java.util.stream.Collectors;2930import org.testng.Assert;31import org.testng.IMethodInstance;32import org.testng.IMethodInterceptor;33import org.testng.TestListenerAdapter;34import org.testng.TestNG;35import org.testng.annotations.BeforeTest;36import org.testng.annotations.DataProvider;37import org.testng.annotations.Test;383940/*41* @test42* @bug 4463345 4244670 8030781 826598943* @summary Simple test of System getProperty, setProperty, clearProperty,44* getProperties, and setProperties45* @run testng/othervm PropertyTest46*/4748@Test49public class PropertyTest {5051@DataProvider(name = "requiredProperties")52static Object[][] requiredProperties() {53return new Object[][]{54{"java.version"},55{"java.version.date"},56{"java.vendor"},57{"java.vendor.url"},58{"java.home"},59{"java.vm.specification.version"},60{"java.vm.specification.vendor"},61{"java.vm.specification.name"},62{"java.vm.version"},63{"java.vm.vendor"},64{"java.vm.name"},65{"java.specification.version"},66{"java.specification.vendor"},67{"java.specification.name"},68{"java.class.version"},69{"java.class.path"},70{"java.library.path"},71{"java.io.tmpdir"},72{"os.arch"},73{"os.version"},74{"file.separator"},75{"path.separator"},76{"line.separator"},77{"user.name"},78{"user.home"},79{"user.dir"},80{"java.runtime.version"},81{"java.runtime.name"},82{"native.encoding"},83};84}8586@Test87static void getTest() {88System.setProperty("blah", "blech");89Assert.assertEquals(System.getProperty("blah"), "blech");9091try {92System.getProperty(null);93Assert.fail("Failed: expected NullPointerException");94} catch (NullPointerException npe) {95// Correct result96}97try {98System.getProperty("");99Assert.fail("Failed: expected IllegalArgumentException");100} catch (IllegalArgumentException iae) {101// Correct result102}103}104105@Test106static void clearTest() {107System.setProperty("blah", "blech");108Assert.assertEquals(System.getProperty("blah"), "blech");109110System.clearProperty("blah");111Assert.assertNull(System.getProperty("blah"));112113try {114System.clearProperty(null);115Assert.fail("Failed: expected NullPointerException");116} catch (NullPointerException npe) {117// Correct result118}119try {120System.clearProperty("");121Assert.fail("Failed: expected IllegalArgumentException");122} catch (IllegalArgumentException iae) {123// Correct result124}125}126127@Test128static void setTest() {129System.setProperty("blah", "blech");130Assert.assertEquals(System.getProperty("blah"), "blech");131132System.setProperty("blah", "");133Assert.assertEquals(System.getProperty("blah"), "");134135try {136System.setProperty(null, null);137Assert.fail("Failed: expected NullPointerException");138} catch (NullPointerException npe) {139// Correct result140}141142try {143System.setProperty("blah", null);144Assert.fail("Failed: expected NullPointerException");145} catch (NullPointerException npe) {146// Correct result147}148149try {150System.setProperty(null, "blech");151Assert.fail("Failed: expected NullPointerException");152} catch (NullPointerException npe) {153// Correct result154}155156try {157System.setProperty("", "blech");158Assert.fail("Failed: expected IllegalArgumentException");159} catch (IllegalArgumentException iae) {160// Correct result161}162try {163System.setProperty("", "");164Assert.fail("Failed: expected IllegalArgumentException");165} catch (IllegalArgumentException iae) {166// Correct result167}168}169170@Test171static void replaceSetProperties() {172Properties oldProps = System.getProperties();173Properties newProps = new Properties();174oldProps.forEach( (k,v) -> newProps.put(k,v));175System.setProperties(newProps);176177Assert.assertSame(System.getProperties(), newProps,178"getProperties not the same as setProperties");179180final String KEY = "blah";181final String VALUE = "blech";182183// Set via Property instance; get via System methods184newProps.setProperty(KEY, VALUE);185Assert.assertEquals(System.getProperty(KEY), VALUE, KEY);186187String s = (String)newProps.remove(KEY);188Assert.assertEquals(s, VALUE);189Assert.assertNull(System.getProperty(KEY), KEY);190191// Set via System methods; Get via Property instance;192System.setProperty(KEY, VALUE);193Assert.assertEquals(newProps.getProperty(KEY), VALUE);194195String t = System.clearProperty(KEY);196Assert.assertEquals(t, VALUE, KEY);197Assert.assertNull(newProps.getProperty(KEY), KEY);198}199200@Test201static void setNullProperties() {202Properties oldProps = System.getProperties();203Properties savedProps = new Properties();204oldProps.forEach((k,v) -> {205if (v == null) {206throw new RuntimeException("null value, key: " + k);207}208savedProps.put(k,v);209});210211// Re-initialize properties212System.setProperties(null);213214Properties newProps = System.getProperties();215Object[][] propnames = requiredProperties();216for (Object[] p : propnames) {217String name = (String)p[0];218Assert.assertEquals(System.getProperty(name), savedProps.getProperty(name), name);219220Assert.assertEquals(newProps.getProperty(name), savedProps.getProperty(name), name);221}222}223224// Verify all the required properties have values from System.getProperty and225// System.getProperties()226@Test(dataProvider = "requiredProperties")227static void checkRequiredProperties(String name) {228Assert.assertNotNull(System.getProperty(name), name);229230Properties props = System.getProperties();231Assert.assertNotNull(props.getProperty(name), name);232}233234@SuppressWarnings("raw_types")235@Test(enabled=false)236public static void main(String[] args) {237TestListenerAdapter tla = new TestListenerAdapter();238239Class<?>[] testclass = {PropertyTest.class};240TestNG testng = new TestNG();241testng.setTestClasses(testclass);242testng.addListener(tla);243if (args.length > 0) {244IMethodInterceptor intercept = (m, c) -> {245List<IMethodInstance> x = m.stream()246.filter(m1 -> m1.getMethod().getMethodName().contains(args[0]))247.collect(Collectors.toList());248return x;249};250testng.setMethodInterceptor(intercept);251}252testng.run();253tla.getPassedTests()254.stream().forEach(t -> System.out.printf("Passed: %s%s%n", t.getName(),255List.of(t.getParameters())));256tla.getFailedTests()257.stream().forEach(t -> System.out.printf("Failed: %s%s%n", t.getName(),258List.of(t.getParameters())));259}260}261262263