一次System.setProperties踩过的坑

2019-12-03  本文已影响0人  皇甫立波

importjava.util.HashMap;

importjava.util.Map;

importjava.util.Properties;

/**

*@ClassNamePropUtilsTest

*@DescriptionTODO

*@Date2019-11-25 15:11

*@Version1.0

**/

publicclassPropUtilsTest{

@BeforeMethod

publicvoidbefore() {

Mapproperties=newHashMap();

PropertiesinitProps=newProperties();

initProps.setProperty("a","a");

initProps.setProperty("off","off");

initProps.setProperty("0","0");

initProps.setProperty("false","false");

initProps.setProperty("no","no");

initProps.setProperty("on","on");

initProps.setProperty("1","1");

initProps.setProperty("true","true");

initProps.setProperty("yes","yes");

System.setProperties(initProps);    // 这里把会把System里的JAVA_HOME给冲掉了

//Now load in any of the System properties.

//We do this after loading the config file so that we can override

//items via the command line

finalPropertiesprops=System.getProperties();

for(finalObjectelement:props.keySet()) {

finalStringkey=(String) element;

properties.put(key, props.getProperty(key));

        }

    }

@Test

publicvoidTestGetBoolean() {

Assert.assertFalse(PropUtils.getBoolean("off",false));

Assert.assertFalse(PropUtils.getBoolean("0",false));

Assert.assertFalse(PropUtils.getBoolean("false",false));

Assert.assertFalse(PropUtils.getBoolean("no",false));

Assert.assertTrue(PropUtils.getBoolean("on",false));

Assert.assertTrue(PropUtils.getBoolean("1",false));

Assert.assertTrue(PropUtils.getBoolean("true",false));

Assert.assertTrue(PropUtils.getBoolean("yes",false));

Assert.assertFalse(PropUtils.getBoolean("b",false));

    }

@Test

publicvoidTestGetString() {

Assert.assertEquals(PropUtils.getString("a","a"),"a");

Assert.assertEquals(PropUtils.getString("b","a"),"a");

Assert.assertEquals(PropUtils.getString("a"),"a");

    }

}

上一篇下一篇

猜你喜欢

热点阅读