TsFileSystem.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
5  applications.
6 
7  TerraLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License,
10  or (at your option) any later version.
11 
12  TerraLib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with TerraLib. See COPYING. If not, write to
19  TerraLib Team at <terralib-team@terralib.org>.
20  */
21 
22 /*!
23  \file terralib/unittest/core/filesystem/TsFileSystem.cpp
24 
25  \brief A test suit for the TerraLib Core FileSystem Module.
26 
27  \author Matheus Cavassan Zaglia.
28  */
29 
30 // STL
31 #include <string>
32 #include <fstream>
33 
34 // TerraLib
37 
38 // Boost
39 #include <boost/test/unit_test.hpp>
40 
42 
43 BOOST_AUTO_TEST_CASE(filesystem_test)
44 {
45  std::string utf8_path =
46  te::core::CharEncoding::toUTF8("../unittest/filesystem");
47 
48  BOOST_CHECK(te::core::FileSystem::createDirectory(utf8_path));
49  BOOST_CHECK(te::core::FileSystem::exists(utf8_path));
50  BOOST_CHECK(te::core::FileSystem::isDirectory(utf8_path));
51  BOOST_CHECK(te::core::FileSystem::isEmpty(utf8_path));
52 
53  // creating files to test some functions
54  std::string file1_str = utf8_path + "/file1.txt",
55  file2_str = utf8_path + "/file2.sh",
56  file3_str = utf8_path + "/file3.txt";
57  std::ofstream file1(file1_str);
58  std::ofstream file2(file2_str);
59  file1.close();
60  file2.close();
61 
62  BOOST_CHECK(te::core::FileSystem::extension(file1_str) == ".txt");
63  BOOST_CHECK(te::core::FileSystem::extension(file2_str) == ".sh");
64  BOOST_CHECK_NO_THROW(te::core::FileSystem::rename(file1_str, file3_str));
65  BOOST_CHECK_NO_THROW(te::core::FileSystem::copyFile(file3_str, file1_str));
66 
67  BOOST_CHECK(te::core::FileSystem::isRegularFile(file3_str));
68  std::vector<std::string> files =
70 
71  BOOST_CHECK(files.size() == 3);
72 
73  for(size_t i = 0; i < files.size(); ++i)
74  BOOST_CHECK(te::core::FileSystem::remove(files[i]));
75 
76  BOOST_CHECK(te::core::FileSystem::remove(utf8_path));
77 }
78 
79 BOOST_AUTO_TEST_SUITE_END()
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
static bool isDirectory(const std::string &path)
Checks if a given path in UTF-8 is a directory.
Definition: FileSystem.cpp:87
static std::vector< std::string > listDirectory(const std::string &path)
Lists a directory from a given path in UTF-8.
Definition: FileSystem.cpp:178
static bool isEmpty(const std::string &path)
Checks if a given path in UTF-8 is an empty directory.
Definition: FileSystem.cpp:93
static void rename(const std::string &old_p, const std::string &new_p)
Renames a file or directory from a given path in UTF-8.
Definition: FileSystem.cpp:171
static std::string extension(const std::string &path)
Retrives the extension of a given file path in UTF-8.
Definition: FileSystem.cpp:82
static bool remove(const std::string &path)
Removes a file or directory from a given path in UTF-8.
Definition: FileSystem.cpp:166
A class for handling system files and paths.
A class for handling character enconding/decoding.
static void copyFile(const std::string &from, const std::string &to)
Copies a file.
Definition: FileSystem.cpp:159
static bool createDirectory(const std::string &path)
Creates a directory from a given path in UTF-8.
Definition: FileSystem.cpp:147
BOOST_AUTO_TEST_SUITE(filesystem) BOOST_AUTO_TEST_CASE(filesystem_test)
static std::string toUTF8(const std::string &src)
Convert a string from a current locale encoding to UTF-8.
BOOST_AUTO_TEST_CASE(encoding_test_utf8_latin1)
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
Definition: FileSystem.cpp:98