1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.lang.System;
  26 import java.util.Arrays;
  27 import java.util.List;
  28 import java.util.Objects;
  29 import java.util.Properties;
  30 import java.util.TreeMap;
  31 import java.util.stream.Collectors;
  32 
  33 import org.testng.Assert;
  34 import org.testng.IMethodInstance;
  35 import org.testng.IMethodInterceptor;
  36 import org.testng.TestListenerAdapter;
  37 import org.testng.TestNG;
  38 import org.testng.annotations.BeforeTest;
  39 import org.testng.annotations.DataProvider;
  40 import org.testng.annotations.Test;
  41 
  42 
  43 /*
  44  * @test
  45  * @bug 4463345 4244670 8030781
  46  * @summary Simple test of System getProperty, setProperty, clearProperty,
  47  *      getProperties, and setProperties
  48  * @run testng/othervm PropertyTest
  49  */
  50 
  51 @Test
  52 public class PropertyTest {
  53 
  54     @DataProvider(name = "requiredProperties")
  55     static Object[][] requiredProperties() {
  56         return new Object[][]{
  57                 {"java.version"},
  58                 {"java.version.date"},
  59                 //                {"java.vendor"},
  60                 //                {"java.vendor.url"},
  61                 {"java.home"},
  62                 {"java.vm.specification.version"},
  63                 {"java.vm.specification.vendor"},
  64                 {"java.vm.specification.name"},
  65                 {"java.vm.version"},
  66                 {"java.vm.vendor"},
  67                 {"java.vm.name"},
  68                 {"java.specification.version"},
  69                 {"java.specification.vendor"},
  70                 {"java.specification.name"},
  71                 {"java.class.version"},
  72                 {"java.class.path"},
  73                 {"java.library.path"},
  74                 {"java.io.tmpdir"},
  75                 {"os.arch"},
  76                 {"os.version"},
  77                 {"file.separator"},
  78                 {"path.separator"},
  79                 {"line.separator"},
  80                 {"user.name"},
  81                 {"user.home"},
  82                 {"user.dir"},
  83                 {"java.runtime.version"},
  84                 {"java.runtime.name"},
  85         };
  86     }
  87 
  88     @Test
  89     static void getTest() {
  90         System.setProperty("blah", "blech");
  91         Assert.assertEquals(System.getProperty("blah"), "blech");
  92 
  93         try {
  94             System.getProperty(null);
  95             Assert.fail("Failed: expected NullPointerException");
  96         } catch (NullPointerException npe) {
  97             // Correct result
  98         }
  99         try {
 100             System.getProperty("");
 101             Assert.fail("Failed: expected IllegalArgumentException");
 102         } catch (IllegalArgumentException iae) {
 103             // Correct result
 104         }
 105     }
 106 
 107     @Test
 108     static void clearTest() {
 109         System.setProperty("blah", "blech");
 110         Assert.assertEquals(System.getProperty("blah"), "blech");
 111 
 112         System.clearProperty("blah");
 113         Assert.assertNull(System.getProperty("blah"));
 114 
 115         try {
 116             System.clearProperty(null);
 117             Assert.fail("Failed: expected NullPointerException");
 118         } catch (NullPointerException npe) {
 119             // Correct result
 120         }
 121         try {
 122             System.clearProperty("");
 123             Assert.fail("Failed: expected IllegalArgumentException");
 124         } catch (IllegalArgumentException iae) {
 125             // Correct result
 126         }
 127     }
 128 
 129     @Test
 130     static void setTest() {
 131         System.setProperty("blah", "blech");
 132         Assert.assertEquals(System.getProperty("blah"), "blech");
 133 
 134         System.setProperty("blah", "");
 135         Assert.assertEquals(System.getProperty("blah"), "");
 136 
 137         try {
 138             System.setProperty(null, null);
 139             Assert.fail("Failed: expected NullPointerException");
 140         } catch (NullPointerException npe) {
 141             // Correct result
 142         }
 143 
 144         try {
 145             System.setProperty("blah", null);
 146             Assert.fail("Failed: expected NullPointerException");
 147         } catch (NullPointerException npe) {
 148             // Correct result
 149         }
 150 
 151         try {
 152             System.setProperty(null, "blech");
 153             Assert.fail("Failed: expected NullPointerException");
 154         } catch (NullPointerException npe) {
 155             // Correct result
 156         }
 157 
 158         try {
 159             System.setProperty("", "blech");
 160             Assert.fail("Failed: expected IllegalArgumentException");
 161         } catch (IllegalArgumentException iae) {
 162             // Correct result
 163         }
 164         try {
 165             System.setProperty("", "");
 166             Assert.fail("Failed: expected IllegalArgumentException");
 167         } catch (IllegalArgumentException iae) {
 168             // Correct result
 169         }
 170     }
 171 
 172     @Test
 173     static void replaceSetProperties() {
 174         Properties oldProps = System.getProperties();
 175         Properties newProps = new Properties();
 176         oldProps.forEach( (k,v) -> newProps.put(k,v));
 177         System.setProperties(newProps);
 178 
 179         Assert.assertSame(System.getProperties(), newProps,
 180                 "getProperties not the same as setProperties");
 181 
 182         final String KEY = "blah";
 183         final String VALUE = "blech";
 184 
 185         // Set via Property instance; get via System methods
 186         newProps.setProperty(KEY, VALUE);
 187         Assert.assertEquals(System.getProperty(KEY), VALUE, KEY);
 188 
 189         String s = (String)newProps.remove(KEY);
 190         Assert.assertEquals(s, VALUE);
 191         Assert.assertNull(System.getProperty(KEY), KEY);
 192 
 193         // Set via System methods; Get via Property instance;
 194         System.setProperty(KEY, VALUE);
 195         Assert.assertEquals(newProps.getProperty(KEY), VALUE);
 196 
 197         String t = System.clearProperty(KEY);
 198         Assert.assertEquals(t, VALUE, KEY);
 199         Assert.assertNull(newProps.getProperty(KEY), KEY);
 200     }
 201 
 202     @Test
 203     static void setNullProperties() {
 204         Properties oldProps = System.getProperties();
 205         Properties savedProps = new Properties();
 206         oldProps.forEach((k,v) -> {
 207             if (v == null) {
 208                 throw new RuntimeException("null value, key: " + k);
 209             }
 210             savedProps.put(k,v);
 211         });
 212 
 213         // Re-initialize properties
 214         System.setProperties(null);
 215 
 216         Properties newProps = System.getProperties();
 217         Object[][] propnames = requiredProperties();
 218         for (Object[] p : propnames) {
 219             String name = (String)p[0];
 220             Assert.assertEquals(System.getProperty(name), savedProps.getProperty(name), name);
 221 
 222             Assert.assertEquals(newProps.getProperty(name), savedProps.getProperty(name), name);
 223         }
 224     }
 225 
 226     // Verify all the required properties have values from System.getProperty and
 227     // System.getProperties()
 228     @Test(dataProvider = "requiredProperties")
 229     static void checkRequiredProperties(String name) {
 230         Assert.assertNotNull(System.getProperty(name), name);
 231 
 232         Properties props = System.getProperties();
 233         Assert.assertNotNull(props.getProperty(name), name);
 234     }
 235 
 236     @Test
 237     static void printAll() {
 238         TreeMap<Object, Object> tm = new TreeMap<>(System.getProperties());
 239         tm.forEach((k,v) -> System.out.println(k + ": " + v));
 240     }
 241 
 242     @SuppressWarnings("raw_types")
 243     @Test(enabled=false)
 244     public static void main(String[] args) {
 245         TestListenerAdapter tla = new TestListenerAdapter();
 246 
 247         Class<?>[] testclass = {PropertyTest.class};
 248         TestNG testng = new TestNG();
 249         testng.setTestClasses(testclass);
 250         testng.addListener(tla);
 251         if (args.length > 0) {
 252             IMethodInterceptor intercept = (m, c) -> {
 253                 List<IMethodInstance> x = m.stream()
 254                         .filter(m1 -> m1.getMethod().getMethodName().contains(args[0]))
 255                         .collect(Collectors.toList());
 256                 return x;
 257             };
 258             testng.setMethodInterceptor(intercept);
 259         }
 260         testng.run();
 261         tla.getPassedTests()
 262                 .stream().forEach(t -> System.out.printf("Passed: %s%s%n", t.getName(),
 263                 List.of(t.getParameters())));
 264         tla.getFailedTests()
 265                 .stream().forEach(t -> System.out.printf("Failed: %s%s%n", t.getName(),
 266                 List.of(t.getParameters())));
 267     }
 268 }