You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by ep...@apache.org on 2004/11/07 18:37:34 UTC

cvs commit: jakarta-turbine-fulcrum/hsqldb/src/test/org/apache/fulcrum/hsqldb HSQLServiceTest.java

epugh       2004/11/07 09:37:34

  Added:       hsqldb   LICENSE.txt project.properties project.xml
               hsqldb/xdocs index.xml changes.xml navigation.xml
               hsqldb/src/test test.properties TestComponentConfig.xml
                        test.script TestRoleConfig.xml
               hsqldb/src/java/org/apache/fulcrum/hsqldb
                        HSQLServiceImpl.java HSQLService.java
               hsqldb/src/test/org/apache/fulcrum/hsqldb
                        HSQLServiceTest.java
  Log:
  add hsql component
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-fulcrum/hsqldb/LICENSE.txt
  
  Index: LICENSE.txt
  ===================================================================
  /*
   * Copyright 2004 Apache Software Foundation
   * Licensed  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.
   */
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/project.properties
  
  Index: project.properties
  ===================================================================
  
  
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/project.xml
  
  Index: project.xml
  ===================================================================
  <?xml version="1.0"?>
  <project>
    <extend>${basedir}/../project.xml</extend>
    <id>fulcrum-hsqldb</id>
    <name>Fulcrum HSQLDB Server Component</name>
    <currentVersion>1.0-dev</currentVersion>
  
    <dependencies>
    
      <dependency>
          <groupId>hsqldb</groupId>
          <artifactId>hsqldb</artifactId>
          <version>1.7.2.2</version>
          <url>http://hsqldb.sourceforge.net/</url>
      </dependency> 
      
      <!--  Needed only for testing -->
      <dependency>
        <groupId>fulcrum</groupId>
        <artifactId>fulcrum-testcontainer</artifactId>
        <version>1.0.4-dev</version>
      </dependency>
      
      <dependency>
        <groupId>fulcrum</groupId>
        <artifactId>fulcrum-yaafi</artifactId>
        <version>1.0.3-dev</version>
      </dependency>
  
    </dependencies>
  
  </project>
  
  
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/xdocs/index.xml
  
  Index: index.xml
  ===================================================================
  <?xml version="1.0"?>
  
  <document>
  
    <properties>
      <title>HSQLDB Service</title>
      <author email="siegfried.goeschl@it20one.at">Siegfried Goeschl</author>
    </properties>
  
    <body>
  
    <section name="Overview">
      <p>
       This service starts a HSQLDB server instance with a single database.
     </p>      
    </section>
    
  <section name="Configuration">
  
      <p>
        First, here is the role configuration.
      </p>
  
      <source>
      <![CDATA[
        <role
            name="org.apache.fulcrum.hsqldb.HSQLService"
            default-class="org.apache.fulcrum.hsqldb.HSQLServiceImpl"
            shorthand="HSQLService" 
         />            
      ]]>
      </source>
  
        <p>
          And here is the configuration:
        </p>
      <source>
  
      <![CDATA[
          <HSQLService database="./src/test/test" dbname="test" trace="true" silent="false" port="9001"/>
      ]]>
      </source>
  
    </section>
  
    <section name="Usage">
      
      <p>
        Please look at unit test how to access the HSQLDB server.
      </p>
  
    </section> 
  
  </body>
  </document>
  
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  <?xml version="1.0"?>
  <document>
    <properties>
      <title>Fulcrum HSQLDB Service</title>
      <author email="siegfried.goeschl@it20one.at">Siegfried Goeschl</author>
    </properties>
  
    <body>
      <release version="1.0.0" date="as in CVS">
       <action dev="epugh" type="update">
          Add additional tests submitted by Peter Tillemans in Scarab version of
          this service.
        </action>        
       <action dev="goeschsi" type="update">
          Initial release
        </action>      
      </release>    
    </body>
  </document>
  
  
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/xdocs/navigation.xml
  
  Index: navigation.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  <project
    name="cache"
    href="http://jakarta.apache.org/turbine/fulcrum/cache/">
  
    <body>
      <links>
        <item name="Turbine"
              href="http://jakarta.apache.org/turbine/"/>
        <item name="Fulcrum"
              href="http://jakarta.apache.org/turbine/fulcrum/"/>
        <item name="HSQLDb"
              href="http://hsqldb.sourceforge.net/"/>            
      </links>
  
      <menu name="Overview">
        <item name="Main"                 href="/index.html"/>
      </menu>
    </body>
  </project>
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/src/test/test.properties
  
  Index: test.properties
  ===================================================================
  #HSQL database
  #Wed Nov 03 17:55:58 CET 2004
  hsqldb.cache_file_scale=1
  runtime.gc_interval=0
  hsqldb.first_identity=0
  version=1.7.2
  modified=no
  hsqldb.script_format=0
  sql.enforce_size=false
  hsqldb.cache_size_scale=10
  hsqldb.cache_scale=14
  hsqldb.version=1.7.2
  hsqldb.log_size=200
  sql.enforce_strict_size=false
  readonly=true
  hsqldb.compatible_version=1.7.2
  hsqldb.original_version=1.7.2
  sql.compare_in_locale=false
  hsqldb.nio_data_file=false
  hsqldb.cache_version=1.7.0
  
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/src/test/TestComponentConfig.xml
  
  Index: TestComponentConfig.xml
  ===================================================================
  <componentConfig>
      <HSQLService database="./src/test/test" dbname="test" trace="true" silent="false" port="9001"/>
  </componentConfig>
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/src/test/test.script
  
  Index: test.script
  ===================================================================
  CREATE TABLE ID_TABLE(ID_TABLE_ID INTEGER NOT NULL PRIMARY KEY,TABLE_NAME VARCHAR(255),NEXT_ID INTEGER,QUANTITY INTEGER,CONSTRAINT SYS_CT_73 UNIQUE(TABLE_NAME))
  CREATE TABLE TURBINE_PERMISSION(PERMISSION_ID INTEGER NOT NULL PRIMARY KEY,PERMISSION_NAME VARCHAR(99) NOT NULL,OBJECTDATA BINARY,CONSTRAINT SYS_CT_75 UNIQUE(PERMISSION_NAME))
  CREATE TABLE TURBINE_ROLE(ROLE_ID INTEGER NOT NULL PRIMARY KEY,ROLE_NAME VARCHAR(99) NOT NULL,OBJECTDATA BINARY,CONSTRAINT SYS_CT_77 UNIQUE(ROLE_NAME))
  CREATE TABLE TURBINE_GROUP(GROUP_ID INTEGER NOT NULL PRIMARY KEY,GROUP_NAME VARCHAR(99) NOT NULL,OBJECTDATA BINARY,CONSTRAINT SYS_CT_79 UNIQUE(GROUP_NAME))
  CREATE TABLE TURBINE_ROLE_PERMISSION(ROLE_ID INTEGER NOT NULL,PERMISSION_ID INTEGER NOT NULL,CONSTRAINT SYS_PK_TURBINE_ROLE_PERMISSION PRIMARY KEY(ROLE_ID,PERMISSION_ID),CONSTRAINT SYS_FK_81 FOREIGN KEY(ROLE_ID) REFERENCES TURBINE_ROLE(ROLE_ID),CONSTRAINT SYS_FK_82 FOREIGN KEY(PERMISSION_ID) REFERENCES TURBINE_PERMISSION(PERMISSION_ID))
  CREATE TABLE TURBINE_USER(USER_ID INTEGER NOT NULL PRIMARY KEY,LOGIN_NAME VARCHAR(32) NOT NULL,PASSWORD_VALUE VARCHAR(32) NOT NULL,FIRST_NAME VARCHAR(99) NOT NULL,LAST_NAME VARCHAR(99) NOT NULL,EMAIL VARCHAR(99),CONFIRM_VALUE VARCHAR(99),MODIFIED TIMESTAMP,CREATED TIMESTAMP,LAST_LOGIN TIMESTAMP,OBJECTDATA BINARY,CONSTRAINT SYS_CT_87 UNIQUE(LOGIN_NAME))
  CREATE TABLE TURBINE_USER_GROUP_ROLE(USER_ID INTEGER NOT NULL,GROUP_ID INTEGER NOT NULL,ROLE_ID INTEGER NOT NULL,CONSTRAINT SYS_PK_TURBINE_USER_GROUP_ROLE PRIMARY KEY(USER_ID,GROUP_ID,ROLE_ID),CONSTRAINT SYS_FK_89 FOREIGN KEY(USER_ID) REFERENCES TURBINE_USER(USER_ID),CONSTRAINT SYS_FK_90 FOREIGN KEY(GROUP_ID) REFERENCES TURBINE_GROUP(GROUP_ID),CONSTRAINT SYS_FK_91 FOREIGN KEY(ROLE_ID) REFERENCES TURBINE_ROLE(ROLE_ID))
  CREATE TABLE TURBINE_SCHEDULED_JOB(JOB_ID INTEGER NOT NULL PRIMARY KEY,SECOND INTEGER DEFAULT '-1' NOT NULL,MINUTE INTEGER DEFAULT '-1' NOT NULL,HOUR INTEGER DEFAULT '-1' NOT NULL,WEEK_DAY INTEGER DEFAULT '-1' NOT NULL,DAY_OF_MONTH INTEGER DEFAULT '-1' NOT NULL,TASK VARCHAR(99) NOT NULL,EMAIL VARCHAR(99),PROPERTY BINARY)
  CREATE USER SA PASSWORD "" ADMIN
  INSERT INTO ID_TABLE VALUES(1,'TURBINE_PERMISSION',100,10)
  INSERT INTO ID_TABLE VALUES(2,'TURBINE_ROLE',100,10)
  INSERT INTO ID_TABLE VALUES(3,'TURBINE_GROUP',100,10)
  INSERT INTO ID_TABLE VALUES(4,'TURBINE_ROLE_PERMISSION',100,10)
  INSERT INTO ID_TABLE VALUES(5,'TURBINE_USER',100,10)
  INSERT INTO ID_TABLE VALUES(6,'TURBINE_USER_GROUP_ROLE',100,10)
  INSERT INTO ID_TABLE VALUES(7,'TURBINE_SCHEDULED_JOB',100,10)
  INSERT INTO TURBINE_PERMISSION VALUES(1,'admin_users',NULL)
  INSERT INTO TURBINE_ROLE VALUES(1,'turbine_root',NULL)
  INSERT INTO TURBINE_GROUP VALUES(1,'global',NULL)
  INSERT INTO TURBINE_ROLE_PERMISSION VALUES(1,1)
  INSERT INTO TURBINE_USER VALUES(0,'tomcat','tomcat','tomcat','tomcat','admin@it20one.at',NULL,NULL,NULL,NULL,NULL)
  
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/src/test/TestRoleConfig.xml
  
  Index: TestRoleConfig.xml
  ===================================================================
  <!-- This configuration file for Avalon components is used for testing the TestComponent -->
  <role-list>
      <role
          name="org.apache.fulcrum.hsqldb.HSQLService"
          default-class="org.apache.fulcrum.hsqldb.HSQLServiceImpl"
          shorthand="HSQLService"
          early-init="false"
      />
  </role-list>
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/src/java/org/apache/fulcrum/hsqldb/HSQLServiceImpl.java
  
  Index: HSQLServiceImpl.java
  ===================================================================
  package org.apache.fulcrum.hsqldb;
  
  /*
   * Copyright 2004 Apache Software Foundation
   * Licensed  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.
   */
  
  import java.io.File;
  
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.activity.Startable;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  
  import org.hsqldb.HsqlProperties;
  import org.hsqldb.Server;
  import org.hsqldb.ServerConstants;
  
  /**
   * The originial implementation was taken from
   * http://scarab.tigris.org/source/browse/scarab/src/java/org/tigris/scarab/services/hsql/
   * and tweaked a little bit.
   *
   * <p>
   * The component is configured from the componentConfig.xml file by specifying
   * attributes on the service element
   * </p>
   * <p>
   *
   * <dl>
   * <dt>database</dt>
   * <dd>The directory path where the database files will be stored</dd>
   * <dt>dbname</dt>
   * <dd>The alias path used to refer to the database from the JDBC url.</dd>
   * <dt>trace</dt>
   * <dd>(true/false) a flag enabling tracing in the hsql server.</dd>
   * <dt>silent</dt>
   * <dd>(true/false) a flag to control the logging output oh thr hsql server.</dd>
   * <dt>start</dt>
   * <dd>(true/false) when true the database is started at configuration time, and does
   * not need to be started under application control.</dd>
   * <dt>port</dt>
   * <dd>The listening port of the hsql server.</dd>
   * </dl>
   *
   * Example:
   *  ...
   *  <HSQLService database="./target" dbname="test" trace="true" silent="false" start="true" port="9001"/>
   *  ...
   *
   * @author <a href="mailto:pti@elex.be">Peter Tillemans</a>
   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
   */
  public class HSQLServiceImpl 
  	extends AbstractLogEnabled 
  	implements HSQLService, Configurable, Initializable, Contextualizable, Startable, Disposable
  {
      /** the HSQLDB server instance */
      private Server server;
      
      /** the configuration properties */
      private HsqlProperties serverProperties;
      
      /** the application directory */
      private File applicationDir;
      
      /////////////////////////////////////////////////////////////////////////
      // Avalon Service Lifecycle Implementation
      /////////////////////////////////////////////////////////////////////////
      
      /**
       * Constructor
       */
      public HSQLServiceImpl()
      {
      }
      
      public boolean isRunning() {
          // return true id server is online
          return server.getState() == ServerConstants.SERVER_STATE_ONLINE;
      }    
      
      /**
       * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
       */
      public void contextualize(Context context) throws ContextException
      {
          this.applicationDir = (File) context.get("urn:avalon:home");
      }
  
      /**
       * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
       */
      public void configure(Configuration cfg) throws ConfigurationException 
      {
          String[] names = cfg.getAttributeNames();
      
          for (int i = 0; i < names.length; i++) 
          {
              getLogger().debug(names[i] + " --> " + cfg.getAttribute(names[i]));
          }
  
          this.serverProperties = new HsqlProperties();
          this.serverProperties.setProperty("server.database.0", cfg.getAttribute("database"));
          this.serverProperties.setProperty("server.dbname.0", cfg.getAttribute("dbname"));
          this.serverProperties.setProperty("server.trace", cfg.getAttributeAsBoolean("trace"));
          this.serverProperties.setProperty("server.silent", cfg.getAttributeAsBoolean("silent"));        
          this.serverProperties.setProperty("server.port", cfg.getAttribute("port"));
      }
      
      /**
       * @see org.apache.avalon.framework.activity.Initializable#initialize()
       */
      public void initialize() throws Exception
      {
          this.server = new Server();
          this.server.setProperties( this.serverProperties );        
      }
      
      /**
       * Starts the HSQLDB server. The implementation polls to ensure
       * that the HSQLDB server is fully initialized otherwise we get
       * spurious connection exceptions. If the HSQLDB server is not
       * upand running within 10 seconds we throw an exception.
       *  
       * @see org.apache.avalon.framework.activity.Startable#start()
       */
      public void start() throws Exception
      {
          // The method start() waits for current state to change from
          // SERVER_STATE_OPENNING. In order to discover the success or failure
          // of this operation, server state must be polled or a subclass of Server
          // must be used that overrides the setState method to provide state
          // change notification.
          
          server.start();
          
          // poll for 10 seconds until HSQLDB is up and running
  
          this.pollForState( ServerConstants.SERVER_STATE_ONLINE, 100 );        
      }
  
      /**
       * Stop the HSQLDB server. The implementation polls to ensure
       * that the HSQLDB server has terminated otherwise someone
       * could call System.exit() and break the database.
       * 
       * @see org.apache.avalon.framework.activity.Startable#stop()
       */
      public void stop() throws Exception
      {
          this.server.stop();  
          
          // poll for 10 seconds until HSQLDB is down 
  
          this.pollForState( ServerConstants.SERVER_STATE_SHUTDOWN, 100 );  
      }
      
      /**
       * @see org.apache.avalon.framework.activity.Disposable#dispose()
       */
      public void dispose()
      {
          this.server = null;
          this.serverProperties = null;
          this.applicationDir = null;
      }   
      
      /**
       * Poll the HSQLDB server for a state change
       * @param desiredState the state we are waiting for
       * @throws Exception something went wrong
       */
      
      private void pollForState( int desiredState, int lim )
      	throws Exception
      {
          int currentState;
          boolean isSuccessful = false;
          
          this.getLogger().debug( "Polling for state : " + desiredState );
          
          for( int i=0; i<lim; i++ )
          {
              currentState = this.server.getState();
              
              if( desiredState == currentState )
              {
                  isSuccessful = true;
                  break;
              }
              
              Thread.sleep(100);
          }
          
          if( isSuccessful == false )
          {
              String msg = "Unable to change the HSQLDB server to state : " + desiredState;
                  
              if( this.server.getServerError() != null )
              {
                  this.getLogger().error( msg, this.server.getServerError() );
                  
  	            if( this.server.getServerError() instanceof Exception )
  	            {
  	                throw (Exception) this.server.getServerError();
  	            }
  
  	            if( this.server.getServerError() instanceof Throwable )
  	            {
  	                throw new RuntimeException( this.server.getServerError().getMessage() );
  	            }
              }
              else
              {
  	            this.getLogger().error(msg);
  	            throw new RuntimeException( msg );
              }
          }        
      }
  }
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/src/java/org/apache/fulcrum/hsqldb/HSQLService.java
  
  Index: HSQLService.java
  ===================================================================
  package org.apache.fulcrum.hsqldb;
  
  /*
   * Copyright 2004 Apache Software Foundation
   * Licensed  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.
   */
  
  /**
   * A definition for a HSQLService component which configures a single hsqldb
   * database.
   * 
   * @author <a href="mailto:pti@elex.be">Peter Tillemans</a>
   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
   */
  public interface HSQLService 
  {
      /**
       * Check if the server is running
       * 
       * @return the state of the hsqldb server
       */
      public boolean isRunning();
  }
  
  
  1.1                  jakarta-turbine-fulcrum/hsqldb/src/test/org/apache/fulcrum/hsqldb/HSQLServiceTest.java
  
  Index: HSQLServiceTest.java
  ===================================================================
  package org.apache.fulcrum.hsqldb;
  
  /*
   * Copyright 2004 Apache Software Foundation
   * Licensed  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.
   */
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.Statement;
  
  import org.apache.fulcrum.testcontainer.BaseUnitTest;
  
  /**
   * HSQLServiceTest
   *
   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
   */
  public class HSQLServiceTest extends BaseUnitTest
  {
  	private HSQLService service;
  	
      /**
       * Defines the testcase name for JUnit.
       *
       * @param name the testcase's name.
       */
      public HSQLServiceTest(String name)
      {
          super(name);
      }
  
      protected void setUp() throws Exception
      {
          super.setUp();
  
          try
          {
              Class.forName("org.hsqldb.jdbcDriver");
              service = (HSQLService) this.lookup(HSQLService.class.getName());
          }
          catch (Exception e)
          {
              e.printStackTrace();
              fail(e.getMessage());
          }
      }
          
      protected Connection getConnection(String db) throws Exception
      {
          String connectionString = "jdbc:hsqldb:hsql://localhost:9001/" + db;
          return DriverManager.getConnection(
              connectionString,
              "sa",
              ""
              );
      }
      /**
       * Simple test that verify the select from the HSQLDB server
       * @throws Exception
       */
      public void testMe() throws Exception
      {        
          Connection conn = null;
          
          try
          {
              assertNotNull( "HSQL Service must be available", service);
              
              conn = this.getConnection("test"); 
              Statement stmt = conn.createStatement();
              stmt.execute("SELECT * FROM TURBINE_USER;");
              ResultSet rs = stmt.getResultSet();
              
              while( rs.next() )
              {
                  String loginName = rs.getString("LOGIN_NAME");
                  assertTrue( loginName.length() > 0 );
              }
          }
          catch (Exception e)
          {
              e.printStackTrace();
              fail(e.getMessage());
          }
          finally
          {
              if( conn != null )
              {
                  conn.close();
              }
          }
      }
      
      public void testIsRunning() throws Exception {
  
          assertTrue("Server was not started", service.isRunning());
          ((HSQLServiceImpl)service).stop();
          assertFalse("Server is still running", service.isRunning());
         
      }
   
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org