You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jm...@apache.org on 2004/06/11 04:51:45 UTC

cvs commit: jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl HibernateBasicMessage.hbm.xml HibernateResourcesTestCase.java jdbc.test.config.properties JDBCResourcesTestCase.java

jmitchell    2004/06/10 19:51:45

  Added:       resources/src/test/org/apache/commons/resources/impl
                        HibernateBasicMessage.hbm.xml
                        HibernateResourcesTestCase.java
                        jdbc.test.config.properties
                        JDBCResourcesTestCase.java
  Log:
  Changes required to support move of JDBC and Hibernate impls.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/HibernateBasicMessage.hbm.xml
  
  Index: HibernateBasicMessage.hbm.xml
  ===================================================================
  <?xml version="1.0"?>
  <!DOCTYPE hibernate-mapping SYSTEM 
  	"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
  
  
  <hibernate-mapping>
  
      <class name="org.apache.commons.resources.impl.HibernateBasicMessage" table="resources">
  
      	<composite-id>
  		 <key-property name="locale"	type="java.lang.String"	column="locale"/>
  		 <key-property name="key"	type="java.lang.String"	column="msgKey"/>
  		</composite-id>
  
          <property name="value" type="java.lang.String" column="val"/>
      </class>
  
  	<query name="QueryByLocale">
  		
  		    from 
  		    	org.apache.commons.resources.impl.HibernateBasicMessage as msgRes
  		    where 
  		    	msgRes.locale = ?
  		
  	</query>
  
  </hibernate-mapping>
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/HibernateResourcesTestCase.java
  
  Index: HibernateResourcesTestCase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/HibernateResourcesTestCase.java,v 1.1 2004/06/11 02:51:45 jmitchell Exp $
   * $Revision: 1.1 $
   * $Date: 2004/06/11 02:51:45 $
   *
   * ====================================================================
   *
   *  Copyright 2003-2004 The 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.
   *
   */
  package org.apache.commons.resources.impl;
  
  import java.net.URL;
  import java.sql.Connection;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import net.sf.hibernate.HibernateException;
  import net.sf.hibernate.Session;
  import net.sf.hibernate.SessionFactory;
  import net.sf.hibernate.cfg.Configuration;
  
  /**
   * <p>Unit tests for
   * <code>org.apache.commons.resources.impl.HibernateResources</code>.
   * </p>
   *
   * @author James Mitchell
   * @version $Revision: 1.1 $ $Date: 2004/06/11 02:51:45 $
   */
  
  public class HibernateResourcesTestCase
      extends CollectionResourcesBaseTestCase {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      // ----------------------------------------------------------- Constructors
  
  
      public HibernateResourcesTestCase(String name) {
          super(name);
      }
  
  
      // --------------------------------------------------- Overall Test Methods
  
  
      // Set up instance variables required by this test case
      public void setUp() throws Exception {
          factory = new HibernateResourcesFactory();
          resources = factory.getResources(
                  NAME, 
                  "hibernate.cfg.xml");
          String sql = "";
          sql += "create table resources (" ;
          sql +="  locale            varchar(10)      not null,";
          sql +="  msgKey            varchar(255)     not null,";
          sql +="  val               varchar(255),";
          sql +="  Primary Key(";
          sql +="    locale,";
          sql +="    msgKey";
          sql +="  )";
          sql +=" )";
          
          runSql(sql);
          runSql("Insert into resources (locale, msgKey, val) Values ('', 'test.base', '[Base] ONLY');");
          runSql("Insert into resources (locale, msgKey, val) Values ('', 'test.specific', '[Base] SPECIFIC');");
          runSql("Insert into resources (locale, msgKey, val) Values ('', 'test.inherit', '[Base] INHERIT');");
          runSql("Insert into resources (locale, msgKey, val) Values ('', 'test.message', '[Base] REPLACE {0} WITH {1}');");
          runSql("Insert into resources (locale, msgKey, val) Values ('en', 'test.specific', '[en] SPECIFIC');");
          runSql("Insert into resources (locale, msgKey, val) Values ('en', 'test.inherit', '[en] INHERIT');");
          runSql("Insert into resources (locale, msgKey, val) Values ('en_US', 'test.specific', '[en_US] SPECIFIC');");
          runSql("Insert into resources (locale, msgKey, val) Values ('fr', 'test.specific', '[fr] SPECIFIC');");
          runSql("Insert into resources (locale, msgKey, val) Values ('fr', 'test.inherit', '[fr] INHERIT');");
          
      }
  
  	// Return the tests included in this test suite
      public static Test suite() {
          return (new TestSuite(HibernateResourcesTestCase.class));
      }
  
      // Tear down the instance variables required by this test case
      public void tearDown() {
          try{
          	runSql("drop table resources");
          }catch(Exception e){
          	// not really necessary to fail if creation also fails
          	e.printStackTrace();
          }
          resources = null;
          factory = null;
      }
  
  
      // ------------------------------------------------ Individual Test Methods
  
  
      // ------------------------------------------------------ Protected Methods
      protected String getBase() throws Exception
      {
          // default file name
          URL url = 
              HibernateResourcesTestCase.class.getResource
              ("hibernate.cfg.xml");
      
          if (url == null) {
              fail("URL NOT FOUND");
          }
          String string = url.toExternalForm();
          String base = string.substring(0, string.length() - 11);
          return base;
      }
  
  
      // ------------------------------------------------------ Private Methods
  
      /**
  	 * @param sql
  	 * @throws SQLException
  	 */
  	private void runSql(String sql) throws SQLException {
  		Session session = null;
          try {            
              // TODO - change this to load the specified hibernate config file (baseUrl)
              SessionFactory sessionFactory =
                  new Configuration().configure().buildSessionFactory();
              session = sessionFactory.openSession();
              
              Connection cn = session.connection();
              Statement st = cn.createStatement();
              st.execute(sql);
      		session.close();
          
          } catch (HibernateException e) {
              e.printStackTrace();
          }
  	}
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/jdbc.test.config.properties
  
  Index: jdbc.test.config.properties
  ===================================================================
  jdbc.connect.driver               = org.hsqldb.jdbcDriver
  jdbc.connect.url                  = jdbc:hsqldb:.
  jdbc.connect.login                = sa
  jdbc.connect.password             = 
  
  jdbc.sql.db                       = RESOURCES
  jdbc.sql.table                    = RESOURCES
  jdbc.sql.locale.column            = locale
  jdbc.sql.key.column               = msgKey
  jdbc.sql.val.column               = val
  
  org.apache.commons.resource.CACHE = true
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/JDBCResourcesTestCase.java
  
  Index: JDBCResourcesTestCase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/JDBCResourcesTestCase.java,v 1.1 2004/06/11 02:51:45 jmitchell Exp $
   * $Revision: 1.1 $
   * $Date: 2004/06/11 02:51:45 $
   *
   * ====================================================================
   *
   *  Copyright 2003-2004 The 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.
   *
   */
  package org.apache.commons.resources.impl;
  
  import java.net.URL;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * <p>Unit tests for
   * <code>org.apache.commons.resources.impl.JDBCResources</code>.
   * </p>
   *
   * @author James Mitchell
   * @version $Revision: 1.1 $ $Date: 2004/06/11 02:51:45 $
   */
  
  public class JDBCResourcesTestCase
      extends CollectionResourcesBaseTestCase {
  
  
      // ----------------------------------------------------- Instance Variables
  	Connection con = null;
  
  
      protected String getBase() throws Exception
      {
          URL url = 
              JDBCResourcesTestCase.class.getResource
              ("/org/apache/commons/resources/impl/jdbc.test.config.properties");
      
          if (url == null) {
              fail("URL NOT FOUND");
          }
          String string = url.toExternalForm();
          String base = string.substring(0, string.length() - 11);
          return base;
      }
  
      // ----------------------------------------------------------- Constructors
  
  
      public JDBCResourcesTestCase(String name) {
          super(name);
      }
  
  
      // --------------------------------------------------- Overall Test Methods
  
  
      // Set up instance variables required by this test case
      public void setUp() throws Exception {
          factory = new JDBCResourcesFactory();
          resources = factory.getResources(NAME, getBase());
          
          String[] sql = {""
          + "create table resources (" 
          + "  locale            varchar(10)      not null,"
          + "  msgKey            varchar(255)     not null,"
          + "  val               varchar(255),"
          + "  Primary Key("
          + "    locale,"
          + "    msgKey"
          + "  )"
          + " );"
  		, "Insert into resources (locale, msgKey, val) Values ('', 'test.base', '[Base] ONLY');"
          , "Insert into resources (locale, msgKey, val) Values ('', 'test.specific', '[Base] SPECIFIC');"
          , "Insert into resources (locale, msgKey, val) Values ('', 'test.inherit', '[Base] INHERIT');"
          , "Insert into resources (locale, msgKey, val) Values ('', 'test.message', '[Base] REPLACE {0} WITH {1}');"
          , "Insert into resources (locale, msgKey, val) Values ('en', 'test.specific', '[en] SPECIFIC');"
          , "Insert into resources (locale, msgKey, val) Values ('en', 'test.inherit', '[en] INHERIT');"
          , "Insert into resources (locale, msgKey, val) Values ('en_US', 'test.specific', '[en_US] SPECIFIC');"
          , "Insert into resources (locale, msgKey, val) Values ('fr', 'test.specific', '[fr] SPECIFIC');"
          , "Insert into resources (locale, msgKey, val) Values ('fr', 'test.inherit', '[fr] INHERIT');"
          };
          runSql(sql);
      }
  
      // Return the tests included in this test suite
      public static Test suite() {
          return (new TestSuite(JDBCResourcesTestCase.class));
      }
  
      // Tear down the instance variables required by this test case
      public void tearDown() {
          resources = null;
          factory = null;
          try{
          	runSql(new String[]{"drop table resources"});
  			con.close();
          }catch(Exception e){
          	// not really necessary to fail if creation also fails
          	e.printStackTrace();
          }
  
      }
  
  
      // ------------------------------------------------ Individual Test Methods
  
  
      // ------------------------------------------------------ Protected Methods
  
  
      // ------------------------------------------------------ Private Methods
  
      /**
  	 * @param sql
  	 * @throws SQLException
  	 */
  	private void runSql(String[] sql) throws SQLException {
  
  		String url = "jdbc:hsqldb:.";
  		
  		Statement stmt;
  		try {
  			Class.forName("org.hsqldb.jdbcDriver");
  
  		} catch(java.lang.ClassNotFoundException e) {
  			fail("ClassNotFoundException:" + e.getMessage());
  		}
  
  		try {
  			// TODO change this to pull from jdbc.test.config.properties
  			if (con == null || con.isClosed())
  				con = DriverManager.getConnection(url, "sa", "");
  			stmt = con.createStatement();
  			
  			for (int i = 0; i < sql.length; i++) {
  		   	    stmt.execute(sql[i]);
  			}
  			stmt.close();
  	
  		} catch(SQLException ex) {
  			fail("SQLException: " + ex.getMessage());
  		}	
      }
  }
  
  
  

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