/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/test/SaffronTestCase.java#1 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. // // jhyde, Apr 26, 2002 */ package saffron.test; import junit.framework.TestCase; import saffron.Statement; import saffron.runtime.SyntheticObject; import java.util.*; import java.lang.reflect.Field; public abstract class SaffronTestCase extends TestCase { protected Statement.Argument[] arguments; static final Statement.Argument[] emptyArguments = new Statement.Argument[0]; public SaffronTestCase(String s) throws Exception { super(s); init(); } protected void init() throws Exception { this.arguments = emptyArguments; } /** Runs a query and returns the result. **/ protected Object runQuery(String query, Statement.Argument[] arguments) { Statement statement = new Statement(); return statement.execute(query, arguments); } /** Runs a query, using the default arguments, and returns the result. **/ protected Object runQuery(String query) { Statement statement = new Statement(); return statement.execute(query, this.arguments); } /** Runs a query, and returns any exception. **/ protected Throwable runQueryCatch(String query) { try { Object o = runQuery(query); return null; } catch (Throwable throwable) { return throwable; } } protected static void assertEqualsDeep(Object o, Object o2) { if (o instanceof Object[] && o2 instanceof Object[]) { Object[] a = (Object[]) o; Object[] a2 = (Object[]) o2; assertEquals(a.length, a2.length); for (int i = 0; i < a.length; i++) { assertEqualsDeep(a[i], a2[i]); } return; } if (o != null && o2 != null && o.getClass().isArray() && o.getClass() == o2.getClass()) { boolean eq; if (o instanceof boolean[]) { eq = Arrays.equals((boolean[]) o, (boolean[]) o2); } else if (o instanceof byte[]) { eq = Arrays.equals((byte[]) o, (byte[]) o2); } else if (o instanceof char[]) { eq = Arrays.equals((char[]) o, (char[]) o2); } else if (o instanceof short[]) { eq = Arrays.equals((short[]) o, (short[]) o2); } else if (o instanceof int[]) { eq = Arrays.equals((int[]) o, (int[]) o2); } else if (o instanceof long[]) { eq = Arrays.equals((long[]) o, (long[]) o2); } else if (o instanceof float[]) { eq = Arrays.equals((float[]) o, (float[]) o2); } else if (o instanceof double[]) { eq = Arrays.equals((double[]) o, (double[]) o2); } else { eq = false; } if (!eq) { fail("arrays not equal"); } } else { // will handle the case 'o instanceof int[]' ok, because // shallow comparison is ok for ints assertEquals(o, o2); } } /** * Checks that <code>o</code> is a result set with a given number of * rows, a list of columns with given names, and every row is * the same type. */ protected void assertSynthetic(Object o, int rows, String[] columnNames) { assertSynthetic(o, rows, columnNames, null); } /** * Checks that <code>o</code> is a result set with a given number of * rows, a list of columns with given names and types, and every row is * the same type. */ protected void assertSynthetic( Object o, int rows, String[] columnNames, Class[] columnClasses) { assertTrue(o instanceof SyntheticObject[]); SyntheticObject[] a = (SyntheticObject[]) o; assertEquals("row count", rows, a.length); Class rowClazz0 = null; for (int i = 0; i < a.length; i++) { SyntheticObject row = a[i]; Class rowClazz = row.getClass(); if (i == 0) { rowClazz0 = rowClazz; Field[] fields = row.getFields(); assertEquals( "number of fields", columnNames.length, fields.length); for (int j = 0; j < fields.length; j++) { Field field = fields[j]; assertEquals( "field name", columnNames[j], fields[j].getName()); if (columnClasses != null) { Class valueClazz = field.getType(); assertEquals( "column class", columnClasses[j], valueClazz); } } } else { assertEquals("row class same as first", rowClazz0, rowClazz); } } } /** * Fails if <code>throwable</code> is null, or if its message does not * contain the string <code>pattern</code>. */ protected void assertThrowableContains( Throwable throwable, String pattern) { if (throwable == null) { fail( "expected exception containing pattern <" + pattern + "> but got none"); } String message = throwable.getMessage(); if (message.indexOf(pattern) < 0) { fail( "expected pattern <" + pattern + "> in exception <" + throwable + ">"); } } /** * Returns an iterator over the elements of an array. */ protected static Iterator makeIterator(Object[] a) { return Arrays.asList(a).iterator(); } /** * Converts an iterator to a list. */ protected static List toList(Iterator iterator) { ArrayList list = new ArrayList(); while (iterator.hasNext()) { list.add(iterator.next()); } return list; } /** * Converts an enumeration to a list. */ protected static List toList(Enumeration enumeration) { ArrayList list = new ArrayList(); while (enumeration.hasMoreElements()) { list.add(enumeration.nextElement()); } return list; } /** * Checks that an iterator returns the same objects as the contents of an * array. */ protected void assertEquals(Iterator iterator, Object[] a) { ArrayList list = new ArrayList(); while (iterator.hasNext()) { list.add(iterator.next()); } assertEquals(list, a); } /** * Checks that a list has the same contents as an array. */ protected void assertEquals(List list, Object[] a) { Object[] b = list.toArray(); assertTrue(a.length == b.length); for (int i = 0; i < a.length; i++) { assertEquals(a[i], b[i]); } } protected static final String[] emptyStringArray = new String[0]; protected void setUp() throws Exception { super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); } } // End SaffronTestCase.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 1748 | Julian Hyde |
saffron: convert unit tests to JUnit; add CallingConvention.ITERABLE; lots of other stuff; release 0.1. |