db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r634721 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management: JDBCMBeanTest.java MBeanTest.java _Suite.java
Date Fri, 07 Mar 2008 15:59:47 GMT
Author: djd
Date: Fri Mar  7 07:59:46 2008
New Revision: 634721

URL: http://svn.apache.org/viewvc?rev=634721&view=rev
Log:
DERBY-3385 a junit test for the JDBCMBean
Contributed by John H. Embretsen Email: John dot Embretsen at Sun dot com

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/JDBCMBeanTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/MBeanTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/_Suite.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/JDBCMBeanTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/JDBCMBeanTest.java?rev=634721&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/JDBCMBeanTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/JDBCMBeanTest.java
Fri Mar  7 07:59:46 2008
@@ -0,0 +1,148 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.management.JDBCMBeanTest
+
+   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.management;
+
+import java.io.BufferedReader;
+import java.sql.DatabaseMetaData;
+import java.util.Hashtable;
+import javax.management.ObjectName;
+import junit.framework.Test;
+
+
+/**
+ * <p>
+ * This JUnit test class is for testing the JDBCMBean that is available in
+ * Derby. Running these tests requires a JVM supporting J2SE 5.0 or better, due 
+ * to the implementation's dependency of the platform management agent.</p>
+ * <p>
+ * This class currently tests the following:</p>
+ * <ul>
+ *   <li>That the attributes we expect to be available exist</li>
+ *   <li>That these attributes are readable</li>
+ *   <li>That these attributes have the correct type</li>
+ *   <li>That these attributes have the correct value</li>
+ *   <li>That the MBean operations we expect to see are invokeable, and that
+ *       their return values are as expected.</li>
+ * <p>
+ * The test fixtures will fail if an exception occurs (will be reported as an 
+ * error in JUnit).</p>
+ */
+public class JDBCMBeanTest extends MBeanTest {
+    
+    public JDBCMBeanTest(String name) {
+        super(name);
+    }
+    
+    public static Test suite() {
+        
+        return MBeanTest.suite(JDBCMBeanTest.class, 
+                                        "JDBCMBeanTest");
+    }
+    
+    /**
+     * <p>
+     * Creates an object name instance for the MBean whose object name's textual
+     * representation contains:</p>
+     * <ul>
+     *   <li>type=JDBC</li>
+     * </ul>
+     * @return the object name representing Derby's JDBCMBean
+     * @throws MalformedObjectNameException if the object name is not valid
+     */
+    private ObjectName getJdbcMBeanObjectName() 
+            throws Exception {
+        
+        // get a reference to the JDBCMBean instance
+        Hashtable<String, String> keyProps = new Hashtable<String, String>();
+        keyProps.put("type", "JDBC");
+        return getDerbyMBeanName(keyProps);
+    }
+    
+    //
+    // ---------- TEST FIXTURES ------------
+    //
+    // This MBean currently has only read-only attributes, which will be tested.
+    // Expected operations will be invoked.
+    
+    
+    public void testAttributeCompliantDriver() throws Exception {
+        // we expect Derby's driver always to be JDBC compliant
+        assertBooleanAttribute(true, getJdbcMBeanObjectName(), "CompliantDriver");
+    }
+    
+    public void testAttributeDriverLevel() throws Exception {
+        String expected = "[Unable to get driver level from sysinfo]";
+        // Get the expected value from sysinfo
+        BufferedReader sysinfoOutput = getSysinfoFromServer();
+        String line = null;
+        while ((line = sysinfoOutput.readLine()) != null) {
+            /* Looking for:
+             *--------- Derby Information --------
+             *JRE - JDBC: J2SE 5.0 - JDBC 3.0
+             *            ^^^^^^^^^^^^^^^^^^^
+             * (actual JRE/JDBC values may vary)*/
+            if (line.matches("^JRE - JDBC: .*")) {
+                expected = line.substring(line.indexOf(": ") + 2);
+            }
+        }
+        
+        // test the attribute value against the expected value
+        assertStringAttribute(expected,getJdbcMBeanObjectName(), "DriverLevel");
+    }
+    
+    public void testAttributeMajorVersion() throws Exception {
+        DatabaseMetaData dbmd = getConnection().getMetaData();
+        int expected = dbmd.getDriverMajorVersion();
+        assertIntAttribute(expected, getJdbcMBeanObjectName(), "MajorVersion");
+    }
+    
+    public void testAttributeMinorVersion() throws Exception {
+        DatabaseMetaData dbmd = getConnection().getMetaData();
+        int expected = dbmd.getDriverMinorVersion();
+        assertIntAttribute(expected, getJdbcMBeanObjectName(), "MinorVersion");
+    }
+
+    public void testOperationAcceptsURL() throws Exception {
+        String opName = "acceptsURL";
+        ObjectName objName = getJdbcMBeanObjectName();
+        Object[] params = new Object[1];
+        String[] signature = { "java.lang.String" };
+        Boolean accepted;
+        
+        // first, test that a simple valid embedded driver URL is accepted
+        params[0] = "jdbc:derby:testDatabase";
+        accepted = (Boolean)invokeOperation(objName, opName, params, signature);
+        assertTrue("URL: " + params[0], accepted);
+                
+        // then, test that a valid embedded URL with a number of attributes is
+        // accepted
+        params[0] = "jdbc:derby:testDB;create=true;user=tester;password=mypass";
+        accepted = (Boolean)invokeOperation(objName, opName, params, signature);
+        assertTrue("URL: " + params[0], accepted);
+        
+        // then, check that an invalid URL is not accepted
+        params[0] = "jdbc:invalidProtocol:newDatabase";
+        accepted = (Boolean)invokeOperation(objName, opName, params, signature);
+        assertFalse("URL: " + params[0], accepted);
+    }
+
+}

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

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/MBeanTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/MBeanTest.java?rev=634721&r1=634720&r2=634721&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/MBeanTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/MBeanTest.java
Fri Mar  7 07:59:46 2008
@@ -21,7 +21,12 @@
 
 package org.apache.derbyTesting.functionTests.tests.management;
 
-import java.io.InputStream;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.Set;
@@ -32,16 +37,20 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
-import org.apache.derbyTesting.junit.BaseTestCase;
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
 import org.apache.derbyTesting.junit.NetworkServerTestSetup;
 import org.apache.derbyTesting.junit.SecurityManagerSetup;
 import org.apache.derbyTesting.junit.TestConfiguration;
 
 /**
  * Class that provided utility methods for the
- * testing of Derby's MBeans.
+ * testing of Derby's MBeans. Requires J2SE 5.0 or higher (platform management).
+ * 
+ * Subclasses may require JDBC access for verifying values returned by the
+ * MBeans, which is why this class extends BaseJDBCTestCase instead of 
+ * BaseTestCase.
  */
-abstract class MBeanTest extends BaseTestCase {
+abstract class MBeanTest extends BaseJDBCTestCase {
     
     /**
      * JMX connection to use throughout the instance.
@@ -123,8 +132,8 @@
     /**
      * Returns a set of startup properties suitable for VersionMBeanTest.
      * These properties are used to configure JMX in a different JVM.
-     * Will set up remote JMX using the port 9999 (TODO: make this 
-     * configurable), and with JMX security (authentication & SSL) disabled.
+     * Will set up remote JMX using the port defined by the current test 
+     * configuration, and with JMX security (authentication & SSL) disabled.
      * 
      * @return a set of Java system properties to be set on the command line
      *         when starting a new JVM in order to enable remote JMX.
@@ -191,7 +200,7 @@
     /**
      * Enables Derby's MBeans in the MBeanServer by accessing Derby's 
      * ManagementMBean. If Derby JMX management has already been enabled, no 
-     * changes will be made. The test fixtures in this class require that
+     * changes will be made. The test fixtures in some subclasses require that
      * JMX Management is enabled in Derby, hence this method.
      * 
      * @throws Exception JMX-related exceptions if an unexpected error occurs.
@@ -281,17 +290,40 @@
     }
     
     /**
-     * Invoke an operation with no arguments.
+     * Invokes an operation with no arguments.
      * @param objName MBean to operate on
      * @param name Operation name.
+     * @return the value returned by the operation being invoked, or 
+     *         <code>null</code> if there is no return value.
      */
-    protected void invokeOperation(ObjectName objName, String name)
-        throws Exception
+    protected Object invokeOperation(ObjectName objName, String name)
+            throws Exception
+    {
+        return invokeOperation(objName, name, new Object[0], new String[0]);
+    }
+    
+    /**
+     * Invokes an operation with arguments.
+     * 
+     * @param objName MBean to operate on
+     * @param name Operation name.
+     * @param params An array containing the parameters to be set when the 
+     *        operation is invoked.
+     * @param sign An array containing the signature of the operation, i.e.
+     *        the types of the parameters.
+     * @return the value returned by the operation being invoked, or 
+     *         <code>null</code> if there is no return value.
+     */
+    protected Object invokeOperation(ObjectName objName, 
+                                     String name, 
+                                     Object[] params, 
+                                     String[] sign)
+            throws Exception
     {
-        getMBeanServerConnection().invoke(
+        return getMBeanServerConnection().invoke(
                 objName, 
                 name, 
-                new Object[0], new String[0]); // no arguments
+                params, sign);
     }
     
     /**
@@ -316,6 +348,22 @@
         assertEquals(expected, bool.booleanValue());
     }
     
+    protected void assertIntAttribute(int expected,
+            ObjectName objName, String name) throws Exception
+    {
+        Integer integer = (Integer) getAttribute(objName, name);
+        assertNotNull(integer);
+        assertEquals(expected, integer.intValue());
+    }
+    
+    protected void assertStringAttribute(String expected,
+            ObjectName objName, String name) throws Exception
+    {
+        String str = (String) getAttribute(objName, name);
+        assertNotNull(str);
+        assertEquals(expected, str);
+    }
+    
     /**
      * Checks the readability and type of an attribute value that is supposed 
      * to be a boolean.
@@ -361,5 +409,47 @@
         
         String value = (String)getAttribute(objName, name);
         println(name + " = " + value); // for debugging
+    }
+    
+    /**
+     * Calls the public method <code>getInfo</code> of the sysinfo tool within
+     * this JVM and returns a <code>BufferedReader</code> for reading its 
+     * output. This is useful for obtaining system information that could be 
+     * used to verify (for example) values returned by Derby MBeans.
+     * 
+     * @return a buffering character-input stream containing the output from
+     *         sysinfo
+     * @see org.apache.derby.tools.sysinfo#getInfo(java.io.PrintWriter out)
+     */
+    protected BufferedReader getSysinfoLocally() {
+        ByteArrayOutputStream byteStream = new ByteArrayOutputStream(20 * 1024);
+        PrintWriter pw = new PrintWriter(byteStream, true); // autoflush
+        org.apache.derby.tools.sysinfo.getInfo(pw);
+        pw.flush();
+        pw.close();
+        byte[] outBytes = byteStream.toByteArray();
+        BufferedReader sysinfoOutput = new BufferedReader(
+                    new InputStreamReader(
+                            new ByteArrayInputStream(outBytes)));
+        return sysinfoOutput;
+    }
+    
+    /**
+     * <p>Calls the public method <code>getSysInfo()</code> of the Network

+     * Server instance associated with the current test configuration and 
+     * returns the result as a BufferedReader, making it easy to analyse the 
+     * output line by line.</p>
+     * 
+     * <p>This is useful for obtaining system information that could be 
+     * used to verify (for example) values returned by Derby MBeans.</p>
+     * 
+     * @return a buffering character-input stream containing the output from 
+     *         the server's sysinfo.
+     * @see org.apache.derby.drda.NetworkServerControl#getSysinfo()
+     */
+    protected BufferedReader getSysinfoFromServer() throws Exception {
+        
+        return new BufferedReader(new StringReader(
+                NetworkServerTestSetup.getNetworkServerControl().getSysinfo()));
     }
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/_Suite.java?rev=634721&r1=634720&r2=634721&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/_Suite.java
Fri Mar  7 07:59:46 2008
@@ -55,6 +55,7 @@
         suite.addTest(ManagementMBeanTest.suite());
         suite.addTest(InactiveManagementMBeanTest.suite());
         suite.addTest(VersionMBeanTest.suite());
+        suite.addTest(JDBCMBeanTest.suite());
         
         return suite;
     }



Mime
View raw message