File: TestFileSystem.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (119 lines) | stat: -rwxr-xr-x 3,079 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

#include "System/FileSystem/FileSystem.h"

#include <string>
#include <cstdio>

#define BOOST_TEST_MODULE FileSystem
#include <boost/test/unit_test.hpp>


namespace {
	struct PrepareFileSystem {
		PrepareFileSystem() {
			oldDir = FileSystem::GetCwd();
			testCwd = GetTempDir();
			if (!testCwd.empty()) {
				FileSystem::ChDir(testCwd);

				// create files and dirs to test
				FileSystem::CreateDirectory("testDir");
				WriteFile("testFile.txt", "a");
			}
		}
		~PrepareFileSystem() {
			if (!testCwd.empty()) {
				// delete files and dirs created in the ctor
				FileSystem::DeleteFile("testFile.txt");
				FileSystem::DeleteFile("testDir");
			}

			FileSystem::ChDir(oldDir);
			if (!testCwd.empty()) {
				FileSystem::DeleteFile(testCwd);
			}
		}
		static void WriteFile(const std::string& filePath, const std::string& content) {
			FILE* testFile = fopen(filePath.c_str(), "w");
			if (testFile != NULL) {
				fprintf(testFile, "%s", content.c_str());
				fclose(testFile);
				testFile = NULL;
			} else {
				BOOST_FAIL("Failed to create test-file " + filePath);
			}
		}
		std::string GetTempDir() {
			if (testCwd.empty()) {
				char* tmpDir = tmpnam(NULL);
				if (tmpDir != NULL) {
					testCwd = tmpDir;
					FileSystem::CreateDirectory(testCwd);
				} else {
					BOOST_FAIL("Failed to create temporary test dir");
				}
			}
			return testCwd;
		}

		std::string testCwd;
	private:
		std::string oldDir;
	};
}

BOOST_FIXTURE_TEST_SUITE(everything, PrepareFileSystem)


BOOST_AUTO_TEST_CASE(FileExists)
{
	BOOST_CHECK(FileSystem::FileExists("testFile.txt"));
	BOOST_CHECK(!FileSystem::FileExists("testFile99.txt"));
	BOOST_CHECK(!FileSystem::FileExists("testDir"));
	BOOST_CHECK(!FileSystem::FileExists("testDir99"));
}


BOOST_AUTO_TEST_CASE(GetFileSize)
{
	BOOST_CHECK(FileSystem::GetFileSize("testFile.txt") == 1);
	BOOST_CHECK(FileSystem::GetFileSize("testFile99.txt") == 0);
	BOOST_CHECK(FileSystem::GetFileSize("testDir") > 0);
	BOOST_CHECK(FileSystem::GetFileSize("testDir99") == 0);
}

BOOST_AUTO_TEST_CASE(CreateDirectory)
{
	BOOST_CHECK(FileSystem::CreateDirectory("testDir")); // already exists
	BOOST_CHECK(FileSystem::CreateDirectory("testDir1")); // should be created
	FileSystem::DeleteFile("testDir1");
	BOOST_CHECK(!FileSystem::CreateDirectory("testFile.txt")); // file with this name already exists
}


BOOST_AUTO_TEST_CASE(GetDirectory)
{
#define CHECK_DIR_EXTRACTION(path, dir) \
		BOOST_CHECK(FileSystem::GetDirectory(path) == dir)

	CHECK_DIR_EXTRACTION("testFile.txt", "");
	CHECK_DIR_EXTRACTION("./foo/testFile.txt", "./foo/");

#undef CHECK_DIR_EXTRACTION
}


BOOST_AUTO_TEST_CASE(GetNormalizedPath)
{
#define CHECK_NORM_PATH(path, normPath) \
		BOOST_CHECK(FileSystem::GetNormalizedPath(path) == normPath)

	CHECK_NORM_PATH("/home/userX/.spring/foo/bar///./../test.log", "/home/userX/.spring/foo/test.log");
	CHECK_NORM_PATH("./symLinkToHome/foo/bar///./../test.log", "./symLinkToHome/foo/test.log");
	CHECK_NORM_PATH("C:\\foo\\bar\\\\\\.\\..\\test.log", "C:/foo/test.log");

#undef CHECK_NORM_PATH
}

BOOST_AUTO_TEST_SUITE_END()