Return-Path: Delivered-To: apmail-db-torque-dev-archive@www.apache.org Received: (qmail 99332 invoked from network); 15 Jun 2006 12:58:22 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 15 Jun 2006 12:58:22 -0000 Received: (qmail 70308 invoked by uid 500); 15 Jun 2006 12:58:21 -0000 Delivered-To: apmail-db-torque-dev-archive@db.apache.org Received: (qmail 70173 invoked by uid 500); 15 Jun 2006 12:58:21 -0000 Mailing-List: contact torque-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Apache Torque Developers List" Reply-To: "Apache Torque Developers List" Delivered-To: mailing list torque-dev@db.apache.org Received: (qmail 70162 invoked by uid 500); 15 Jun 2006 12:58:21 -0000 Received: (qmail 70148 invoked by uid 99); 15 Jun 2006 12:58:21 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 15 Jun 2006 05:58:21 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 15 Jun 2006 05:58:19 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 17A9F1A9844; Thu, 15 Jun 2006 05:57:59 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r414572 - in /db/torque: runtime/trunk/src/java/org/apache/torque/om/ site/trunk/xdocs/ templates/trunk/src/templates/om/ test/trunk/test-project/src/java/org/apache/torque/om/ test/trunk/test-project/src/schema/ Date: Thu, 15 Jun 2006 12:57:57 -0000 To: torque-commits@db.apache.org From: tfischer@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060615125759.17A9F1A9844@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: tfischer Date: Thu Jun 15 05:57:57 2006 New Revision: 414572 URL: http://svn.apache.org/viewvc?rev=414572&view=rev Log: Added setByName and setByPosition methods in the generated object classes that match the already existing getByName and getByPosition methods Thanks to Greg Monroe for the contribution. Fixes TORQUE-26. Added: db/torque/test/trunk/test-project/src/java/org/apache/torque/om/OMByNameMethodsTest.java Modified: db/torque/runtime/trunk/src/java/org/apache/torque/om/BaseObject.java db/torque/site/trunk/xdocs/changes.xml db/torque/templates/trunk/src/templates/om/Object.vm db/torque/templates/trunk/src/templates/om/ObjectWithManager.vm db/torque/test/trunk/test-project/src/schema/test-schema.xml Modified: db/torque/runtime/trunk/src/java/org/apache/torque/om/BaseObject.java URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/om/BaseObject.java?rev=414572&r1=414571&r2=414572&view=diff ============================================================================== --- db/torque/runtime/trunk/src/java/org/apache/torque/om/BaseObject.java (original) +++ db/torque/runtime/trunk/src/java/org/apache/torque/om/BaseObject.java Thu Jun 15 05:57:57 2006 @@ -163,10 +163,10 @@ /** * Retrieves a field from the object by name. Must be overridden if called. * BaseObject's implementation will throw an Error. - * + * * @param field The name of the field to retrieve. * @return The retrieved field value - * + * */ public Object getByName(String field) { @@ -174,10 +174,26 @@ } /** - * Retrieves a field from the object by name passed in - * as a String. Must be overridden if called. - * BaseObject's implementation will throw an Error. - * + * Set a field in the object by field (Java) name. + * + * @param name field name + * @param value field value + * @return True if value was set, false if not (invalid name / protected + * field). + * @throws IllegalArgumentException if object type of value does not match + * field object type. + * @throws TorqueException If a problem occures with the set[Field] method. + */ + public boolean setByName(String name, Object value) throws TorqueException, + IllegalArgumentException + { + throw new Error("BaseObject.setByName: " + NOT_IMPLEMENTED); + } + + /** + * Retrieves a field from the object by name passed in as a String. Must be + * overridden if called. BaseObject's implementation will throw an Error. + * * @param name field name * @return value of the field */ @@ -187,10 +203,27 @@ } /** - * Retrieves a field from the object by position as specified - * in a database schema for example. Must be overridden if called. - * BaseObject's implementation will throw an Error. - * + * Set field values by Peer Field Name + * + * @param name field name + * @param value field value + * @return True if value was set, false if not (invalid name / protected + * field). + * @throws IllegalArgumentException if object type of value does not match + * field object type. + * @throws TorqueException If a problem occures with the set[Field] method. + */ + public boolean setByPeerName(String name, Object value) + throws TorqueException, IllegalArgumentException + { + throw new Error("BaseObject.setByPeerName: " + NOT_IMPLEMENTED); + } + + /** + * Retrieves a field from the object by position as specified in a database + * schema for example. Must be overridden if called. BaseObject's + * implementation will throw an Error. + * * @param pos field position * @return value of the field */ @@ -200,12 +233,30 @@ } /** - * Compares this with another BaseObject instance. If - * obj is an instance of BaseObject, delegates to - * equals(BaseObject). Otherwise, returns false. - * + * Set field values by it's position (zero based) in the XML schema. + * + * @param position The field position + * @param value field value + * @return True if value was set, false if not (invalid position / protected + * field). + * @throws IllegalArgumentException if object type of value does not match + * field object type. + * @throws TorqueException If a problem occures with the set[Field] method. + */ + public boolean setByPosition(int position, Object value) + throws TorqueException, IllegalArgumentException + { + throw new Error("BaseObject.setByPosition: " + NOT_IMPLEMENTED); + } + + /** + * Compares this with another BaseObject instance. If + * obj is an instance of BaseObject, + * delegates to equals(BaseObject). Otherwise, returns + * false. + * * @param obj The object to compare to. - * @return Whether equal to the object specified. + * @return Whether equal to the object specified. */ public boolean equals(Object obj) { Modified: db/torque/site/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/db/torque/site/trunk/xdocs/changes.xml?rev=414572&r1=414571&r2=414572&view=diff ============================================================================== --- db/torque/site/trunk/xdocs/changes.xml (original) +++ db/torque/site/trunk/xdocs/changes.xml Thu Jun 15 05:57:57 2006 @@ -29,6 +29,10 @@ + + Added setByName and setByPosition methods in the generated object classes + that match the already existing getByName and getByPosition methods. + Fixed wrong constant CLASS_DEFAULT_NAME in generated peer classes. The error occurred only if the option torque.subpackage.object Modified: db/torque/templates/trunk/src/templates/om/Object.vm URL: http://svn.apache.org/viewvc/db/torque/templates/trunk/src/templates/om/Object.vm?rev=414572&r1=414571&r2=414572&view=diff ============================================================================== --- db/torque/templates/trunk/src/templates/om/Object.vm (original) +++ db/torque/templates/trunk/src/templates/om/Object.vm Thu Jun 15 05:57:57 2006 @@ -255,7 +255,7 @@ for (int i = 0; i < ${collName}.size(); i++) { ((${tblFK.JavaName}) ${collName}.get(i)) - .${colFK.SetterName}(v); + .${colFK.SetterName}(v); } } #end @@ -946,7 +946,7 @@ #else * If this $table.JavaName is new, this method will return #end - * an empty collection; or if this $table.JavaName has previously + * an empty collection; or if this $table.JavaName has previously * been saved, it will retrieve related ${relCol} from storage. * * This method is protected by default in order to keep the public @@ -1034,7 +1034,7 @@ } /** - * Retrieves a field from the object by name passed in as a String. + * Retrieves a field from the object by field (Java) name passed in as a String. * * @param name field name * @return value @@ -1070,6 +1070,91 @@ } /** + * Set a field in the object by field (Java) name. + * + * @param name field name + * @param value field value + * @return True if value was set, false if not (invalid name / protected field). + * @throws IllegalArgumentException if object type of value does not match field object type. + * @throws TorqueException If a problem occures with the set[Field] method. + */ + public boolean setByName(String name, Object value ) + throws TorqueException, IllegalArgumentException + { + #foreach ($col in $table.Columns) + #set ( $cjtype = $col.JavaNative ) + if (name.equals("${col.JavaName}")) + { + #if ( ${col.isPrimitive()} ) + #if ($cjtype == "int") + if (value == null || ! (Integer.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object."); + } + ${col.SetterName}(((Integer) value).intValue()); + #elseif ($cjtype == "long") + if (value == null || ! (Long.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Long object."); + } + ${col.SetterName}(((Long) value).longValue()); + #elseif ($cjtype == "float") + if (value == null || ! (Float.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Float object."); + } + ${col.SetterName}(((Float) value).floatValue()); + #elseif ($cjtype == "double") + if (value == null || ! (Double.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Double object."); + } + ${col.SetterName}(((Double) value).doubleValue()); + #elseif ($cjtype == "boolean") + if (value == null || ! (Boolean.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Boolean object."); + } + ${col.SetterName}(((Boolean)value).booleanValue()); + #elseif ($cjtype == "short") + if (value == null || ! (Short.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Short object."); + } + ${col.SetterName}(((Short)value).shortValue()); + #elseif ($cjtype == "byte") + if (value == null || ! (Byte.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Byte object."); + } + ${col.SetterName}(((Byte) value).byteValue()); + #elseif ($cjtype == "char") + if (value == null || ! (Character.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Character object."); + } + ${col.SetterName}(((Character) value).charValue()); + #end + #else + #if ( ${col.JavaNative} ) + // Object fields can be null + if (value != null && ! ${col.JavaNative}.class.isInstance(value)) + { + throw new IllegalArgumentException("Invalid type of object specified for value in setByName"); + } + ${col.SetterName}(($cjtype) value); + #else + // Hmm, using column type that can't be mapped to a Java object class, make this obvious. + throw IllegalStateException("Can't determine Java Object type for column, ${col.Name}!"); + #end + #end + return true; + } + #end + return false; + } + + /** * Retrieves a field from the object by name passed in * as a String. The String must be one of the static * Strings defined in this Class' Peer. @@ -1109,6 +1194,28 @@ } /** + * Set field values by Peer Field Name + * + * @param name field name + * @param value field value + * @return True if value was set, false if not (invalid name / protected field). + * @throws IllegalArgumentException if object type of value does not match field object type. + * @throws TorqueException If a problem occures with the set[Field] method. + */ + public boolean setByPeerName(String name, Object value) + throws TorqueException, IllegalArgumentException + { + #foreach ($col in $table.Columns) + #set ( $cup=$col.Name.toUpperCase() ) + if (${table.JavaName}Peer.${cup}.equals(name)) + { + return setByName("${col.JavaName}", value); + } + #end + return false; + } + + /** * Retrieves a field from the object by Position as specified * in the xml schema. Zero-based. * @@ -1146,6 +1253,30 @@ #end return null; } + + /** + * Set field values by its position (zero based) in the XML schema. + * + * @param position The field position + * @param value field value + * @return True if value was set, false if not (invalid position / protected field). + * @throws IllegalArgumentException if object type of value does not match field object type. + * @throws TorqueException If a problem occures with the set[Field] method. + */ + public boolean setByPosition(int position, Object value) + throws TorqueException, IllegalArgumentException + { + #set ($i = 0) + #foreach ($col in $table.Columns) + #set ( $cup=$col.Name.toUpperCase() ) + if (position == $i) + { + return setByName("${col.JavaName}", value); + } + #set ( $i = $i + 1) + #end + return false; + } #end ## ends the if(addGetByNameMethod) #if (!$table.isAlias() && $addSaveMethod) @@ -1630,7 +1761,7 @@ return copyInto(copyObj, true); } #end - + /** * Fills the copyObj with the contents of this object. #if ($complexObjectModel) Modified: db/torque/templates/trunk/src/templates/om/ObjectWithManager.vm URL: http://svn.apache.org/viewvc/db/torque/templates/trunk/src/templates/om/ObjectWithManager.vm?rev=414572&r1=414571&r2=414572&view=diff ============================================================================== --- db/torque/templates/trunk/src/templates/om/ObjectWithManager.vm (original) +++ db/torque/templates/trunk/src/templates/om/ObjectWithManager.vm Thu Jun 15 05:57:57 2006 @@ -389,6 +389,16 @@ #else $varName = ${interfaceName}Manager.getInstance($arglist); #end + + /* The following can be used instead of the line above to + guarantee the related object contains a reference + to this object, but this level of coupling + may be undesirable in many circumstances. + As it can lead to a db query with many results that may + never be used. + $className obj = ${className}Peer.retrieveByPK($arglist); + obj.add${pCollName}(this); + */ } #end return $varName; @@ -435,10 +445,20 @@ ${interfaceName}Manager.putInstance($varName); } #end + + /* The following can be used instead of the line above to + guarantee the related object contains a reference + to this object, but this level of coupling + may be undesirable in many circumstances. + As it can lead to a db query with many results that may + never be used. + $className obj = ${className}Peer.retrieveByPK($arglist, connection); + obj.add${pCollName}(this); + */ } return $varName; } - + /** * Provides convenient way to set a relationship based on a * ObjectKey, for example @@ -459,6 +479,7 @@ #set ($i = 0) #foreach ($colName in $fk.LocalColumns) #set ($col = $table.getColumn($colName) ) + #set ($fktype = $col.JavaNative) #if ($fktype == "short") ${col.SetterName}(((NumberKey) keys[$i]).shortValue()); @@ -491,7 +512,6 @@ #elseif($fktype == "Date") ${col.SetterName}(((DateKey)keys[$i]).getDate()); #end - #set ( $i = $i + 1 ) #end #else @@ -1029,7 +1049,7 @@ } /** - * Retrieves a field from the object by name passed in as a String. + * Retrieves a field from the object by field (Java) name passed in as a String. * * @param name field name * @return value @@ -1065,6 +1085,91 @@ } /** + * Set a field in the object by field (Java) name. + * + * @param name field name + * @param value field value + * @return True if value was set, false if not (invalid name / protected field). + * @throws IllegalArgumentException if object type of value does not match field object type. + * @throws TorqueException If a problem occures with the set[Field] method. + */ + public boolean setByName(String name, Object value ) + throws TorqueException, IllegalArgumentException + { + #foreach ($col in $table.Columns) + #set ( $cjtype = $col.JavaNative ) + if (name.equals("${col.JavaName}")) + { + #if ( ${col.isPrimitive()} ) + #if ($cjtype == "int") + if (value == null || ! (Integer.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object."); + } + ${col.SetterName}(((Integer) value).intValue()); + #elseif ($cjtype == "long") + if (value == null || ! (Long.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Long object."); + } + ${col.SetterName}(((Long) value).longValue()); + #elseif ($cjtype == "float") + if (value == null || ! (Float.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Float object."); + } + ${col.SetterName}(((Float) value).floatValue()); + #elseif ($cjtype == "double") + if (value == null || ! (Double.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Double object."); + } + ${col.SetterName}(((Double) value).doubleValue()); + #elseif ($cjtype == "boolean") + if (value == null || ! (Boolean.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Boolean object."); + } + ${col.SetterName}(((Boolean)value).booleanValue()); + #elseif ($cjtype == "short") + if (value == null || ! (Short.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Short object."); + } + ${col.SetterName}(((Short)value).shortValue()); + #elseif ($cjtype == "byte") + if (value == null || ! (Byte.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Byte object."); + } + ${col.SetterName}(((Byte) value).byteValue()); + #elseif ($cjtype == "char") + if (value == null || ! (Character.class.isInstance(value))) + { + throw new IllegalArgumentException("setByName: value parameter was null or not a Character object."); + } + ${col.SetterName}(((Character) value).charValue()); + #end + #else + #if ( ${col.JavaNative} ) + // Object fields can be null + if (value != null && ! ${col.JavaNative}.class.isInstance(value)) + { + throw new IllegalArgumentException("Invalid type of object specified for value in setByName"); + } + ${col.SetterName}(($cjtype) value); + #else + // Hmm, using column type that can't be mapped to a Java object class, make this obvious. + throw IllegalStateException("Can't determine Java Object type for column, ${col.Name}!"); + #end + #end + return true; + } + #end + return false; + } + + /** * Retrieves a field from the object by name passed in * as a String. The String must be one of the static * Strings defined in this Class' Peer. @@ -1104,6 +1209,28 @@ } /** + * Set field values by Peer Field Name + * + * @param name field name + * @param value field value + * @return True if value was set, false if not (invalid name / protected field). + * @throws IllegalArgumentException if object type of value does not match field object type. + * @throws TorqueException If a problem occures with the set[Field] method. + */ + public boolean setByPeerName(String name, Object value) + throws TorqueException, IllegalArgumentException + { + #foreach ($col in $table.Columns) + #set ( $cup=$col.Name.toUpperCase() ) + if (${table.JavaName}Peer.${cup}.equals(name)) + { + return setByName("${col.JavaName}", value); + } + #end + return false; + } + + /** * Retrieves a field from the object by Position as specified * in the xml schema. Zero-based. * @@ -1141,6 +1268,30 @@ #end return null; } + + /** + * Set field values by its position (zero based) in the XML schema. + * + * @param position The field position + * @param value field value + * @return True if value was set, false if not (invalid position / protected field). + * @throws IllegalArgumentException if object type of value does not match field object type. + * @throws TorqueException If a problem occures with the set[Field] method. + */ + public boolean setByPosition(int position, Object value) + throws TorqueException, IllegalArgumentException + { + #set ($i = 0) + #foreach ($col in $table.Columns) + #set ( $cup=$col.Name.toUpperCase() ) + if (position == $i) + { + return setByName("${col.JavaName}", value); + } + #set ( $i = $i + 1) + #end + return false; + } #end ## ends the if(addGetByNameMethod) #if (!$table.isAlias() && $addSaveMethod) @@ -1282,7 +1433,6 @@ { ${table.JavaName}Peer.doUpdate(($table.JavaName) this, con); } - #if ($table.PrimaryKey.size() > 0) #set ($tableHasPrimaryKey = true) #else @@ -1634,7 +1784,7 @@ { return copyInto(new ${table.JavaName}(), deepcopy); } - #end + #end #end #if ($complexObjectModel) @@ -1648,7 +1798,7 @@ return copyInto(copyObj, true); } #end - + /** * Fills the copyObj with the contents of this object. #if ($complexObjectModel) Added: db/torque/test/trunk/test-project/src/java/org/apache/torque/om/OMByNameMethodsTest.java URL: http://svn.apache.org/viewvc/db/torque/test/trunk/test-project/src/java/org/apache/torque/om/OMByNameMethodsTest.java?rev=414572&view=auto ============================================================================== --- db/torque/test/trunk/test-project/src/java/org/apache/torque/om/OMByNameMethodsTest.java (added) +++ db/torque/test/trunk/test-project/src/java/org/apache/torque/om/OMByNameMethodsTest.java Thu Jun 15 05:57:57 2006 @@ -0,0 +1,629 @@ +package org.apache.torque.om; + +/* + * Copyright 2001-2006 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. + */ + +import java.math.BigDecimal; +import java.util.Date; + +import org.apache.torque.BaseRuntimeTestCase; +import org.apache.torque.TorqueException; +import org.apache.torque.test.TypesObject; +import org.apache.torque.test.TypesPrimitive; + +/** + * Test the various setBy and getBy methods that can be used to access field + * values. + *

+ * Depends on names and ordering in the following tables to match the static + * fields defined in this class: + *

+ * + * TypesObject table - which contain column definitions for all (AFAIK) Torque + * supported column types that use Java Objects for storage. E.g. Integer and + * not int. + *

+ * + * TypesPrimitive table - which is the same as TypesObjects except that it uses + * primitive types for storage. + *

+ * + * InheritanceTest table - which contains a protected field. + *

+ * + * @author () + * methods for all known object and primitive types. + */ + public void testSetByNameMethod() throws Exception + { + + // Testing SetByName method for Object Types + BaseObject objectTypes = new TypesObject(); + try + { + for (int i = 0; i < OBJECT_COLUMN_NAMES.length; i++) + { + boolean status = objectTypes.setByName(OBJECT_COLUMN_NAMES[i], + OBJECT_TEST_VALUES[i]); + assertTrue("setByName returned false setting column " + + OBJECT_COLUMN_NAMES[i], status); + } + } + catch (TorqueException e) + { + fail("Exception caught trying to call TypesObject.setByName() " + + "method!\nWas OM generated with torque.addGetByNameMethod " + + "property = true?\nError message was: '" + e.getMessage() + + "'"); + } + String eMsg = "Did not get expected value for object column: "; + int iValue = 0; + // "OBit", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOBit())); + // "OTinyint", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOTinyint())); + // "OSmallint", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOSmallint())); + // "OBigint", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOBigint())); + // "OFloat", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOFloat())); + // "OReal", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOReal())); + // "ONumeric", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getONumeric())); + // "ODecimal", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getODecimal())); + // "OChar", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOChar())); + // "OVarchar", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOVarchar())); + // "OLongvarchar", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOLongvarchar())); + // "ODate", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getODate())); + // "OTime", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOTime())); + // "OInteger", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOInteger())); + // "OTimestamp", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOTimestamp())); + // "OBinary", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOBinary())); + // "OVarbinary", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOVarbinary())); + // "OLongvarbinary", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOLongvarbinary())); + // "OBlob", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOBlob())); + // "OClob", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOClob())); + // "OBooleanint", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOBooleanint())); + // "OBooleanchar", + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getOBooleanchar())); + // "ODouble" + assertTrue(eMsg + OBJECT_COLUMN_NAMES[iValue], + OBJECT_TEST_VALUES[iValue++].equals(((TypesObject) objectTypes) + .getODouble())); + // Test Primitive Types + BaseObject primitiveTypes = new TypesPrimitive(); + try + { + for (int i = 0; i < PRIMITIVE_COLUMN_NAMES.length; i++) + { + boolean status = primitiveTypes.setByName( + PRIMITIVE_COLUMN_NAMES[i], PRIMITIVE_TEST_VALUES[i]); + assertTrue("setByName returned false setting column " + + PRIMITIVE_COLUMN_NAMES[i], status); + } + } + catch (TorqueException e) + { + fail("Exception caught trying to call TypesObject.setByName() " + + "method!\nWas OM generated with torque.addGetByNameMethod " + + "property = true?\nError message was: '" + e.getMessage() + + "'"); + } + eMsg = "Did not get expected value for primitive column: "; + // "PBit", true, //boolean + assertTrue(eMsg + "PBit", + ((TypesPrimitive) primitiveTypes).getPBit() == true); + // "PTinyint", (byte) 1 //byte + assertTrue(eMsg + "PTinyint", ((TypesPrimitive) primitiveTypes) + .getPTinyint() == (byte) 1); + // "PSmallint", (short) 1 //short + assertTrue(eMsg + "PSmallint", ((TypesPrimitive) primitiveTypes) + .getPSmallint() == (short) 1); + // "PBigint", (long) 1.0 //long + assertTrue(eMsg + "PBigint", ((TypesPrimitive) primitiveTypes) + .getPBigint() == (long) 1.0); + // "PFloat", 1.0 //double + assertTrue(eMsg + "PFloat", ((TypesPrimitive) primitiveTypes) + .getPFloat() == 1.0); + // "PReal", 1.0 //float + assertTrue(eMsg + "PReal", + ((TypesPrimitive) primitiveTypes).getPReal() == 1.0); + // "PInteger", 1 //int + assertTrue(eMsg + "PInteger", ((TypesPrimitive) primitiveTypes) + .getPInteger() == 1); + // "PBooleanint", true, //boolean + assertTrue(eMsg + "PBooleanint", ((TypesPrimitive) primitiveTypes) + .getPBooleanint() == true); + // "PBooleanchar", true, //boolean + assertTrue(eMsg + "PBooleanchar", ((TypesPrimitive) primitiveTypes) + .getPBooleanchar() == true); + // "PDouble" 1.0 //double + assertTrue(eMsg + "PDouble", ((TypesPrimitive) primitiveTypes) + .getPDouble() == 1.0); + } + + /* + * Validate that the getBy* methods work using a BaseObject class type. + * Checks that getValue returns the value set with a setBy call for all + * known object and primitive types. + * + * @throws Exception + */ + public void testGetByNameMethod() throws Exception + { + // Testing GetByName method for Object Types + BaseObject objectTypes = new TypesObject(); + try + { + for (int i = 0; i < OBJECT_COLUMN_NAMES.length; i++) + { + objectTypes.setByName(OBJECT_COLUMN_NAMES[i], + OBJECT_TEST_VALUES[i]); + } + } + catch (TorqueException e) + { + fail("Exception caught trying to call TypesObject.setByName() " + + "method!\nWas OM generated with torque.addGetByNameMethod " + + "property = true?\nError message was: '" + e.getMessage() + + "'"); + } + String eMsg = "Did not get expected value for object column: "; + for (int i = 0; i < OBJECT_COLUMN_NAMES.length; i++) + { + assertTrue(eMsg + OBJECT_COLUMN_NAMES[i], OBJECT_TEST_VALUES[i] + .equals(objectTypes.getByName(OBJECT_COLUMN_NAMES[i]))); + } + + // Test Primative Types + BaseObject primitiveTypes = new TypesPrimitive(); + try + { + for (int i = 0; i < PRIMITIVE_COLUMN_NAMES.length; i++) + { + primitiveTypes.setByName(PRIMITIVE_COLUMN_NAMES[i], + PRIMITIVE_TEST_VALUES[i]); + } + } + catch (TorqueException e) + { + fail("Exception caught trying to call TypesObject.setByName() " + + "method!\nWas OM generated with torque.addGetByNameMethod " + + "property = true?\nError message was: '" + e.getMessage() + + "'"); + } + for (int i = 0; i < PRIMITIVE_COLUMN_NAMES.length; i++) + { + assertTrue(eMsg + PRIMITIVE_COLUMN_NAMES[i], + PRIMITIVE_TEST_VALUES[i].equals( + primitiveTypes.getByName(PRIMITIVE_COLUMN_NAMES[i]))); + } + } + + /* + * Validate that the setByPeerName methods work using a BaseObject class type. + * Checks that getValue returns the value set with a setByPeerName call for all + * known object and primitive types. + */ + public void testSetByPeerNameMethod() throws Exception + { + // Testing GetByName method for Object Types + BaseObject objectTypes = new TypesObject(); + try + { + for (int i = 0; i < OBJECT_PEER_NAMES.length; i++) + { + boolean status = objectTypes.setByPeerName( + OBJECT_PEER_NAMES[i], OBJECT_TEST_VALUES[i]); + assertTrue("setByPeerName returned false setting column " + + OBJECT_COLUMN_NAMES[i], status); + } + } + catch (TorqueException e) + { + fail("Exception caught trying to call TypesObject.setByName() " + + "method!\nWas OM generated with torque.addGetByNameMethod " + + "property = true?\nError message was: '" + e.getMessage() + + "'"); + } + String eMsg = "Did not get expected value for object column: "; + for (int i = 0; i < OBJECT_COLUMN_NAMES.length; i++) + { + assertTrue(eMsg + OBJECT_COLUMN_NAMES[i], OBJECT_TEST_VALUES[i] + .equals(objectTypes.getByName(OBJECT_COLUMN_NAMES[i]))); + } + + // Test Primitive Types + BaseObject primitiveTypes = new TypesPrimitive(); + try + { + for (int i = 0; i < PRIMITIVE_PEER_NAMES.length; i++) + { + boolean status = primitiveTypes.setByPeerName( + PRIMITIVE_PEER_NAMES[i], OBJECT_TEST_VALUES[i]); + assertTrue("setByPeerName returned false setting column " + + PRIMITIVE_PEER_NAMES[i], status); + } + } + catch (TorqueException e) + { + fail("Exception caught trying to call TypesObject.setByName() " + + "method!\nWas OM generated with torque.addGetByNameMethod " + + "property = true?\nError message was: '" + e.getMessage() + + "'"); + } + for (int i = 0; i < PRIMITIVE_COLUMN_NAMES.length; i++) + { + assertTrue(eMsg + PRIMITIVE_COLUMN_NAMES[i], + PRIMITIVE_TEST_VALUES[i].equals( + primitiveTypes.getByName(PRIMITIVE_COLUMN_NAMES[i]))); + } + } + + /* + * Validate that the setByPostion methods work using a BaseObject class type. + * Checks that getByPosition returns the value set with a setByPosition call + * for all known object and primitive types. + */ + public void testSetByPositionMethod() throws Exception + { + // Testing GetByName method for Object Types + BaseObject objectTypes = new TypesObject(); + try + { + for (int i = 0; i < OBJECT_PEER_NAMES.length; i++) + { + boolean status = objectTypes.setByPosition(i, + OBJECT_TEST_VALUES[i]); + assertTrue("objectTypes.setByPosition(int, Object ) returned " + + "false setting position " + i, status); + } + } + catch (TorqueException e) + { + fail("Exception caught trying to call TypesObject.setByName() " + + "method!\nWas OM generated with torque.addGetByNameMethod " + + "property = true?\nError message was: '" + e.getMessage() + + "'"); + } + String eMsg = "Did not get expected value for object column: "; + for (int i = 0; i < OBJECT_COLUMN_NAMES.length; i++) + { + assertTrue(eMsg + OBJECT_COLUMN_NAMES[i], OBJECT_TEST_VALUES[i] + .equals(objectTypes.getByName(OBJECT_COLUMN_NAMES[i]))); + } + + // Test Primitive Types + BaseObject primitiveTypes = new TypesPrimitive(); + try + { + for (int i = 0; i < PRIMITIVE_PEER_NAMES.length; i++) + { + boolean status = primitiveTypes.setByPosition(i, + OBJECT_TEST_VALUES[i]); + assertTrue("primitiveTypes.setByPosition(int, Object) returned " + + "false setting position " + i, status); + } + } + catch (TorqueException e) + { + fail("Exception caught trying to call TypesPrimitive.setByName() " + + "method!\nWas OM generated with torque.addGetByNameMethod " + + "property = true?\nError message was: '" + e.getMessage() + + "'"); + } + for (int i = 0; i < PRIMITIVE_COLUMN_NAMES.length; i++) + { + assertTrue(eMsg + PRIMITIVE_COLUMN_NAMES[i], + PRIMITIVE_TEST_VALUES[i].equals( + primitiveTypes.getByName(PRIMITIVE_COLUMN_NAMES[i]))); + } + + } + + /* + * Validate that various an IllegalArgumentException if thrown if the + * Object type of the value is not correct. + */ + public void testInvalidObjectErrors() throws Exception + { + BaseObject objectTypes = new TypesObject(); + BaseObject primitiveTypes = new TypesPrimitive(); + // Test catching invalid object types + boolean error = false; + try + { + objectTypes.setByName("OBit", new Integer(1)); + } + catch (IllegalArgumentException e) + { + error = true; + } + assertTrue( + "setByName for OBit column did not catch illegal object type!", + error); + + error = false; + try + { + primitiveTypes.setByName("PBit", new Integer(99)); + } + catch (IllegalArgumentException e) + { + error = true; + } + assertTrue( + "setByName for PBit column did not catch illegal object type!", + error); + } + + /* + * Validate that a false rc is returned if non-column names passed to methods. + */ + public void testInvalidNameErrors() throws Exception + { + BaseObject objectTypes = new TypesObject(); + // Test that false status is returned for invalid column names. + boolean status = objectTypes.setByName("xxxOBit", new Integer(1)); + assertFalse("Did not get a false status from setByName with " + + "invalid column name!", status); + + status = objectTypes.setByPeerName("xxxOBit", new Integer(1)); + assertFalse("Did not get a false status from setByPeerName with " + + "invalid column name!", status); + + status = objectTypes.setByPosition(1000, new Integer(1)); + assertFalse("Did not get a false status from setByPosition with " + + "invalid position!", status); + } + + /** + * Verify that null handling (can't use them for primitives) works. + */ + public void testNullHandling() throws Exception + { + BaseObject objectTypes = new TypesObject(); + BaseObject primitiveTypes = new TypesPrimitive(); + // Object type fields should allow nulls + boolean error = false; + try + { + objectTypes.setByName("OBit", null); + } + catch (IllegalArgumentException e) + { + error = true; + } + assertFalse("objectTypes.setByName(\"OBit\",null) did not allow " + + "a null value!", error); + + // Primitive types should not allow null values + error = false; + try + { + primitiveTypes.setByName("PBit", null); + } + catch (IllegalArgumentException e) + { + error = true; + } + assertTrue("primitiveTypes.setByName(\"PBit\",null) allowed " + + "a null value!", error); + } +} Modified: db/torque/test/trunk/test-project/src/schema/test-schema.xml URL: http://svn.apache.org/viewvc/db/torque/test/trunk/test-project/src/schema/test-schema.xml?rev=414572&r1=414571&r2=414572&view=diff ============================================================================== --- db/torque/test/trunk/test-project/src/schema/test-schema.xml (original) +++ db/torque/test/trunk/test-project/src/schema/test-schema.xml Thu Jun 15 05:57:57 2006 @@ -413,4 +413,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
+ --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org For additional commands, e-mail: torque-dev-help@db.apache.org