db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmars...@apache.org
Subject svn commit: r604074 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/derbynet/ junit/
Date Fri, 14 Dec 2007 00:47:45 GMT
Author: kmarsden
Date: Thu Dec 13 16:47:44 2007
New Revision: 604074

URL: http://svn.apache.org/viewvc?rev=604074&view=rev
Log:
DERBY-3273 convert derbynet/testconnection.java to junit


Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlClientCommandTest.java
  (with props)
Removed:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/testconnection.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/testconnection_app.properties
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/testconnection_sed.properties
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlApiTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Utilities.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlApiTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlApiTest.java?rev=604074&r1=604073&r2=604074&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlApiTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlApiTest.java
Thu Dec 13 16:47:44 2007
@@ -26,11 +26,15 @@
 import org.apache.derbyTesting.functionTests.tests.lang.SimpleTest;
 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
 import org.apache.derbyTesting.junit.Derby;
+import org.apache.derbyTesting.junit.NetworkServerTestSetup;
 import org.apache.derbyTesting.junit.SecurityManagerSetup;
 import org.apache.derbyTesting.junit.SupportFilesSetup;
 import org.apache.derbyTesting.junit.TestConfiguration;
 
 import java.io.File;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.security.AccessController;
 import java.security.Policy;
 import java.security.PrivilegedActionException;
@@ -61,7 +65,7 @@
      */
     public void testTraceCommands() throws Exception
     {
-        NetworkServerControl nsctrl = new NetworkServerControl();
+        NetworkServerControl nsctrl = NetworkServerTestSetup.getNetworkServerControl();
         String derbySystemHome = getSystemProperty("derby.system.home");
         nsctrl.setTraceDirectory(derbySystemHome);
        
@@ -88,6 +92,60 @@
                         
     }
 
+    /**
+     * Test NetworkServerControl ping command.
+     * @throws Exception
+     */
+    public void testPing() throws Exception
+    {
+        String currentHost = TestConfiguration.getCurrent().getHostName();
+        
+        NetworkServerControl nsctrl = NetworkServerTestSetup.getNetworkServerControl();
+        nsctrl.ping();
+        
+        // Note:Cannot test ping with unknown host because it fails in
+        // InetAddress.getByName()
+        
+        nsctrl = new NetworkServerControl(privInetAddressGetByName(currentHost), 9393);
+        try {        
+        	nsctrl.ping();
+        	fail("Should not have been able to ping on port 9393");
+        }catch (Exception e){
+        	// expected exception
+        }
+    }
+    
+        /**
+         * Wraps InitAddress.getByName in privilege block.
+         * 
+         * @param host  host to resolve
+         * @return InetAddress of host
+         * @throws UnknownHostException
+         */
+        private InetAddress privInetAddressGetByName(final String host) throws UnknownHostException
+        {
+            InetAddress inetAddr = null;
+            try {
+                inetAddr = (InetAddress) AccessController
+                    .doPrivileged(new PrivilegedExceptionAction() {
+                        public Object run() throws UnknownHostException {
+                            return InetAddress.getByName(host);
+                        }
+                    });
+            } catch (PrivilegedActionException pe) {
+                Exception e = pe.getException();
+                if (e instanceof UnknownHostException)
+                    throw (UnknownHostException) e;
+                else
+                    throw (SecurityException) e;
+            }
+        return inetAddr;
+            
+        }
+        
+        
+    
+    
     private boolean fileExists(String filename) {
         final File file = new File(filename);
         try {

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlClientCommandTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlClientCommandTest.java?rev=604074&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlClientCommandTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlClientCommandTest.java
Thu Dec 13 16:47:44 2007
@@ -0,0 +1,109 @@
+package org.apache.derbyTesting.functionTests.tests.derbynet;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Locale;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.derbyTesting.junit.Derby;
+import org.apache.derbyTesting.junit.JDBC;
+import org.apache.derbyTesting.junit.SecurityManagerSetup;
+import org.apache.derbyTesting.junit.TestConfiguration;
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.Utilities;
+
+public class NetworkServerControlClientCommandTest extends BaseJDBCTestCase {
+
+    public NetworkServerControlClientCommandTest(String name) {
+        super(name);
+
+    }
+
+    /**
+     * Test various ping commands from the command line
+     * 
+     * @throws Exception
+     */
+    public void testPing() throws Exception {
+        String currentHost = TestConfiguration.getCurrent().getHostName();
+        String currentPort = Integer.toString(TestConfiguration.getCurrent().getPort());
+        
+        String[] pingCmd1 = new String[] {
+                "org.apache.derby.drda.NetworkServerControl", "ping" };
+        assertSuccessfulPing(pingCmd1);
+      
+        String[] pingCmd2 = new String[] {
+                "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
+                currentHost, "-p", currentPort};
+        assertSuccessfulPing(pingCmd2);
+        
+        String[] pingCmd3 = new String[] {"org.apache.derby.drda.NetworkServerControl",
+        "ping", "-h", currentHost};
+        assertSuccessfulPing(pingCmd3);
+        
+        String[] pingCmd4 = new String[] {
+                "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
+                "nothere" };
+        assertFailedPing(pingCmd4,"Unable to find host");
+        String[] pingCmd5= new String[] {"org.apache.derby.drda.NetworkServerControl",
+        "ping", "-h", currentHost, "-p", "9393"};
+        assertFailedPing(pingCmd5,"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
{
+        
+        InputStream is = Utilities.execJavaCmd(pingCmd, 0);
+        byte[] b = new byte[80];
+        is.read(b, 0, 80);
+        String output = new String(b);
+        assertTrue(output.startsWith("Connection obtained"));
+        
+    }
+    /**
+     * 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 {
+        
+        InputStream is = Utilities.execJavaCmd(pingCmd, 1);
+        byte[] b = new byte[80];
+        is.read(b, 0, 80);
+        String output = new String(b);
+        assertTrue(output.startsWith(expectedMessage));
+        
+    }
+    
+
+    public static Test suite() {
+
+        TestSuite suite = new TestSuite("NetworkServerControlClientCommandTest");
+        
+
+        // need network server, english locale so we can compare command output 
+        // and we don't run on J2ME because java command is different.
+        if (!Derby.hasServer() || !Locale.getDefault().getLanguage().equals("en") ||
+                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;
+    }
+
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlClientCommandTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java?rev=604074&r1=604073&r2=604074&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
Thu Dec 13 16:47:44 2007
@@ -56,6 +56,7 @@
         suite.addTest(SSLTest.suite());
         suite.addTest(NetIjTest.suite());
         suite.addTest(NSinSameJVMTest.suite());
+        suite.addTest(NetworkServerControlClientCommandTest.suite());
         
         
         // Disabled due to "java.sql.SQLSyntaxErrorException: The class

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Utilities.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Utilities.java?rev=604074&r1=604073&r2=604074&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Utilities.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Utilities.java Thu Dec
13 16:47:44 2007
@@ -19,11 +19,19 @@
  */
 package org.apache.derbyTesting.junit;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 
+import junit.framework.Assert;
+
 /**
  * General non-JDBC related utilities relocated from TestUtil
  *
@@ -141,6 +149,54 @@
                 else
                     System.out.println("};\n");
             }
+        }
+        /**
+         * Execute a java command and check for the appropriate exit code.
+         * return an InputStream 
+         * @param args
+         * @param expectedExitCode
+         * 
+         * @throws InterruptedException
+         */
+        public static InputStream execJavaCmd(String[] args, int expectedExitCode) throws
IOException, InterruptedException {
+            InputStream is = null;
+            
+            int totalSize = 3 + args.length;
+            String[] cmd = new String[totalSize];
+            cmd[0] = "java";
+            cmd[1] = "-classpath";
+            cmd[2] = BaseTestCase.getSystemProperty("java.class.path");
+                    
+            System.arraycopy(args, 0, cmd, 3, args.length);
+            
+            final String[] command = cmd;
+            Process pr = null;
+            try {
+                pr = (Process) AccessController
+                    .doPrivileged(new PrivilegedExceptionAction() {
+                        public Object run() throws IOException {
+                            Process result = null;
+        
+                                result = Runtime.getRuntime().exec(command);
+        
+                            return result;
+                        }
+                    });
+            } catch (PrivilegedActionException pe) {
+                Exception e = pe.getException();
+                if (e instanceof IOException)
+                    throw (IOException) e;
+                else
+                    throw (SecurityException) e;
+            }
+            is = pr.getInputStream();
+        
+            // wait until the process exits
+            pr.waitFor();
+        
+            Assert.assertEquals(expectedExitCode,pr.exitValue());
+            
+            return is;
         }       
 
 }



Mime
View raw message