summaryrefslogtreecommitdiffstats
blob: f7234d9102cb6b2502f0e3e1cb8659eb5c0bef72 (plain)
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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Parser/PayloadParserFactoryCollection.h>

using namespace Swift;

class PayloadParserFactoryCollectionTest : public CppUnit::TestFixture
{
        CPPUNIT_TEST_SUITE(PayloadParserFactoryCollectionTest);
        CPPUNIT_TEST(testGetPayloadParserFactory);
        CPPUNIT_TEST(testGetPayloadParserFactory_NoMatchingFactory);
        CPPUNIT_TEST(testGetPayloadParserFactory_TwoMatchingFactories);
        CPPUNIT_TEST(testGetPayloadParserFactory_MatchWithDefaultFactory);
        CPPUNIT_TEST(testGetPayloadParserFactory_NoMatchWithDefaultFactory);
        CPPUNIT_TEST_SUITE_END();

    public:
        PayloadParserFactoryCollectionTest() {}

        void setUp() {
        }

        void tearDown() {
        }

        void testGetPayloadParserFactory() {
            PayloadParserFactoryCollection testling;
            DummyFactory factory1("foo");
            testling.addFactory(&factory1);
            DummyFactory factory2("bar");
            testling.addFactory(&factory2);
            DummyFactory factory3("baz");
            testling.addFactory(&factory3);

            PayloadParserFactory* factory = testling.getPayloadParserFactory("bar", "", AttributeMap());

            CPPUNIT_ASSERT(factory == &factory2);
        }

        void testGetPayloadParserFactory_NoMatchingFactory() {
            PayloadParserFactoryCollection testling;
            DummyFactory factory("foo");

            CPPUNIT_ASSERT(!testling.getPayloadParserFactory("bar", "", AttributeMap()));
        }

        void testGetPayloadParserFactory_TwoMatchingFactories() {
            PayloadParserFactoryCollection testling;
            DummyFactory factory1("foo");
            testling.addFactory(&factory1);
            DummyFactory factory2("foo");
            testling.addFactory(&factory2);

            PayloadParserFactory* factory = testling.getPayloadParserFactory("foo", "", AttributeMap());

            CPPUNIT_ASSERT(factory == &factory2);
        }

        void testGetPayloadParserFactory_MatchWithDefaultFactory() {
            PayloadParserFactoryCollection testling;
            DummyFactory factory1("foo");
            testling.addFactory(&factory1);
            DummyFactory factory2;
            testling.setDefaultFactory(&factory2);

            PayloadParserFactory* factory = testling.getPayloadParserFactory("foo", "", AttributeMap());

            CPPUNIT_ASSERT(factory == &factory1);
        }

        void testGetPayloadParserFactory_NoMatchWithDefaultFactory() {
            PayloadParserFactoryCollection testling;
            DummyFactory factory1("foo");
            testling.addFactory(&factory1);
            DummyFactory factory2;
            testling.setDefaultFactory(&factory2);

            PayloadParserFactory* factory = testling.getPayloadParserFactory("baz", "", AttributeMap());

            CPPUNIT_ASSERT(factory == &factory2);
        }


    private:
        struct DummyFactory : public PayloadParserFactory {
            DummyFactory(const std::string& element = "") : element(element) {}
            virtual bool canParse(const std::string& e, const std::string&, const AttributeMap&) const {
                return element.empty() ? true : element == e;
            }
            virtual PayloadParser* createPayloadParser() { return nullptr; }
            std::string element;
        };
};

CPPUNIT_TEST_SUITE_REGISTRATION(PayloadParserFactoryCollectionTest);