?? datatest.java
字號:
package org.apache.torque;/* * Copyright 2001-2004 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.sql.Connection;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Arrays;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.torque.adapter.DBHypersonicSQL;import org.apache.torque.adapter.DBOracle;import org.apache.torque.om.StringKey;import org.apache.torque.test.A;import org.apache.torque.test.APeer;import org.apache.torque.test.Author;import org.apache.torque.test.AuthorPeer;import org.apache.torque.test.BitTest;import org.apache.torque.test.BitTestPeer;import org.apache.torque.test.BlobTest;import org.apache.torque.test.BlobTestPeer;import org.apache.torque.test.Book;import org.apache.torque.test.BookPeer;import org.apache.torque.test.BooleanCheck;import org.apache.torque.test.BooleanCheckPeer;import org.apache.torque.test.ClobTest;import org.apache.torque.test.ClobTestPeer;import org.apache.torque.test.DateTest;import org.apache.torque.test.DateTestPeer;import org.apache.torque.test.IntegerPk;import org.apache.torque.test.LargePk;import org.apache.torque.test.LargePkPeer;import org.apache.torque.test.MultiPk;import org.apache.torque.test.MultiPkForeignKey;import org.apache.torque.test.MultiPkPeer;import org.apache.torque.test.NullValueTable;import org.apache.torque.util.BasePeer;import org.apache.torque.util.CountHelper;import org.apache.torque.util.Criteria;import com.workingdogs.village.Record;/** * Runtime tests. * * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a> * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a> * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a> * @version $Id: DataTest.java,v 1.28 2005/07/13 21:12:07 tfischer Exp $ */public class DataTest extends BaseRuntimeTestCase{ private static Log log = LogFactory.getLog(DataTest.class);; /** * Creates a new instance. */ public DataTest(String name) { super(name); } public void setUp() { super.setUp(); } /** * test whether we can connect to the database at all * @throws Exception if no connection can be established */ public void testConnect() throws Exception { Connection connection = null; try { connection = Torque.getConnection(); connection.close(); connection = null; } finally { if (connection != null) { connection.close(); } } } /** * does some inserts. * @throws Exception if the test fails */ public void testInsertData() throws Exception { // 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(); } } // clean booleancheck table (because insert uses fixed keys) Criteria criteria = new Criteria(); criteria.add(BooleanCheckPeer.TEST_KEY, (Object) null, Criteria.NOT_EQUAL); BooleanCheckPeer.doDelete(criteria); BooleanCheck bc = new BooleanCheck(); bc.setTestKey("t1"); bc.setBintValue(true); bc.setBcharValue(true); bc.save(); bc = new BooleanCheck(); bc.setTestKey("f1"); bc.setBintValue(false); bc.setBcharValue(false); bc.save(); } /** * multiple pk test (TRQ12) * @throws Exception if the test fails */ public void testMultiplePk() throws Exception { // clean table Criteria criteria = new Criteria(); criteria.add(MultiPkPeer.PK1, (Object) null, Criteria.NOT_EQUAL); MultiPkPeer.doDelete(criteria); // do test MultiPk mpk = new MultiPk(); mpk.setPrimaryKey("Svarchar:N5:Schar:"); mpk.save(); } private static final String[] validTitles = { "Book 7 - Author 8", "Book 6 - Author 8", "Book 7 - Author 7", "Book 6 - Author 7", "Book 7 - Author 6", "Book 6 - Author 6", "Book 7 - Author 5", "Book 6 - Author 5", "Book 7 - Author 4", "Book 6 - Author 4"}; /** * test limit/offset which was broken for oracle (TRQ47) * @throws Exception if the test fails */ public void testLimitOffset() throws Exception { Map titleMap = new HashMap(); for (int j = 0; j < validTitles.length; j++) { titleMap.put(validTitles[j], null); } Criteria crit = new Criteria(); Criteria.Criterion c = crit.getNewCriterion(BookPeer.TITLE, (Object) "Book 6 - Author 1", Criteria.GREATER_EQUAL); c.and(crit.getNewCriterion(BookPeer.TITLE, (Object) "Book 8 - Author 3", Criteria.LESS_EQUAL)); crit.add(c); crit.addDescendingOrderByColumn(BookPeer.BOOK_ID); crit.setLimit(10); crit.setOffset(5); List books = BookPeer.doSelect(crit); assertTrue("List should have 10 books, not " + books.size(), books.size() == 10); for (Iterator i = books.iterator(); i.hasNext();) { String title = ((Book) i.next()).getTitle(); assertTrue("Incorrect title: " + title, titleMap.containsKey(title)); } } /** * Checks whether the setSingleRecord() method in criteria works */ public void testSingleRecord() throws Exception { Criteria criteria = new Criteria(); criteria.setSingleRecord(true); criteria.setLimit(1); criteria.setOffset(5); List books = BookPeer.doSelect(criteria); assertTrue("List should have 1 books, not " + books.size(), books.size() == 1); criteria.clear(); criteria.setSingleRecord(true); criteria.setLimit(2); try { books = BookPeer.doSelect(criteria); fail("doSelect should have failed " + "because two records were selected " + " and one was expected"); } catch (TorqueException e) { } } /** * tests whether null values can be processed successfully by datadump * For this, a row containing null values is inserted here, * the actual test is done later * @throws Exception if inserting the test data fails */ public void testDataDump() throws Exception { NullValueTable nvt = new NullValueTable(); nvt.setNumber1(1); nvt.setNumber3(3); nvt.setText1("text"); nvt.setNumberObj1(new Integer(1)); nvt.save(); } /** * test boolean values * @throws Exception if the test fails */ public void testBooleanValues() throws Exception { BooleanCheck bc = BooleanCheckPeer.retrieveByPK(new StringKey("t1")); assertTrue("BOOLEANINT should be true but is: " + bc.getBintValue(), bc.getBintValue()); assertTrue("BOOLEANCHAR should be true but is: " + bc.getBcharValue(), bc.getBcharValue()); bc = BooleanCheckPeer.retrieveByPK(new StringKey("f1")); assertFalse("BOOLEANINT should be false but is: " + bc.getBintValue(), bc.getBintValue()); assertFalse("BOOLEANCHAR should be false but is: " + bc.getBcharValue(), bc.getBcharValue()); } /** * Tests whether column type BIT can be written and read correctly * and works in criteria as expected * @throws Exception if the test fails */ public void testBitType() throws Exception { if (Torque.getDB(Torque.getDefaultDB()) instanceof DBOracle) { log.error("testBitType(): BIT is known not to work with Oracle"); // failing is "expected", so exit without error return; } // clean table Criteria criteria = new Criteria(); criteria.add(BitTestPeer.ID, (Object) null, Criteria.NOT_EQUAL); BitTestPeer.doDelete(criteria); // insert Data BitTest bitTest = new BitTest(); bitTest.setId("t1"); bitTest.setBitValue(true); bitTest.save(); bitTest = new BitTest(); bitTest.setId("f1"); bitTest.setBitValue(false); bitTest.save(); // read data bitTest = BitTestPeer.retrieveByPK(new StringKey("t1")); assertTrue("BIT should be true but is: " + bitTest.getBitValue(), bitTest.getBitValue()); bitTest = BitTestPeer.retrieveByPK(new StringKey("f1")); assertFalse("BIT should be false but is: " + bitTest.getBitValue(), bitTest.getBitValue()); // query data criteria.clear(); criteria.add(BitTestPeer.BIT_VALUE, new Boolean(true)); List bitTestList = BitTestPeer.doSelect(criteria); assertTrue("Should have read 1 dataset " + "but read " + bitTestList.size(), bitTestList.size() == 1); bitTest = (BitTest) bitTestList.get(0); // use trim() for testkey because some databases will return the // testkey filled up with blanks, as it is defined as char(10) assertTrue("Primary key of data set should be t1 but is " + bitTest.getId().trim(), "t1".equals(bitTest.getId().trim())); criteria.clear(); criteria.add(BitTestPeer.BIT_VALUE, new Boolean(false)); bitTestList = BitTestPeer.doSelect(criteria); assertTrue("Should have read 1 dataset " + "but read " + bitTestList.size(), bitTestList.size() == 1); bitTest = (BitTest) bitTestList.get(0); assertTrue("Primary key of data set should be f1 but is " + bitTest.getId().trim(), "f1".equals(bitTest.getId().trim())); } /** * check whether we can select from boolean columns * @throws Exception if the test fails */ public void testBooleanSelects() throws Exception { Criteria criteria = new Criteria(); criteria.add(BooleanCheckPeer.BCHAR_VALUE, new Boolean(true)); criteria.add(BooleanCheckPeer.BINT_VALUE, new Boolean(true)); List booleanCheckList = BooleanCheckPeer.doSelect(criteria); assertTrue("Should have read 1 dataset with both values true " + "but read " + booleanCheckList.size(), booleanCheckList.size() == 1); BooleanCheck booleanCheck = (BooleanCheck) booleanCheckList.get(0); // use trim() for testkey because some databases will return the // testkey filled up with blanks, as it is defined as char(10) assertTrue("Primary key of data set should be t1 but is " + booleanCheck.getTestKey().trim(), "t1".equals(booleanCheck.getTestKey().trim())); criteria.clear(); criteria.add(BooleanCheckPeer.BCHAR_VALUE, new Boolean(false)); criteria.add(BooleanCheckPeer.BINT_VALUE, new Boolean(false)); booleanCheckList = BooleanCheckPeer.doSelect(criteria); assertTrue("Should have read 1 dataset with both values false " + "but read " + booleanCheckList.size(), booleanCheckList.size() == 1); booleanCheck = (BooleanCheck) booleanCheckList.get(0); assertTrue("Primary key of data set should be f1 but is " + booleanCheck.getTestKey().trim(), "f1".equals(booleanCheck.getTestKey().trim())); } /** * test whether delete works as expected * @throws Exception if the test fails */ public void testDelete() throws Exception { cleanBookstore(); Author author = new Author(); author.setName("Name"); author.save(); Book book = new Book(); book.setTitle("title"); book.setAuthor(author); book.setIsbn("ISBN"); book.save(); // delete without matching data Criteria criteria = new Criteria(); criteria.add( AuthorPeer.AUTHOR_ID, author.getAuthorId(), Criteria.NOT_EQUAL); AuthorPeer.doDelete(criteria); List authorResult = AuthorPeer.doSelect(new Criteria()); assertTrue("deleted too many records", authorResult.size() == 1); BookPeer.doDelete(book); List bookResult = BookPeer.doSelect(new Criteria()); authorResult = AuthorPeer.doSelect(new Criteria()); // check that the book has disappeared assertTrue("delete by object failed", bookResult.size() == 0); // check that the underlying author has not been deleted assertTrue("delete by object deleted in cascade", authorResult.size() == 1); // delete with matching data criteria.clear(); criteria.add(AuthorPeer.AUTHOR_ID, author.getAuthorId());
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -