1//
2// NameValueCollectionTest.cpp
3//
4// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "NameValueCollectionTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Net/NameValueCollection.h"
15#include "Poco/Exception.h"
16
17
18using Poco::Net::NameValueCollection;
19using Poco::NotFoundException;
20
21
22NameValueCollectionTest::NameValueCollectionTest(const std::string& name): CppUnit::TestCase(name)
23{
24}
25
26
27NameValueCollectionTest::~NameValueCollectionTest()
28{
29}
30
31
32void NameValueCollectionTest::testNameValueCollection()
33{
34 NameValueCollection nvc;
35
36 assertTrue (nvc.empty());
37 assertTrue (nvc.size() == 0);
38
39 nvc.set("name", "value");
40 assertTrue (!nvc.empty());
41 assertTrue (nvc["name"] == "value");
42 assertTrue (nvc["Name"] == "value");
43
44 nvc.set("name2", "value2");
45 assertTrue (nvc.get("name2") == "value2");
46 assertTrue (nvc.get("NAME2") == "value2");
47
48 assertTrue (nvc.size() == 2);
49
50 try
51 {
52 std::string value = nvc.get("name3");
53 fail("not found - must throw");
54 }
55 catch (NotFoundException&)
56 {
57 }
58
59 try
60 {
61 std::string value = nvc["name3"];
62 fail("not found - must throw");
63 }
64 catch (NotFoundException&)
65 {
66 }
67
68 assertTrue (nvc.get("name", "default") == "value");
69 assertTrue (nvc.get("name3", "default") == "default");
70
71 assertTrue (nvc.has("name"));
72 assertTrue (nvc.has("name2"));
73 assertTrue (!nvc.has("name3"));
74
75 nvc.add("name3", "value3");
76 assertTrue (nvc.get("name3") == "value3");
77
78 nvc.add("name3", "value31");
79
80 NameValueCollection::ConstIterator it = nvc.find("Name3");
81 assertTrue (it != nvc.end());
82 std::string v1 = it->second;
83 assertTrue (it->first == "name3");
84 ++it;
85 assertTrue (it != nvc.end());
86 std::string v2 = it->second;
87 assertTrue (it->first == "name3");
88
89 assertTrue ((v1 == "value3" && v2 == "value31") || (v1 == "value31" && v2 == "value3"));
90
91 nvc.erase("name3");
92 assertTrue (!nvc.has("name3"));
93 assertTrue (nvc.find("name3") == nvc.end());
94
95 it = nvc.begin();
96 assertTrue (it != nvc.end());
97 ++it;
98 assertTrue (it != nvc.end());
99 ++it;
100 assertTrue (it == nvc.end());
101
102 nvc.clear();
103 assertTrue (nvc.empty());
104
105 assertTrue (nvc.size() == 0);
106}
107
108
109void NameValueCollectionTest::setUp()
110{
111}
112
113
114void NameValueCollectionTest::tearDown()
115{
116}
117
118
119CppUnit::Test* NameValueCollectionTest::suite()
120{
121 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NameValueCollectionTest");
122
123 CppUnit_addTest(pSuite, NameValueCollectionTest, testNameValueCollection);
124
125 return pSuite;
126}
127