Thursday, October 28, 2010

Java - Enum Type Sample usage

Recently I got problem where i need to use, the type enum which i never used before,

The Problem: We have clustered DataBase Setup, So the Automation test should connect to the DataBase server based on some requirement. (Say it has an Activation pair, and DataStorage pairs.)

So here the typical way is we need to define the connection properties for each pair and connect then accordingly. But when we need to repeat this in a lot of tests, So here the use of the type enum comes.

Usage:

1. Define public enum DataBaseServer
2. Define the connection properties

      private String dbDriver ;
      private String dbServerUrl ;
      private String userName ;
      private String password ;
      private String port;

3. Now initialize the DataBaseServer based on the Type For example for a primary activation server.

PRIMARY_ACTIVATION_SERVER("Primary Registry Database Server"){

       public String getDbDriver() {
           return "PrimaryRegistryDBDriver";
       }
       public String getDbServerUrl() {
           return "PrimaryRegistryDBUrl";
       }
       public String getPort() {
           return "PrimaryRegistryDBPort";
       }
       public String getUserName() {
              return "PrimaryRegistryDBUsername";
       }
       public String getPassword() {
         return "PrimaryRegistryDBPassword";
       }
}, SECONDARY_ACTIVATION_SERVER("Primary Registry Database Server"){ //init code here, check the primary code},
PRIMARY_DATA_SERVER("Primary Registry Database Server"){ //init code here},
SECONDARY_DATA_SERVER("Primary Registry Database Server"){ //init code here} ;

4. In your data base connection call you can just specify the type like this

  DBConnector.connect(DataBaseServer.PRIMARY_DATA_SERVER);

note that the connect will accept the DataBaseServer Type and based on it will get the properties..

-------