From 862b455e157c44afc1f79f6f49a2e7ba72b39954 Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Date: Tue, 6 Jul 2010 15:17:34 -0400
Subject: [PATCH] [#3648] Add unit test for yaml emitter

---
 sflphone-common/src/config/yamlemitter.cpp | 41 +++++++++++++++++--
 sflphone-common/src/config/yamlemitter.h   | 47 ++++++++++++++++++++--
 sflphone-common/src/managerimpl.cpp        |  4 +-
 sflphone-common/src/sip/sipaccount.cpp     |  4 +-
 sflphone-common/test/Makefile.am           |  2 +-
 sflphone-common/test/configurationtest.cpp | 40 +++++-------------
 sflphone-common/test/configurationtest.h   |  7 +++-
 7 files changed, 99 insertions(+), 46 deletions(-)

diff --git a/sflphone-common/src/config/yamlemitter.cpp b/sflphone-common/src/config/yamlemitter.cpp
index 1e0f1b481f..89ddaa79a8 100755
--- a/sflphone-common/src/config/yamlemitter.cpp
+++ b/sflphone-common/src/config/yamlemitter.cpp
@@ -32,13 +32,46 @@
 
 namespace Conf {
 
-YamlEmitter::YamlEmitter() {}
+YamlEmitter::YamlEmitter() 
+{
+  open();
+}
+
+YamlEmitter::~YamlEmitter() 
+{
+  close();
+}
+
+void YamlEmitter::open() 
+{
+  fd = fopen(filename.c_str(), "wb");
+
+  if(!fd)
+    throw YamlEmitterException("Could not open file descriptor");
+
+  if(!yaml_emitter_initialize(&emitter))
+    throw YamlEmitterException("Could not open file descriptor");
 
-YamlEmitter::~YamlEmitter() {}
+  // Use unicode format
+  yaml_emitter_set_unicode(&emitter, 1);
 
-void YamlEmitter::open() {}
+  yaml_emitter_set_output_file(&emitter, fd);
 
-void YamlEmitter::close() {}
+  yaml_document_initialize(&document, NULL, NULL, NULL, 0, 0);
+}
+
+void YamlEmitter::close() 
+{
+  yaml_emitter_delete(&emitter);
+
+  if(!fd)
+    throw YamlEmitterException("File descriptor not valid");
+
+  if(!fclose(fd))
+    throw YamlEmitterException("Error closing file descriptor");
+
+  yaml_document_delete(&document);
+}
 
 void YamlEmitter::read() {}
 
diff --git a/sflphone-common/src/config/yamlemitter.h b/sflphone-common/src/config/yamlemitter.h
index 3e55d102b1..6219f6788c 100755
--- a/sflphone-common/src/config/yamlemitter.h
+++ b/sflphone-common/src/config/yamlemitter.h
@@ -31,15 +31,31 @@
 #ifndef __YAMLEMITTER_H__
 #define __YAMLEMITTER_H__
 
+#include <yaml.h>
 #include <exception>
+#include <string>
 
 namespace Conf {
 
-class YamlEmitterException : public std::exception {
+#define EMITTER_BUFFERSIZE 65536
+#define EMITTER_MAXEVENT 1024
+
+class YamlEmitterException : public std::exception 
+{
+ public:
+  YamlEmitterException(const std::string& str="") throw() : errstr(str) {}
+
+  virtual ~YamlEmitterException() throw() {}
+
+  virtual const char *what() const throw() {
+    std::string expt("YamlParserException occured: ");
+    expt.append(errstr);
+    
+    return expt.c_str();
+  }
+ private:
+  std::string errstr;
 
-    virtual const char *what() const throw() {
-      return "YamlEmitterException occured";
-    }
 };
 
 class YamlEmitter {
@@ -61,6 +77,29 @@ class YamlEmitter {
 
  private:
 
+  std::string filename;
+
+  FILE *fd;
+
+  /**
+   * The parser structure. 
+   */
+  yaml_emitter_t emitter;
+
+  /**
+   * The event structure array.
+   */ 
+  yaml_event_t events[EMITTER_MAXEVENT];
+
+  /**
+   * 
+   */
+  unsigned char buffer[EMITTER_BUFFERSIZE];
+
+  yaml_document_t document;
+
+  
+
 };
 
 }
diff --git a/sflphone-common/src/managerimpl.cpp b/sflphone-common/src/managerimpl.cpp
index a138658c99..5d0b79c92f 100755
--- a/sflphone-common/src/managerimpl.cpp
+++ b/sflphone-common/src/managerimpl.cpp
@@ -3824,7 +3824,9 @@ void ManagerImpl::setAccountDetails (const std::string& accountID,
     acc = getAccount(accountID);
     
     if (acc != NULL) {
-        acc->loadConfig();
+      
+        
+        // acc->loadConfig();
 		
 	if (acc->isEnabled()) {
 	  acc->registerVoIPLink();
diff --git a/sflphone-common/src/sip/sipaccount.cpp b/sflphone-common/src/sip/sipaccount.cpp
index 495aec57d0..5c6513cda6 100755
--- a/sflphone-common/src/sip/sipaccount.cpp
+++ b/sflphone-common/src/sip/sipaccount.cpp
@@ -134,7 +134,7 @@ SIPAccount::~SIPAccount()
 
 void SIPAccount::serialize(Engine *engine) {
 
-
+  
 }
 
 
@@ -159,9 +159,7 @@ void SIPAccount::unserialize(Conf::MappingNode *map)
   val = (Conf::ScalarNode *)(map->getValue(passwordKey));
   if(val) { _password = val->getValue(); val = NULL; }
   val = (Conf::ScalarNode *)(map->getValue(hostnameKey));
-  _debug("------------------------------------- hostname from config: %s, %s", _accountID.c_str(), val->getValue().c_str());
   if(val) { _hostname = val->getValue(); val = NULL; }
-  _debug("------------------------------------- hostname from config: %s, %s", _accountID.c_str(), _hostname.c_str());
   val = (Conf::ScalarNode *)(map->getValue(accountEnableKey));
   if(val) { _enabled = (val->getValue().compare("true") == 0) ? true : false; val = NULL; }
   //  val = (Conf::ScalarNode *)(map->getValue(mailboxKey));
diff --git a/sflphone-common/test/Makefile.am b/sflphone-common/test/Makefile.am
index ed3baa511e..9da8e2f603 100644
--- a/sflphone-common/test/Makefile.am
+++ b/sflphone-common/test/Makefile.am
@@ -40,5 +40,5 @@ LLIBS=$(CPPUNIT_LIBS) \
 	../src/sflphoned-numbercleaner.o \
 	../src/sflphoned-observer.o \
 	../src/sflphoned-voiplink.o \
+	../src/sflphoned-preferences.o \
 	../src/libsflphone.la
-	
diff --git a/sflphone-common/test/configurationtest.cpp b/sflphone-common/test/configurationtest.cpp
index 1c5805af03..bf92974cab 100755
--- a/sflphone-common/test/configurationtest.cpp
+++ b/sflphone-common/test/configurationtest.cpp
@@ -176,39 +176,17 @@ void ConfigurationTest::testYamlParser()
   
 }
 
-
-void ConfigurationTest::testYamlComposition() 
+void ConfigurationTest::testYamlEmitter()
 {
+  Conf::YamlParser *emitter;
 
-  /*
-  Conf::YamlDocument *doc = new Conf::YamlDocument();
-
-  Conf::SequenceNode *seq = new Conf::SequenceNode(doc);
-  Conf::MappingNode *map = new Conf::MappingNode();
-  Conf::ScalarNode *sclr = new Conf::ScalarNode();
-
-  CPPUNIT_ASSERT(seq->getType() == Conf::SEQUENCE);
-  CPPUNIT_ASSERT(map->getType() == Conf::MAPPING);
-  CPPUNIT_ASSERT(sclr->getType() == Conf::SCALAR);
-
-  seq->addNode(map);
-  seq->addNode(sclr);
-
-  Conf::Key key("username");
-  Conf::ScalarNode *val = new Conf::ScalarNode("alexandre");
-
-  map->setKeyValue(key, val);
-
-  Conf::YamlNode *node = map->getValue(key);
-
-  CPPUNIT_ASSERT(node->getType() == Conf::SCALAR);
-
-  delete val;
-
-  delete seq;
-  delete map;
-  delete sclr;
-  */
+  try{
+    emitter = new Conf::YamlParser("sequenceEmiter.yml");
 
+    delete emitter;
+  }
+  catch (Conf::YamlEmitterException &e) {
+    _error("ConfigTree: %s", e.what());
+  }
 
 }
diff --git a/sflphone-common/test/configurationtest.h b/sflphone-common/test/configurationtest.h
index 8c9ff82fea..00ee45e7ea 100755
--- a/sflphone-common/test/configurationtest.h
+++ b/sflphone-common/test/configurationtest.h
@@ -51,6 +51,7 @@
 #include "global.h"
 #include "user_cfg.h"
 #include "config/yamlparser.h"
+#include "config/yamlemitter.h"
 #include "config/yamlnode.h"
 
 class ConfigurationTest: public CppUnit::TestFixture {
@@ -65,7 +66,7 @@ CPPUNIT_TEST_SUITE( ConfigurationTest );
 //	CPPUNIT_TEST( testDefaultValueSignalisation );
 //	CPPUNIT_TEST( testInitAudioDriver );
             CPPUNIT_TEST( testYamlParser );
-	    CPPUNIT_TEST( testYamlComposition );
+	    CPPUNIT_TEST( testYamlEmitter );
 	CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -98,7 +99,9 @@ public:
 
 	void testYamlParser();
 
-	void testYamlComposition();
+	void testYamlEmitter();
+
+	
 };
 /* Register our test module */
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConfigurationTest, "ConfigurationTest");
-- 
GitLab