You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2012/08/09 20:55:46 UTC

svn commit: r1371381 - in /incubator/jspwiki/trunk: ./ tests/org/apache/wiki/ tests/org/apache/wiki/auth/authorize/ tests/org/apache/wiki/auth/user/

Author: juanpablo
Date: Thu Aug  9 18:55:45 2012
New Revision: 1371381

URL: http://svn.apache.org/viewvc?rev=1371381&view=rev
Log:
    * HSQL connections are handled inside the appropiate unit tests in order to ensure HSQL 
      server shutdown
      
    * Hypersonic updated to 1.8.0.10. clean-deps target needs to be run to ensure that old
      hsqldb jar doesn't remain in path.

Added:
    incubator/jspwiki/trunk/tests/org/apache/wiki/HsqlDbUtils.java
Modified:
    incubator/jspwiki/trunk/.classpath
    incubator/jspwiki/trunk/build.properties
    incubator/jspwiki/trunk/build.properties.win
    incubator/jspwiki/trunk/build.xml
    incubator/jspwiki/trunk/tests/org/apache/wiki/auth/authorize/JDBCGroupDatabaseTest.java
    incubator/jspwiki/trunk/tests/org/apache/wiki/auth/user/JDBCUserDatabaseTest.java

Modified: incubator/jspwiki/trunk/.classpath
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/.classpath?rev=1371381&r1=1371380&r2=1371381&view=diff
==============================================================================
--- incubator/jspwiki/trunk/.classpath (original)
+++ incubator/jspwiki/trunk/.classpath Thu Aug  9 18:55:45 2012
@@ -32,7 +32,7 @@
 	<classpathentry kind="lib" path="lib/commons-fileupload-1.2.1.jar"/>
 	<classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/>
 	<classpathentry kind="lib" path="tests/lib/commons-el-1.0.jar"/>
-	<classpathentry kind="lib" path="tests/lib/hsqldb-1.8.0.jar"/>
+	<classpathentry kind="lib" path="tests/lib/hsqldb-1.8.0.10.jar"/>
 	<classpathentry kind="lib" path="tests/lib/jasper-compiler-5.5.25.jar"/>
 	<classpathentry kind="lib" path="tests/lib/jasper-runtime-5.5.25.jar"/>
 	<classpathentry kind="lib" path="tests/lib/jetty-jmx-5.1.14.jar"/>

Modified: incubator/jspwiki/trunk/build.properties
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/build.properties?rev=1371381&r1=1371380&r2=1371381&view=diff
==============================================================================
--- incubator/jspwiki/trunk/build.properties (original)
+++ incubator/jspwiki/trunk/build.properties Thu Aug  9 18:55:45 2012
@@ -39,7 +39,7 @@ jks.password = jspwiki
 #  JDBC testing properties
 #
 jdbc.driver.id=hsql
-jdbc.driver.jar=tests/lib/hsqldb-1.8.0.jar
+jdbc.driver.jar=tests/lib/hsqldb-1.8.0.10.jar
 jdbc.driver.class=org.hsqldb.jdbcDriver
 jdbc.driver.url=jdbc:hsqldb:hsql://localhost/jspwiki
 jdbc.admin.id=SA

Modified: incubator/jspwiki/trunk/build.properties.win
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/build.properties.win?rev=1371381&r1=1371380&r2=1371381&view=diff
==============================================================================
--- incubator/jspwiki/trunk/build.properties.win (original)
+++ incubator/jspwiki/trunk/build.properties.win Thu Aug  9 18:55:45 2012
@@ -42,7 +42,7 @@ jks.password = jspwiki
 #  JDBC testing properties
 #
 jdbc.driver.id=hsql
-jdbc.driver.jar=tests/lib/hsqldb-1.8.0.jar
+jdbc.driver.jar=tests/lib/hsqldb-1.8.0.10.jar
 jdbc.driver.class=org.hsqldb.jdbcDriver
 jdbc.driver.url=jdbc:hsqldb:hsql://localhost/jspwiki
 jdbc.admin.id=SA

Modified: incubator/jspwiki/trunk/build.xml
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/build.xml?rev=1371381&r1=1371380&r2=1371381&view=diff
==============================================================================
--- incubator/jspwiki/trunk/build.xml (original)
+++ incubator/jspwiki/trunk/build.xml Thu Aug  9 18:55:45 2012
@@ -254,7 +254,7 @@
      	<!-- tests dependencies  -->
      	<get-element to-file="${libs.tests}/commons-el-1.0.jar" url="${central.url}/commons-el/commons-el/1.0/commons-el-1.0.jar" />
         <get-element to-file="${libs.tests}/custom_rhino-0.4.3.jar" url="${central.url}/org/dojotoolkit/custom_rhino/0.4.3/custom_rhino-0.4.3.jar" />
-        <get-element to-file="${libs.tests}/hsqldb-1.8.0.jar" url="${svn.repository}/libs/${libs.tests}/hsqldb-1.8.0.jar" />
+     	<get-element to-file="${libs.tests}/hsqldb-1.8.0.10.jar" url="${central.url}/org/hsqldb/hsqldb/1.8.0.10/hsqldb-1.8.0.10.jar" />
         <get-element to-file="${libs.tests}/jasper-compiler-5.5.25.jar" url="${svn.repository}/libs/${libs.tests}/jasper-compiler-5.5.25.jar" />
         <get-element to-file="${libs.tests}/jasper-runtime-5.5.25.jar" url="${svn.repository}/libs/${libs.tests}/jasper-runtime-5.5.25.jar" />
         <get-element to-file="${libs.tests}/jetty-jmx-5.1.14.jar" url="${svn.repository}/libs/${libs.tests}/jetty-jmx-5.1.14.jar" />
@@ -1429,7 +1429,7 @@ To automate the JAR signing processs, yo
        for details and examples.
   -->
 
-  <target name="tests-db-init" depends="db-properties,hsql-init,db-setup"/>
+  <target name="tests-db-init" depends="db-properties"/>
 
   <target name="db-properties" depends="init" if="jdbc.driver.id">
     <!-- Load the JDBC props we need to do table maintenance -->
@@ -1500,16 +1500,6 @@ To automate the JAR signing processs, yo
     <echoproperties prefix="jdbc." destfile="${tests.src}/etc/db/jdbc.properties" />
   </target>
 
-  <target name="db-setup" depends="db-properties,hsql-init" if="db.props.exist">
-    <echo message="Setting up the database tables." />
-    <exec-sql file="${userdb.setup}" />
-  </target>
-
-  <target name="db-teardown" depends="db-properties,hsql-init" if="db.props.exist">
-    <echo message="Tearing down the database tables." />
-    <exec-sql file="${userdb.teardown}" />
-  </target>
-
   <!-- Some convenience macrodefs -->
   <macrodef name="check-property">
     <attribute name="prop"/>
@@ -1527,65 +1517,6 @@ To automate the JAR signing processs, yo
     </sequential>
   </macrodef>
 
-  <macrodef name="exec-sql">
-    <attribute name="file" />
-    <sequential>
-      <sql driver="${jdbc.driver.class}"
-        classpath="${jdbc.driver.jar}" url="${jdbc.driver.url}"
-        userid="${jdbc.admin.id}" password="${jdbc.admin.password}"
-        src="@{file}" onerror="continue" autocommit="true" />
-    </sequential>
-  </macrodef>
-
-  <!-- ============================================================== -->
-
-  <!-- Hypersonic embedded database startup/shutdown (for testing JDBC) -->
-
-  <!-- Special "init" target for Hypersonic -->
-  <target name="hsql-init" depends="hsql-check-start,hsql-start" />
-
-  <target name="hsql-check-start" depends="db-properties">
-    <echo message="Checking to see if Hypersonic JDBC server is running already." />
-    <property file="tests/etc/db/hsql/server.properties" prefix="hsql" />
-    <fail unless="hsql.server.port">Failed to load Hypersonic JDBC properties from tests/etc/db/hsql/server.properties</fail>
-    <condition property="hsql.up">
-      <socket server="localhost" port="${hsql.server.port}" />
-    </condition>
-  </target>
-
-  <target name="hsql-start" depends="hsql-check-start" unless="hsql.up"
-    description="Starts the Hypersonic database for testing.">
-    <echo message="Starting up Hypersonic JDBC server on localhost." />
-    <delete file="tests/etc/db/hsql/jspwiki.lck"/>
-    <java fork="yes" spawn="yes" classname="org.hsqldb.Server"
-      dir="tests/etc/db/hsql">
-      <classpath>
-        <pathelement location="${jdbc.driver.jar}" />
-      </classpath>
-    </java>
-    <sleep seconds="5" />
-    <available file="tests/etc/db/hsql/jspwiki.lck" property="hsql.up"/>
-    <fail unless="hsql.up">Hypersonic didn't appear to start up properly. You can start it manually from the command line as follows:
-   cd tests/etc/db/hsql
-   java -cp ${jdbc.driver.jar} org.hsqldb.Server
-    </fail>
-    <echo message="Done." />
-  </target>
-
-  <target name="hsql-stop" depends="hsql-check-start" if="hsql.up"
-    description="Shuts down the Hypersonic database, if it's up.">
-    <echo message="Shutting down Hypersonic JDBC server on localhost." />
-    <sql driver="${jdbc.driver.class}"
-      classpath="${jdbc.driver.jar}" url="${jdbc.driver.url}"
-      onerror="continue" autocommit="true"
-      userid="${jdbc.admin.id}" password="${jdbc.admin.password}">
-      SHUTDOWN
-    </sql>
-    <!-- The lock file should be deleted automatically, but just in case... -->
-    <delete file="tests/etc/db/hsql/jspwiki.lck" />
-    <echo message="Done." />
-  </target>
-
   <!-- ============================================================== -->
   <target name="i18n-create-template" description="Creates a given directory structure with all the needed files to make an i18n jar">
 	<input message="i18n template code to generate (ie: es_ES):" addproperty="i18n.template" />

Added: incubator/jspwiki/trunk/tests/org/apache/wiki/HsqlDbUtils.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/HsqlDbUtils.java?rev=1371381&view=auto
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/HsqlDbUtils.java (added)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/HsqlDbUtils.java Thu Aug  9 18:55:45 2012
@@ -0,0 +1,229 @@
+/* 
+    JSPWiki - a JSP-based WikiWiki clone.
+
+    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.wiki;
+
+import java.io.*;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Properties;
+
+import org.apache.log4j.Logger;
+import org.hsqldb.Server;
+import org.hsqldb.util.SqlFile;
+
+
+/**
+ * Helper class to handle Hypersonic Server connections and SQL scripts executions.
+ * 
+ * Standard usage:
+ * <code>
+ * <ol>
+ *   <li>HsqlDbUtils hsqldbu = new HsqlDbUtils()</li>
+ *   <li>hsqldbu.start()</li>
+ *   <li>hsqldbu.exec( "tests/etc/db/hsql/userdb-setup.ddl" )</li>
+ *   <li>hsqldbu.exec( "tests/etc/db/hsql/userdb-teardown.ddl" )</li>
+ *   <li>hsqldbu.stop()</li>
+ * </ol>
+ * </code>
+ * There are also a couple of convenience methods for unit testing:
+ * <code>
+ * <ol>
+ *   <li>hsqldbu.setUp()</li>
+ *   <li>hsqldbu.tearDown()</li>
+ * </ol>
+ * </code>
+ */
+public class HsqlDbUtils
+{
+    
+    private static final Logger LOG = Logger.getLogger( HsqlDbUtils.class );
+    
+    Properties hProps = null;
+    Server hsqlServer = null;
+    
+    /**
+     * Convenience Hypersonic startup method for unit tests.
+     */
+    public void setUp() 
+    {
+        try
+        {
+            start();
+        }
+        catch( Exception e )
+        {
+            LOG.error( e.getMessage(), e );
+        }
+        exec( "tests/etc/db/hsql/userdb-setup.ddl" );
+    }
+    
+    /**
+     * Convenience Hypersonic shutdown method for unit tests.
+     */
+    public void tearDown() 
+    {
+        exec( "tests/etc/db/hsql/userdb-teardown.ddl" );
+        stop();
+    }
+    
+    /**
+     * Starts the Hypersonic server.
+     */
+    public void start() throws Exception
+    {
+        // pre-checks
+        File lock = new File( "tests/etc/db/hsql/jspwiki.lck" );
+        if( lock.exists() ) 
+        {
+            lock.delete();
+        }
+        
+        Thread.sleep( 5000L );
+        
+        if( lock.exists() ) 
+        {
+            throw new InternalWikiException( "Another HSQL server seems to be running" );
+        }
+        
+        // start Hypersonic server
+        Properties hProps = loadPropertiesFrom( "tests/etc/db/hsql/server.properties" );
+        
+        hsqlServer = new Server();
+        hsqlServer.setPort( Integer.valueOf( hProps.getProperty( "server.port" ) ) );
+        hsqlServer.setDatabaseName( 0, hProps.getProperty( "server.dbname.0" ) );
+        hsqlServer.setDatabasePath( 0, hProps.getProperty( "server.database.0" ) );
+        hsqlServer.start();
+        
+        Class.forName( "org.hsqldb.jdbcDriver" );
+        
+        if( !lock.exists() ) 
+        {
+            throw new InternalWikiException( "Unable to start HSQL server, check logs" );
+        }
+    }
+    
+    /**
+     * Executes the given script.
+     * 
+     * @param file script to execute.
+     */
+    public void exec( String file ) 
+    {
+        Connection conn = null;
+        try
+        {
+            conn = getConnection();
+            SqlFile userDbSetup = new SqlFile( new File( file ), false, null );
+            userDbSetup.execute( conn, true );
+        }
+        catch( Exception e ) 
+        {
+            LOG.error( e.getMessage(), e );
+        }
+        finally
+        {
+            close( conn ); 
+        }
+    }
+
+    /**
+     * Stops the Hypersonic server.
+     */
+    public void stop() 
+    {
+        LOG.info( "Shutting down Hypersonic JDBC server on localhost." );
+        if( hsqlServer != null ) 
+        {
+            Connection conn = null;
+            try
+            {
+                conn = getConnection();
+                conn.setAutoCommit( true );
+                conn.prepareStatement( "SHUTDOWN" ).execute();
+            }
+            catch( Exception e )
+            {
+                LOG.error( e.getMessage(), e );
+            }
+            finally
+            {
+                close( conn );
+            }
+            
+            hsqlServer.stop();
+        }
+    }
+    
+    /**
+     * Obtains a {@link Connection}.
+     * 
+     * @return the obtained {@link Connection}.
+     * @throws IOException problems occurred loading jdbc properties file.
+     * @throws SQLException problems occurred obtaining the {@link Connection}.
+     */
+    Connection getConnection() throws IOException, SQLException
+    {
+        Connection conn;
+        Properties jProps = loadPropertiesFrom( "tests/etc/db/jdbc.properties" );
+        conn = DriverManager.getConnection( jProps.getProperty( "jdbc.driver.url" ), 
+                                            jProps.getProperty( "jdbc.admin.id" ),
+                                            jProps.getProperty( "jdbc.admin.password" ) );
+        return conn;
+    }
+    
+    /**
+     * Closes the given {@link Connection}.
+     * 
+     * @param conn given {@link Connection}.
+     */
+    void close( Connection conn ) 
+    {
+        if( conn != null ) 
+        {
+            try
+            {
+                conn.close();
+            }
+            catch( SQLException e )
+            {
+                conn = null;
+            }
+        } 
+    }
+    
+    /**
+     * Loads a {@link Properties} object with {@code fileLocation}.
+     * 
+     * @param fileLocation properties file
+     * @return {@link Properties} holding {@code fileLocation} properties.
+     * @throws IOException if {@code fileLocation} cannot be readed.
+     */
+    Properties loadPropertiesFrom( String fileLocation ) throws IOException 
+    {
+        Properties p = new Properties();
+        InputStream inStream = new BufferedInputStream( new FileInputStream( fileLocation ) );
+        p.load( inStream );
+        inStream.close();
+        return p;
+    }
+    
+}

Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/auth/authorize/JDBCGroupDatabaseTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/auth/authorize/JDBCGroupDatabaseTest.java?rev=1371381&r1=1371380&r2=1371381&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/auth/authorize/JDBCGroupDatabaseTest.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/auth/authorize/JDBCGroupDatabaseTest.java Thu Aug  9 18:55:45 2012
@@ -41,12 +41,14 @@ import org.apache.wiki.auth.WikiSecurity
  */
 public class JDBCGroupDatabaseTest extends TestCase
 {
+    private HsqlDbUtils       m_hu   = new HsqlDbUtils();
+    
     private Connection        m_conn = null;
 
-    private JDBCGroupDatabase m_db  = null;
+    private JDBCGroupDatabase m_db   = null;
 
     private String            m_wiki;
-
+    
     /**
      * @see junit.framework.TestCase#setUp()
      */
@@ -54,6 +56,7 @@ public class JDBCGroupDatabaseTest exten
     {
         super.setUp();
 
+        m_hu.setUp();
         Properties props = new Properties();
         props.load( TestEngine.findTestProperties() );
         WikiEngine engine = new TestEngine( props );
@@ -93,6 +96,7 @@ public class JDBCGroupDatabaseTest exten
         {
             m_conn.close();
         }
+        m_hu.tearDown();
     }
 
     public void testDelete() throws WikiException

Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/auth/user/JDBCUserDatabaseTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/auth/user/JDBCUserDatabaseTest.java?rev=1371381&r1=1371380&r2=1371381&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/auth/user/JDBCUserDatabaseTest.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/auth/user/JDBCUserDatabaseTest.java Thu Aug  9 18:55:45 2012
@@ -36,6 +36,7 @@ import javax.sql.DataSource;
 
 import junit.framework.TestCase;
 
+import org.apache.wiki.HsqlDbUtils;
 import org.apache.wiki.TestJDBCDataSource;
 import org.apache.wiki.TestJNDIContext;
 import org.apache.wiki.auth.NoSuchPrincipalException;
@@ -46,6 +47,8 @@ import org.apache.wiki.util.CryptoUtil;
  */
 public class JDBCUserDatabaseTest extends TestCase
 {
+    private HsqlDbUtils      m_hu   = new HsqlDbUtils();
+    
     private JDBCUserDatabase m_db   = null;
     
     private static final String TEST_ATTRIBUTES = "rO0ABXNyABFqYXZhLnV0aWwuSGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAx3CAAAABAAAAACdAAKYXR0cmlidXRlMXQAEXNvbWUgcmFuZG9tIHZhbHVldAAKYXR0cmlidXRlMnQADWFub3RoZXIgdmFsdWV4";
@@ -82,6 +85,7 @@ public class JDBCUserDatabaseTest extend
     {
         super.setUp();
 
+        m_hu.setUp();
         // Set up the mock JNDI initial context
         TestJNDIContext.initialize();
         Context initCtx = new InitialContext();
@@ -122,6 +126,13 @@ public class JDBCUserDatabaseTest extend
             throw (SQLException) e.fillInStackTrace();
         }
     }
+    
+    @Override
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+        m_hu.tearDown();
+    }
 
     public void testDeleteByLoginName() throws WikiSecurityException
     {