db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r669808 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit: NetworkServerControlWrapper.java TestConfiguration.java
Date Fri, 20 Jun 2008 06:58:17 GMT
Author: kristwaa
Date: Thu Jun 19 23:58:17 2008
New Revision: 669808

URL: http://svn.apache.org/viewvc?rev=669808&view=rev
Log:
DERBY-3151: Reduce dependency on NetworkServerControl in TestConfiguration.
Added a NetworkServerControl wrapper using reflection to use the class and invoke its methods.
The purpose of the change is to be able to run JUnit tests without having derbynet.jar (and
derbyrun.jar) on the classpath.
Patch file: derby-3151-4b-wrapper_with_reflection.diff

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerControlWrapper.java
  (with props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerControlWrapper.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerControlWrapper.java?rev=669808&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerControlWrapper.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerControlWrapper.java
Thu Jun 19 23:58:17 2008
@@ -0,0 +1,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", null);
+        // Invoke method to obtain the NSC instance.
+        this.ctrl = m.invoke(null, null);
+
+        // Create the NSC method objects.
+        METHODS[PING] = clazzSC.getMethod("ping", null);
+        METHODS[SHUTDOWN] = clazzSC.getMethod("shutdown", null);
+        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});
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerControlWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java?rev=669808&r1=669807&r2=669808&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
Thu Jun 19 23:58:17 2008
@@ -31,8 +31,6 @@
 import java.util.ArrayList;
 import java.util.Hashtable;
 
-import org.apache.derby.drda.NetworkServerControl;
-
 import junit.extensions.TestSetup;
 import junit.framework.Assert;
 import junit.framework.Test;
@@ -1387,8 +1385,8 @@
      */
     public void stopNetworkServer() {
         try {
-            NetworkServerControl networkServer =
-                NetworkServerTestSetup.getNetworkServerControl();
+            NetworkServerControlWrapper networkServer =
+                    new NetworkServerControlWrapper();
 
             networkServer.shutdown();
             if (serverOutput != null) {
@@ -1409,9 +1407,9 @@
         Exception failException = null;
         try {
             
-            NetworkServerControl networkServer =
-                NetworkServerTestSetup.getNetworkServerControl();
-	    
+            NetworkServerControlWrapper networkServer =
+                    new NetworkServerControlWrapper();
+
  	    serverOutput = (FileOutputStream)
             AccessController.doPrivileged(new PrivilegedAction() {
                 public Object run() {



Mime
View raw message