TsLibrary.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 /*!
22  \file terralib/unittest/core/lib/TsLibrary.cpp
23 
24  \brief A test suite for the TerraLib Core Lib Module.
25 
26  \author Frederico Augusto BedĂȘ.
27  */
28 
29 // TerraLib
30 #include <terralib/Defines.h>
34 
35 // Boost
36 #include <boost/test/unit_test.hpp>
37 #include <boost/filesystem.hpp>
38 
39 // STL
40 #include <iostream>
41 
42 static const std::string g_shared_library_name(te::core::Library::getNativeName("terralib_unittest_core_lib_function"));
43 static const std::string g_shared_library_path(te::core::FindInTerraLibPath("example"));
44 
45 BOOST_AUTO_TEST_SUITE( lib_test_case )
46 
47 BOOST_AUTO_TEST_CASE(test_constructor)
48 {
49  te::core::Library* l1 = nullptr;
50 
51  /* Unloaded library. */
52  /* -------------------- */
53  // Shared library exist.
54  BOOST_CHECK(l1 = new te::core::Library(g_shared_library_name, true));
55  delete l1;
56 
57  // Shared library do not exist.
58  BOOST_CHECK(l1 = new te::core::Library("fred.dll", true));
59  delete l1;
60 
61  // Name of the library is empty
62  BOOST_CHECK_THROW(l1 = new te::core::Library("", true), te::core::LibraryNameException);
63 
64  /* Loaded library. */
65  /* -------------------- */
66  // Shared library exist.
67  BOOST_CHECK(l1 = new te::core::Library(g_shared_library_path + "/" + g_shared_library_name));
68 
69  // Shared library does not exist.
70  BOOST_CHECK_THROW(l1 = new te::core::Library("fred.dll"), te::core::LibraryLoadException);
71 
72  return;
73 }
74 
75 BOOST_AUTO_TEST_CASE(test_destructor)
76 {
77  /* Testing destructor on a library not loaded. */
79 
80  BOOST_CHECK_NO_THROW(delete l);
81 
82  /* Testing destructor on a loaded library. */
84 
85  BOOST_CHECK_NO_THROW(delete l);
86 
87  return;
88 }
89 
91 {
92  /* Correct */
94  BOOST_CHECK_NO_THROW(l1.load());
95 
96  // Loading twice
97  BOOST_CHECK_NO_THROW(l1.load());
98 
99  // Shared library do not exist.
100  te::core::Library l2("fred.dll", true);
101  BOOST_CHECK_THROW(l2.load(), te::core::LibraryLoadException);
102 
103  return;
104 }
105 
107 {
108  /* Correct */
110  BOOST_CHECK_NO_THROW(l1.unload());
111 
112  /* Existing library but not loaded yet */
114  BOOST_CHECK_NO_THROW(l2.unload());
115 
116  // Shared library does not exist.
117  te::core::Library l3("fred.dll", true);
118  BOOST_CHECK_NO_THROW(l3.unload());
119 
120  return;
121 }
122 
123 BOOST_AUTO_TEST_CASE(test_isloaded)
124 {
125  /* Correct */
127  BOOST_CHECK(l1.isLoaded());
128  l1.unload();
129  BOOST_CHECK(!l1.isLoaded());
130  l1.load();
131  BOOST_CHECK(l1.isLoaded());
132 
133  /* Existing library but not loaded yet */
135  BOOST_CHECK(!l2.isLoaded());
136 
137  // Shared library do not exist.
138  te::core::Library l3("fred.dll", true);
139  BOOST_CHECK(!l3.isLoaded());
140 
141  return;
142 }
143 
144 BOOST_AUTO_TEST_CASE(test_getFileName)
145 {
146  /* Checking fileName */
147  std::string fileName(g_shared_library_path + "/" + g_shared_library_name);
148  te::core::Library l1(fileName);
149  BOOST_CHECK(l1.getFileName().compare(fileName) == 0);
150 
151  return;
152 }
153 
154 BOOST_AUTO_TEST_CASE(test_getAddress)
155 {
156  /* Correct */
158 
159  /* Testing get a function that exists. */
160  BOOST_CHECK(l1.getAddress("fatorial"));
161 
162  /* Testing get a function that does not exists. */
163  BOOST_CHECK_THROW(l1.getAddress("fatorializando"), te::core::LibrarySymbolNotFoundException);
164 
165  return;
166 }
167 
168 BOOST_AUTO_TEST_CASE(test_getNativeName)
169 {
170  /* Correct */
171  std::string name("terralib_unittest_core_lib_function");
172  std::string nname = te::core::Library::getNativeName(name);
173  std::string prefix,
174  suffix;
175 
176 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
177 #ifdef NDEBUG
178  suffix = ".dll";
179 #else
180  suffix = "d.dll";
181 #endif
182 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
183  prefix = "lib";
184  suffix = ".so";
185 #elif TE_PLATFORM == TE_PLATFORMCODE_APPLE
186  prefix = "lib";
187  suffix = ".dylib";
188 #else
189 #error "Platform not supported yet! Please contact terralib-team@dpi.inpe.br"
190 #endif
191 
192  std::string trueName = prefix + name + suffix;
193 
194  BOOST_CHECK(nname.compare(trueName) == 0);
195 
196  return;
197 }
198 
199 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
200 BOOST_AUTO_TEST_CASE(test_addSearchDir)
201 {
202  std::string lName = te::core::Library::getNativeName("terralib_unittest_core_lib_function");
203 
204  /* Correct */
205  BOOST_CHECK_NO_THROW(te::core::Library::addSearchDir(te::core::FindInTerraLibPath("example")));
206 
207  /* Empty path. */
209 
210  /* Path that does not exist. */
212 
213  /* Path with more characters than maximum size. */
214  BOOST_CHECK_THROW(te::core::Library::addSearchDir("X:/funcate/kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"), te::core::LibraryInvalidSearchPathException);
215 
216  /* Trying to load a library that can not be found by the operational system. */
217  BOOST_CHECK_THROW(te::core::Library(lName.c_str()), te::core::LibraryLoadException);
218 
220 
221  /* Trying to load a library that can be found by the operational system. */
222 
223  BOOST_CHECK_NO_THROW(te::core::Library(lName.c_str()));
224 
225  return;
226 }
227 
228 BOOST_AUTO_TEST_CASE(test_resetSearchDir)
229 {
230  /* Testing reset on an unchanged path. */
231  BOOST_CHECK_NO_THROW(te::core::Library::resetSearchPath());
232 
233  /* Testing load a shared library adding a search path. */
236  BOOST_CHECK_NO_THROW(l.load());
237 
238  l.unload();
239 
240  /* Testing load library after reset search path. */
243 
244  return;
245 }
246 #endif
247 
248 BOOST_AUTO_TEST_SUITE_END()
A class for handling shared libraries.
An exception indicating an error when loading a shared library.
This file is a wrapper around platform specific include files.
Specific exception types for Library Manager.
te::gm::LineString * l3
static void addSearchDir(const std::string &dir_name)
Add the informed dir to the path used by the operational system to lookup for shared libraries...
Definition: Library.cpp:279
void unload()
Force the unload of the shared library from memory.
Definition: Library.cpp:180
A class for handling shared libraries (DLLs, SO, DyLibs).
Definition: Library.h:73
Proxy file for the real file terralib_defines.h.
static const std::string g_shared_library_name(te::core::Library::getNativeName("terralib_unittest_core_lib_function"))
static void resetSearchPath()
Comes back the application lookup path to the original state, before any add_search_dir has been call...
Definition: Library.cpp:304
void * getAddress(const char *symbol) const
Return the address where the given symbol is loaded into memory.
Definition: Library.cpp:227
te::gm::LineString * l2
const std::string & getFileName() const
Return the shared library file name as informed in the constructor.
Definition: Library.cpp:221
BOOST_AUTO_TEST_SUITE(lib_test_case) BOOST_AUTO_TEST_CASE(test_constructor)
Definition: TsLibrary.cpp:45
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
static const std::string g_shared_library_path(te::core::FindInTerraLibPath("example"))
te::gm::LineString * l1
An exception indicating an error when adding a given path to the operational system search library pa...
BOOST_AUTO_TEST_CASE(test_destructor)
Definition: TsLibrary.cpp:75
An exception indicating an error when searching for a given symbol in a shared library.
bool isLoaded() const
Return true if the shared library is loaded otherwise return false.
Definition: Library.cpp:215
void load()
Load the shared library to memory.
Definition: Library.cpp:152
static std::string getNativeName(const std::string &name)
Given a shared library name without file extensions, prefixes and nor suffixes it will construct a li...
Definition: Library.cpp:256
An execption indicating an error when the library name is empty.