summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java
blob: 3e4c30291cb5b2ecf7a777a2f12aecb0fb56f2cd (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
/*
 *
 * 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;

import java.util.EnumSet;

import org.apache.qpid.test.utils.QpidTestCase;

/**
 * Test to verify the command line parsing within the Main class, by
 * providing it a series of command line arguments and verifying the
 * BrokerOptions emerging for use in starting the Broker instance.
 */
public class MainTest extends QpidTestCase
{
    public void testNoOptionsSpecified()
    {
        BrokerOptions options = startDummyMain("");

        assertTrue(options.getPorts().isEmpty());
        assertTrue(options.getSSLPorts().isEmpty());
        assertEquals(null, options.getJmxPortRegistryServer());
        assertEquals(null, options.getConfigFile());
        assertEquals(null, options.getLogConfigFile());
        assertEquals(null, options.getBind());

        for(ProtocolExclusion pe : EnumSet.allOf(ProtocolExclusion.class))
        {
            assertEquals(0, options.getExcludedPorts(pe).size());
        }
    }

    public void testPortOverriddenSingle()
    {
        BrokerOptions options = startDummyMain("-p 1234");

        assertTrue(options.getPorts().contains(1234));
        assertEquals(1, options.getPorts().size());
        assertTrue(options.getSSLPorts().isEmpty());
    }

    public void testPortOverriddenMultiple()
    {
        BrokerOptions options = startDummyMain("-p 1234 -p 4321");

        assertTrue(options.getPorts().contains(1234));
        assertTrue(options.getPorts().contains(4321));
        assertEquals(2, options.getPorts().size());
        assertTrue(options.getSSLPorts().isEmpty());
    }

    public void testSSLPortOverriddenSingle()
    {
        BrokerOptions options = startDummyMain("-s 5678");

        assertTrue(options.getSSLPorts().contains(5678));
        assertEquals(1, options.getSSLPorts().size());
        assertTrue(options.getPorts().isEmpty());
    }

    public void testSSLPortOverriddenMultiple()
    {
        BrokerOptions options = startDummyMain("-s 5678 -s 8765");

        assertTrue(options.getSSLPorts().contains(5678));
        assertTrue(options.getSSLPorts().contains(8765));
        assertEquals(2, options.getSSLPorts().size());
        assertTrue(options.getPorts().isEmpty());
    }

    public void testNonSSLandSSLPortsOverridden()
    {
        BrokerOptions options = startDummyMain("-p 5678 -s 8765");

        assertTrue(options.getPorts().contains(5678));
        assertTrue(options.getSSLPorts().contains(8765));
        assertEquals(1, options.getPorts().size());
        assertEquals(1, options.getSSLPorts().size());
    }

    public void testJmxPortRegistryServerOverridden()
    {
        BrokerOptions options = startDummyMain("--jmxregistryport 3456");

        assertEquals(Integer.valueOf(3456), options.getJmxPortRegistryServer());

         options = startDummyMain("-m 3457");
         assertEquals(Integer.valueOf(3457), options.getJmxPortRegistryServer());
    }

    public void testJmxPortConnectorServerOverridden()
    {
        BrokerOptions options = startDummyMain("--jmxconnectorport 3456");

        assertEquals(Integer.valueOf(3456), options.getJmxPortConnectorServer());
    }

    public void testExclude0_10()
    {
        BrokerOptions options = startDummyMain("-p 3456 --exclude-0-10 3456");

        assertTrue(options.getPorts().contains(3456));
        assertEquals(1, options.getPorts().size());
        assertTrue(options.getExcludedPorts(ProtocolExclusion.v0_10).contains(3456));
        assertEquals(1, options.getExcludedPorts(ProtocolExclusion.v0_10).size());
        assertEquals(0, options.getExcludedPorts(ProtocolExclusion.v0_9_1).size());
    }

    public void testConfig()
    {
        BrokerOptions options = startDummyMain("-c abcd/config.xml");

        assertEquals("abcd/config.xml", options.getConfigFile());
    }

    public void testLogConfig()
    {
        BrokerOptions options = startDummyMain("-l wxyz/log4j.xml");

        assertEquals("wxyz/log4j.xml", options.getLogConfigFile());
    }

    public void testLogWatch()
    {
        BrokerOptions options = startDummyMain("-w 9");

        assertEquals(9, options.getLogWatchFrequency());
    }

    private BrokerOptions startDummyMain(String commandLine)
    {
        return (new TestMain(commandLine.split("\\s"))).getOptions();
    }

    private class TestMain extends Main
    {
        private BrokerOptions _options;

        public TestMain(String[] args)
        {
            super(args);
        }

        @Override
        protected void startBroker(BrokerOptions options)
        {
            _options = options;
        }

        @Override
        protected void setExceptionHandler()
        {
        }

        public BrokerOptions getOptions()
        {
            return _options;
        }
    }
}