File: NetworkServerMBeanTest.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (317 lines) | stat: -rw-r--r-- 13,014 bytes parent folder | download | duplicates (4)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*

   Derby - Class org.apache.derbyTesting.functionTests.tests.management.NetworkServerMBeanTest

   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.derbyTesting.functionTests.tests.management;

import java.util.Hashtable;
import javax.management.ObjectName;
import junit.framework.Test;
import org.apache.derbyTesting.junit.TestConfiguration;

/**
 * <p>
 * This JUnit test class is for testing the NetworkServerMBean that is available
 * with the Derby Network Server. Running these tests requires a JVM supporting 
 * J2SE 5.0 or better, due to the implementation's dependency of the platform 
 * management agent.</p>
 * <p>
 * This class currently tests the following:</p>
 * <ul>
 *   <li>That the attributes we expect to be available exist</li>
 *   <li>That these attributes are readable</li>
 *   <li>That these attributes have the correct type</li>
 *   <li>That these attributes have the correct value (with some exceptions)</li>
 * </ul>
 * <p>
 * The test fixtures will fail if an exception occurs (will be reported as an 
 * error in JUnit).</p>
 */
public class NetworkServerMBeanTest extends MBeanTest {
    
    public NetworkServerMBeanTest(String name) {
        super(name);
    }
    
    public static Test suite() {
        
        // assumes that the suite will be wrapped by a setup or decorator
        // starting the network server.
        return MBeanTest.suite(NetworkServerMBeanTest.class, 
                                        "NetworkServerMBeanTest");
        
    }
    
    /**
     * <p>
     * Creates an object name instance for the Derby MBean whose object name's 
     * textual representation includes the following key properties:</p>
     * <ul>
     *   <li>type=NetworkServer</li>
     * </ul>
     * <p>
     * The object name may also include other key properties such as a system
     * identifier (DERBY-3466).</p>
     * @return the object name representing the NetworkServerMBean for the 
     *         Derby Network Server instance associated with this test 
     *         configuration.
     * @throws MalformedObjectNameException if the object name is not valid
     */
    private ObjectName getNetworkServerMBeanObjectName() 
            throws Exception {
        
        // get a reference to the NetworkServerMBean instance
        Hashtable<String, String> keyProps = new Hashtable<String, String>();
        keyProps.put("type", "NetworkServer");
        return getDerbyMBeanName(keyProps);
    }
    
        
    //
    // ---------- TEST FIXTURES ------------
    
    public void testAttributeAccumulatedConnectionCount() throws Exception {
        // TODO - make a connection or two and verify that the number increases
        Integer count = (Integer) getAttribute(
                getNetworkServerMBeanObjectName(), 
                "AccumulatedConnectionCount");
        assertNotNull(count);
        // allowing the possibility that there has been some server activity
        assertTrue(count >= 0);
    }

    public void testAttributeActiveConnectionCount() throws Exception {
        // TODO - make a connection or two and verify that the number changes
        Integer count = (Integer) getAttribute(
                getNetworkServerMBeanObjectName(), 
                "ActiveConnectionCount");
        assertNotNull(count);
        // allowing the possibility that there are active connections
        assertTrue(count >= 0);
    }

    public void testAttributeBytesReceived() throws Exception {
        // TODO - do some DB work and verify that the number increases
        Long bytesReceived = (Long) getAttribute(
                getNetworkServerMBeanObjectName(), 
                "BytesReceived");
        assertNotNull(bytesReceived);
        // allowing the possibility that there has been some server activity
        assertTrue(bytesReceived >= 0);
    }

    public void testAttributeBytesReceivedPerSecond() throws Exception {
        // TODO - do some DB work and verify that the number changes
        Integer bytesPerSec = (Integer) getAttribute(
                getNetworkServerMBeanObjectName(), 
                "BytesReceivedPerSecond");
        assertNotNull(bytesPerSec);
        // allowing the possibility that there has been some server activity
        assertTrue(bytesPerSec >= 0);
    }

    public void testAttributeBytesSent() throws Exception {
        // TODO - do some DB work and verify that the number increases
        Long bytesSent = (Long) getAttribute(
                getNetworkServerMBeanObjectName(), 
                "BytesSent");
        assertNotNull(bytesSent);
        // allowing the possibility that there has been some server activity
        assertTrue(bytesSent >= 0);
    }

    public void testAttributeBytesSentPerSecond() throws Exception {
        // TODO - do some DB work and verify that the number changes
        Integer bytesPerSec = (Integer) getAttribute(
                getNetworkServerMBeanObjectName(), 
                "BytesSentPerSecond");
        assertNotNull(bytesPerSec);
        // allowing the possibility that there has been some server activity
        assertTrue(bytesPerSec >= 0);
    }

    public void testAttributeConnectionCount() throws Exception {
        // TODO - connect to and disconnect from a DB and verify that the number changes
        Integer count = (Integer) getAttribute(
                getNetworkServerMBeanObjectName(), 
                "ConnectionCount");
        assertNotNull(count);
        // allowing the possibility that there is or has been some server activity
        assertTrue(count >= 0);

    }
    
    public void testAttributeConnectionThreadPoolSize() throws Exception {
        // TODO - connect to and disconnect from a DB and verify that the number changes
        Integer size = (Integer) getAttribute(
                getNetworkServerMBeanObjectName(), 
                "ConnectionThreadPoolSize");
        assertNotNull(size);
        // allowing the possibility that there has been some server activity
        assertTrue(size >= 0);
    }
    
    public void testAttributeDrdaHost() throws Exception {
        // localhost may also be 127.0.0.1
        // serverHost = expected host
        String serverHost = TestConfiguration.getCurrent().getHostName();
        if (serverHost.equals("localhost") || serverHost.equals("127.0.0.1") ||
                serverHost.equals(" 0:0:0:0:0:0:0:1")) {
            String mbeanHost = (String) getAttribute(
                getNetworkServerMBeanObjectName(), 
                "DrdaHost");
            assertNotNull(mbeanHost);
            assertTrue("mbeanHost = " + mbeanHost + " (not localhost, 127.0.0.1, or  0:0:0:0:0:0:0:1)", 
            mbeanHost.equals("localhost") 
                    || mbeanHost.equals("127.0.0.1") ||
                    mbeanHost.equals("0:0:0:0:0:0:0:1"));
        } else {
            assertStringAttribute(serverHost,
                    getNetworkServerMBeanObjectName(), 
                    "DrdaHost");
        }
    }
    
    public void testDrdaKeepAlive() throws Exception {
        // assumes that the default is kept and is true
        assertBooleanAttribute(true, 
                getNetworkServerMBeanObjectName(), 
                "DrdaKeepAlive");
    }
    
    public void testAttributeDrdaMaxThreads() throws Exception {
        // assuming the default, 0
        assertIntAttribute(0, 
                getNetworkServerMBeanObjectName(), 
                "DrdaMaxThreads");
    }
    
    public void testAttributeDrdaPortNumber() throws Exception {
        assertIntAttribute(TestConfiguration.getCurrent().getPort(), 
                getNetworkServerMBeanObjectName(), 
                "DrdaPortNumber");
    }
    
    public void testAttributeDrdaSecurityMechanism() throws Exception {
        // assuming no security mechanism
        assertStringAttribute("", 
                getNetworkServerMBeanObjectName(), 
                "DrdaSecurityMechanism");
    }

    public void testAttributeDrdaSslMode() throws Exception {
        // assuming that SSL is not enabled (off)
        assertStringAttribute("off", 
                getNetworkServerMBeanObjectName(), 
                "DrdaSslMode");
    }
    
    public void testAttributeDrdaStreamOutBufferSize() throws Exception {
        // assuming that the buffer size is 0 (default)
        assertIntAttribute(0,
                getNetworkServerMBeanObjectName(), 
                "DrdaStreamOutBufferSize");
    }
    
    public void testAttributeDrdaTimeSlice() throws Exception {
        // assuming 0 timeslice (default)
        assertIntAttribute(0, 
                getNetworkServerMBeanObjectName(), 
                "DrdaTimeSlice");
    }
    
    public void testAttributeDrdaTraceAll() throws Exception {
        // assuming that traceall is not set (default)
        assertBooleanAttribute(false, 
                getNetworkServerMBeanObjectName(), 
                "DrdaTraceAll");
    }
    
    public void testAttributeDrdaTraceDirectory() throws Exception {
        // assuming that the tracedirectory has not been set, meaning that it
        // is the value of derby.system.home, or user.dir (of the Network 
        // Server) if this has not been set.
        //
        // Temporary: NetworkServerTestSetup seems volatile in this area at the
        // moment (see derby-dev 2008-03-06); will defer value checking until
        // later.
          //assertStringAttribute("SomeDirectoryPath here", 
          //      getNetworkServerMBeanObjectName(), 
          //      "DrdaTraceDirectory");
        checkStringAttributeValue(getNetworkServerMBeanObjectName(), 
                "DrdaTraceDirectory");
    }
    
    public void testAttributeStartTime() throws Exception {
        // Haven't figured out how to test the actual value yet...
        // This will only check the attribute's existence, readability and 
        // return type.
        checkLongAttributeValue(getNetworkServerMBeanObjectName(), "StartTime");
    }
    
    public void testAttributeUptime() throws Exception {
        // Haven't figured out how to test the actual value yet...
        // This will only check the attribute's existence, readability and 
        // return type.
        checkLongAttributeValue(getNetworkServerMBeanObjectName(), "Uptime");
    }
    
    public void testAttributeWaitingConnectionCount() throws Exception {
        // assuming that no connections are waiting.
        assertIntAttribute(0, getNetworkServerMBeanObjectName(), 
                "WaitingConnectionCount");
    }
    
    /**
     * This method invokes the ping operation. Because this will currently
     * result in a security exception on the server side when running with Jars
     * (due to a lacking SocketPermission), the ping operation is actually
     * only invoked when running from the classes directory.
     * This is hopefully only a temporary solution...
     * 
     * @throws java.lang.Exception if the operation fails
     */
    public void testOperationPing() throws Exception {
        /* disabling the contents of this test fixture when running with 
         * jars, until the network server has the permission to connect to 
         * itself.
         * Otherwise we get a security exception (if the security manager has 
         * been enabled).
         * 
         *    java.net.SocketPermission <host>:<port> connect,resolve
         * 
         * Since the default server policy file doesn't work when running with
         * classes, the network server should in that case have been started 
         * with no security manager (see 
         * NetworkServerTestSetup#startSeparateProcess).
         */
        if (TestConfiguration.loadingFromJars()) {
            println("testOperationPing: Won't invoke the ping operation " +
                    "since the code has been loaded from the jars.");
            return;
        } 
        // if the server is not running, an exception will be thrown when
        // invoking the ping operation.
        // assumes noSecurityManager or that the required SocketPermission has
        // been given to the network server.
        invokeOperation(getNetworkServerMBeanObjectName(), "ping");
    }
    
}