File: RuntimeInfoTest.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 (366 lines) | stat: -rw-r--r-- 13,764 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
/*

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

   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.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Locale;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.LocaleTestSetup;
import org.apache.derbyTesting.junit.NetworkServerTestSetup;
import org.apache.derbyTesting.junit.SecurityManagerSetup;
import org.apache.derbyTesting.junit.TestConfiguration;

/**
 This tests the runtimeinfo command
 */

public class RuntimeInfoTest extends BaseJDBCTestCase {

	private static String[] RuntimeinfoCmd = new String[] {
            "-Demma.verbosity.level=silent",
			"org.apache.derby.drda.NetworkServerControl", "runtimeinfo",
			"-p", String.valueOf(TestConfiguration.getCurrent().getPort()) };
	private static String[] RuntimeinfoLocaleCmd = new String[] {
            "-Demma.verbosity.level=silent",
			"-Duser.language=err", "-Duser.country=DE",
			"org.apache.derby.drda.NetworkServerControl", "runtimeinfo",
			"-p", String.valueOf(TestConfiguration.getCurrent().getPort()) };
	
    private static final String POLICY_FILE_NAME =
        "org/apache/derbyTesting/functionTests/tests/derbynet/RuntimeInfoTest.policy";

    private static final Locale englishLocale = new Locale("en", "US");
    private static final Locale germanLocale = new Locale("de", "DE");
    private static final String stdout_err_tags = "<[^<>]*STD.*>";
	
	/**
	 * Constructor
	 * 
	 * @param name
	 */
	public RuntimeInfoTest(String name) {
		super(name);
	}

	/**
	 * Creates a suite with two testcases, with and without some extra 
	 * system properties.
	 * 
	 * @return The test suite with both English and German locales.
	 */
	public static Test suite() {
        BaseTestSuite suite = new BaseTestSuite("RuntimeInfoTest");

        // Run testRunTests in both English and German locale
        suite.addTest(decorateTest(englishLocale, "testRunTests"));
        suite.addTest(decorateTest(germanLocale, "testRunTests"));

        // Other test cases, only tested in a single locale.
        suite.addTest(
                decorateTest(englishLocale, "testRuntimeInfoWithLongValues"));

		return suite;
	}	
	
	/**
	 * This is the wrapper that calls the x_tests in order.
	 * These fixtures rely on the order of the commands being issued.
	 */
	public void testRunTests() throws Exception {
		x_testRuntimeInfoWithActiveConn();
		x_testRuntimeInfoLocale();
		x_testRuntimeInfoAfterConnClose();
	}
	
	/**
	 * Test runtimeinfo
	 * 
	 * @throws Exception
	 */
	public void x_testRuntimeInfoWithActiveConn() throws Exception {
		Process p = execJavaCmd(RuntimeinfoCmd);
		String output = sed(readProcessOutput(p));
		
		printIfVerbose("testRuntimeInfo", output);
		
		String expectedOutput = ((HashMap)outputs.get(Locale.getDefault())).get("RuntimeInfoWithActiveConn").toString();
		assertEquals("Output doesn't match", expectedOutput, output);
	}
	
	/**
	 * Test runtimeinfo w/ foreign (non-English) locale
	 */
	public void x_testRuntimeInfoLocale() throws Exception {      
		Connection conn1 = getConnection();
		// Now get a couple of connections with some prepared statements
		Connection conn2 = openDefaultConnection();
		PreparedStatement ps = prepareAndExecuteQuery(conn1,
				"SELECT count(*) from sys.systables");
		PreparedStatement ps2 = prepareAndExecuteQuery(conn1, "VALUES(1)");

		Connection conn3 = openDefaultConnection();
		PreparedStatement ps3 = prepareAndExecuteQuery(conn2,
				"SELECT count(*) from sys.systables");
		PreparedStatement ps4 = prepareAndExecuteQuery(conn2, "VALUES(2)");

		Process p = execJavaCmd(RuntimeinfoLocaleCmd);
		String output = sed(readProcessOutput(p));
		
		printIfVerbose("testRuntimeInfoLocale", output);
		
		int matched = 0;
		String matchString = "\tSYSLH0001\tSELECT count(*) from sys.systables\n	SYSLH0002\tVALUES(1)\n";
		String invertedMatchString = "\tSYSLH0002\tVALUES(1)\n\tSYSLH0001\tSELECT count(*) from sys.systables\n";
		
		/* The IF clause accomodates for the different order that the result may have */ 
		matched = output.indexOf(matchString);
		if (matched == -1) { /* The order was incorrect, try the other one */
			matched = output.indexOf(invertedMatchString);
			assertTrue(matched != -1);
		}
		
		matched = 0;
		matchString = "\tSYSLH0001\tSELECT count(*) from sys.systables\n	SYSLH0002\tVALUES(2)\n";
		invertedMatchString = "\tSYSLH0002\tVALUES(2)\n\tSYSLH0001\tSELECT count(*) from sys.systables\n";
		
		/* Same as above, but with VALUES(2) */ 
		matched = output.indexOf(matchString);
		if (matched == -1) { /* The order was incorrect, try the other one */
			matched = output.indexOf(invertedMatchString);
			assertTrue(matched != -1);
		}
		
		/* Match the empty session */
		matched = 0;
		matchString = ((HashMap)outputs.get(Locale.getDefault())).get("RuntimeInfoLocaleString").toString();
		
		assertTrue(output.indexOf(matchString) != -1);

		ps.close();
		ps2.close();
		ps3.close();
		ps4.close();
		conn1.close();
		conn2.close();
		conn3.close();

	}

	/**
	 * once more after closing the connections 
	 * - by calling NetworkServerControl.getRuntimeInfo 
	 * @throws Exception
	 */
	public void x_testRuntimeInfoAfterConnClose() throws Exception {

        String expectedOutput =
            outputs.get(Locale.getDefault()).get("RuntimeInfoAfterConnClose");

        // DERBY-1455 and DERBY-6701: The closed connections may not be
        // cleaned up by the network server immediately. Retry the
        // getRuntimeInfo() call for up to one minute until we get the
        // expected response.
        String s = null;
        int retriesLeft = 60;
        while (true) {
            s = sed(NetworkServerTestSetup
					.getNetworkServerControl(TestConfiguration.getCurrent().getPort())
                    .getRuntimeInfo());

            // Keep retrying until we either get the expected response, or
            // we reach the maximum number of retries.
            if (expectedOutput.equals(s) || (--retriesLeft <= 0)) {
                break;
            }

            sleep(1000L);
        }

		NetworkServerTestSetup.getNetworkServerControl().shutdown();
		
		printIfVerbose("testRuntimeInfoMethod", s);
		
		assertEquals("Output doesn't match", expectedOutput, s);
	}

    /**
     * Regression test case for DERBY-6456, which caused an infinite loop if
     * the runtimeinfo output was more than 32KB.
     */
    public void testRuntimeInfoWithLongValues() throws Exception {
        // First open many connections on the server, so that the reply from
        // getRuntimeInfo() will be long.
        for (int i = 0; i < 200; i++) {
            prepareAndExecuteQuery(openDefaultConnection(),
                "VALUES 'Hello, World! How are you today?',\n"
              + "'Not that bad today, actually. Thanks for asking.'\n"
              + "-- Let's add some more text to increase the output length.\n"
              + "-- And even more here... The statement text, including this\n"
              + "-- comment, will be included in the runtimeinfo output.\n");
        }

        // This call used to hang.
        String runtimeinfo =
            NetworkServerTestSetup.getNetworkServerControl().getRuntimeInfo();

        // For debugging:
        println(runtimeinfo);

        // Output gets truncated to 65535 bytes (DERBY-5220).
        assertEquals(65535, runtimeinfo.length());
    }

	public static PreparedStatement prepareAndExecuteQuery(Connection conn,
			String sql) throws SQLException {
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		rs.next();
		return ps;
	}
	
	/**
     * Replace memory values in the output string
     * Removes output tags for STDOUT and STDERR from readProcessOutput
     * Also trims the string to make it easier to compare.
     * 
     * @param s the string to remove lines from
     * @return the string with the lines removed
     */
    private String sed(String s) {
        HashMap<String, String> strings = outputs.get(Locale.getDefault());
        s = s.replaceAll(strings.get("sedMemorySearch"),
                         strings.get("sedMemoryReplace"));
        s = s.replaceAll(strings.get("sedSessionNumberSearch"),
                         strings.get("sedSessionNumberReplace"));
		s = s.replaceAll(stdout_err_tags, "");
		s = s.trim();
		return s;
    }
    
    /**
     * Prints strings to System.out to make it easier to update the tests
     * when the output changes. BaseTestCase.println() only prints when on VERBOSE
     * 
     * @param name just a label to identify the string
     * @param s the string to be printed
     */
    private void printIfVerbose(String name,String s) {
		println("\n\n>>>" + name + ">>>");
		println(s);
		println("<<<" + name + "<<<\n\n");
    }

	/**
	 * Decorate a test with SecurityManagerSetup, clientServersuite, and
	 * SupportFilesSetup.
	 * 
	 * @return the decorated test
	 */
    private static Test decorateTest(Locale serverLocale, String testName) {
        Test test = new RuntimeInfoTest(testName);

        test = TestConfiguration.clientServerDecorator(test);
        
        /* A single use database must be used to ensure the consistent output.
         * The output would change whether the test was being ran for the first
         * or subsequent times. */
        test = TestConfiguration.singleUseDatabaseDecorator(test);
        test = new LocaleTestSetup(test, serverLocale);
        // Install a security manager using the initial policy file.
        return new SecurityManagerSetup(test, POLICY_FILE_NAME);
    }
	
	private static final HashMap<Locale, HashMap<String, String>> outputs;
	static {
		HashMap<String, String> englishOutputs = new HashMap<String, String>();
		englishOutputs.put("RuntimeInfoWithActiveConn",
				"--- Derby Network Server Runtime Information ---\n" + 
				"---------- Session Information ---------------\n" + 
                "Session # :##\n" +
				"\n" + 
				"\n" + 
				"-------------------------------------------------------------\n" + 
				"# Connection Threads : 1\n" + 
				"# Active Sessions : 1\n" + 
				"# Waiting  Sessions : 0\n" + 
				"\n" + 
				"Total Memory : #####	Free Memory : #####");
		englishOutputs.put("RuntimeInfoAfterConnClose", 
				"--- Derby Network Server Runtime Information ---\n" + 
				"---------- Session Information ---------------\n" + 
                "Session # :##\n" +
				"\n" + 
				"\n" + 
				"-------------------------------------------------------------\n" + 
				"# Connection Threads : 4\n" + 
				"# Active Sessions : 1\n" + 
				"# Waiting  Sessions : 0\n" + 
				"\n" + 
				"Total Memory : #####	Free Memory : #####");
		englishOutputs.put("sedMemorySearch", "(?m)Memory : [0-9]*");
		englishOutputs.put("sedMemoryReplace", "Memory : #####");
        englishOutputs.put("sedSessionNumberSearch", "(?m)^(Session # :)\\d+");
        englishOutputs.put("sedSessionNumberReplace", "$1##");
		englishOutputs.put("RuntimeInfoLocaleString", "\tStmt ID\t\tSQLText\n\t-------------\t-----------\n\n\n\nSession");
		
		HashMap<String, String> germanOutputs = new HashMap<String, String>();
		germanOutputs.put("RuntimeInfoWithActiveConn",
				"--- Laufzeitinformationen zu Derby Network Server ---\n" + 
				"---------- Sessioninformationen ---------------\n" + 
                "Sessionnummer:##\n" +
				"\n" + 
				"\n" + 
				"-------------------------------------------------------------\n" + 
				"Anzahl Verbindungsthreads: 1\n" + 
				"Anzahl aktive Sessions: 1\n" + 
				"Anzahl wartende Sessions: 0\n" + 
				"\n" + 
				"Speicher gesamt: #####	Freier Speicher: #####");
		germanOutputs.put("RuntimeInfoAfterConnClose", 
				"--- Laufzeitinformationen zu Derby Network Server ---\n" + 
				"---------- Sessioninformationen ---------------\n" + 
                "Sessionnummer:##\n" +
				"\n" + 
				"\n" + 
				"-------------------------------------------------------------\n" + 
				"Anzahl Verbindungsthreads: 4\n" + 
				"Anzahl aktive Sessions: 1\n" + 
				"Anzahl wartende Sessions: 0\n" + 
				"\n" + 
				"Speicher gesamt: #####	Freier Speicher: #####");
		germanOutputs.put("sedMemorySearch", "Speicher gesamt: [0-9]*	Freier Speicher: [0-9]*");
		germanOutputs.put("sedMemoryReplace", "Speicher gesamt: #####	Freier Speicher: #####");
        germanOutputs.put("sedSessionNumberSearch", "(?m)^(Sessionnummer:)\\d+");
        germanOutputs.put("sedSessionNumberReplace", "$1##");
		germanOutputs.put("RuntimeInfoLocaleString", "\tAnwsg-ID\t\tSQL-Text\n\t-------------\t-----------\n\n\n\nSessionnummer");
		
		outputs = new HashMap<Locale, HashMap<String, String>>();
		outputs.put(englishLocale, englishOutputs);
		outputs.put(germanLocale, germanOutputs);
	}
}