Source code formating unification
[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             std::ostringstream valueStream;
79
80             valueStream << "value_";
81             valueStream << static_cast<unsigned long>(m_prefix);
82             valueStream << "_";
83             valueStream << static_cast<unsigned long>(i);
84
85             std::string value = valueStream.str();
86
87             connection.ExecCommand(
88                 "INSERT INTO test VALUES ('%s');",
89                 value.c_str());
90
91             countCommand->BindString(1, value.c_str());
92
93             RUNNER_ASSERT(countCommand->Step());
94
95             RUNNER_ASSERT(countCommand->GetColumnString(0) == "1");
96
97             countCommand->Reset();
98         }
99
100         countCommand.reset();
101
102         return 0;
103     }
104
105   public:
106     StressGenerator(size_t prefix,
107                     const std::string &dbFileName,
108                     AbstractSynchronizationObjectGenerator *generator) :
109         m_prefix(prefix),
110         m_dbFileName(dbFileName),
111         m_generator(generator)
112     {}
113 };
114
115 typedef std::shared_ptr<DPL::Thread> ThreadPtr;
116
117 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator)
118 {
119     DPL::DB::SqlConnection connection(PATH_DB,
120                                       DPL::DB::SqlConnection::Flag::UseLucene,
121                                       DPL::DB::SqlConnection::Flag::RW);
122
123     connection.ExecCommand("CREATE TABLE test(value TEXT);");
124
125     const size_t STRESS_GENERATOR_COUNT = 5;
126     ThreadPtr stressGenerators[STRESS_GENERATOR_COUNT];
127
128     for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i) {
129         stressGenerators[i].reset(
130             new StressGenerator(i, PATH_DB, generator));
131
132         stressGenerators[i]->Run();
133     }
134
135     for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i) {
136         stressGenerators[i]->Quit();
137     }
138
139     connection.ExecCommand("DROP TABLE test;");
140 }
141
142 RUNNER_TEST(SqlConnection_MassiveReadWrite_NaiveSynchronization)
143 {
144     srand(time(NULL));
145
146     NaiveSynchronizationObjectGenerator m_generator;
147     MassiveReadWriteTest(&m_generator);
148 }
149
150 RUNNER_TEST(SqlConnection_Not_Connected_Lucene)
151 {
152     Try {
153         DPL::DB::SqlConnection connection(
154             "/notexisitingdirectiory/foo",
155             DPL::DB::SqlConnection::Flag::
156                 UseLucene,
157             DPL::DB::SqlConnection::Flag::RW);
158         RUNNER_ASSERT_MSG(false,
159                           "connection should throw on accessing "
160                           "nonexistent file as a database");
161     }
162     Catch(DPL::DB::SqlConnection::Exception::ConnectionBroken)
163     {
164         RUNNER_ASSERT(true);
165     } catch (DPL::Exception) {
166         RUNNER_ASSERT_MSG(false, "Wrong exception found");
167     }
168 }
169
170 RUNNER_TEST(SqlConnection_Not_Connected)
171 {
172     Try {
173         DPL::DB::SqlConnection connection("/notexisitingdirectiory/foo",
174                                           DPL::DB::SqlConnection::Flag::None,
175                                           DPL::DB::SqlConnection::Flag::RW);
176         RUNNER_ASSERT_MSG(false,
177                           "connection should throw on accessing "
178                           "nonexistent file as a database");
179     }
180     Catch(DPL::DB::SqlConnection::Exception::ConnectionBroken)
181     {
182         RUNNER_ASSERT(true);
183     } catch (DPL::Exception) {
184         RUNNER_ASSERT_MSG(false, "Wrong exception found");
185     }
186 }
187
188 RUNNER_TEST(SqlConnection_Null_Query)
189 {
190     DPL::DB::SqlConnection connection(PATH_DB,
191                                       DPL::DB::SqlConnection::Flag::UseLucene,
192                                       DPL::DB::SqlConnection::Flag::RW);
193     Try
194     {
195         connection.ExecCommand(NULL);
196         RUNNER_ASSERT_MSG(false,
197                           "Null pointer should not be accepted");
198     }
199     Catch(DPL::DB::SqlConnection::Exception::SyntaxError)
200     {
201         RUNNER_ASSERT(true);
202     } catch (DPL::Exception) {
203         RUNNER_ASSERT_MSG(false, "Wrong exception found");
204     }
205 }
206
207 RUNNER_TEST(SqlConnection_Bad_Query)
208 {
209     DPL::DB::SqlConnection connection(PATH_DB,
210                                       DPL::DB::SqlConnection::Flag::UseLucene,
211                                       DPL::DB::SqlConnection::Flag::RW);
212     Try
213     {
214         connection.ExecCommand("Some stupid string");
215         RUNNER_ASSERT_MSG(false, "This string should not be accepted");
216     }
217     Catch(DPL::DB::SqlConnection::Exception::SyntaxError)
218     {
219         RUNNER_ASSERT(true);
220     } catch (DPL::Exception) {
221         RUNNER_ASSERT_MSG(false, "Wrong exception found");
222     }
223 }