HttpClient + TestNG Interface Automatic Test fifth chapter

Source: Internet
Author: User

1. Implement read, write, and update operations on the Java configuration file properties

 PackageCom.config;/*** Implementation of Read, write, and update operations on Java configuration file properties*/   ImportJava.io.BufferedInputStream; ImportJava.io.BufferedReader;ImportJava.io.FileInputStream; Importjava.io.FileNotFoundException; ImportJava.io.FileOutputStream; Importjava.io.IOException; ImportJava.io.InputStream; ImportJava.io.InputStreamReader;ImportJava.io.OutputStream; Importjava.util.Properties; /**  * @author  * @version  */    Public classProperty {//path to the properties file    StaticString ProfilePath = System.getproperty ("User.dir") + "/web-inf/configuration.properties"; /*** Using static methods*/       Private StaticProperties props =NewProperties (); Static {           Try{InputStream in=NewFileInputStream (ProfilePath); BufferedReader BF=NewBufferedReader (NewInputStreamReader (In, "UTF-8"));         Props.load (BF); } Catch(FileNotFoundException e) {e.printstacktrace (); System.exit (-1); } Catch(IOException e) {system.exit (-1); }       }         /*** Read the value of the corresponding key in the properties file *@paramKey * PRIMARY KEY *@returnString*/        Public Staticstring Getkeyvalue (String key) {returnProps.getproperty (key); }         /*** Read the value of the primary key according to the primary key ' key '@paramFilePath Property File path *@paramKey Key Name*/        Public Staticstring ReadValue (String FilePath, String key) {Properties props=NewProperties (); Try{InputStream in=NewBufferedinputstream (NewFileInputStream (FilePath));               Props.load (in); String value=Props.getproperty (key); SYSTEM.OUT.PRINTLN (Key+ "The value of the key is:" +value); returnvalue; } Catch(Exception e) {e.printstacktrace (); return NULL; }       }             /*** UPDATE (or insert) a pair of properties information (primary key and its key value) * If the primary key already exists, update the value of the primary key; * If the primary key does not exist, the plug-in pair of key values. * @paramkeyname Key Name *@paramkeyvalue Key Value*/        Public Static voidwriteproperties (String keyname,string keyvalue) {Try {               //Call the Hashtable method put and use the GetProperty method to provide parallelism. //enforces the use of strings for the keys and values of the properties.   The return value is the result of the Hashtable call put. OutputStream fos =NewFileOutputStream (ProfilePath);               Props.setproperty (KeyName, keyvalue); //In a format that is appropriate for loading into the Properties table using the Load method,//writes the list of attributes (key and element pairs) in this properties table to the output streamProps.store (FOS, "Update" + KeyName + "' Value")); } Catch(IOException e) {System.err.println ("Property file Update Error"); }       }         /*** Update the properties file's key value pair * If the primary key already exists, update the value of the primary key; * If the primary key does not exist, the plug-in pair of key values. * @paramkeyname Key Name *@paramkeyvalue Key Value*/        Public voidupdateproperties (String keyname,string keyvalue) {Try{props.load (NewFileInputStream (ProfilePath)); //Call the Hashtable method put and use the GetProperty method to provide parallelism. //enforces the use of strings for the keys and values of the properties.   The return value is the result of the Hashtable call put. OutputStream fos =NewFileOutputStream (ProfilePath);               Props.setproperty (KeyName, keyvalue); //In a format that is appropriate for loading into the Properties table using the Load method,//writes the list of attributes (key and element pairs) in this properties table to the output streamProps.store (FOS, "Update" + KeyName + "' Value")); } Catch(IOException e) {System.err.println ("Property file Update Error"); }       }       //Test Code     Public Static voidMain (string[] args) {System.out.println (ReadValue (ProfilePath,"Test")); System.out.println (Getkeyvalue ("Test")); System.out.println (Getkeyvalue ("Zhongwen")); WriteProperties ("Mail_server_incoming", "[email protected]"); System.out.println ("Operation Complete"); }   }   

HttpClient + TestNG Interface Automatic Test fifth chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.