commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils ResultSetIteratorTest.java MockResultSet.java
Date Wed, 22 Oct 2003 23:54:53 GMT
dgraham     2003/10/22 16:54:53

  Modified:    dbutils/src/test/org/apache/commons/dbutils
                        MockResultSet.java
  Added:       dbutils/src/test/org/apache/commons/dbutils
                        ResultSetIteratorTest.java
  Log:
  Added ResultSetIteratorTest.
  
  Revision  Changes    Path
  1.2       +91 -66    jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/MockResultSet.java
  
  Index: MockResultSet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/MockResultSet.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockResultSet.java	22 Oct 2003 04:16:04 -0000	1.1
  +++ MockResultSet.java	22 Oct 2003 23:54:53 -0000	1.2
  @@ -75,72 +75,97 @@
    */
   class MockResultSet implements InvocationHandler {
   
  -    private Object[] rows = null;
  +	private Object[] rows = null;
   
  -    private ResultSetMetaData metaData = null;
  +	private ResultSetMetaData metaData = null;
   
  -    private Iterator iter = null;
  +	private Iterator iter = null;
   
  -    private Object[] currentRow = null;
  +	private Object[] currentRow = null;
   
  -    private Boolean wasNull = Boolean.FALSE;
  -
  -    /**
  -     * Create a <code>MockResultSet</code> proxy object.  This is equivalent
to:
  -     * <pre>
  -     * ProxyFactory.instance().createResultSet(new MockResultSet(metaData, rows));
  -     * </pre>
  -     * 
  -     * @param metaData
  -     * @param rows
  -     * @return
  -     */
  -    public static ResultSet create(ResultSetMetaData metaData, Object[][] rows) {
  -        return ProxyFactory.instance().createResultSet(
  -            new MockResultSet(metaData, rows));
  -    }
  -
  -    public MockResultSet(ResultSetMetaData metaData, Object[][] rows) {
  -        super();
  -        this.metaData = metaData;
  -        this.rows = rows;
  -        this.iter = Arrays.asList(rows).iterator();
  -    }
  -
  -    public Object invoke(Object proxy, Method method, Object[] args)
  -        throws Throwable {
  -
  -        String methodName = method.getName();
  -
  -        if (methodName.equals("getMetaData")) {
  -            return this.metaData;
  -
  -        } else if (methodName.equals("next")) {
  -            if (!iter.hasNext()) {
  -                return Boolean.FALSE;
  -            } else {
  -                this.currentRow = (Object[]) iter.next();
  -                return Boolean.TRUE;
  -            }
  -
  -        } else if (methodName.equals("previous")) {
  -
  -        } else if (methodName.equals("close")) {
  -
  -        } else if (methodName.equals("getObject")) {
  -            int col = ((Integer) args[0]).intValue() - 1;
  -
  -            Object obj = this.currentRow[col];
  -            if (obj == null) {
  -                this.wasNull = (obj == null) ? Boolean.TRUE : Boolean.FALSE;
  -            }
  -
  -            return obj;
  -
  -        } else if (methodName.equals("wasNull")) {
  -            return this.wasNull;
  -        }
  -
  -        return null;
  -    }
  +	private Boolean wasNull = Boolean.FALSE;
  +
  +	/**
  +	 * Create a <code>MockResultSet</code> proxy object.  This is equivalent to:
  +	 * <pre>
  +	 * ProxyFactory.instance().createResultSet(new MockResultSet(metaData, rows));
  +	 * </pre>
  +	 * 
  +	 * @param metaData
  +	 * @param rows
  +	 * @return
  +	 */
  +	public static ResultSet create(
  +		ResultSetMetaData metaData,
  +		Object[][] rows) {
  +            
  +		return ProxyFactory.instance().createResultSet(
  +			new MockResultSet(metaData, rows));
  +	}
  +
  +	public MockResultSet(ResultSetMetaData metaData, Object[][] rows) {
  +		super();
  +		this.metaData = metaData;
  +		this.rows = rows;
  +		this.iter = Arrays.asList(rows).iterator();
  +	}
  +
  +	public Object invoke(Object proxy, Method method, Object[] args)
  +		throws Throwable {
  +
  +		String methodName = method.getName();
  +
  +		if (methodName.equals("getMetaData")) {
  +			return this.getMetaData();
  +
  +		} else if (methodName.equals("next")) {
  +			return this.next();
  +
  +		} else if (methodName.equals("previous")) {
  +
  +		} else if (methodName.equals("close")) {
  +
  +		} else if (methodName.equals("getObject")) {
  +			int col = ((Integer) args[0]).intValue() - 1;
  +			return this.getObject(col);
  +
  +		} else if (methodName.equals("wasNull")) {
  +			return this.wasNull();
  +
  +		} else if (methodName.equals("isLast")) {
  +			return this.isLast();
  +		}
  +
  +		return null;
  +	}
  +
  +	protected Boolean isLast() {
  +		return this.iter.hasNext() ? Boolean.FALSE : Boolean.TRUE;
  +	}
  +
  +	protected Object getObject(int columnIndex) {
  +		Object obj = this.currentRow[columnIndex];
  +		if (obj == null) {
  +			this.wasNull = (obj == null) ? Boolean.TRUE : Boolean.FALSE;
  +		}
  +
  +		return obj;
  +	}
  +
  +	protected Boolean next() {
  +		if (!this.iter.hasNext()) {
  +			return Boolean.FALSE;
  +		} else {
  +			this.currentRow = (Object[]) iter.next();
  +			return Boolean.TRUE;
  +		}
  +	}
  +
  +	protected ResultSetMetaData getMetaData() {
  +		return this.metaData;
  +	}
  +
  +	protected Boolean wasNull() {
  +		return this.wasNull;
  +	}
   }
  
  
  
  1.1                  jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/ResultSetIteratorTest.java
  
  Index: ResultSetIteratorTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/ResultSetIteratorTest.java,v
1.1 2003/10/22 23:54:53 dgraham Exp $
   * $Revision: 1.1 $
   * $Date: 2003/10/22 23:54:53 $
   * 
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 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 acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 Software Foundation.
   *
   * 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.dbutils;
  
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  import java.util.Iterator;
  
  import junit.framework.TestCase;
  
  /**
   * ResultSetIteratorTest
   * 
   * @author David Graham
   */
  public class ResultSetIteratorTest extends TestCase {
  
  	private static ResultSetMetaData metaData = null;
  
  	private static Object[][] rows = null;
  
  	/**
  	 * Constructor for ResultSetIteratorTest.
  	 * @param arg0
  	 */
  	public ResultSetIteratorTest(String arg0) {
  		super(arg0);
  	}
  
  	protected void setUp() throws Exception {
  		super.setUp();
  
  		String[] columnNames = new String[] { "one", "two", "three" };
  		metaData = MockResultSetMetaData.create(columnNames);
  
  		rows = new Object[][] { { "1", "2", "3" }, {
  				"4", "5", "6" }
  		};
  	}
  
  	public void testNext() {
  		ResultSet rs = MockResultSet.create(metaData, rows);
  		Iterator iter = new ResultSetIterator(rs);
  
  		int rowCount = 0;
  		Object[] row = null;
  		while (iter.hasNext()) {
  			rowCount++;
  			row = (Object[]) iter.next();
  
  			assertNotNull(row);
  			assertEquals(3, row.length);
  		}
  
  		assertEquals(2, rowCount);
  		assertEquals("4", row[0]);
  		assertEquals("5", row[1]);
  		assertEquals("6", row[2]);
  	}
  
  }
  
  
  

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


Mime
View raw message