惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Scott Helme
Scott Helme
N
Netflix TechBlog - Medium
AI
AI
Security Latest
Security Latest
GbyAI
GbyAI
P
Proofpoint News Feed
Y
Y Combinator Blog
A
Arctic Wolf
G
Google Developers Blog
U
Unit 42
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
C
Check Point Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
Latest news
Latest news
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
美团技术团队
WordPress大学
WordPress大学
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
The Hacker News
The Hacker News
Simon Willison's Weblog
Simon Willison's Weblog
V
V2EX
Project Zero
Project Zero
博客园_首页

博客园 - 荣-

学习实践:使用模式,原则实现一个C++数据库访问类 C++字符转换等常用方法 DLL内存管理模板类 字符串处理代码(国际化转换C++版) 我的C++数据库访问库--临界区处理类 我的C++数据库访问库 批处理文件的学习 C++中,以类成员函数指针作为参数对std::map中的元素进行迭代处理 取得MySQL数据库表,列信息的SQL语句 我的我的C#数据库操作类(与大家交流) ACE项目的重构整理 安装SQL Server 2000和sp补丁时,安装程序提示"以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机"。 我的计算机安装步骤 创建oracle用户 我的重构步骤 我的C++代码检查列表 PowerDesigner高级应用 文档的阅读 如何设计类
学习实践:使用模式,原则实现一个C++自动化测试程序
荣- · 2014-08-21 · via 博客园 - 荣-
/**
* @brief 添加测试对象。
*
*/
#define TEST_INIT(info, sub) {\
    ostringstream oss;\
    oss<<"position:"<<__FILE__<<"-"<<__LINE__<<"-"<<__FUNCTION__<<endl;\
    info.name = __FUNCTION__;/*oss.str();*/\
}\
    info.subName = sub;\
    info.remark = "";\
    info.isOK = true;
#define TESTFUN_INIT(name) m_Tests.push_back(std::bind(&name, this, std::tr1::placeholders::_1))
/**
* @brief 数据库操作测试类。
*
*/
class  HisDBTest: public TestBaseEX
{
public:
    HisDBTest();
    ~HisDBTest();
private:
    /**
    * @brief 执行Open接口测试(连接字符串正确)。
    * @param[in] info    测试数据对象
    * @retval true:成功,false;失败
    */
    bool OnOpen(TestInfo& info);
    bool DropTable(TestInfo&);
    bool CreateTable(TestInfo&);
    bool AddRecorder(TestInfo&);
    bool AddRecorder2(TestInfo&);
    bool Scalar(TestInfo&);
    bool Scalar2(TestInfo&);
    bool Scalar3(TestInfo&);
    bool ReadRecorders(TestInfo&);
    bool ReadRecorders2(TestInfo&);
    bool ReadRecorders3(TestInfo&);
    bool DeleteRecorder(TestInfo&);
    bool OnClose(TestInfo&);
    bool OnClose_Repeat(TestInfo&);
    bool OnConnRelace2(TestInfo&);
    bool OnConnRelace3(TestInfo&);
private:
    HiDB*    m_DB;
};
using namespace std;
HisDBTest::HisDBTest()
{
    TESTFUN_INIT(HisDBTest::OnOpen);
    TESTFUN_INIT(HisDBTest::DropTable);
    TESTFUN_INIT(HisDBTest::CreateTable);
    TESTFUN_INIT(HisDBTest::AddRecorder);
    TESTFUN_INIT(HisDBTest::AddRecorder2);
    TESTFUN_INIT(HisDBTest::Scalar);
    TESTFUN_INIT(HisDBTest::Scalar2);
    TESTFUN_INIT(HisDBTest::Scalar3);
    TESTFUN_INIT(HisDBTest::ReadRecorders);
    TESTFUN_INIT(HisDBTest::ReadRecorders2);
    TESTFUN_INIT(HisDBTest::ReadRecorders3);
    TESTFUN_INIT(HisDBTest::DeleteRecorder);
    TESTFUN_INIT(HisDBTest::OnConnRelace2);
    TESTFUN_INIT(HisDBTest::OnConnRelace3);
    this->m_DB = new HiDB(HiDBType_MySQL, false);
    
}
HisDBTest::~HisDBTest()
{
    if (this->m_DB)
    {
        this->m_DB->Close();
        delete this->m_DB;
        this->m_DB = NULL;
    }
}
bool HisDBTest::OnOpen(TestInfo& info)
{
    TEST_INIT(info, "打开数据库");
    info.remark = "(请提供数据库:host=127.0.0.1;port=3306;"
        "dbname=test;user=root;pwd=root;charset=gbk;";
    return this->m_DB->Open(
        "host=127.0.0.1;port=3306;dbname=test;user=root;pwd=root;charset=gbk;"
        );
}
bool HisDBTest::DropTable(TestInfo& info)
{
    TEST_INIT(info, "ExecuteNoQuery 无参");
    return this->m_DB->ExecuteNoQuery("drop table if exists table1;");
}
bool HisDBTest::CreateTable(TestInfo& info)
{
    TEST_INIT(info, "ExecuteNoQuery 无参");
    return this->m_DB->ExecuteNoQuery(
        "create table table1(column1 varchar(6) not null,"
        "column2  varchar(40) not null,"
        "column3  int not null default 1,"
        "column4 int, "
        "column5 timestamp not null default CURRENT_TIMESTAMP,"
        "column6 varchar(512),primary key (column1));");
}
bool HisDBTest::AddRecorder(TestInfo& info)
{
    TEST_INIT(info, "ExecuteNoQuery C语言方式(printf)");
    return this->m_DB->ExecuteNoQuery(
        "INSERT INTO table1(Column1,Column2,Column3,Column4,Column6) "
        "VALUES('%s', '%s', %d, NULL, '%s')",
        "mytest", "my second test recorder",
        80, "this test create by xuminrong");
}
bool HisDBTest::AddRecorder2(TestInfo& info)
{
    TEST_INIT(info, "Create方法,自动组成SQL语句");
    vector<HiDBParamer> paramers;
    HiDBParamer paramer1("column1", "hitest");
    paramers.push_back(paramer1);
    HiDBParamer paramer2("column2", "this is a test by xmr");
    paramers.push_back(paramer2);
    HiDBParamer paramer3(HiDBDataType_Short, "column3", "59");
    paramers.push_back(paramer3);
    HiDBParamer paramer4(HiDBDataType_Short, "column4", "59");
    paramer4.m_IsNull = true;
    paramers.push_back(paramer4);
    HiDBParamer paramer6("column6", "this is a test");// = {0};
    paramers.push_back(paramer6);
    return this->m_DB->Create("table1", paramers);
}
bool HisDBTest::Scalar(TestInfo& info)
{
    TEST_INIT(info, "ExecuteScalar 使用参数数组,以HiDBRetVal作为返回值");
    vector<HiDBParamer> paramers;
    HiDBParamer paramer1("column1", "hitest");
    paramers.push_back(paramer1);
    HiDBParamer paramer2(HiDBDataType_Short, "column3", "59");
    paramers.push_back(paramer2);
    HiDBRetVal val;
    try
    {
        if (!this->m_DB->ExecuteScalar("SELECT column6 FROM table1 WHERE ? AND ?",paramers, &val))
        {
            return false;
        }
    }
    catch (HiDBException& e)
    {
        info.remark = "产生HiDBException异常:" + e.m_descript + e.m_position;
    }
    if (::strcmp(val.m_Value.c_str(), "this is a test") != 0)
    {
        return false;
    }
    return true;
}
bool HisDBTest::Scalar2(TestInfo& info)
{
    TEST_INIT(info, "ExecuteScalar C语言方式(printf),以string作为返回值");
    string val;
    try
    {
        return this->m_DB->ExecuteScalar(
            "SELECT column4 FROM table1 WHERE column1='%s' AND column3=%d",
            &val, "hitest", 59);
    }
    catch (HiDBException& e)
    {
        info.remark = "产生HiDBException异常:" + e.m_descript + e.m_position;
        return false;
    }
}
bool HisDBTest::Scalar3(TestInfo& info)
{
    TEST_INIT(info, "ExecuteScalar C语言方式(printf),返回空值");
    HiDBRetVal val;
    try
    {
        if (!this->m_DB->ExecuteScalar(
            "SELECT column4 FROM table1 WHERE column1='%s' AND column3=%d",
            &val, "hitest", 59))
        {
            return false;
        }
    }
    catch (HiDBException& e)
    {
        info.remark = "产生HiDBException异常:" + e.m_descript + e.m_position;
        return false;
    }
    if (val.m_IsNull)
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool HisDBTest::ReadRecorders(TestInfo& info)
{
    TEST_INIT(info, "ExecuteQuery 使用参数数组");
    vector<HiDBParamer> paramers;
    HiDBParamer paramer1("column1", "hitest");
    paramers.push_back(paramer1);
    HiDBParamer paramer2( "column1", "mytest");
    paramers.push_back(paramer2);
    std::shared_ptr<HiDBTable> table = this->m_DB->ExecuteQuery(
        "SELECT column1,column2 FROM table1 WHERE ? OR ? ORDER BY column1", paramers);
    if (table == NULL)
    {
        return false;
    }
    if (table->size() != 2)
    {
        return false;
    }
    
    ostringstream oss;
    for (auto it = table->begin(); it != table->end(); ++it)
    {
        for(auto item = (*it).begin(); item != (*it).end(); ++item)
        {
            //oss<<"field:"<<item->second.m_Field.c_str()<<",value:"<<item->second.m_Value.c_str()<<"\r\n";
            oss<<item->second.ToSrting()<<"\n";
        }
    }
    info.remark.append(oss.str().c_str());
    return true;
}
bool HisDBTest::ReadRecorders2(TestInfo& info)
{
    TEST_INIT(info, "ExecuteQuery C语言方式(printf)");
    std::shared_ptr<HiDBTable> table = this->m_DB->ExecuteQuery(
        "SELECT column1,column2 FROM table1 WHERE column1='%s' OR column1='%s'",        
        "hitest", "mytest");
    if (table == NULL)
    {
        return false;
    }
    if (table->size() != 2)
    {
        return true;
    }
    ostringstream oss;
    for (auto it = table->begin(); it != table->end(); ++it)
    {
        for(auto item = (*it).begin(); item != (*it).end(); ++item)
        {
            //oss<<"field:"<<item->second.m_Field.c_str()<<",value:"<<item->second.m_Value.c_str()<<"\r\n";
            oss<<item->second.ToSrting()<<"\n";
        }
    }
    info.remark.append(oss.str().c_str());
    return true;
}
bool HisDBTest::ReadRecorders3(TestInfo& info)
{
    TEST_INIT(info, "生成SQL语句测试");
    vector<HiDBParamer> list;
    HiDBParamer parm1("nCameraNo", 12345);
    list.push_back(parm1);
    HiDBParamer parm2(HiDBDataType_Time, "dtTime", "2012-08-06 16:44:32");
    parm2.m_Oper = HiOper_GreaterEqual;
    list.push_back(parm2);
    HiDBParamer parm3(HiDBDataType_Time, "dtTime", "2012-08-07 16:44:32");
    parm3.m_Oper = HiOper_LessEqual;
    list.push_back(parm3);
    
    HiDBParamer parm4("nEPType", 3);
    list.push_back(parm4);
    HiDBParamer parm6( "nFoward", 5);
    list.push_back(parm6);
    ostringstream oss;
    oss<<"SELECT nCameraNo,nCarNoType,cCarNo,dtTime,cAddress,"
        "cRouteChannel,nFoward,nEPType,nCaptureType,cAction,"
        "nTotalTime,nColor  FROM Illegals";
    int count = (int)list.size();
    if (count > 0)
    {
        oss<< " WHERE ";
    }
    for (int i = 0; i < count; i++)
    {
        oss<<" ? ";
        if (i != count - 1)
        {
            oss<<" AND ";
        }
    }
    oss<<" ORDER BY nNo LIMIT "<<3<<" "<<50<<endl;
    try
    {
        string sql = oss.str();
        std::shared_ptr<HiDBTable> table = this->m_DB->ExecuteQuery(sql.c_str(),list);
        if (table == NULL)
        {
            return true;
        }
        return false;
    }
    catch (HiDBException& ex)
    {
        info.remark = ex.m_sql;
        return true;
        
    }
    catch (...)
    {
        return false;
    }
}
bool HisDBTest::DeleteRecorder(TestInfo& info)
{
    TEST_INIT(info, "Delete 使用参数数组");
    vector<HiDBParamer> paramers;
    HiDBParamer paramer1("column1", "hitest");
    paramers.push_back(paramer1);
    HiDBParamer paramer2( HiDBDataType_Short, "column3", "59");
    paramers.push_back(paramer2);
    return this->m_DB->Delete("table1", paramers);
}
static string  getConn(string ip, TestInfo& info);
bool HisDBTest::OnConnRelace2(TestInfo& info)
{
    TEST_INIT(info, "替换数据库IP");
    return getConn("127.0.15.43", info)=="host=127.0.0.1;port=3306;"
        "dbname=test;user=root;pwd=root;charset=gbk;";
}
bool HisDBTest::OnConnRelace3(TestInfo& info)
{
    TEST_INIT(info, "替换数据库IP");
    return getConn("127.1.5.1", info)=="host=127.1.5.1;port=3306;"
        "dbname=test;user=root;pwd=root;charset=gbk;";
}
static string  getConn(string m_CenterIP, TestInfo& info)
{
    string conn = "host=127.0.0.1;port=3306;"
        "dbname=test;user=root;pwd=root;charset=gbk;";
    if (!m_CenterIP.empty())
    {
        string::size_type pos1 = conn.find_first_of('=');
        string::size_type pos2 = conn.find_first_of(';', pos1);
        //取数据库IP
        string ip = conn.substr(pos1 + 1, pos2 - pos1 - 1);
        string::size_type pos_connect = ip.find_first_of('.', ip.find_first_of('.') + 1);
        string::size_type pos_center = m_CenterIP.find_first_of('.', 
            m_CenterIP.find_first_of('.') + 1);
        //比较IP前两段是否一样
        if (ip.substr(0, pos_connect) != m_CenterIP.substr(0, pos_center))
        {
            conn.replace(pos1 + 1, ip.size(), m_CenterIP);
        }
    }
    return conn;
}