commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql RowTag.java ResultSetTag.java SqlTagLibrary.java ResultImpl.java
Date Thu, 05 Sep 2002 16:41:17 GMT
jstrachan    2002/09/05 09:41:17

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/sql
                        SqlTagLibrary.java ResultImpl.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/sql RowTag.java
                        ResultSetTag.java
  Log:
  Added new SQL tags that allow a ResultSet to be created programatically without actually
performing any SQL.
  This can be useful for implementing Mock Tags (like Mock Objects but Tags) etc.
  
  Revision  Changes    Path
  1.3       +4 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/SqlTagLibrary.java
  
  Index: SqlTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/SqlTagLibrary.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SqlTagLibrary.java	17 May 2002 15:18:09 -0000	1.2
  +++ SqlTagLibrary.java	5 Sep 2002 16:41:17 -0000	1.3
  @@ -79,6 +79,10 @@
           registerTag("setDataSource", SetDataSourceTag.class);
           registerTag("transaction", TransactionTag.class);
           registerTag("update", UpdateTag.class);
  +        
  +        // extension tags for Mock Tags support
  +        registerTag("resultSet", ResultSetTag.class);
  +        registerTag("row", RowTag.class);
       }
   
   }
  
  
  
  1.2       +48 -2     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/ResultImpl.java
  
  Index: ResultImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/ResultImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ResultImpl.java	15 May 2002 06:25:47 -0000	1.1
  +++ ResultImpl.java	5 Sep 2002 16:41:17 -0000	1.2
  @@ -89,8 +89,8 @@
        * @exception if a database error occurs
        */
       public ResultImpl(ResultSet rs, int startRow, int maxRows)
  -        throws SQLException 
  -    {
  +        throws SQLException {
  +            
           rowMap = new ArrayList();
           rowByIndex = new ArrayList();
   
  @@ -131,6 +131,52 @@
               rowMap.add(columnMap);
               rowByIndex.add(columns);
               processedRows++;
  +        }
  +    }
  +
  +    /**
  +     * This constructor is given a List of Maps where each Map represents a Row of data.
  +     * This constructor is typically used to create a Mock Object representing a result
set.
  +     *
  +     * @param listOfMaps is a list of Maps where a Map represents a Row keyed by the column
name
  +     */
  +    public ResultImpl(List listOfMaps) {
  +            
  +        rowMap = new ArrayList();
  +        rowByIndex = new ArrayList();
  +        isLimited = false;
  +
  +        // lets build up a Set of all the unique column names
  +        HashSet keySet = new HashSet();
  +        for (Iterator iter = listOfMaps.iterator(); iter.hasNext(); ) {
  +            Map row = (Map) iter.next();
  +            keySet.addAll( row.keySet() );
  +        }
  +        
  +        // Create the column name array
  +        int noOfColumns = keySet.size();
  +        columnNames = new String[noOfColumns];        
  +        int i = 0;
  +        for (Iterator iter = keySet.iterator(); iter.hasNext(); i++ ) {
  +            columnNames[i] = (String) iter.next();
  +        }
  +        
  +        // Now add each row to the result set
  +        for (Iterator iter = listOfMaps.iterator(); iter.hasNext(); ) {
  +            Map row = (Map) iter.next();
  +            
  +            Object[] columns = new Object[noOfColumns];
  +            SortedMap columnMap = 
  +                new TreeMap(String.CASE_INSENSITIVE_ORDER);
  +
  +            for (i = 0; i < noOfColumns; i++) {
  +                String columnName = columnNames[i];
  +                Object value = row.get(columnName);
  +                columns[i] = value;
  +                columnMap.put(columnName, value);
  +            }
  +            rowMap.add(columnMap);
  +            rowByIndex.add(columns);
           }
       }
   
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/RowTag.java
  
  Index: RowTag.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.jelly.tags.sql;
  
  import java.util.Map;
  
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.MapTagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  /**
   * Adds a new row to a parent &lt;resultSet&gt; Tag. 
   * This tag is useful for unit testing with Mock Tags to simulate the results returned by
databases.
   * 
   */
  public class RowTag extends MapTagSupport {
      
      // Tag interface
      //------------------------------------------------------------------------- 
      public void doTag(XMLOutput output) throws Exception {
          ResultSetTag tag = (ResultSetTag) findAncestorWithClass( ResultSetTag.class );
          if ( tag == null ) {
              throw new JellyException( "This tag must be nested with in a <resultSet>
tag" );
          }
          tag.addRow( getAttributes() );
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/ResultSetTag.java
  
  Index: ResultSetTag.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.jelly.tags.sql;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  
  import org.apache.commons.jelly.MissingAttributeException;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  /**
   * This Tag creates a result set object based on its body content via child row tags. 
   * This tag is useful for unit testing with Mock Tags to simulate the results returned by
databases.
   * 
   */
  public class ResultSetTag extends TagSupport {
      
      private List rows;
      private String var;
      
      /** 
       * Adds the given row to the list of rows
       */
      public void addRow(Map row) {
          rows.add(row);
      }                    
      
      // Tag interface
      //------------------------------------------------------------------------- 
      public void doTag(XMLOutput output) throws Exception {
          if (var == null) {
              throw new MissingAttributeException( "var" );
          }
          rows = new ArrayList();
          invokeBody(output);
          
          // now lets create a new Result implementation
          ResultImpl results = new ResultImpl( rows );
          context.setVariable(var, results);
          rows = null;
      }
      
      // Properties
      //------------------------------------------------------------------------- 
      /**
       * Sets the variable to export the result set to.
       */
      public void setVar(String var) {
          this.var = var;
      }
  
  }
  
  
  

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


Mime
View raw message