File: ServerPropertiesTest.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 (548 lines) | stat: -rw-r--r-- 22,555 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*

   Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.ServerPropertiesTest

   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.derbynet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.AccessController;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Properties;
import junit.framework.Test;
import org.apache.derby.drda.NetworkServerControl;
import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.Derby;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.NetworkServerTestSetup;
import org.apache.derbyTesting.junit.SecurityManagerSetup;
import org.apache.derbyTesting.junit.TestConfiguration;

/** 
 * This test tests the derby.properties, system properties and command line
 * parameters to make sure the pick up settings in the correct order. 
 * Search order is:
 *     command line parameters
 *     System properties
 *     derby.properties
 *     default     
 * The command line should take precedence
 * 
 * The test also tests start server by specifying system properties without
 * values; in this case the server will use default values.
 */

public class ServerPropertiesTest  extends BaseJDBCTestCase {
    
    //create own policy file
    private static final String POLICY_FILE_NAME =
        "org/apache/derbyTesting/functionTests/tests/derbynet/ServerPropertiesTest.policy";
    private int[] portsSoFar;
    private int basePort;
    
    public ServerPropertiesTest(String name) {
        super(name);
        this.basePort = TestConfiguration.getCurrent().getPort();
    }
    
    public static Test suite()
    {
        BaseTestSuite suite = new BaseTestSuite("ServerPropertiesTest");
          
        if (!Derby.hasServer()) return suite;
        // don't run with JSR169 for 1. this is network server and
        // 2. the java executable may be named differently
        if (JDBC.vmSupportsJSR169()) return suite;
        
        // this fixture doesn't use a client/server setup, instead does the 
        // relevant starting/stopping inside the test
        // Add security manager policy that allows executing java commands
        suite.addTest(decorateTest("ttestSetPortPriority", 
                new String[] {}, new String[] {}, false));
        
        // test unfinished properties settings. 
        // decorateTest adds policy file and sets up properties
        // the properties settings are incorrect i.e. they have no value
        String[] badServerProperties = {
                "derby.drda.logConnections=",
                "derby.drda.traceAll=",
                "derby.drda.traceDirectory=",
                "derby.drda.keepAlive=",
                "derby.drda.timeSlice=",
                "derby.drda.host=",
                "derby.drda.portNumber=",
                "derby.drda.minThreads=",
                "derby.drda.maxThreads=",
                "derby.drda.startNetworkServer=",
                "derby.drda.debug="
                };
        // fixture hits error DRDA_MissingNetworkJar (Cannot find derbynet.jar) so,
        // only run with jars
        if (TestConfiguration.loadingFromJars()) 
            suite.addTest(decorateTest("ttestDefaultProperties", 
                badServerProperties, new String[] {}, true));
        
        // The other fixtures, testToggleTrace (trace on/off), 
        // and testToggleLogConnections (logconnections on/off)
        // can all use the default setup with adjusted policy
        
        // need english locale so we can compare command output for those tests 
        if (!Locale.getDefault().getLanguage().equals("en"))
            return suite;
        
        Test test = TestConfiguration
            .clientServerSuite(ServerPropertiesTest.class);
        
        // Install a security manager using the special policy file.
        test = decorateWithPolicy(test);
        suite.addTest(test);
        return suite;
    }
    
    public void tearDown() throws Exception {
        super.tearDown();
        if (portsSoFar != null)
        {
            for (int i = 0 ; i < portsSoFar.length ; i++)
            {
                try {
                    shutdownServer(portsSoFar[i], true);
                } catch (SQLException e) {
                    fail("could not shutdown server at port " + portsSoFar[i]);
                }
            }
            portsSoFar=null;
        }
    }
    
    /**
     * <p>
     * Compose the required decorators to bring up the server in the correct
     * configuration.
     * </p>
     */
    private static Test decorateTest(String testName, 
            String[] startupProperties, String[] startupArgs,
            boolean startServer)
    {
        ServerPropertiesTest spt = new ServerPropertiesTest(testName);
        String [] startupProps;
        if (startupProperties == null)
            startupProps = new String[] {};
        else
            startupProps = startupProperties;
        if (startupArgs == null)
            startupArgs = new String[]{};
        NetworkServerTestSetup networkServerTestSetup;
        if (startServer)
        {
            // start networkServer as a process
            networkServerTestSetup = new NetworkServerTestSetup(
                spt, startupProps, startupArgs, true);
        }
        else
        {
            // get networkserver setup but don't start anything
            networkServerTestSetup = new NetworkServerTestSetup(
                spt, true, false);
        }
        Test test = decorateWithPolicy(networkServerTestSetup);
        test = TestConfiguration.defaultServerDecorator(test);
        return test;
    }   
    
    // grant ALL FILES execute, and getPolicy permissions,
    // as well as write for the trace files.
    private static Test decorateWithPolicy(Test test) {
        //
        // Install a security manager using the initial policy file.
        //
        return new SecurityManagerSetup(test, POLICY_FILE_NAME);
    }
    
    private static Properties getTheProperties() {
        Properties p;
        try {
            p = NetworkServerTestSetup.getNetworkServerControl().getCurrentProperties();
        } catch (Exception e) {
            p = null; // should be ok to set to null (to satisfy compiler)
            // as fail will exit without further checks.
            e.printStackTrace();
            fail("unexpected exception getting properties from server");
        }
        return p;
    }

    // check for 1 Property/Value pair, passing in expected as a String
    // "<propertyName>=<expectedPropertyValue>"
    private static void verifyProperties(String expectedPropAndValue) {
        Properties p = getTheProperties();
        assertPropertyValueCorrect(p, expectedPropAndValue);
        p=null;
    }
    
    // check for an array of Property/Value pairs, passing each expected pair 
    // in as a String "<propertyName>=<expectedPropertyValue>"
    private static void verifyProperties(String[] expectedValues) { 
        Properties p = getTheProperties();
        // for debugging:
        for (int i=0 ; i<expectedValues.length; i++){
            println("expV: " + expectedValues[i]);
        }
        assertEquals(expectedValues.length , p.size());
        for ( int i = 0 ; i < expectedValues.length ; i++)
        {
            assertPropertyValueCorrect(p, expectedValues[i]);
        }
        p = null;
    }
    
    private static void assertPropertyValueCorrect(Properties p, String expectedPropAndValue) {
        String[] expPropValueString = expectedPropAndValue.split("=");
        try {
            // for debugging
            String actualPropertyValue = p.getProperty(expPropValueString[0]);
            println("exppropName: " + expPropValueString[0]);
            println("exppropValue: " + expPropValueString[1]);
            println("actualpropValue: " + actualPropertyValue);
            assertEquals(expPropValueString[1],actualPropertyValue);
        } catch(Exception e) {
            fail("could not find property " + expPropValueString[0]);
        }
    }
    
    /**
     *  Ping for the server to be up - or down.
     *  @param port port number to be used in the ping
     *  @param expectServerUp indicator whether the server is expected to be up
     */
    private boolean canPingServer(int port, boolean expectServerUp) 
    throws SQLException {
        
        boolean serverUp = false;
        try {
            serverUp = NetworkServerTestSetup.pingForServerUp(
                NetworkServerTestSetup.getNetworkServerControl(port), null,
                expectServerUp);
        } catch (Exception e) {
            fail("unexpected Exception while pinging");
        }
        return serverUp;
    }
    
    // obtain & shutdown the network server;
    // port needs to be passed in to verify it's down;
    private String shutdownServer(int port, boolean specifyPort) 
    throws SQLException {
        try {
            if (specifyPort)
            {
                NetworkServerControl nsctrl = 
                    NetworkServerTestSetup.getNetworkServerControl(port);
                nsctrl.shutdown();
            }
            else
            {
                NetworkServerControl nsctrl = 
                    NetworkServerTestSetup.getNetworkServerControlDefault();
                nsctrl.shutdown();
            }
        } catch (Exception e) {
            return "failed to shutdown server with API parameter (" + e + ")";
        }
        if (canPingServer(port,false)) {
            return "Can still ping server";
        }
        return null;
    }


    // obtain & start the network server without specifying port;
    // port needs to be passed in to verify it's up.
    public String startServer(int port, boolean specifyPort) 
    throws SQLException {
        try {
            if (specifyPort)
            {
                NetworkServerControl nsctrl = 
                    NetworkServerTestSetup.getNetworkServerControl(port);
                // For debugging, to make output come to console uncomment:
                //nsctrl.start(new PrintWriter(System.out, true));
                // and comment out:
                nsctrl.start(null);
                NetworkServerTestSetup.waitForServerStart(nsctrl);
            }
            else
            {
                NetworkServerControl nsctrl = 
                    NetworkServerTestSetup.getNetworkServerControlDefault();
                // For debugging, to make output come to console uncomment:
                //nsctrl.start(new PrintWriter(System.out, true));
                // and comment out:
                nsctrl.start(null);
                NetworkServerTestSetup.waitForServerStart(nsctrl);
            }
        } catch (Exception e) {
            return "failed to start server with port " + port;
        }
        // check that we have this server up now
        if (!canPingServer(port, true)) {
            return "Cannot ping server started with port set to " + port;
        }
        return null;
    }
    
   public void checkWhetherNeedToShutdown(int[] ports, String failReason) {
       
       portsSoFar = ports;
       if (!(failReason == null))
       {
           fail(failReason);
       }
   }
   
   /**
    * Execute command and verify that it completes successfully
    * @param Cmd array of java arguments for command
    * @throws InterruptedException
    * @throws IOException
    */
   private void  assertSuccessfulCmd(String expectedString, String[] Cmd) throws InterruptedException, IOException {
       assertExecJavaCmdAsExpected(new String[] {expectedString}, Cmd, 0);
   }

    /**
     *  Test port setting priority
     */
    public void ttestSetPortPriority() 
    throws SQLException, InterruptedException, IOException {
        // default is 1527. The test harness configuration would
        // use the API and add the port number. We want to test all
        // 4 mechanisms for specifying the port.
        // To ensure getting a unique port number, this test leaves open
        // each server for a bit.
        // as we need to test the default as well as with setting various
        // properties, this test can't rely on the testsetup.

        // So, first, bring default server down if up
        // Note: if the harness gets modified to accomodate splitting
        //    over different networkservers, there maybe something more
        //    appropriate than shutting down the default server.
        // we really expect the server to be down, let's
        // not do any waiting around
        NetworkServerTestSetup.setWaitTime(0);
        if (canPingServer(basePort, false)) {
            // for now, shutdown
            shutdownServer(basePort, false);
        }
        NetworkServerTestSetup.setDefaultWaitTime();

        // Wait until the server has shut down completely and released the
        // port.
        NetworkServerTestSetup.waitForAvailablePort();

        /* 
         * The port should be default, but it might not be, so the second argument
         * must be true to accommodate for every case.
         */
        String actionResult = startServer(basePort, true);
        checkWhetherNeedToShutdown(new int[] {basePort}, actionResult);
        
        // set derby.drda.portNumber to an alternate number in derby.properties
        int firstAlternatePort = TestConfiguration.getCurrent().getNextAvailablePort();
        final Properties derbyProperties = new Properties();
        derbyProperties.put("derby.drda.portNumber", 
                Integer.toString(firstAlternatePort));

        final String derbyHome = getSystemProperty("derby.system.home");
        boolean b = AccessController.doPrivileged
        (new java.security.PrivilegedAction<Boolean>(){
            public Boolean run(){
                boolean fail = false;
                try {
                    FileOutputStream propFile = 
                        new FileOutputStream(derbyHome + File.separator + "derby.properties");
                    derbyProperties.store(propFile,"testing derby.properties");
                    propFile.close();
                } catch (IOException ioe) {
                    fail = true;
                }
                return fail;
            }
        });
        if (b)
        {
            checkWhetherNeedToShutdown(new int[] {TestConfiguration.getCurrent().getPort()}, "failed to write derby.properties");
        }
        // have to shutdown engine to force read of derby.properties
        TestConfiguration.getCurrent().shutdownEngine();
        actionResult = startServer(firstAlternatePort, false);
        checkWhetherNeedToShutdown(new int[] {basePort, firstAlternatePort}, actionResult);

        final int secondAlternatePort = TestConfiguration.getCurrent().getNextAvailablePort();
        // Now set system properties.
        setSystemProperty("derby.drda.portNumber", 
            Integer.toString(secondAlternatePort));
        actionResult = startServer(secondAlternatePort, false);
        checkWhetherNeedToShutdown( new int[] {basePort, firstAlternatePort, secondAlternatePort},
            actionResult);
        
        // now try with specifying port
        // Note that we didn't unset the system property yet, nor did
        // we get rid of derby.properties...
        // command line parameter should take hold
        int thirdAlternatePort = TestConfiguration.getCurrent().getNextAvailablePort();
        actionResult = startServer(thirdAlternatePort, true);
        checkWhetherNeedToShutdown(new int[] {basePort, firstAlternatePort, secondAlternatePort,
            thirdAlternatePort}, actionResult);

        // now with -p. 
        int fourthAlternatePort = TestConfiguration.getCurrent().getNextAvailablePort();
        String[] commandArray = {"-Dderby.system.home=" + derbyHome,
            "org.apache.derby.drda.NetworkServerControl", "-p",
            String.valueOf(fourthAlternatePort).toString(), 
            "-noSecurityManager", "start"};
        execJavaCmd(commandArray);
        
        if (!canPingServer(fourthAlternatePort, true)) {
            actionResult = "Can not ping server specified with -p";
        }
        checkWhetherNeedToShutdown(new int[] {basePort, firstAlternatePort, secondAlternatePort,
            thirdAlternatePort, fourthAlternatePort}, actionResult);
                        
        // shutdown with -p
        commandArray = new String[] {"-Dderby.system.home=" + derbyHome,
                "org.apache.derby.drda.NetworkServerControl", "-p",
                String.valueOf(fourthAlternatePort).toString(), 
                "-noSecurityManager", "shutdown"};
        execJavaCmd(commandArray);

        if (canPingServer(fourthAlternatePort, false)) {
            actionResult = "Can still ping server specified with -p";
        }
        checkWhetherNeedToShutdown(new int[] {basePort, firstAlternatePort, secondAlternatePort,
            thirdAlternatePort, fourthAlternatePort}, actionResult);
            
        // shutdown with port specified in constructor
        actionResult = shutdownServer(thirdAlternatePort, true);
        checkWhetherNeedToShutdown( new int[] {basePort, firstAlternatePort, secondAlternatePort,
            thirdAlternatePort}, actionResult);
        
        // shutdown using System property
        actionResult = shutdownServer(secondAlternatePort, false);
        checkWhetherNeedToShutdown ( new int[] {basePort, firstAlternatePort, secondAlternatePort},
            actionResult);
        // remove system property
        removeSystemProperty("derby.drda.portNumber");

        // shutdown server with port set in derby.properties
        actionResult = shutdownServer(firstAlternatePort, false);
        checkWhetherNeedToShutdown ( new int[] {basePort, firstAlternatePort},
            actionResult);
        // remove derby.properties
        if (!PrivilegedFileOpsForTests.delete(
                new File(derbyHome, "derby.properties"))) {
            checkWhetherNeedToShutdown ( new int[] {basePort, firstAlternatePort},
                "unable to remove derby.properties");
        }
        // have to shutdown engine to force re-evaluation of derby.properties
        TestConfiguration.getCurrent().shutdownEngine();
        
        // shutdown the default server
        actionResult = shutdownServer(basePort, true);
        checkWhetherNeedToShutdown ( new int[] {basePort}, actionResult);
    }
    
    /**
     *   Test start server specifying system properties without values
     */
    public void ttestDefaultProperties() throws SQLException
    {
        //check that default properties are used
        verifyProperties(new String[] {
                "derby.drda.maxThreads=0", 
                "derby.drda.sslMode=off", 
                "derby.drda.keepAlive=true", 
                "derby.drda.minThreads=0", 
                "derby.drda.portNumber=" + 
                    String.valueOf(TestConfiguration.getCurrent().getPort()),
                "derby.drda.logConnections=false", 
                "derby.drda.timeSlice=0", 
                "derby.drda.startNetworkServer=false",
                "derby.drda.host=" +
                    String.valueOf(TestConfiguration.getCurrent().getHostName()), 
                "derby.drda.traceAll=false"});
    }
       
    /**
     *   Test trace command on - property traceAll should get set
     */
    public void testToggleTrace() 
    throws SQLException, IOException, InterruptedException
    {        
        // we only care about the traceAll property, the rest will be unchanged
        String expectedTraceOff = "derby.drda.traceAll=false";
        String expectedTraceOn = "derby.drda.traceAll=true";     
        
        verifyProperties(expectedTraceOff);     

        int port = TestConfiguration.getCurrent().getPort();

        String[] traceCmd = new String[] {
            "org.apache.derby.drda.NetworkServerControl", "trace", "on", "-p",String.valueOf(port) };
        assertSuccessfulCmd("Trace turned on for all sessions.", traceCmd);
        verifyProperties(expectedTraceOn);     

        traceCmd = new String[] {
                "org.apache.derby.drda.NetworkServerControl", "trace", "off", "-p",String.valueOf(port) };
        assertSuccessfulCmd("Trace turned off for all sessions", traceCmd);
        // traceAll should be back to false
        verifyProperties(expectedTraceOff);     
    }

    /**
     *   Test logconnections on
     */
    public void testToggleLogConnections() 
    throws SQLException, IOException, InterruptedException
    {
        // only care about the LogConnections property; the rest is unchanged
        String expectedLogConnectionsOff = "derby.drda.logConnections=false";
        String expectedLogConnectionsOn = "derby.drda.logConnections=true";
        
        verifyProperties(expectedLogConnectionsOff);     

        int port = TestConfiguration.getCurrent().getPort();

        String[] cmd = new String[] {
            "org.apache.derby.drda.NetworkServerControl", "logconnections", "on","-p",String.valueOf(port) };
        assertSuccessfulCmd("Log Connections turned on.", cmd);
        verifyProperties(expectedLogConnectionsOn);     

        cmd = new String[] {
                "org.apache.derby.drda.NetworkServerControl", "logconnections", "off", "-p",String.valueOf(port) };
        assertSuccessfulCmd("Log Connections turned off.", cmd);
        // traceAll should be back to false
        verifyProperties(expectedLogConnectionsOff);    
    }
}