summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/plugins/ConfigurationPluginTest.java
blob: 14c7b8cb2026c7c049fa6c085fadc21f4ed63d2a (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.server.configuration.plugins;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

import org.apache.qpid.server.util.InternalBrokerBaseCase;

import java.util.List;

/**
 * Test that verifies that given a Configuration a ConfigurationPlugin can
 * process and validate that data.
 */
public class ConfigurationPluginTest extends InternalBrokerBaseCase
{
    private static final double DOUBLE = 3.14;
    private static final long POSITIVE_LONG = 1000;
    private static final long NEGATIVE_LONG = -1000;
    private static final int LIST_SIZE = 3;

    class ConfigPlugin extends ConfigurationPlugin
    {
        @Override
        public String[] getElementsProcessed()
        {
            return new String[]{"[@property]", "name",
                                "positiveLong", "negativeLong",
                                "true", "list", "double"};
        }

        @Override
        public void validateConfiguration() throws ConfigurationException
        {
            // no validation requried
        }

        public String getName()
        {
            return getStringValue("name");
        }

        public String getProperty()
        {
            return getStringValue("[@property]");
        }


    }

    private ConfigPlugin _plugin;

    @Override
    public void setUp() throws Exception
    {
        // Test does not directly use the AppRegistry but the configured broker
        // is required for the correct ConfigurationPlugin processing
        super.setUp();
        XMLConfiguration xmlconfig = new XMLConfiguration();
        xmlconfig.addProperty("base.element[@property]", "property");
        xmlconfig.addProperty("base.element.name", "name");
        // We make these strings as that is how they will be read from the file.
        xmlconfig.addProperty("base.element.positiveLong", String.valueOf(POSITIVE_LONG));
        xmlconfig.addProperty("base.element.negativeLong", String.valueOf(NEGATIVE_LONG));
        xmlconfig.addProperty("base.element.boolean", String.valueOf(true));
        xmlconfig.addProperty("base.element.double", String.valueOf(DOUBLE));
        for (int i = 0; i < LIST_SIZE; i++)
        {
            xmlconfig.addProperty("base.element.list", i);
        }

        //Use a composite configuration as this is what our broker code uses.
        CompositeConfiguration composite = new CompositeConfiguration();
        composite.addConfiguration(xmlconfig);

        _plugin = new ConfigPlugin();

        try
        {
            _plugin.setConfiguration("base.element", composite.subset("base.element"));
        }
        catch (ConfigurationException e)
        {
            e.printStackTrace();
            fail(e.toString());
        }

    }

    public void testHasConfiguration()
    {
        assertTrue("Plugin has no configuration ", _plugin.hasConfiguration());
        _plugin = new ConfigPlugin();
        assertFalse("Plugins has configuration", _plugin.hasConfiguration());
    }

    public void testValuesRetreived()
    {
        assertEquals("Name not correct", "name", _plugin.getName());
        assertEquals("Property not correct", "property", _plugin.getProperty());
    }

    public void testContainsPositiveLong()
    {
        assertTrue("positiveLong is not positive", _plugin.containsPositiveLong("positiveLong"));
        assertFalse("NonExistentValue was found", _plugin.containsPositiveLong("NonExistentValue"));

        try
        {
            _plugin.validatePositiveLong("positiveLong");
        }
        catch (ConfigurationException e)
        {
            fail(e.getMessage());
        }

        try
        {
            _plugin.validatePositiveLong("negativeLong");
            fail("negativeLong should not be positive");
        }
        catch (ConfigurationException e)
        {
            assertEquals("negativeLong should not be reported as positive",
                         "ConfigPlugin: unable to configure invalid negativeLong:" + NEGATIVE_LONG, e.getMessage());
        }

    }

    public void testDouble()
    {
        assertEquals("Double value not returned", DOUBLE, _plugin.getDoubleValue("double"));
        assertEquals("default Double value not returned", 0.0, _plugin.getDoubleValue("NonExistent"));
        assertEquals("set default Double value not returned", DOUBLE, _plugin.getDoubleValue("NonExistent", DOUBLE));
    }

    public void testLong()
    {
        assertTrue("Long value not returned", _plugin.containsLong("positiveLong"));
        assertFalse("Long value returned", _plugin.containsLong("NonExistent"));
        assertEquals("Long value not returned", POSITIVE_LONG, _plugin.getLongValue("positiveLong"));
        assertEquals("default Long value not returned", 0, _plugin.getLongValue("NonExistent"));
        assertEquals("set default Long value not returned", NEGATIVE_LONG, _plugin.getLongValue("NonExistent", NEGATIVE_LONG));
    }

    public void testInt()
    {
        assertTrue("Int value not returned", _plugin.containsInt("positiveLong"));
        assertFalse("Int value returned", _plugin.containsInt("NonExistent"));
        assertEquals("Int value not returned", (int) POSITIVE_LONG, _plugin.getIntValue("positiveLong"));
        assertEquals("default Int value not returned", 0, _plugin.getIntValue("NonExistent"));
        assertEquals("set default Int value not returned", (int) NEGATIVE_LONG, _plugin.getIntValue("NonExistent", (int) NEGATIVE_LONG));
    }

    public void testString()
    {
        assertEquals("String value not returned", "name", _plugin.getStringValue("name"));
        assertNull("Null default String value not returned", _plugin.getStringValue("NonExistent", null));
        assertNull("default String value not returned", _plugin.getStringValue("NonExistent"));
        assertEquals("default String value not returned", "Default", _plugin.getStringValue("NonExistent", "Default"));
    }

    public void testBoolean()
    {
        assertTrue("Boolean value not returned", _plugin.containsBoolean("boolean"));
        assertFalse("Boolean value not returned", _plugin.containsBoolean("NonExistent"));
        assertTrue("Boolean value not returned", _plugin.getBooleanValue("boolean"));
        assertFalse("default String value not returned", _plugin.getBooleanValue("NonExistent"));
        assertTrue("set default String value not returned", _plugin.getBooleanValue("NonExistent", true));
    }

    public void testList()
    {
        assertTrue("list not found in plugin", _plugin.contains("list"));
        List list = _plugin.getListValue("list");
        assertNotNull("Returned list should not be null", list);
        assertEquals("List should not be empty", LIST_SIZE, list.size());

        list = _plugin.getListValue("NonExistent");
        assertNotNull("Returned list should not be null", list);
        assertEquals("List is not empty", 0, list.size());
    }

    public void testContains()
    {
        assertTrue("list not found in plugin", _plugin.contains("list"));
        assertFalse("NonExistent found in plugin", _plugin.contains("NonExistent"));
    }

}