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

« back to all changes in this revision

Viewing changes to Util/testsuite/src/XMLConfigurationTest.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
// XMLConfigurationTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Util/testsuite/src/XMLConfigurationTest.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 "XMLConfigurationTest.h"
 
34
#include "CppUnit/TestCaller.h"
 
35
#include "CppUnit/TestSuite.h"
 
36
#include "Poco/Util/XMLConfiguration.h"
 
37
#include "Poco/AutoPtr.h"
 
38
#include "Poco/Exception.h"
 
39
#include <sstream>
 
40
#include <algorithm>
 
41
 
 
42
 
 
43
using Poco::Util::XMLConfiguration;
 
44
using Poco::Util::AbstractConfiguration;
 
45
using Poco::AutoPtr;
 
46
using Poco::NotImplementedException;
 
47
using Poco::NotFoundException;
 
48
 
 
49
 
 
50
XMLConfigurationTest::XMLConfigurationTest(const std::string& name): CppUnit::TestCase(name)
 
51
{
 
52
}
 
53
 
 
54
 
 
55
XMLConfigurationTest::~XMLConfigurationTest()
 
56
{
 
57
}
 
58
 
 
59
 
 
60
void XMLConfigurationTest::testLoad()
 
61
{
 
62
        static const std::string xmlFile = 
 
63
                "<config>"
 
64
                "       <prop1>value1</prop1>"
 
65
                "       <prop2>value2</prop2>"
 
66
                "       <prop3>"
 
67
                "               <prop4 attr='value3'/>"
 
68
                "               <prop4 attr='value4'/>"
 
69
                "       </prop3>"
 
70
                "       <prop5>value5</prop5>"
 
71
                "       <prop5>value6</prop5>"
 
72
                "</config>";
 
73
                
 
74
        std::istringstream istr(xmlFile);       
 
75
        AutoPtr<XMLConfiguration> pConf = new XMLConfiguration(istr);
 
76
        
 
77
        assert (pConf->getString("prop1") == "value1");
 
78
        assert (pConf->getString("prop2") == "value2");
 
79
        assert (pConf->getString("prop3.prop4[@attr]") == "value3");
 
80
        assert (pConf->getString("prop3.prop4[1][@attr]") == "value4");
 
81
        assert (pConf->getString("prop5") == "value5");
 
82
        assert (pConf->getString("prop5[0]") == "value5");
 
83
        assert (pConf->getString("prop5[1]") == "value6");
 
84
        
 
85
        AbstractConfiguration::Keys keys;
 
86
        pConf->keys(keys);
 
87
        assert (keys.size() == 5);
 
88
        assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
 
89
        assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
 
90
        assert (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
 
91
        assert (std::find(keys.begin(), keys.end(), "prop5") != keys.end());
 
92
        assert (std::find(keys.begin(), keys.end(), "prop5[1]") != keys.end());
 
93
        
 
94
        pConf->keys("prop3", keys);
 
95
        assert (keys.size() == 2);
 
96
        assert (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
 
97
        assert (std::find(keys.begin(), keys.end(), "prop4[1]") != keys.end());
 
98
        
 
99
        try
 
100
        {
 
101
                pConf->setString("foo", "bar");
 
102
                fail("Not supported - must throw");
 
103
        }
 
104
        catch (NotImplementedException&)
 
105
        {
 
106
        }
 
107
        
 
108
        try
 
109
        {
 
110
                std::string s = pConf->getString("foo");
 
111
                fail("No property - must throw");
 
112
        }
 
113
        catch (NotFoundException&)
 
114
        {
 
115
        }
 
116
}
 
117
 
 
118
 
 
119
void XMLConfigurationTest::setUp()
 
120
{
 
121
}
 
122
 
 
123
 
 
124
void XMLConfigurationTest::tearDown()
 
125
{
 
126
}
 
127
 
 
128
 
 
129
CppUnit::Test* XMLConfigurationTest::suite()
 
130
{
 
131
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("XMLConfigurationTest");
 
132
 
 
133
        CppUnit_addTest(pSuite, XMLConfigurationTest, testLoad);
 
134
 
 
135
        return pSuite;
 
136
}