Tizen 2.0 Release
[framework/web/wrt-commons.git] / tests / dpl / db / test_sql_connection.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        test_sql_connection.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of sql connection tests
21  */
22 #include <dpl/test/test_runner.h>
23 #include <dpl/db/sql_connection.h>
24 #include <dpl/db/naive_synchronization_object.h>
25 #include <memory>
26 #include <dpl/log/log.h>
27 #include <sstream>
28 #include <string>
29 #include <cstdlib>
30 #include <ctime>
31
32 extern const char* PATH_DB;
33
34 RUNNER_TEST_GROUP_INIT(DPL)
35
36 class AbstractSynchronizationObjectGenerator
37 {
38 public:
39     virtual ~AbstractSynchronizationObjectGenerator() {}
40
41     virtual DPL::DB::SqlConnection::SynchronizationObject *Create() = 0;
42 };
43
44 class NaiveSynchronizationObjectGenerator
45     : public AbstractSynchronizationObjectGenerator
46 {
47 public:
48     virtual DPL::DB::SqlConnection::SynchronizationObject *Create()
49     {
50         return new DPL::DB::NaiveSynchronizationObject();
51     }
52 };
53
54 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator);
55
56 class StressGenerator
57     : public DPL::Thread
58 {
59 private:
60     size_t m_prefix;
61     std::string m_dbFileName;
62     AbstractSynchronizationObjectGenerator *m_generator;
63
64 protected:
65     virtual int ThreadEntry()
66     {
67         DPL::DB::SqlConnection connection(
68             m_dbFileName,
69             DPL::DB::SqlConnection::Flag::None,
70             DPL::DB::SqlConnection::Flag::RW,
71             m_generator->Create());
72
73         DPL::DB::SqlConnection::DataCommandAutoPtr countCommand =
74             connection.PrepareDataCommand(
75                 "SELECT COUNT(*) FROM test WHERE value=?");
76
77         for (size_t i = 0; i < 10; ++i)
78         {
79             std::ostringstream valueStream;
80
81             valueStream << "value_";
82             valueStream << static_cast<unsigned long>(m_prefix);
83             valueStream << "_";
84             valueStream << static_cast<unsigned long>(i);
85
86             std::string value = valueStream.str();
87
88             connection.ExecCommand(
89                 "INSERT INTO test VALUES ('%s');",
90                 value.c_str());
91
92             countCommand->BindString(1, value.c_str());
93
94             RUNNER_ASSERT(countCommand->Step());
95
96             RUNNER_ASSERT(countCommand->GetColumnString(0) == "1");
97
98             countCommand->Reset();
99         }
100
101         countCommand.reset();
102
103         return 0;
104     }
105
106 public:
107     StressGenerator(size_t prefix,
108                     const std::string &dbFileName,
109                     AbstractSynchronizationObjectGenerator *generator)
110         : m_prefix(prefix),
111           m_dbFileName(dbFileName),
112           m_generator(generator)
113     {
114     }
115 };
116
117 typedef std::shared_ptr<DPL::Thread> ThreadPtr;
118
119 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator)
120 {
121     DPL::DB::SqlConnection connection(PATH_DB,
122                                       DPL::DB::SqlConnection::Flag::UseLucene,
123                                       DPL::DB::SqlConnection::Flag::RW);
124
125     connection.ExecCommand("CREATE TABLE test(value TEXT);");
126
127     const size_t STRESS_GENERATOR_COUNT = 5;
128     ThreadPtr stressGenerators[STRESS_GENERATOR_COUNT];
129
130     for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i)
131     {
132         stressGenerators[i].reset(
133             new StressGenerator(i, PATH_DB, generator));
134
135         stressGenerators[i]->Run();
136     }
137
138     for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i)
139         stressGenerators[i]->Quit();
140
141     connection.ExecCommand("DROP TABLE test;");
142 }
143
144 RUNNER_TEST(SqlConnection_MassiveReadWrite_NaiveSynchronization)
145 {
146     srand(time(NULL));
147
148     NaiveSynchronizationObjectGenerator m_generator;
149     MassiveReadWriteTest(&m_generator);
150 }
151
152
153
154
155
156
157 RUNNER_TEST(SqlConnection_Not_Connected_Lucene)
158 {
159     Try {
160         DPL::DB::SqlConnection connection("/notexisitingdirectiory/foo",
161                                           DPL::DB::SqlConnection::Flag::UseLucene,
162                                           DPL::DB::SqlConnection::Flag::RW);
163         RUNNER_ASSERT_MSG(false,
164                           "connection should throw on accessing "
165                           "nonexistent file as a database");
166     }
167     Catch (DPL::DB::SqlConnection::Exception::ConnectionBroken)
168     {
169         RUNNER_ASSERT(true);
170     }
171     catch (DPL::Exception)
172     {
173         RUNNER_ASSERT_MSG(false, "Wrong exception found");
174     }
175 }
176
177 RUNNER_TEST(SqlConnection_Not_Connected)
178 {
179     Try {
180         DPL::DB::SqlConnection connection("/notexisitingdirectiory/foo",
181                                           DPL::DB::SqlConnection::Flag::None,
182                                           DPL::DB::SqlConnection::Flag::RW);
183         RUNNER_ASSERT_MSG(false,
184                           "connection should throw on accessing "
185                           "nonexistent file as a database");
186     }
187     Catch (DPL::DB::SqlConnection::Exception::ConnectionBroken)
188     {
189         RUNNER_ASSERT(true);
190     }
191     catch (DPL::Exception)
192     {
193         RUNNER_ASSERT_MSG(false, "Wrong exception found");
194     }
195 }
196
197 RUNNER_TEST(SqlConnection_Null_Query)
198 {
199     DPL::DB::SqlConnection connection(PATH_DB,
200                                       DPL::DB::SqlConnection::Flag::UseLucene,
201                                       DPL::DB::SqlConnection::Flag::RW);
202     Try
203     {
204         connection.ExecCommand(NULL);
205         RUNNER_ASSERT_MSG(false,
206                           "Null pointer should not be accepted");
207     }
208     Catch (DPL::DB::SqlConnection::Exception::SyntaxError)
209     {
210         RUNNER_ASSERT(true);
211     }
212     catch (DPL::Exception)
213     {
214         RUNNER_ASSERT_MSG(false, "Wrong exception found");
215     }
216
217 }
218
219 RUNNER_TEST(SqlConnection_Bad_Query)
220 {
221     DPL::DB::SqlConnection connection(PATH_DB,
222                                       DPL::DB::SqlConnection::Flag::UseLucene,
223                                       DPL::DB::SqlConnection::Flag::RW);
224     Try
225     {
226         connection.ExecCommand("Some stupid string");
227         RUNNER_ASSERT_MSG(false, "This string should not be accepted");
228     }
229     Catch (DPL::DB::SqlConnection::Exception::SyntaxError)
230     {
231         RUNNER_ASSERT(true);
232     }
233     catch (DPL::Exception)
234     {
235         RUNNER_ASSERT_MSG(false, "Wrong exception found");
236     }
237 }