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);
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)
79 std::ostringstream valueStream;
81 valueStream << "value_";
82 valueStream << static_cast<unsigned long>(m_prefix);
84 valueStream << static_cast<unsigned long>(i);
86 std::string value = valueStream.str();
88 connection.ExecCommand(
89 "INSERT INTO test VALUES ('%s');",
92 countCommand->BindString(1, value.c_str());
94 RUNNER_ASSERT(countCommand->Step());
96 RUNNER_ASSERT(countCommand->GetColumnString(0) == "1");
98 countCommand->Reset();
101 countCommand.reset();
107 StressGenerator(size_t prefix,
108 const std::string &dbFileName,
109 AbstractSynchronizationObjectGenerator *generator)
111 m_dbFileName(dbFileName),
112 m_generator(generator)
117 typedef std::shared_ptr<DPL::Thread> ThreadPtr;
119 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator)
121 DPL::DB::SqlConnection connection(PATH_DB,
122 DPL::DB::SqlConnection::Flag::UseLucene,
123 DPL::DB::SqlConnection::Flag::RW);
125 connection.ExecCommand("CREATE TABLE test(value TEXT);");
127 const size_t STRESS_GENERATOR_COUNT = 5;
128 ThreadPtr stressGenerators[STRESS_GENERATOR_COUNT];
130 for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i)
132 stressGenerators[i].reset(
133 new StressGenerator(i, PATH_DB, generator));
135 stressGenerators[i]->Run();
138 for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i)
139 stressGenerators[i]->Quit();
141 connection.ExecCommand("DROP TABLE test;");
144 RUNNER_TEST(SqlConnection_MassiveReadWrite_NaiveSynchronization)
148 NaiveSynchronizationObjectGenerator m_generator;
149 MassiveReadWriteTest(&m_generator);
157 RUNNER_TEST(SqlConnection_Not_Connected_Lucene)
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");
167 Catch (DPL::DB::SqlConnection::Exception::ConnectionBroken)
171 catch (DPL::Exception)
173 RUNNER_ASSERT_MSG(false, "Wrong exception found");
177 RUNNER_TEST(SqlConnection_Not_Connected)
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");
187 Catch (DPL::DB::SqlConnection::Exception::ConnectionBroken)
191 catch (DPL::Exception)
193 RUNNER_ASSERT_MSG(false, "Wrong exception found");
197 RUNNER_TEST(SqlConnection_Null_Query)
199 DPL::DB::SqlConnection connection(PATH_DB,
200 DPL::DB::SqlConnection::Flag::UseLucene,
201 DPL::DB::SqlConnection::Flag::RW);
204 connection.ExecCommand(NULL);
205 RUNNER_ASSERT_MSG(false,
206 "Null pointer should not be accepted");
208 Catch (DPL::DB::SqlConnection::Exception::SyntaxError)
212 catch (DPL::Exception)
214 RUNNER_ASSERT_MSG(false, "Wrong exception found");
219 RUNNER_TEST(SqlConnection_Bad_Query)
221 DPL::DB::SqlConnection connection(PATH_DB,
222 DPL::DB::SqlConnection::Flag::UseLucene,
223 DPL::DB::SqlConnection::Flag::RW);
226 connection.ExecCommand("Some stupid string");
227 RUNNER_ASSERT_MSG(false, "This string should not be accepted");
229 Catch (DPL::DB::SqlConnection::Exception::SyntaxError)
233 catch (DPL::Exception)
235 RUNNER_ASSERT_MSG(false, "Wrong exception found");