#include "ModeXMLParser.h"
using std::string;
+using std::set;
MODES_NAMESPACE_USE;
ModeManager::ModeManager()
{
- //directoryOfMode = MODES_MODE_DEFAULT_DIR;
- directoryOfMode = "/usr/share/modes/";
+ //string dirPath = MODES_MODE_DEFAULT_DIR;
+ string dirPath = "/usr/share/modes/";
- std::string xsdFile = directoryOfMode + "/" + ACTION_SCHEMA_XSD_PATH;
+ directoriesOfMode.insert(dirPath);
+
+ std::string xsdFile = dirPath + "/" + ACTION_SCHEMA_XSD_PATH;
if(!actionXsdFilePath.empty())
- xsdFile = directoryOfMode + "/" + getActionXsdFilePath();
+ xsdFile = dirPath + "/" + getActionXsdFilePath();
- schemaMgr.makeActionSchemaMap(directoryOfMode, xsdFile);
- makeModeMap(directoryOfMode);
+ schemaMgr.makeActionSchemaMap(dirPath, xsdFile);
+ makeModeMap(dirPath);
}
void ModeManager::loadPlugin()
return MODES_ERROR_NONE;
}
-void ModeManager::makeModeMap(const string &dirPath)
+bool ModeManager::makeModeMap(const string &dirPath)
{
DBG("dirPath : [%s]", dirPath.c_str());
DIR *dir;
struct dirent *entry;
- if ((dir = opendir(dirPath.c_str())) == NULL){
+
+ if ((dir = opendir(dirPath.c_str())) == NULL) {
ERR("%s dir open error! [%s]", dirPath.c_str(), strerror(errno));
- return;
+ return false;
}
while ((entry = readdir(dir)) != NULL) {
}
closedir(dir);
- return;
+ return true;
+}
+
+void ModeManager::addModeDirectory(const string &dirPath)
+{
+ set<string>::iterator result = directoriesOfMode.find(dirPath);
+ if (result != directoriesOfMode.end())
+ throw std::invalid_argument("Already registered directory (" + dirPath + ")");
+
+ if (!makeModeMap(dirPath))
+ throw std::invalid_argument("Inaccessible directory (" + dirPath + ")");
+
+ directoriesOfMode.insert(dirPath);
}
public:
ModeManager();
+ void addModeDirectory(const std::string &dirPath);
int applyMode(const std::string &modeName);
void setPluginDirPath(std::string pluginDirPath);
void setModeXMLDirPathList(std::set<std::string>list);
void setActionSchemaDirPath(std::string dirPath);
private:
void loadPlugin();
- void makeModeMap(const std::string &dirPath);
+ bool makeModeMap(const std::string &dirPath);
void addModeName(ModeParser *parser, std::string &path);
ModeParser* getParser(const std::string &path);
std::string getPluginDirPath();
std::string getActionXsdFilePath();
std::string getActionSchemaDirPath();
- std::string directoryOfMode;
+ std::set<std::string> directoriesOfMode;
+
std::map<std::string, std::string> modeMap;
ActionSchemaManager schemaMgr;
PluginManager pluginManager;
std::string actionSchemaDirPath;
#ifdef MDS_TEST
- friend class TestModeManager;
+ friend class ModeManagerTest;
#endif
};
#include <iostream>
+#include <exception>
#include <gtest/gtest.h>
#include "supervisor/ModeManager.h"
+
MODES_NAMESPACE_USE;
using namespace std;
MODES_NAMESPACE_BEGIN
-class TestModeManager {
+class ModeManagerTest{
public:
- string getDir(ModeManager& m);
- map<string, string> getModeMap(ModeManager& m);
+ set<string> getDirectories(ModeManager &m);
+ map<string, string> getModeMap(ModeManager &m);
+ static void printModeMap(map<string, string> &modemap);
};
MODES_NAMESPACE_END
-string TestModeManager::getDir(ModeManager& m)
+set<string> ModeManagerTest::getDirectories(ModeManager &m)
{
- return m.directoryOfMode;
+ return m.directoriesOfMode;
}
-map<string, string> TestModeManager::getModeMap(ModeManager& m)
+map<string, string> ModeManagerTest::getModeMap(ModeManager &m)
{
return m.modeMap;
}
-TEST(modemanager, getdefaultdir) {
- ModeManager mdMgr;
- TestModeManager mdmgrT;
+void ModeManagerTest::printModeMap(map<string, string> &modemap)
+{
+ if (!modemap.empty()) {
+ cout << "| "; cout.width(15); cout << "Mode Name" << " | " << "ModeXML file Path" << endl;
+
+ map<string, string>::iterator it;
+ for (it = modemap.begin(); it != modemap.end(); it++) {
+ cout << "| "; cout.width(15); cout << it->first.c_str() << " | " << it->second.c_str() << endl;
+ }
+ }
+}
+
+TEST(ModeManagerTest, test_getModemap) {
+ ModeManagerTest mdmgrtest;
+ ModeManager md_mgr;
+ md_mgr.addModeDirectory(".");
+
+ map<string, string> modemap = mdmgrtest.getModeMap(md_mgr);
+ EXPECT_FALSE(modemap.empty()) << "Mode Map is empty!" << endl;
+
+ mdmgrtest.printModeMap(modemap);
+}
+
+TEST(ModeManagerTest, test_addModeDirectory) {
+ ModeManagerTest mdmgrtest;
+ ModeManager md_mgr;
+ md_mgr.addModeDirectory(".");
+
+ string dir_extra = "./extra";
+ try {
+ md_mgr.addModeDirectory(dir_extra);
+ }
+ catch (exception &e) {
+ ADD_FAILURE() << "Fail to append : " << e.what() << endl;
+ return;
+ }
+
+ map<string, string> modemap = mdmgrtest.getModeMap(md_mgr);
+ EXPECT_FALSE(modemap.empty()) << "Mode Map is empty!" << endl;
+
+ mdmgrtest.printModeMap(modemap);
+}
+
+TEST(ModeManagerTest, test_addModeDirectory_withwrongdir) {
+ ModeManager md_mgr;
+ md_mgr.addModeDirectory(".");
+
+ string dir_wrong = "./dir_wrong";
+ try {
+ md_mgr.addModeDirectory(dir_wrong);
+ }
+ catch (exception &e) {
+ cout << "Fail to append : " << e.what() << endl;
+ return;
+ }
- EXPECT_EQ("/usr/share/modes/mode", mdmgrT.getDir(mdMgr));
+ ADD_FAILURE() << "addModeDirectory() did not detect a wrong directory!" << endl;
}
-TEST(modemanager, getmodemap) {
- ModeManager mdMgr;
- TestModeManager mdmgrT;
+TEST(ModeManagerTest, test_addModeDirectory_withappendeddir) {
+ ModeManagerTest mdmgrtest;
+ ModeManager md_mgr;
+ md_mgr.addModeDirectory(".");
- map<string, string> map = mdmgrT.getModeMap(mdMgr);
- EXPECT_FALSE(map.empty());
+ string dir_extra = "./extra";
+ try {
+ md_mgr.addModeDirectory(dir_extra);
+ }
+ catch (exception &e) {
+ cout << "Fail to append : " << e.what() <<endl;
+ ADD_FAILURE();
+ return;
+ }
+ map<string, string> modemap = mdmgrtest.getModeMap(md_mgr);
+ EXPECT_FALSE(modemap.empty()) << "Mode Map is empty!" << endl;
- if (!map.empty()) {
- std::map<string, string>::iterator it;
- printf("| %-15.15s | %s\n", "Mode Name", "ModeXML file Path");
+ mdmgrtest.printModeMap(modemap);
- for(it = map.begin(); it != map.end(); it++)
- printf("| %-15.15s | %s\n", it->first.c_str(), it->second.c_str());
+ string dir_appended = dir_extra;
+ try {
+ md_mgr.addModeDirectory(dir_appended);
+ }
+ catch (exception &e) {
+ cout << "Fail to append : " << e.what() << endl;
+ return;
}
+
+ ADD_FAILURE() << "addModeDirectory() did not detect a directory that is already attached!" << endl;
}