< prev index next >

test/jdk/java/lang/System/PropertyTest.java

Print this page
rev 52426 : 8185496: Improve performance of system properties initialization in initPhase1
8213424: VersionProps duplicate and skipped initialization


   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 /*
  25  * @test
  26  * @bug 4463345
  27  * @summary Simple test of System.clearProperty
  28  * @run main/othervm ClearProperty

  29  */
  30 
  31 public class ClearProperty {
  32     public static void main(String [] argv) throws Exception {
  33         clearTest();
  34         paramTest();































  35     }
  36 
  37     static void clearTest() throws Exception {

  38         System.setProperty("blah", "blech");
  39         if (!System.getProperty("blah").equals("blech"))
  40             throw new RuntimeException("Clear test failed 1");
  41         System.clearProperty("blah");
  42         if (System.getProperty("blah") != null)
  43             throw new RuntimeException("Clear test failed 2");


  44     }















  45 
  46     static void paramTest() throws Exception {
  47         try {
  48             System.clearProperty(null);
  49             throw new RuntimeException("Param test failed");
  50         } catch (NullPointerException npe) {
  51             // Correct result
  52         }
  53         try {
  54             System.clearProperty("");
  55             throw new RuntimeException("Param test failed");





































  56         } catch (IllegalArgumentException iae) {
  57             // Correct result
  58         }

































































































  59     }
  60 }


   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.lang.System;
  25 import java.util.Arrays;
  26 import java.util.List;
  27 import java.util.Objects;
  28 import java.util.Properties;
  29 import java.util.stream.Collectors;
  30 
  31 import org.testng.Assert;
  32 import org.testng.IMethodInstance;
  33 import org.testng.IMethodInterceptor;
  34 import org.testng.TestListenerAdapter;
  35 import org.testng.TestNG;
  36 import org.testng.annotations.BeforeTest;
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Test;
  39 
  40 
  41 /*
  42  * @test
  43  * @bug 4463345 4244670 8030781
  44  * @summary Simple test of System getProperty, setProperty, clearProperty,
  45  *      getProperties, and setProperties
  46  * @run testng/othervm PropertyTest
  47  */
  48 
  49 @Test
  50 public class PropertyTest {
  51 
  52     @DataProvider(name = "requiredProperties")
  53     static Object[][] requiredProperties() {
  54         return new Object[][]{
  55                 {"java.version"},
  56                 {"java.version.date"},
  57                 {"java.vendor"},
  58                 {"java.vendor.url"},
  59                 {"java.home"},
  60                 {"java.vm.specification.version"},
  61                 {"java.vm.specification.vendor"},
  62                 {"java.vm.specification.name"},
  63                 {"java.vm.version"},
  64                 {"java.vm.vendor"},
  65                 {"java.vm.name"},
  66                 {"java.specification.version"},
  67                 {"java.specification.vendor"},
  68                 {"java.specification.name"},
  69                 {"java.class.version"},
  70                 {"java.class.path"},
  71                 {"java.library.path"},
  72                 {"java.io.tmpdir"},
  73                 {"os.arch"},
  74                 {"os.version"},
  75                 {"file.separator"},
  76                 {"path.separator"},
  77                 {"line.separator"},
  78                 {"user.name"},
  79                 {"user.home"},
  80                 {"user.dir"},
  81                 {"java.runtime.version"},
  82                 {"java.runtime.name"},
  83         };
  84     }
  85 
  86     @Test
  87     static void getTest() {
  88         System.setProperty("blah", "blech");
  89         Assert.assertEquals(System.getProperty("blah"), "blech");
  90 
  91         try {
  92             System.getProperty(null);
  93             Assert.fail("Failed: expected NullPointerException");
  94         } catch (NullPointerException npe) {
  95             // Correct result
  96         }
  97         try {
  98             System.getProperty("");
  99             Assert.fail("Failed: expected IllegalArgumentException");
 100         } catch (IllegalArgumentException iae) {
 101             // Correct result
 102         }
 103     }
 104 
 105     @Test
 106     static void clearTest() {
 107         System.setProperty("blah", "blech");
 108         Assert.assertEquals(System.getProperty("blah"), "blech");
 109 
 110         System.clearProperty("blah");
 111         Assert.assertNull(System.getProperty("blah"));
 112 

 113         try {
 114             System.clearProperty(null);
 115             Assert.fail("Failed: expected NullPointerException");
 116         } catch (NullPointerException npe) {
 117             // Correct result
 118         }
 119         try {
 120             System.clearProperty("");
 121             Assert.fail("Failed: expected IllegalArgumentException");
 122         } catch (IllegalArgumentException iae) {
 123             // Correct result
 124         }
 125     }
 126 
 127     @Test
 128     static void setTest() {
 129         System.setProperty("blah", "blech");
 130         Assert.assertEquals(System.getProperty("blah"), "blech");
 131 
 132         System.setProperty("blah", "");
 133         Assert.assertEquals(System.getProperty("blah"), "");
 134 
 135         try {
 136             System.setProperty(null, null);
 137             Assert.fail("Failed: expected NullPointerException");
 138         } catch (NullPointerException npe) {
 139             // Correct result
 140         }
 141 
 142         try {
 143             System.setProperty("blah", null);
 144             Assert.fail("Failed: expected NullPointerException");
 145         } catch (NullPointerException npe) {
 146             // Correct result
 147         }
 148 
 149         try {
 150             System.setProperty(null, "blech");
 151             Assert.fail("Failed: expected NullPointerException");
 152         } catch (NullPointerException npe) {
 153             // Correct result
 154         }
 155 
 156         try {
 157             System.setProperty("", "blech");
 158             Assert.fail("Failed: expected IllegalArgumentException");
 159         } catch (IllegalArgumentException iae) {
 160             // Correct result
 161         }
 162         try {
 163             System.setProperty("", "");
 164             Assert.fail("Failed: expected IllegalArgumentException");
 165         } catch (IllegalArgumentException iae) {
 166             // Correct result
 167         }
 168     }
 169 
 170     @Test
 171     static void replaceSetProperties() {
 172         Properties oldProps = System.getProperties();
 173         Properties newProps = new Properties();
 174         oldProps.forEach( (k,v) -> newProps.put(k,v));
 175         System.setProperties(newProps);
 176 
 177         Assert.assertSame(System.getProperties(), newProps,
 178                 "getProperties not the same as setProperties");
 179 
 180         final String KEY = "blah";
 181         final String VALUE = "blech";
 182 
 183         // Set via Property instance; get via System methods
 184         newProps.setProperty(KEY, VALUE);
 185         Assert.assertEquals(System.getProperty(KEY), VALUE, KEY);
 186 
 187         String s = (String)newProps.remove(KEY);
 188         Assert.assertEquals(s, VALUE);
 189         Assert.assertNull(System.getProperty(KEY), KEY);
 190 
 191         // Set via System methods; Get via Property instance;
 192         System.setProperty(KEY, VALUE);
 193         Assert.assertEquals(newProps.getProperty(KEY), VALUE);
 194 
 195         String t = System.clearProperty(KEY);
 196         Assert.assertEquals(t, VALUE, KEY);
 197         Assert.assertNull(newProps.getProperty(KEY), KEY);
 198     }
 199 
 200     @Test
 201     static void setNullProperties() {
 202         Properties oldProps = System.getProperties();
 203         Properties savedProps = new Properties();
 204         oldProps.forEach((k,v) -> {
 205             if (v == null) {
 206                 throw new RuntimeException("null value, key: " + k);
 207             }
 208             savedProps.put(k,v);
 209         });
 210 
 211         // Re-initialize properties
 212         System.setProperties(null);
 213 
 214         Properties newProps = System.getProperties();
 215         Object[][] propnames = requiredProperties();
 216         for (Object[] p : propnames) {
 217             String name = (String)p[0];
 218             Assert.assertEquals(System.getProperty(name), savedProps.getProperty(name), name);
 219 
 220             Assert.assertEquals(newProps.getProperty(name), savedProps.getProperty(name), name);
 221         }
 222     }
 223 
 224     // Verify all the required properties have values from System.getProperty and
 225     // System.getProperties()
 226     @Test(dataProvider = "requiredProperties")
 227     static void checkRequiredProperties(String name) {
 228         Assert.assertNotNull(System.getProperty(name), name);
 229 
 230         Properties props = System.getProperties();
 231         Assert.assertNotNull(props.getProperty(name), name);
 232     }
 233 
 234     @SuppressWarnings("raw_types")
 235     @Test(enabled=false)
 236     public static void main(String[] args) {
 237         TestListenerAdapter tla = new TestListenerAdapter();
 238 
 239         Class<?>[] testclass = {PropertyTest.class};
 240         TestNG testng = new TestNG();
 241         testng.setTestClasses(testclass);
 242         testng.addListener(tla);
 243         if (args.length > 0) {
 244             IMethodInterceptor intercept = (m, c) -> {
 245                 List<IMethodInstance> x = m.stream()
 246                         .filter(m1 -> m1.getMethod().getMethodName().contains(args[0]))
 247                         .collect(Collectors.toList());
 248                 return x;
 249             };
 250             testng.setMethodInterceptor(intercept);
 251         }
 252         testng.run();
 253         tla.getPassedTests()
 254                 .stream().forEach(t -> System.out.printf("Passed: %s%s%n", t.getName(),
 255                 List.of(t.getParameters())));
 256         tla.getFailedTests()
 257                 .stream().forEach(t -> System.out.printf("Failed: %s%s%n", t.getName(),
 258                 List.of(t.getParameters())));
 259     }
 260 }
< prev index next >