Return-Path: X-Original-To: apmail-db-torque-dev-archive@www.apache.org Delivered-To: apmail-db-torque-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0282E99F5 for ; Fri, 2 Mar 2012 04:17:34 +0000 (UTC) Received: (qmail 36185 invoked by uid 500); 2 Mar 2012 04:17:33 -0000 Delivered-To: apmail-db-torque-dev-archive@db.apache.org Received: (qmail 35867 invoked by uid 500); 2 Mar 2012 04:17:22 -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 35753 invoked by uid 500); 2 Mar 2012 04:17:17 -0000 Received: (qmail 35750 invoked by uid 99); 2 Mar 2012 04:17:17 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 02 Mar 2012 04:17:17 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 02 Mar 2012 04:17:13 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 1205B2388994; Fri, 2 Mar 2012 04:16:42 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1296046 - in /db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque: BaseDatabaseTestCase.java DataTest.java generated/dataobject/SaveTest.java Date: Fri, 02 Mar 2012 04:16:41 -0000 To: torque-commits@db.apache.org From: tfischer@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120302041642.1205B2388994@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tfischer Date: Fri Mar 2 04:16:41 2012 New Revision: 1296046 URL: http://svn.apache.org/viewvc?rev=1296046&view=rev Log: factored out tests for save Added: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/SaveTest.java Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/BaseDatabaseTestCase.java db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/BaseDatabaseTestCase.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/BaseDatabaseTestCase.java?rev=1296046&r1=1296045&r2=1296046&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/BaseDatabaseTestCase.java (original) +++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/BaseDatabaseTestCase.java Fri Mar 2 04:16:41 2012 @@ -34,6 +34,7 @@ import org.apache.torque.test.AuthorPeer import org.apache.torque.test.Book; import org.apache.torque.test.BookPeer; import org.apache.torque.util.BasePeer; +import org.apache.torque.util.CountHelper; /** * Base functionality for Test cases which use standard database setup. @@ -172,4 +173,64 @@ public abstract class BaseDatabaseTestCa } return result; } + + /** + * Checks that the bookstore tables contain exactly the records + * in the passed list. + * The books in the authors are also checked. + * + * @param toVerify the list of authors to check. + * + * @throws TorqueException if reading data fails. + */ + protected void verifyBookstore(List toVerify) throws TorqueException + { + int numBooks = 0; + + for (Author author : toVerify) + { + Criteria criteria = new Criteria() + .where(AuthorPeer.NAME, author.getName()) + .and(AuthorPeer.AUTHOR_ID, author.getAuthorId()); + criteria.setSingleRecord(true); + List selectedAuthorList = AuthorPeer.doSelect(criteria); + assertEquals("Could not find author with id " + + author.getAuthorId() + + " and name " + + author.getName(), + 1, + selectedAuthorList.size()); + + numBooks += author.getBooks().size(); + + for (Book book : author.getBooks()) + { + criteria = new Criteria() + .where(BookPeer.TITLE, book.getTitle()) + .and(BookPeer.BOOK_ID, book.getBookId()) + .and(BookPeer.AUTHOR_ID, book.getAuthorId()) + .and(BookPeer.ISBN, book.getIsbn()); + criteria.setSingleRecord(true); + List selectedBookList = BookPeer.doSelect(criteria); + assertEquals("Could not find book with id " + + book.getBookId() + + " title " + + book.getTitle() + + " ISBN " + + book.getIsbn() + + " authorId " + + book.getAuthorId(), + 1, + selectedBookList.size()); + } + } + + assertEquals( + toVerify.size(), + new CountHelper().count(AuthorPeer.getTableMap())); + assertEquals( + numBooks, + new CountHelper().count(BookPeer.getTableMap())); + } + } Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java?rev=1296046&r1=1296045&r2=1296046&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java (original) +++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java Fri Mar 2 04:16:41 2012 @@ -69,8 +69,6 @@ import org.apache.torque.test.Inheritanc import org.apache.torque.test.InheritanceClassnameTestPeer; import org.apache.torque.test.InheritanceTest; import org.apache.torque.test.InheritanceTestPeer; -import org.apache.torque.test.IntegerObjectPk; -import org.apache.torque.test.IntegerObjectPkPeer; import org.apache.torque.test.IntegerPk; import org.apache.torque.test.IntegerPkPeer; import org.apache.torque.test.LargePk; @@ -127,59 +125,6 @@ public class DataTest extends BaseDataba } /** - * does some inserts. - * @throws Exception if the test fails - */ - public void testInsertData() throws Exception - { - // remove old authors and books - cleanBookstore(); - - // insert books and authors - for (int i = 1; i <= 10; i++) - { - Author author = new Author(); - author.setName("Author " + i); - author.save(); - assertTrue("authorId should not be 0 after insert", - author.getAuthorId() != 0); - - for (int j = 1; j <= 10; j++) - { - Book book = new Book(); - book.setAuthor(author); - book.setTitle("Book " + j + " - Author " + i); - book.setIsbn("unknown"); - book.save(); - } - } - - // check a primitive id can be set manually - Author author = new Author(); - author.setAuthorId(2000); - author.setName("Author 2000"); - author.save(); - assertEquals(2000, author.getAuthorId()); - Criteria criteria = new Criteria(); - criteria.add(AuthorPeer.AUTHOR_ID, 2000); - List authorList = AuthorPeer.doSelect(criteria); - assertEquals(1, authorList.size()); - - // check an object id can be set manually - criteria = new Criteria(); - IntegerObjectPkPeer.doDelete(criteria); - IntegerObjectPk integerObjectPk = new IntegerObjectPk(); - integerObjectPk.setId(3001); - integerObjectPk.save(); - assertEquals(new Integer(3001), integerObjectPk.getId()); - criteria = new Criteria(); - criteria.add(IntegerObjectPkPeer.ID, 3001); - List integerObjectPkList - = IntegerObjectPkPeer.doSelect(criteria); - assertEquals(1, integerObjectPkList.size()); - } - - /** * multiple pk test (TRQ12) * @throws Exception if the test fails */ @@ -211,6 +156,8 @@ public class DataTest extends BaseDataba */ public void testLimitOffset() throws Exception { + cleanBookstore(); + insertBookstoreData(); Set titleSet = new HashSet(); for (int j = 0; j < validTitles.length; j++) { @@ -404,32 +351,6 @@ public class DataTest extends BaseDataba author.setName("Name"); author.save(); - Criteria criteria = new Criteria(); - criteria.addAscendingOrderByColumn(AuthorPeer.NAME); - - List authors = AuthorPeer.doSelect(criteria); - assertEquals("List should contain 2 authors", 2, authors.size()); - assertEquals("First Author's name should be \"Name\"", - "Name", - authors.get(0).getName()); - assertEquals("Second Author's name should be \"OtherName\"", - "OtherName", - authors.get(1).getName()); - - author.setName("NewName"); - author.save(); - - criteria = new Criteria(); - criteria.addAscendingOrderByColumn(AuthorPeer.NAME); - - authors = AuthorPeer.doSelect(criteria); - assertEquals("List should contain 2 authors", 2, authors.size()); - assertEquals("First Author's name should be \"NewName\"", - "NewName", - authors.get(0).getName()); - assertEquals("Second Author's name should be \"OtherName\"", - "OtherName", - authors.get(1).getName()); // Test doUpdate methods in Peer explicitly Connection connection = Transaction.begin(AuthorPeer.DATABASE_NAME); @@ -437,10 +358,10 @@ public class DataTest extends BaseDataba AuthorPeer.doUpdate(author); Transaction.commit(connection); - criteria = new Criteria(); + Criteria criteria = new Criteria(); criteria.addAscendingOrderByColumn(AuthorPeer.NAME); - authors = AuthorPeer.doSelect(criteria); + List authors = AuthorPeer.doSelect(criteria); assertEquals("List should contain 2 authors", 2, authors.size()); assertEquals("First Author's name should be \"NewName2\"", "NewName2", @@ -464,28 +385,12 @@ public class DataTest extends BaseDataba "OtherName", authors.get(1).getName()); - // Test updates for objects without primary keys. As we do not store - // information which values were modified, we throw an Exception at - // the attempt. - Nopk nopk = new Nopk(); nopk.setName("name"); nopk.save(); - // check that save does not throw an error if nothing is modified - nopk.save(); - - nopk.setName("otherName"); - try - { - nopk.save(); - fail("A Torque exception should be thrown"); - } - catch (TorqueException e) - { - } - - // check the doPupdate Peer methods themselves + // check the doPupdate Peer methods throw exceptions on a modified + // object without primary keys try { NopkPeer.doUpdate(new Nopk()); Added: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/SaveTest.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/SaveTest.java?rev=1296046&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/SaveTest.java (added) +++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/SaveTest.java Fri Mar 2 04:16:41 2012 @@ -0,0 +1,271 @@ +package org.apache.torque.generated.dataobject; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.util.List; + +import org.apache.torque.BaseDatabaseTestCase; +import org.apache.torque.TorqueException; +import org.apache.torque.criteria.Criteria; +import org.apache.torque.test.Author; +import org.apache.torque.test.AuthorPeer; +import org.apache.torque.test.Book; +import org.apache.torque.test.IntegerObjectPk; +import org.apache.torque.test.IntegerObjectPkPeer; +import org.apache.torque.test.Nopk; +import org.apache.torque.test.NopkPeer; +import org.apache.torque.util.CountHelper; + +/** + * Tests whether the save methods work in tge db object classes. + * + * @version $Id: DataTest.java 1103512 2011-05-15 19:37:41Z tfischer $ + */ +public class SaveTest extends BaseDatabaseTestCase +{ + /** + * Tests the save method for a simple object. + * + * @throws Exception if a database error occurs. + */ + public void testInsert() throws Exception + { + // prepare + cleanBookstore(); + List bookstoreContent = insertBookstoreData(); + Author author = new Author(); + author.setName("Author"); + + // execute + author.save(); + + // verify + assertNotNull(author.getAuthorId()); + assertEquals("Author", author.getName()); + + bookstoreContent.add(author); + verifyBookstore(bookstoreContent); + } + + /** + * Check that an id can be set manually. + */ + public void testInsertWithManualId() throws Exception + { + // prepare + Criteria criteria = new Criteria(); + IntegerObjectPkPeer.doDelete(criteria); + + IntegerObjectPk integerObjectPk = new IntegerObjectPk(); + integerObjectPk.setId(3001); + + // execute + integerObjectPk.save(); + + // verify + assertEquals(new Integer(3001), integerObjectPk.getId()); + + criteria = new Criteria().where(IntegerObjectPkPeer.ID, 3001); + List integerObjectPkList + = IntegerObjectPkPeer.doSelect(criteria); + assertEquals(1, integerObjectPkList.size()); + + assertEquals( + 1, + new CountHelper().count(IntegerObjectPkPeer.getTableMap())); + } + + /** + * Tests that an insert works for objects without primary key. + */ + public void testInsertWithoutPk() throws TorqueException + { + // prepare + Criteria criteria = new Criteria(); + NopkPeer.doDelete(criteria); + + Nopk nopk = new Nopk(); + nopk.setName("name"); + nopk.save(); + + // execute + nopk.save(); + + // verify + assertEquals("name", nopk.getName()); + + criteria = new Criteria().where(NopkPeer.NAME, "name"); + List nopkList = NopkPeer.doSelect(criteria); + assertEquals(1, nopkList.size()); + + assertEquals(1, new CountHelper().count(NopkPeer.getTableMap())); + } + + /** + * Tests that save does not throw an exception if save + * is called on an object without pk which is already saved and + * nothing is modified. + */ + public void testSaveWithoutPkNoModification() throws TorqueException + { + // prepare + Criteria criteria = new Criteria(); + NopkPeer.doDelete(criteria); + + Nopk nopk = new Nopk(); + nopk.setName("name"); + nopk.save(); + + // execute + nopk.save(); + + // verify + assertEquals("name", nopk.getName()); + + criteria = new Criteria().where(NopkPeer.NAME, "name"); + List nopkList = NopkPeer.doSelect(criteria); + assertEquals(1, nopkList.size()); + + assertEquals(1, new CountHelper().count(NopkPeer.getTableMap())); + } + + /** + * Tests that save fails if it is called on a modified object without pk + * which is already saved. + */ + public void testSaveWithoutPkModification() throws TorqueException + { + // prepare + Criteria criteria = new Criteria(); + NopkPeer.doDelete(criteria); + + Nopk nopk = new Nopk(); + nopk.setName("name"); + nopk.save(); + + // execute + try + { + nopk.setName("otherName"); + nopk.save(); + + //verify + fail("Exception expected"); + } + catch (TorqueException e) + { + assertEquals( + "doUpdate does not work for objects without primary key", + e.getMessage()); + } + } + + /** + * Tests the save method for a simple object. + * + * @throws Exception if a database error occurs. + */ + public void testUpdate() throws Exception + { + // prepare + cleanBookstore(); + List bookstoreContent = insertBookstoreData(); + Author author = new Author(); + author.setName("Author"); + author.save(); + author.setName("nameModified"); // modify after saving + + // execute + author.save(); + + // verify + assertNotNull(author.getAuthorId()); + assertEquals("nameModified", author.getName()); + bookstoreContent.add(author); + verifyBookstore(bookstoreContent); + } + + /** + * Tests that the save method propagates "down" for a foreign key, + * i.e. if save is called on an object which contains another + * referencing object, the referencing object must also be saved. + * + * @throws Exception if a database error occurs. + */ + public void testInsertPropagationDown() throws Exception + { + // prepare + cleanBookstore(); + List bookstoreContent = insertBookstoreData(); + Author author = new Author(); + author.setName("Author"); + Book book = new Book(); + author.addBook(book); + book.setTitle("Book title"); + book.setIsbn("ISBN"); + + // execute + author.save(); + + // verify + assertNotNull(author.getAuthorId()); + assertEquals("Author", author.getName()); + assertNotNull(book.getBookId()); + assertEquals("Book title", book.getTitle()); + assertEquals("ISBN", book.getIsbn()); + + bookstoreContent.add(author); + verifyBookstore(bookstoreContent); + } + + /** + * Tests that the save method does not propagate "up" for a foreign key, + * i.e. if save is called on an object referencing another object, + * the referenced object must not be saved. + * + * @throws Exception if a database error occurs. + */ + public void testNoPropagationUp() throws Exception + { + // prepare + cleanBookstore(); + Author author = new Author(); + author.setName("Author"); + author.save(); + author.setName("nameModified"); // modify after saving + + Book book = new Book(); + author.addBook(book); + book.setTitle("Book title"); + book.setIsbn("ISBN"); + + // execute + book.save(); + + // verify + // propagation would have been possible, reference is there + assertNotNull(book.getAuthor()); + + // Author in db should still have old name + Criteria criteria = new Criteria().where(AuthorPeer.NAME, "Author"); + List authorList = AuthorPeer.doSelect(criteria); + assertEquals(1, authorList.size()); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org For additional commands, e-mail: torque-dev-help@db.apache.org