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
|
/*
Derby - Class org.apache.derbyTesting.junit.NetworkServerControlWrapper
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.junit;
import java.io.PrintWriter;
import java.lang.reflect.Method;
/**
* A class wrapping a {@code NetworkServerControl} instance, using reflection
* to allow {@code TestConfiguration} to be used without having
* <tt>derbynet.jar</tt> on the classpath.
* <p>
* Only methods actually required by {@code TestConfiguration} are wrapped, and
* this class depends on the functionality implemented by
* {@link org.apache.derbyTesting.junit.NetworkServerTestSetup}.
* <p>
* The problem before was that an exception was thrown during class loading
* time, even if the network server functionality was never required by the
* tests being run. With this wrapper, an exception will be thrown only if the
* functionality is actually required and the necessary classes are not on the
* classpath.
*/
public class NetworkServerControlWrapper {
private static final int PING = 0;
private static final int SHUTDOWN = 1;
private static final int START = 2;
/** Associated {@code NetworkServerControl} instance. */
private final Object ctrl;
/** Array with the various method objects. */
private final Method[] METHODS = new Method[3];
/**
* Creates a new wrapper object.
*
* @throws Exception if creating the {@code NetworkServerControl} instance
* fails
*/
NetworkServerControlWrapper()
throws Exception {
// Try to load the NetworkServerControl class.
Class<?> clazzSC = null;
try {
clazzSC =
Class.forName("org.apache.derby.drda.NetworkServerControl");
} catch (ClassNotFoundException cnfe) {
BaseTestCase.fail("No runtime support for network server", cnfe);
}
Class<?> clazzTS = Class.forName(
"org.apache.derbyTesting.junit.NetworkServerTestSetup");
Method m = clazzTS.getMethod("getNetworkServerControl");
// Invoke method to obtain the NSC instance.
this.ctrl = m.invoke(null);
// Create the NSC method objects.
METHODS[PING] = clazzSC.getMethod("ping");
METHODS[SHUTDOWN] = clazzSC.getMethod("shutdown");
METHODS[START] = clazzSC.getMethod(
"start", new Class[] {PrintWriter.class});
}
/**
* Helper method that invokes a method returning {@code void}.
*
* @param methodIndex index of the method to invoke ({@link #METHODS})
* @param args arguments to pass to the method being invoked
* @throws Exception a broad range of exceptions can be thrown, both
* related to reflection and any exceptions the invoked methods
* themselves might throw
*/
private final void invoke(int methodIndex, Object[] args)
throws Exception {
try {
// Invoke the method with the passed in arguments.
METHODS[methodIndex].invoke(this.ctrl, args);
} catch (IllegalArgumentException iae) {
// Something is off with the passed in arguments.
BaseTestCase.fail("Test framework programming error", iae);
}
}
// Method forwarding / invocation follows below //
/** @see org.apache.derby.drda.NetworkServerControl#ping */
public void ping()
throws Exception {
invoke(PING, null);
}
/** @see org.apache.derby.drda.NetworkServerControl#shutdown */
public void shutdown()
throws Exception {
invoke(SHUTDOWN, null);
}
/** @see org.apache.derby.drda.NetworkServerControl#start */
public void start(PrintWriter printWriter)
throws Exception {
invoke(START, new Object[] {printWriter});
}
}
|