jeudi 7 janvier 2016

Read keywords from file and create objects

Program will receive a file with keywords in each line to be executed as a part of test session. In below code snippet, I read each keyword from file sessionFile and store in a vector keywordList which is member of class KeywordManager. Each KeywordManager instance is also mapped to session ID unique to a test case ( series of keywords).

int SessionResource::InitialiseKeywords()
{
    int status = OK;
    int sID = 0;
    KeywordManager* kwm = new KeywordManager(this->sessionFile);
    kwm->ReadKeywordsFromSessionFile();

    sID = this->GetSessionId();
    this->keywordTable[sID] = kwm;

    return status;  
}

void KeywordManager::ReadKeywordsFromSessionFile()
{
     ifstream inSessionFile(this->sessionFileName);
     string line;

     while (getline(inSessionFile, line))
     {
         this->keywordList.push_back(line);
     }        
}

A keyword object of type cKeyWord is created for each keyword in vector. This keywordObj is then executed and result of execution decide as how many keywords (test steps) in a test session executed successfully.

As it can be seen below, code iterate over entire keyword list, create keyword objects and then call ExecuteKeywords.

int SessionResource::ExecuteKeywords()
{
    int status = OK;
    vector<string>::iterator itr;
    int sID = this->GetSessionId();

    vector<string>& keywordList = this->keywordTable[sID]->GetKeywords();

    for (itr = keywordList.begin(); itr < keywordList.end(); itr++)
    {
        KeywordManager* kwm = this->keywordTable[sID];                              // Create cKeyWord object for each keyword string 
        cKeyWord* keywordObj = kwm->CreateKeyword(*itr);                            // in vector obtained from session file

        status = SessionManager::Instance()->ExecuteKeywords(sID, keywordObj);      // Call ExecuteKeywords for each keyword object created above
        if (status)
        {
            throw invalid_argument("Error in processing keyword");              // Throw exception if execution of any keyword object fails
        }                                                                       
    }

    return status;
}

Above code read keywords from file, store in vector, create objects and execute all keywords in list. Can there be a better design to perform this task more efficiently ?

Many Thanks.

Aucun commentaire:

Enregistrer un commentaire