File: NetworkServerControlClientCommandTest.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 (172 lines) | stat: -rw-r--r-- 6,847 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
/*

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

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.IOException;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.Derby;
import org.apache.derbyTesting.junit.DerbyConstants;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.SecurityManagerSetup;
import org.apache.derbyTesting.junit.TestConfiguration;

public class NetworkServerControlClientCommandTest extends BaseJDBCTestCase {

    public NetworkServerControlClientCommandTest(String name) {
        super(name);
    }
    
    public void testPingWithoutArgs() throws InterruptedException, IOException {
        if (!hasDefaultDerbyPortUsing()) {
            /* If the port isn't the default one, we make sure that the test passes.
             * The -p parameter isn't specified here.
             * Changed to accomodate DERBY-4217
             */
            return;
        }        
        
        String[] pingWithoutArgsCmd = new String[] {
                "org.apache.derby.drda.NetworkServerControl", "ping" };
        
        pingWithoutArgsCmd = decorateCmdWithEnglishLocale(pingWithoutArgsCmd); 
        
        assertSuccessfulPing(pingWithoutArgsCmd);                
    }
    
    private boolean hasDefaultDerbyPortUsing() {
        return TestConfiguration.getCurrent().getPort() ==
                DerbyConstants.DEFAULT_DERBY_PORT;
    }

    /*
     * English locale is neccessary for running on non-English Locale.
     * See #Derby-4260
     */
    private String[] decorateCmdWithEnglishLocale(String[] cmd) {
        String[] newCmd = new String[cmd.length + 1];
        newCmd[0] = "-Dderby.ui.locale=en_US";
        
        System.arraycopy(cmd, 0, newCmd, 1, cmd.length);
        
        return newCmd;
    }
    
    public void testPingWithDefinedHost() throws InterruptedException, IOException {
        if (!hasDefaultDerbyPortUsing()) {
            /* If the port isn't the default one, we make sure that the test passes.
             * The -p parameter isn't specified here.
             * Changed to accomodate DERBY-4217
             */
            return;
        }        
        
        String currentHost = TestConfiguration.getCurrent().getHostName();
        String[] pingWithoutArgsCmd = new String[] {
                "org.apache.derby.drda.NetworkServerControl", "ping", "-h", currentHost};
                
        pingWithoutArgsCmd = decorateCmdWithEnglishLocale(pingWithoutArgsCmd);
                
        assertSuccessfulPing(pingWithoutArgsCmd);
    }
    
    public void testPingWithDefinedHostAndPort() throws InterruptedException, IOException {
        String currentPort = Integer.toString(TestConfiguration.getCurrent().getPort());
        String currentHost = TestConfiguration.getCurrent().getHostName();
        String[] pingWithoutArgsCmd = new String[] {
                "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
                currentHost, "-p", currentPort};
        
        pingWithoutArgsCmd = decorateCmdWithEnglishLocale(pingWithoutArgsCmd);
        
        assertSuccessfulPing(pingWithoutArgsCmd);
    }
    
    public void testPingWithWrongHost() throws InterruptedException, IOException {
        String[] pingWithoutArgsCmd = new String[] {
                "org.apache.derby.drda.NetworkServerControl",
                "ping", "-h", "nothere.invalid"};
                
        pingWithoutArgsCmd = decorateCmdWithEnglishLocale(pingWithoutArgsCmd);
                
        assertFailedPing(pingWithoutArgsCmd, "Unable to find host");
    }
    
    public void testPingWithBogusPort() throws InterruptedException, IOException {
        String currentHost = TestConfiguration.getCurrent().getHostName();
        String bogusPort = Integer.toString(
                TestConfiguration.getCurrent().getBogusPort());
        String[] pingWithoutArgsCmd = new String[] {
                "org.apache.derby.drda.NetworkServerControl",
                "ping", "-h", currentHost, "-p", bogusPort};
                
        pingWithoutArgsCmd = decorateCmdWithEnglishLocale(pingWithoutArgsCmd);
                
        assertFailedPing(pingWithoutArgsCmd, "Could not connect to Derby Network Server");
    }
    
    /**
     * Execute ping command and verify that it completes successfully
     * @param pingCmd array of java arguments for ping command
     * @throws InterruptedException
     * @throws IOException
     */
    private void  assertSuccessfulPing(String[] pingCmd) throws InterruptedException, IOException {
        assertExecJavaCmdAsExpected(new String[] {"Connection obtained"}, pingCmd, 0);
    }
    
    /**
     * Execute ping command and verify that it fails with the expected message
     * 
     * @param pingCmd array of java arguments for ping command
     * @param expectedMessage expected error message
     * @throws InterruptedException
     * @throws IOException
     */
    private void assertFailedPing(String[] pingCmd,String expectedMessage) throws InterruptedException, IOException {
        assertExecJavaCmdAsExpected(new String[] {expectedMessage}, pingCmd, 1);
    }
    

    public static Test suite() {

        BaseTestSuite suite =
            new BaseTestSuite("NetworkServerControlClientCommandTest");

        // need network server so we can compare command output 
        // and we don't run on J2ME because java command is different.
        if (!Derby.hasServer() ||
                JDBC.vmSupportsJSR169())
            return suite;
        
        Test test = TestConfiguration
                .clientServerSuite(NetworkServerControlClientCommandTest.class);
        
        // no security manager because we exec a process and don't have permission for that.
        test = SecurityManagerSetup.noSecurityManager(test);
        suite.addTest(test);
        
        return suite;
    }

}