forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FacilitiesTest.h
210 lines (172 loc) · 8.59 KB
/
FacilitiesTest.h
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cxxtest/TestSuite.h>
#include <fstream>
#include <string>
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/FacilityInfo.h"
#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/Element.h>
#include <Poco/DOM/NodeList.h>
using namespace Mantid::Kernel;
class FacilitiesTest : public CxxTest::TestSuite {
public:
void test_throws_on_missing_facility_name() {
const std::string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<facilities>"
" <facility zeropadding=\"5\" "
"FileExtensions=\".nxs,.raw,.sav,.n*,.s*\">"
" </facility>"
"</facilities>";
TS_ASSERT_THROWS(getFacility(xmlStr), const std::runtime_error &);
}
void test_throws_if_no_file_extensions() {
const std::string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<facilities>"
" <facility name=\"MyFacility\">"
" </facility>"
"</facilities>";
TS_ASSERT_THROWS(getFacility(xmlStr), const std::runtime_error &);
}
void test_throws_if_no_instruments() {
const std::string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<facilities>"
" <facility name=\"MyFacility\" FileExtensions=\".xyz\">"
" </facility>"
"</facilities>";
TS_ASSERT_THROWS(getFacility(xmlStr), const std::runtime_error &);
}
void test_minimal() {
const std::string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<facilities>"
" <facility name=\"MyFacility\" FileExtensions=\".xyz\">"
" <instrument name=\"AnInst\">"
" <technique>Measuring Stuff</technique>"
" </instrument>"
" </facility>"
"</facilities>";
FacilityInfo *fac = nullptr;
TS_ASSERT_THROWS_NOTHING(fac = getFacility(xmlStr));
// Check that the few required things are set and that everything else has
// its default value
TS_ASSERT_EQUALS(fac->name(), "MyFacility");
TS_ASSERT_EQUALS(fac->zeroPadding(), 0);
TS_ASSERT(fac->delimiter().empty())
const std::vector<std::string> exts = fac->extensions();
TS_ASSERT_EQUALS(fac->extensions().size(), 1);
TS_ASSERT_EQUALS(fac->extensions()[0], ".xyz");
TS_ASSERT_EQUALS(fac->preferredExtension(), ".xyz");
TS_ASSERT(fac->archiveSearch().empty());
TS_ASSERT_EQUALS(fac->instruments().size(), 1);
TS_ASSERT_EQUALS(fac->instruments().front().name(), "AnInst");
TS_ASSERT_EQUALS(fac->instruments("Measuring Stuff").front().name(), "AnInst");
TS_ASSERT(fac->instruments("Nonsense").empty());
TS_ASSERT_EQUALS(fac->instrument("AnInst").name(), "AnInst");
TS_ASSERT_THROWS(fac->instrument("NoInst"), const Exception::NotFoundError &);
delete fac;
}
void testFacilities() {
const std::string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<facilities>"
" <facility name=\"ISIS\" zeropadding=\"5\" delimiter=\"%\" "
"FileExtensions=\".nxs,.raw,.sav,.n*,.s*\">"
" <archive>"
" <archiveSearch plugin=\"ADataSearch\" />"
" <archiveSearch plugin=\"BDataSearch\" />"
" </archive>"
" <instrument name=\"HRPD\" shortname=\"HRP\">"
" <technique>Powder Diffraction</technique>"
" </instrument>"
" <instrument name=\"WISH\" >"
" <zeropadding size=\"8\"/>"
" <zeropadding size=\"15\" startRunNumber=\"300\"/>"
" <technique>Powder Diffraction</technique>"
" <technique>Single Crystal Diffraction</technique>"
" </instrument>"
" </facility>"
"</facilities>";
FacilityInfo *fac = getFacility(xmlStr);
TS_ASSERT(fac);
TS_ASSERT_EQUALS(fac->name(), "ISIS");
TS_ASSERT_EQUALS(fac->zeroPadding(), 5);
TS_ASSERT_EQUALS(fac->delimiter(), "%");
const std::vector<std::string> exts = fac->extensions();
TS_ASSERT_EQUALS(exts.size(), 5);
TS_ASSERT_EQUALS(exts[0], ".nxs");
TS_ASSERT_EQUALS(exts[1], ".raw");
TS_ASSERT_EQUALS(exts[2], ".sav");
TS_ASSERT_EQUALS(exts[3], ".n*");
TS_ASSERT_EQUALS(exts[4], ".s*");
TS_ASSERT_EQUALS(fac->preferredExtension(), ".nxs");
TS_ASSERT_EQUALS(fac->archiveSearch().size(), 2);
std::vector<std::string>::const_iterator it = fac->archiveSearch().begin();
TS_ASSERT_EQUALS(*it, "ADataSearch");
TS_ASSERT_EQUALS(*++it, "BDataSearch");
const std::vector<InstrumentInfo> instrums = fac->instruments();
TS_ASSERT_EQUALS(instrums.size(), 2);
TS_ASSERT_THROWS_NOTHING(fac->instrument("HRPD"));
InstrumentInfo instr = fac->instrument("HRP"); // Tests getting by short name
TS_ASSERT_EQUALS(instr.name(), "HRPD");
TS_ASSERT_EQUALS(instr.shortName(), "HRP");
TS_ASSERT_EQUALS(instr.zeroPadding(123), 5);
TS_ASSERT_THROWS_NOTHING(fac->instrument("WISH"));
instr = fac->instrument("WISH");
TS_ASSERT_EQUALS(instr.name(), "WISH");
TS_ASSERT_EQUALS(instr.shortName(), "WISH");
TS_ASSERT_EQUALS(instr.zeroPadding(123), 8);
TS_ASSERT_EQUALS(instr.zeroPadding(301), 15);
const std::vector<InstrumentInfo> pwdInstr = fac->instruments("Powder Diffraction");
TS_ASSERT_EQUALS(pwdInstr.size(), 2);
const std::vector<InstrumentInfo> crysInstr = fac->instruments("Single Crystal Diffraction");
TS_ASSERT_EQUALS(crysInstr.size(), 1);
TS_ASSERT_EQUALS(fac->instruments("rubbish category").size(), 0);
delete fac;
}
void testConfigService() { TS_ASSERT_THROWS_NOTHING(ConfigService::Instance().getFacility("ISIS")); }
void testDefaultInstrument() {
ConfigService::Instance().setString("default.instrument", "HRPD");
InstrumentInfo instr = ConfigService::Instance().getInstrument();
TS_ASSERT_EQUALS(instr.name(), "HRPD");
}
void testFacilitiesArchiveMissing() {
const std::string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<facilities>"
" <facility name=\"ISIS\" zeropadding=\"5\" "
"FileExtensions=\".nxs,.raw,.sav,.n*,.s*\">"
" <instrument name=\"HRPD\" shortname=\"HRP\">"
" <technique>Powder Diffraction</technique>"
" </instrument>"
" <instrument name=\"WISH\" zeropadding=\"8\">"
" <technique>Powder Diffraction</technique>"
" <technique>Single Crystal Diffraction</technique>"
" </instrument>"
" </facility>"
"</facilities>";
FacilityInfo *fac = getFacility(xmlStr);
TS_ASSERT(fac);
TS_ASSERT_EQUALS(fac->name(), "ISIS");
TS_ASSERT_EQUALS(fac->archiveSearch().size(), 0);
delete fac;
}
private:
FacilityInfo *getFacility(const std::string &xmlStr) const {
Poco::XML::DOMParser parser;
Poco::AutoPtr<Poco::XML::Document> pDoc = parser.parseString(xmlStr);
TS_ASSERT(pDoc);
Poco::XML::Element *pRootElem = pDoc->documentElement();
Poco::AutoPtr<Poco::XML::NodeList> pNL_facility = pRootElem->getElementsByTagName("facility");
size_t n = pNL_facility->length();
TS_ASSERT(n > 0);
Poco::XML::Element *elem = dynamic_cast<Poco::XML::Element *>(pNL_facility->item(0));
TS_ASSERT(elem);
FacilityInfo *facility = new FacilityInfo(elem);
return facility;
}
};