2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file test_sql_connection.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of sql connection tests
22 #include <dpl/test/test_runner.h>
23 #include <dpl/db/sql_connection.h>
24 #include <dpl/db/naive_synchronization_object.h>
26 #include <dpl/log/log.h>
32 extern const char* PATH_DB;
34 RUNNER_TEST_GROUP_INIT(DPL)
36 class AbstractSynchronizationObjectGenerator
39 virtual ~AbstractSynchronizationObjectGenerator() {}
41 virtual DPL::DB::SqlConnection::SynchronizationObject *Create() = 0;
44 class NaiveSynchronizationObjectGenerator :
45 public AbstractSynchronizationObjectGenerator
48 virtual DPL::DB::SqlConnection::SynchronizationObject *Create()
50 return new DPL::DB::NaiveSynchronizationObject();
54 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator);
56 class StressGenerator :
61 std::string m_dbFileName;
62 AbstractSynchronizationObjectGenerator *m_generator;
65 virtual int ThreadEntry()
67 DPL::DB::SqlConnection connection(
69 DPL::DB::SqlConnection::Flag::None,
70 DPL::DB::SqlConnection::Flag::RW,
71 m_generator->Create());
73 DPL::DB::SqlConnection::DataCommandAutoPtr countCommand =
74 connection.PrepareDataCommand(
75 "SELECT COUNT(*) FROM test WHERE value=?");
77 for (size_t i = 0; i < 10; ++i) {
78 std::ostringstream valueStream;
80 valueStream << "value_";
81 valueStream << static_cast<unsigned long>(m_prefix);
83 valueStream << static_cast<unsigned long>(i);
85 std::string value = valueStream.str();
87 connection.ExecCommand(
88 "INSERT INTO test VALUES ('%s');",
91 countCommand->BindString(1, value.c_str());
93 RUNNER_ASSERT(countCommand->Step());
95 RUNNER_ASSERT(countCommand->GetColumnString(0) == "1");
97 countCommand->Reset();
100 countCommand.reset();
106 StressGenerator(size_t prefix,
107 const std::string &dbFileName,
108 AbstractSynchronizationObjectGenerator *generator) :
110 m_dbFileName(dbFileName),
111 m_generator(generator)
115 typedef std::shared_ptr<DPL::Thread> ThreadPtr;
117 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator)
119 DPL::DB::SqlConnection connection(PATH_DB,
120 DPL::DB::SqlConnection::Flag::UseLucene,
121 DPL::DB::SqlConnection::Flag::RW);
123 connection.ExecCommand("CREATE TABLE test(value TEXT);");
125 const size_t STRESS_GENERATOR_COUNT = 5;
126 ThreadPtr stressGenerators[STRESS_GENERATOR_COUNT];
128 for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i) {
129 stressGenerators[i].reset(
130 new StressGenerator(i, PATH_DB, generator));
132 stressGenerators[i]->Run();
135 for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i) {
136 stressGenerators[i]->Quit();
139 connection.ExecCommand("DROP TABLE test;");
142 RUNNER_TEST(SqlConnection_MassiveReadWrite_NaiveSynchronization)
146 NaiveSynchronizationObjectGenerator m_generator;
147 MassiveReadWriteTest(&m_generator);
150 RUNNER_TEST(SqlConnection_Not_Connected_Lucene)
153 DPL::DB::SqlConnection connection(
154 "/notexisitingdirectiory/foo",
155 DPL::DB::SqlConnection::Flag::
157 DPL::DB::SqlConnection::Flag::RW);
158 RUNNER_ASSERT_MSG(false,
159 "connection should throw on accessing "
160 "nonexistent file as a database");
162 Catch(DPL::DB::SqlConnection::Exception::ConnectionBroken)
165 } catch (DPL::Exception) {
166 RUNNER_ASSERT_MSG(false, "Wrong exception found");
170 RUNNER_TEST(SqlConnection_Not_Connected)
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");
180 Catch(DPL::DB::SqlConnection::Exception::ConnectionBroken)
183 } catch (DPL::Exception) {
184 RUNNER_ASSERT_MSG(false, "Wrong exception found");
188 RUNNER_TEST(SqlConnection_Null_Query)
190 DPL::DB::SqlConnection connection(PATH_DB,
191 DPL::DB::SqlConnection::Flag::UseLucene,
192 DPL::DB::SqlConnection::Flag::RW);
195 connection.ExecCommand(NULL);
196 RUNNER_ASSERT_MSG(false,
197 "Null pointer should not be accepted");
199 Catch(DPL::DB::SqlConnection::Exception::SyntaxError)
202 } catch (DPL::Exception) {
203 RUNNER_ASSERT_MSG(false, "Wrong exception found");
207 RUNNER_TEST(SqlConnection_Bad_Query)
209 DPL::DB::SqlConnection connection(PATH_DB,
210 DPL::DB::SqlConnection::Flag::UseLucene,
211 DPL::DB::SqlConnection::Flag::RW);
214 connection.ExecCommand("Some stupid string");
215 RUNNER_ASSERT_MSG(false, "This string should not be accepted");
217 Catch(DPL::DB::SqlConnection::Exception::SyntaxError)
220 } catch (DPL::Exception) {
221 RUNNER_ASSERT_MSG(false, "Wrong exception found");