comparison UnitTestsSources/FileStorage.cpp @ 632:17815b9d4280

rename the UnitTests directory to avoid clashes in filenames
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 28 Oct 2013 16:26:51 +0100
parents UnitTests/FileStorage.cpp@7c1faef915a4
children 9e3f21441903
comparison
equal deleted inserted replaced
631:fc6ad5b97219 632:17815b9d4280
1 #include "gtest/gtest.h"
2
3 #include <ctype.h>
4 #include <glog/logging.h>
5
6 #include "../Core/FileStorage/FileStorage.h"
7 #include "../OrthancServer/ServerIndex.h"
8 #include "../Core/Toolbox.h"
9 #include "../Core/OrthancException.h"
10 #include "../Core/Uuid.h"
11 #include "../Core/HttpServer/FilesystemHttpSender.h"
12 #include "../Core/HttpServer/BufferHttpSender.h"
13 #include "../Core/FileStorage/FileStorageAccessor.h"
14 #include "../Core/FileStorage/CompressedFileStorageAccessor.h"
15
16 using namespace Orthanc;
17
18 TEST(FileStorage, Basic)
19 {
20 FileStorage s("FileStorageUnitTests");
21
22 std::string data = Toolbox::GenerateUuid();
23 std::string uid = s.Create(data);
24 std::string d;
25 s.ReadFile(d, uid);
26 ASSERT_EQ(d.size(), data.size());
27 ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
28 }
29
30 TEST(FileStorage, EndToEnd)
31 {
32 FileStorage s("FileStorageUnitTests");
33 s.Clear();
34
35 std::list<std::string> u;
36 for (unsigned int i = 0; i < 10; i++)
37 {
38 u.push_back(s.Create(Toolbox::GenerateUuid()));
39 }
40
41 std::set<std::string> ss;
42 s.ListAllFiles(ss);
43 ASSERT_EQ(10u, ss.size());
44
45 unsigned int c = 0;
46 for (std::list<std::string>::iterator
47 i = u.begin(); i != u.end(); i++, c++)
48 {
49 ASSERT_TRUE(ss.find(*i) != ss.end());
50 if (c < 5)
51 s.Remove(*i);
52 }
53
54 s.ListAllFiles(ss);
55 ASSERT_EQ(5u, ss.size());
56
57 s.Clear();
58 s.ListAllFiles(ss);
59 ASSERT_EQ(0u, ss.size());
60 }
61
62
63 TEST(FileStorageAccessor, Simple)
64 {
65 FileStorage s("FileStorageUnitTests");
66 FileStorageAccessor accessor(s);
67
68 std::string data = "Hello world";
69 FileInfo info = accessor.Write(data, FileContentType_Dicom);
70
71 std::string r;
72 accessor.Read(r, info.GetUuid());
73
74 ASSERT_EQ(data, r);
75 ASSERT_EQ(CompressionType_None, info.GetCompressionType());
76 ASSERT_EQ(11u, info.GetUncompressedSize());
77 ASSERT_EQ(11u, info.GetCompressedSize());
78 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
79 }
80
81
82 TEST(FileStorageAccessor, NoCompression)
83 {
84 FileStorage s("FileStorageUnitTests");
85 CompressedFileStorageAccessor accessor(s);
86
87 accessor.SetCompressionForNextOperations(CompressionType_None);
88 std::string data = "Hello world";
89 FileInfo info = accessor.Write(data, FileContentType_Dicom);
90
91 std::string r;
92 accessor.Read(r, info.GetUuid());
93
94 ASSERT_EQ(data, r);
95 ASSERT_EQ(CompressionType_None, info.GetCompressionType());
96 ASSERT_EQ(11u, info.GetUncompressedSize());
97 ASSERT_EQ(11u, info.GetCompressedSize());
98 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
99 }
100
101
102 TEST(FileStorageAccessor, Compression)
103 {
104 FileStorage s("FileStorageUnitTests");
105 CompressedFileStorageAccessor accessor(s);
106
107 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
108 std::string data = "Hello world";
109 FileInfo info = accessor.Write(data, FileContentType_Dicom);
110
111 std::string r;
112 accessor.Read(r, info.GetUuid());
113
114 ASSERT_EQ(data, r);
115 ASSERT_EQ(CompressionType_Zlib, info.GetCompressionType());
116 ASSERT_EQ(11u, info.GetUncompressedSize());
117 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
118 }
119
120
121 TEST(FileStorageAccessor, Mix)
122 {
123 FileStorage s("FileStorageUnitTests");
124 CompressedFileStorageAccessor accessor(s);
125
126 std::string r;
127 std::string compressedData = "Hello";
128 std::string uncompressedData = "HelloWorld";
129
130 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
131 FileInfo compressedInfo = accessor.Write(compressedData, FileContentType_Dicom);
132
133 accessor.SetCompressionForNextOperations(CompressionType_None);
134 FileInfo uncompressedInfo = accessor.Write(uncompressedData, FileContentType_Dicom);
135
136 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
137 accessor.Read(r, compressedInfo.GetUuid());
138 ASSERT_EQ(compressedData, r);
139
140 accessor.SetCompressionForNextOperations(CompressionType_None);
141 accessor.Read(r, compressedInfo.GetUuid());
142 ASSERT_NE(compressedData, r);
143
144 /*
145 // This test is too slow on Windows
146 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
147 ASSERT_THROW(accessor.Read(r, uncompressedInfo.GetUuid()), OrthancException);
148 */
149 }