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

« back to all changes in this revision

Viewing changes to Util/testsuite/src/LayeredConfigurationTest.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
// LayeredConfigurationTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Util/testsuite/src/LayeredConfigurationTest.cpp#2 $
 
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 "LayeredConfigurationTest.h"
 
34
#include "CppUnit/TestCaller.h"
 
35
#include "CppUnit/TestSuite.h"
 
36
#include "Poco/Util/LayeredConfiguration.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::LayeredConfiguration;
 
45
using Poco::Util::MapConfiguration;
 
46
using Poco::AutoPtr;
 
47
using Poco::NotFoundException;
 
48
using Poco::RuntimeException;
 
49
 
 
50
 
 
51
LayeredConfigurationTest::LayeredConfigurationTest(const std::string& name): CppUnit::TestCase(name)
 
52
{
 
53
}
 
54
 
 
55
 
 
56
LayeredConfigurationTest::~LayeredConfigurationTest()
 
57
{
 
58
}
 
59
 
 
60
 
 
61
void LayeredConfigurationTest::testEmpty()
 
62
{
 
63
        AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
 
64
        
 
65
        AbstractConfiguration::Keys keys;
 
66
        pLC->keys(keys);
 
67
        assert (keys.empty());
 
68
        
 
69
        assert (!pLC->hasProperty("foo"));
 
70
        try
 
71
        {
 
72
                pLC->setString("foo", "bar");
 
73
                fail("empty LayeredConfiguration - must throw");
 
74
        }
 
75
        catch (RuntimeException&)
 
76
        {
 
77
        }
 
78
        
 
79
        try
 
80
        {
 
81
                std::string s = pLC->getString("foo");
 
82
                fail("empty LayeredConfiguration - must throw");
 
83
        }
 
84
        catch (NotFoundException&)
 
85
        {
 
86
        }
 
87
}
 
88
 
 
89
 
 
90
void LayeredConfigurationTest::testOneLayer()
 
91
{
 
92
        AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
 
93
        AutoPtr<MapConfiguration> pMC = new MapConfiguration;
 
94
        
 
95
        pMC->setString("prop1", "value1");
 
96
        pMC->setString("prop2", "value2");
 
97
        
 
98
        pLC->addWriteable(pMC, 0);
 
99
 
 
100
        AbstractConfiguration::Keys keys;
 
101
        pLC->keys(keys);
 
102
        assert (keys.size() == 2);
 
103
        assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
 
104
        assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
 
105
        
 
106
        assert (pLC->getString("prop1") == "value1");
 
107
        assert (pLC->getString("prop2") == "value2");
 
108
 
 
109
        pLC->setString("prop3", "value3");
 
110
        assert (pLC->getString("prop3") == "value3");
 
111
}
 
112
 
 
113
 
 
114
void LayeredConfigurationTest::testTwoLayers()
 
115
{
 
116
        AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
 
117
        AutoPtr<MapConfiguration> pMC1 = new MapConfiguration;
 
118
        AutoPtr<MapConfiguration> pMC2 = new MapConfiguration;
 
119
        
 
120
        pMC1->setString("prop1", "value1");
 
121
        pMC1->setString("prop2", "value2");
 
122
        pMC2->setString("prop2", "value3");
 
123
        pMC2->setString("prop3", "value4");
 
124
        
 
125
        pLC->add(pMC1, 0);
 
126
        pLC->addWriteable(pMC2, 1);
 
127
 
 
128
        AbstractConfiguration::Keys keys;
 
129
        pLC->keys(keys);
 
130
        assert (keys.size() == 3);
 
131
        assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
 
132
        assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
 
133
        assert (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
 
134
        
 
135
        assert (pLC->getString("prop1") == "value1");
 
136
        assert (pLC->getString("prop2") == "value2");
 
137
        assert (pLC->getString("prop3") == "value4");
 
138
 
 
139
        pLC->setString("prop4", "value4");
 
140
        assert (pLC->getString("prop4") == "value4");
 
141
 
 
142
        assert (!pMC1->hasProperty("prop4"));
 
143
        assert (pMC2->hasProperty("prop4"));
 
144
 
 
145
        pLC->setString("prop1", "value11");
 
146
        assert (pLC->getString("prop1") == "value1");
 
147
        assert (pMC2->getString("prop1") == "value11");
 
148
}
 
149
 
 
150
 
 
151
void LayeredConfigurationTest::testThreeLayers()
 
152
{
 
153
        AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
 
154
        AutoPtr<MapConfiguration> pMC1 = new MapConfiguration;
 
155
        AutoPtr<MapConfiguration> pMC2 = new MapConfiguration;
 
156
        AutoPtr<MapConfiguration> pMC3 = new MapConfiguration;
 
157
        
 
158
        pMC1->setString("prop1", "value1");
 
159
        pMC1->setString("prop2", "value2");
 
160
        pMC1->setString("prop3", "value3");
 
161
        pMC2->setString("prop2", "value4");
 
162
        pMC2->setString("prop4", "value5");
 
163
        pMC3->setString("prop5", "value6");
 
164
        pMC3->setString("prop1", "value7");
 
165
        
 
166
        pLC->add(pMC1, 0);
 
167
        pLC->add(pMC2, 1);
 
168
        pLC->add(pMC3, -1);
 
169
        
 
170
        assert (pLC->getString("prop1") == "value7");
 
171
        assert (pLC->getString("prop2") == "value2");
 
172
        assert (pLC->getString("prop3") == "value3");
 
173
        assert (pLC->getString("prop4") == "value5");
 
174
        assert (pLC->getString("prop5") == "value6");
 
175
}
 
176
 
 
177
 
 
178
void LayeredConfigurationTest::setUp()
 
179
{
 
180
}
 
181
 
 
182
 
 
183
void LayeredConfigurationTest::tearDown()
 
184
{
 
185
}
 
186
 
 
187
 
 
188
CppUnit::Test* LayeredConfigurationTest::suite()
 
189
{
 
190
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("LayeredConfigurationTest");
 
191
 
 
192
        CppUnit_addTest(pSuite, LayeredConfigurationTest, testEmpty);
 
193
        CppUnit_addTest(pSuite, LayeredConfigurationTest, testOneLayer);
 
194
        CppUnit_addTest(pSuite, LayeredConfigurationTest, testTwoLayers);
 
195
        CppUnit_addTest(pSuite, LayeredConfigurationTest, testThreeLayers);
 
196
 
 
197
        return pSuite;
 
198
}