#include "configtest.h"
#include <QLockFile>
#include <xmlconfig.h>

using namespace Config;
using namespace TestCase;

ConfigTest::ConfigTest(Configration *test_ins)
    : xml(test_ins)
{

}

void ConfigTest::testConfig()
{
    xml->loadFile("./base_test.xml");
    QStringList keys;
    keys << "test-key" << "test-sub";
    xml->setConfig(keys , "omevalue");
    QVERIFY(xml->getConfig(keys) == "omevalue");

    xml->deleteX(keys);
    QVERIFY(xml->getConfig(keys) == "");

    xml->setConfig(keys , "omevalue");
    QVERIFY(xml->getConfig(keys) == "omevalue");

}

void ConfigTest::testList()
{
    xml->loadFile("./base_test.xml");
    QStringList keys;
    keys << "test-key" << "test-sub";
    QStringList values;
    values << "v0" << "v1";

    xml->setList(keys, values);

    auto temp_list = xml->getList(keys);
    for(auto &v : values){
        QVERIFY(temp_list.contains(v));
    }

    xml->deleteX(keys);
    QVERIFY(xml->getList(keys).count() == 0);

    xml->setList(keys, values);
    temp_list = xml->getList(keys);
    for(auto &v : values){
        QVERIFY(temp_list.contains(v));
    }
}

void ConfigTest::testMap()
{
    xml->loadFile("./base_test.xml");
    QStringList keys;
    keys << "key0" << "key1" << "key2" << "key3";
    QHash<QString,QString> map;
    map["key0"] = "value0";
    map["key1"] = "value1";
    map["key2"] = "value2";
    map["key3"] = "value3";

    xml->setMap(keys, map);
    auto temp_map = xml->getMap(keys);
    for(auto &v : keys){
        QVERIFY(temp_map[v] == map[v]);
    }

    xml->deleteX(keys);
    QVERIFY(xml->getMap(keys).count() == 0);

    xml->setMap(keys, map);
    temp_map = xml->getMap(keys);
    for(auto &v : keys){
        QVERIFY(temp_map[v] == map[v]);
    }

}

void ConfigTest::testReload()
{
    xml->loadFile("./base_test.xml");

    QStringList keys;
    keys << "test-key" << "test-sub";
    xml->setConfig(keys , "omevalue");

    QStringList values;
    values << "v0" << "v1";
    xml->setList(keys, values);

    QHash<QString,QString> map;
    map["key0"] = "value0";
    map["key1"] = "value1";
    map["key2"] = "value2";
    map["key3"] = "value3";
    xml->setMap(keys, map);

    xml->save();

    xml->loadFile("./base_test.xml");
    QVERIFY(xml->getConfig(keys) == "omevalue");

    auto temp_list = xml->getList(keys);
    for(auto &v : values){
        QVERIFY(temp_list.contains(v));
    }

    auto temp_map = xml->getMap(keys);
    for(auto &v : keys){
        QVERIFY(temp_map[v] == map[v]);
    }
}

void ConfigTest::testException()
{
    ParseException *temp = nullptr;
    QFile test_f("./exists_test.xml");
    if(test_f.exists())
        test_f.remove();

    try {
        xml->loadFile("./exists_test.xml");
    }  catch (ParseException *x) {

    }
    QVERIFY2(test_f.exists(), "在指定路径生成默认配置文件失败");

    QFile anchor("./exception_base_test.xml");
    try{
        xml->loadFile("./exception_base_test.xml");
    }  catch (ParseException *x) {
        temp = x;
    }

    QVERIFY2(temp != nullptr, QObject::tr("测试无法打开的Exception失败，未能探知").toLocal8Bit());
    delete temp;
    temp = nullptr;

    anchor.open(QIODevice::WriteOnly | QIODevice::Text);

    QTextStream to(&anchor);
    to << "alkdjfalkfj";
    to.flush();
    anchor.close();

    try{
        xml->loadFile("./exception_base_test.xml");

    }  catch (ParseException *x) {
        temp = x;
    }

    QVERIFY2(temp != nullptr, "配置文件格式错误探知失败");
    delete temp;
}










