~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Util/testsuite/src/ConfigurationMapperTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ConfigurationMapperTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Util/testsuite/src/ConfigurationMapperTest.cpp#1 $
 
5
//
 
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person or organization
 
10
// obtaining a copy of the software and accompanying documentation covered by
 
11
// this license (the "Software") to use, reproduce, display, distribute,
 
12
// execute, and transmit the Software, and to prepare derivative works of the
 
13
// Software, and to permit third-parties to whom the Software is furnished to
 
14
// do so, all subject to the following:
 
15
// 
 
16
// The copyright notices in the Software and this entire statement, including
 
17
// the above license grant, this restriction and the following disclaimer,
 
18
// must be included in all copies of the Software, in whole or in part, and
 
19
// all derivative works of the Software, unless such copies or derivative
 
20
// works are solely in the form of machine-executable object code generated by
 
21
// a source language processor.
 
22
// 
 
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
29
// DEALINGS IN THE SOFTWARE.
 
30
//
 
31
 
 
32
 
 
33
#include "ConfigurationMapperTest.h"
 
34
#include "CppUnit/TestCaller.h"
 
35
#include "CppUnit/TestSuite.h"
 
36
#include "Poco/Util/ConfigurationMapper.h"
 
37
#include "Poco/Util/MapConfiguration.h"
 
38
#include "Poco/AutoPtr.h"
 
39
#include "Poco/Exception.h"
 
40
#include <algorithm>
 
41
 
 
42
 
 
43
using Poco::Util::AbstractConfiguration;
 
44
using Poco::Util::ConfigurationMapper;
 
45
using Poco::Util::MapConfiguration;
 
46
using Poco::AutoPtr;
 
47
 
 
48
 
 
49
ConfigurationMapperTest::ConfigurationMapperTest(const std::string& name): CppUnit::TestCase(name)
 
50
{
 
51
}
 
52
 
 
53
 
 
54
ConfigurationMapperTest::~ConfigurationMapperTest()
 
55
{
 
56
}
 
57
 
 
58
 
 
59
void ConfigurationMapperTest::testMapper1()
 
60
{
 
61
        AutoPtr<AbstractConfiguration> pConf = createConfiguration();
 
62
        AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("", "", pConf);
 
63
        assert (pMapper->hasProperty("config.value1"));
 
64
        assert (pMapper->hasProperty("config.value2"));
 
65
 
 
66
        AbstractConfiguration::Keys keys;
 
67
        pMapper->keys(keys);
 
68
        assert (keys.size() == 1);
 
69
        assert (std::find(keys.begin(), keys.end(), "config") != keys.end());
 
70
 
 
71
        pMapper->keys("config", keys);
 
72
        assert (keys.size() == 3);
 
73
        assert (std::find(keys.begin(), keys.end(), "value1") != keys.end());
 
74
        assert (std::find(keys.begin(), keys.end(), "value2") != keys.end());
 
75
        assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());
 
76
        
 
77
        assert (pMapper->getString("config.value1") == "v1");
 
78
        assert (pMapper->getString("config.sub.value2") == "v4");
 
79
        
 
80
        pMapper->setString("config.value3", "v5");
 
81
        assert (pMapper->getString("config.value3") == "v5");
 
82
        assert (pConf->getString("config.value3") == "v5");
 
83
}
 
84
 
 
85
 
 
86
void ConfigurationMapperTest::testMapper2()
 
87
{
 
88
        AutoPtr<AbstractConfiguration> pConf = createConfiguration();
 
89
        AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("config", "root.conf", pConf);
 
90
 
 
91
        assert (pMapper->hasProperty("root.conf.value1"));
 
92
        assert (pMapper->hasProperty("root.conf.value2"));
 
93
 
 
94
        AbstractConfiguration::Keys keys;
 
95
        pMapper->keys(keys);
 
96
        assert (keys.size() == 1);
 
97
        assert (std::find(keys.begin(), keys.end(), "root") != keys.end());
 
98
 
 
99
        pMapper->keys("root", keys);
 
100
        assert (keys.size() == 1);
 
101
        assert (std::find(keys.begin(), keys.end(), "conf") != keys.end());
 
102
 
 
103
        pMapper->keys("root.conf", keys);
 
104
        assert (keys.size() == 3);
 
105
        assert (std::find(keys.begin(), keys.end(), "value1") != keys.end());
 
106
        assert (std::find(keys.begin(), keys.end(), "value2") != keys.end());
 
107
        assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());
 
108
 
 
109
        assert (pMapper->getString("root.conf.value1") == "v1");
 
110
        assert (pMapper->getString("root.conf.sub.value2") == "v4");
 
111
        
 
112
        pMapper->setString("root.conf.value3", "v5");
 
113
        assert (pMapper->getString("root.conf.value3") == "v5");
 
114
        assert (pConf->getString("config.value3") == "v5");
 
115
}
 
116
 
 
117
 
 
118
void ConfigurationMapperTest::testMapper3()
 
119
{
 
120
        AutoPtr<AbstractConfiguration> pConf = createConfiguration();
 
121
        AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("", "root", pConf);
 
122
 
 
123
        assert (pMapper->hasProperty("root.config.value1"));
 
124
        assert (pMapper->hasProperty("root.config.value2"));
 
125
 
 
126
        AbstractConfiguration::Keys keys;
 
127
        pMapper->keys(keys);
 
128
        assert (keys.size() == 1);
 
129
        assert (std::find(keys.begin(), keys.end(), "root") != keys.end());
 
130
 
 
131
        pMapper->keys("root", keys);
 
132
        assert (keys.size() == 1);
 
133
        assert (std::find(keys.begin(), keys.end(), "config") != keys.end());
 
134
 
 
135
        pMapper->keys("root.config", keys);
 
136
        assert (keys.size() == 3);
 
137
        assert (std::find(keys.begin(), keys.end(), "value1") != keys.end());
 
138
        assert (std::find(keys.begin(), keys.end(), "value2") != keys.end());
 
139
        assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());
 
140
        
 
141
        assert (pMapper->getString("root.config.value1") == "v1");
 
142
        assert (pMapper->getString("root.config.sub.value2") == "v4");
 
143
        
 
144
        pMapper->setString("root.config.value3", "v5");
 
145
        assert (pMapper->getString("root.config.value3") == "v5");
 
146
        assert (pConf->getString("config.value3") == "v5");
 
147
}
 
148
 
 
149
 
 
150
void ConfigurationMapperTest::testMapper4()
 
151
{
 
152
        AutoPtr<AbstractConfiguration> pConf = createConfiguration();
 
153
        AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("config", "", pConf);
 
154
 
 
155
        assert (pMapper->hasProperty("value1"));
 
156
        assert (pMapper->hasProperty("value2"));
 
157
 
 
158
        AbstractConfiguration::Keys keys;
 
159
        pMapper->keys(keys);
 
160
        assert (keys.size() == 3);
 
161
        assert (std::find(keys.begin(), keys.end(), "value1") != keys.end());
 
162
        assert (std::find(keys.begin(), keys.end(), "value2") != keys.end());
 
163
        assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());
 
164
        
 
165
        assert (pMapper->getString("value1") == "v1");
 
166
        assert (pMapper->getString("sub.value2") == "v4");
 
167
        
 
168
        pMapper->setString("value3", "v5");
 
169
        assert (pMapper->getString("value3") == "v5");
 
170
        assert (pConf->getString("config.value3") == "v5");
 
171
}
 
172
 
 
173
 
 
174
AbstractConfiguration* ConfigurationMapperTest::createConfiguration() const
 
175
{
 
176
        AbstractConfiguration* pConfig = new MapConfiguration;
 
177
        
 
178
        pConfig->setString("config.value1", "v1");
 
179
        pConfig->setString("config.value2", "v2");
 
180
        pConfig->setString("config.sub.value1", "v3");
 
181
        pConfig->setString("config.sub.value2", "v4");
 
182
        
 
183
        return pConfig;
 
184
}
 
185
 
 
186
 
 
187
void ConfigurationMapperTest::setUp()
 
188
{
 
189
}
 
190
 
 
191
 
 
192
void ConfigurationMapperTest::tearDown()
 
193
{
 
194
}
 
195
 
 
196
 
 
197
CppUnit::Test* ConfigurationMapperTest::suite()
 
198
{
 
199
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ConfigurationMapperTest");
 
200
 
 
201
        CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper1);
 
202
        CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper2);
 
203
        CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper3);
 
204
        CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper4);
 
205
 
 
206
        return pSuite;
 
207
}