zoukankan      html  css  js  c++  java
  • Java学习-020-Properties 判断是否存在对应的 key 项

    在日常的脚本编写过程中,通常会判断配置文件中是否存在对应的配置项,以判断是否执行相应的业务逻辑。

    小二上码。。。若有不足之处,敬请大神指正,不胜感激!

    判断是否存在 key 项(配置项)的方法源码如下所示:

     1     /**
     2      * Verify the key contains in properties or not
     3      * 
     4      * @author Aaron.ffp
     5      * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java propertiesKeyIsExist, 2014-11-20 16:31:10 Exp $
     6      * 
     7      * @param prop : properties
     8      * @param key  : key
     9      * @return boolean
    10      */
    11     public boolean propertiesKeyIsExist(Properties prop, String key){
    12         boolean success = false;
    13         String item = "";
    14         
    15         // verify the properties file is null
    16         if (prop == null) {
    17             this.message = "The content of properties file is null !";
    18             this.logger.error(this.message);
    19             
    20             success = false;
    21             return success;
    22         }
    23         
    24         // verify the key is null
    25         if ("".equals(key) || key == null) {
    26             this.message = "There is no key {" + key + "} in properties config file.";
    27             this.logger.error(this.message);
    28             
    29             success = false;
    30             return success;
    31         }
    32         
    33         // get keys from properties
    34         Enumeration<?> enu = prop.propertyNames();
    35 
    36         // verify the key is contains in properties or not
    37         while (enu.hasMoreElements()) {
    38             item = (String)enu.nextElement();
    39             
    40             if (item.equals(key)) {
    41                 success = true;
    42             }
    43         }
    44         
    45         return success;
    46     }
    判断 properties 配置文件是否存在相应的配置项源码

    测试源码如下所示:

     1     /**
     2      * Test : Verify the key contains in properties file or not
     3      * 
     4      * @author Aaron.ffp
     5      * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_propertiesKeyIsExist, 2014-11-20 16:35:15 Exp $
     6      *
     7      */
     8     public void test_propertiesKeyIsExist(){
     9         this.message = "
    
    
    TEST:FileUtils.propertiesKeyIsExist(Properties prop, String key)";
    10         this.logger.debug(this.message);
    11         
    12         this.fu = new FileUtils();
    13         String filename = this.constantslist.PROJECTHOME + this.constantslist.FILESEPARATOR + 
    14                           "testng-temp" + this.constantslist.FILESEPARATOR + "propertiesRead.properties";
    15         
    16         Properties prop = this.fu.propertiesRead(filename);
    17         
    18         // print-1
    19         prop.list(System.out);
    20         
    21         System.out.println("
    
    ");
    22         
    23         Assert.assertEquals(this.fu.propertiesKeyIsExist(prop, "host"), true, "Test case failed.");
    24     }
    测试源代码

     

    至此, Java学习-020-Properties 判断是否存在对应的 key 项 顺利完结,希望此文能够给初学 Java 的您一份参考。

    最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

  • 相关阅读:
    linux shell 获取文件夹全文绝对路径
    Selenium自动化获取WebSocket信息
    Automatically generating nice graphs at end of your Load Test with Apache JMeter and JMeter-Plugins
    卡夫卡与风暴:卡夫卡和风暴的明智比较
    Apache Kafka安全| Kafka的需求和组成部分
    Kafka Schema Registry | 学习Avro Schema
    QT 内部使用函数列表
    QT图标添加
    QT快捷键
    QT软件上架以及自启动
  • 原文地址:https://www.cnblogs.com/fengpingfan/p/4607904.html
Copyright © 2011-2022 走看看