📄 datatest.java
字号:
* are handled correctly * @throws Exception if the test fails */ public void testSameColumnName() throws Exception { cleanBookstore(); Author author = new Author(); author.setName("Name"); author.save(); author = new Author(); author.setName("NotCorrespondingName"); author.save(); Book book = new Book(); book.setTitle("Name"); book.setAuthor(author); book.setIsbn("unknown"); book.save(); Criteria criteria = new Criteria(); criteria.addJoin(BookPeer.TITLE, AuthorPeer.NAME); BookPeer.addSelectColumns(criteria); AuthorPeer.addSelectColumns(criteria); // basically a BaseBookPeer.setDbName(criteria); // and BasePeer.doSelect(criteria); List villageRecords = BookPeer.doSelectVillageRecords(criteria); Record record = (Record) villageRecords.get(0); book = new Book(); BookPeer.populateObject(record, 1, book); author = new Author(); AuthorPeer.populateObject(record, BookPeer.numColumns + 1, author); if (book.getAuthorId() == author.getAuthorId()) { fail("wrong Ids read"); } } /** * Tests the date, time and datetime accuracy. * At the moment, no upper limit for the accuracy is checked, * the differences are printed to stdout. * @throws Exception if the test fails */ public void testDateTime() throws Exception { // clean Date table Criteria criteria = new Criteria(); criteria.add( DateTestPeer.DATE_TEST_ID, (Long) null, Criteria.NOT_EQUAL); DateTestPeer.doDelete(criteria); // insert new DateTest object to db DateTest dateTest = new DateTest(); Date now = new Date(); dateTest.setDateValue(now); dateTest.setTimeValue(now); dateTest.setTimestampValue(now); dateTest.save(); DateFormat dateFormat = new SimpleDateFormat(); System.out.println( "testDateTime() : set date to : " + dateFormat.format(now)); // reload dateTest from db DateTest loadedDateTest = DateTestPeer.retrieveByPK(dateTest.getPrimaryKey()); System.out.println( "testDateTime() : retrieved date : " + dateFormat.format(loadedDateTest.getDateValue())); System.out.println( "testDateTime() : retrieved time : " + dateFormat.format(loadedDateTest.getTimeValue())); System.out.println( "testDateTime() : retrieved timestamp : " + dateFormat.format(loadedDateTest.getTimestampValue())); // compute time differences between reloaded and original object long dateDifference = dateTest.getDateValue().getTime() - loadedDateTest.getDateValue().getTime(); long timeDifference = dateTest.getTimeValue().getTime() - loadedDateTest.getTimeValue().getTime(); long timestampDifference = dateTest.getTimestampValue().getTime() - loadedDateTest.getTimestampValue().getTime(); System.out.println( "testDateTime() : Date difference (ms): " + dateDifference); System.out.println( "testDateTime() : Time difference (ms): " + timeDifference); System.out.println( "testDateTime() : Timestamp difference (ms): " + timestampDifference); } /** * tests whether large primary keys are inserted and read correctly * @throws Exception if the test fails */ public void testLargePk() throws Exception { // clean LargePk table Criteria criteria = new Criteria(); criteria.add( LargePkPeer.LARGE_PK_ID, (Long) null, Criteria.NOT_EQUAL); LargePkPeer.doDelete(criteria); long longId = 8771507845873286l; LargePk largePk = new LargePk(); largePk.setLargePkId(longId); largePk.setName("testLargePk"); largePk.save(); List largePkList = LargePkPeer.doSelect(new Criteria()); LargePk readLargePk = (LargePk) largePkList.get(0); assertTrue("the inserted Id, " + largePk.getLargePkId() + " , and the read id, " + readLargePk.getLargePkId() + " , should be equal", readLargePk.getLargePkId() == largePk.getLargePkId()); assertTrue("the inserted Id, " + largePk.getLargePkId() + " , should be equal to " + longId, longId == largePk.getLargePkId()); } /** * Tests the CountHelper class * @throws Exception if the test fails */ public void testCountHelper() throws Exception { cleanBookstore(); Author author = new Author(); author.setName("Name"); author.save(); author = new Author(); author.setName("Name2"); author.save(); author = new Author(); author.setName("Name"); author.save(); Criteria criteria = new Criteria(); int count = new CountHelper().count( criteria, null, AuthorPeer.AUTHOR_ID); if (count != 3) { fail("counted " + count + " datasets, should be 3 "); } criteria = new Criteria(); criteria.setDistinct(); count = new CountHelper().count(criteria, null, AuthorPeer.NAME); if (count != 2) { fail("counted " + count + " distinct datasets, should be 2 "); } criteria = new Criteria(); criteria.add(AuthorPeer.NAME, "Name2"); count = new CountHelper().count(criteria); if (count != 1) { fail("counted " + count + " datasets with name Name2," + " should be 1 "); } } /** * Tests whether we can handle multiple primary keys some of which are * also foreign keys * @throws Exception if the test fails */ public void testMultiplePrimaryForeignKey() throws Exception { IntegerPk integerPk = new IntegerPk(); integerPk.save(); MultiPkForeignKey multiPkForeignKey = new MultiPkForeignKey(); multiPkForeignKey.setId(10); multiPkForeignKey.setIntegerPk(integerPk); multiPkForeignKey.save(); integerPk.save(); } /** * Tests inserting single quotes in Strings. * This may not crash now, but in a later task like datasql, * so the data has to be inserted in a table which does not get cleaned * during the runtime test. * @throws Exception if inserting the test data fails */ public void testSingleQuotes() throws Exception { // clean A table Criteria criteria = new Criteria(); criteria.add(APeer.A_ID, (Long) null, Criteria.NOT_EQUAL); APeer.doDelete(criteria); A a = new A(); a.setName("has Single ' Quote"); a.save(); } /** * check that blob cloumns can be read and written correctly * @throws Exception if the test fails */ public void testBlobs() throws Exception { // clean BlobTest table { Criteria criteria = new Criteria(); criteria.add( BlobTestPeer.ID, (Long) null, Criteria.NOT_EQUAL); BlobTestPeer.doDelete(criteria); } // create a new BlobTest Object with large blob and clob values // and save it BlobTest blobTest = new BlobTest(); { int length = 100000; byte[] bytes = new byte[length]; StringBuffer chars = new StringBuffer(); String charTemplate = "1234567890abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < length; ++i) { bytes[i] = new Integer(i % 256).byteValue(); chars.append(charTemplate.charAt(i % charTemplate.length())); } blobTest.setBlobValue(bytes); } blobTest.save(); // read the BlobTests from the database // and check the values against the original values List blobTestList = BlobTestPeer.doSelect(new Criteria()); assertTrue("blobTestList should contain 1 object but contains " + blobTestList.size(), blobTestList.size() == 1); BlobTest readBlobTest = (BlobTest) blobTestList.get(0); assertTrue("read and written blobs should be equal. " + "Size of read blob is" + readBlobTest.getBlobValue().length + " size of written blob is " + blobTest.getBlobValue().length, Arrays.equals( blobTest.getBlobValue(), readBlobTest.getBlobValue())); } /** * check that clob cloumns can be read and written correctly * @throws Exception if the test fails */ public void testClobs() throws Exception { // clean ClobTest table { Criteria criteria = new Criteria(); criteria.add( ClobTestPeer.ID, (Long) null, Criteria.NOT_EQUAL); ClobTestPeer.doDelete(criteria); } // create a new ClobTest Object with a large clob value // and save it ClobTest clobTest = new ClobTest(); { int length = 10000; StringBuffer chars = new StringBuffer(); String charTemplate = "1234567890abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < length; ++i) { chars.append(charTemplate.charAt(i % charTemplate.length())); } clobTest.setClobValue(chars.toString()); } clobTest.save(); // read the ClobTests from the database // and check the values against the original values List clobTestList = ClobTestPeer.doSelect(new Criteria()); assertTrue("clobTestList should contain 1 object but contains " + clobTestList.size(), clobTestList.size() == 1); ClobTest readClobTest = (ClobTest) clobTestList.get(0); assertTrue("read and written clobs should be equal", clobTest.getClobValue().equals(readClobTest.getClobValue())); } /** * Test whether we can execute queries as prepared statements * @throws Exception */ public void testPreparedStatements() throws Exception { // clean LargePk table Criteria criteria = new Criteria(); criteria.add( LargePkPeer.LARGE_PK_ID, (Long) null, Criteria.NOT_EQUAL); LargePkPeer.doDelete(criteria); LargePk largePk = new LargePk(); largePk.setLargePkId(1); largePk.setName("testLargePk"); largePk.save(); largePk = new LargePk(); largePk.setLargePkId(2); largePk.setName("testLargePk"); largePk.save(); criteria = new Criteria(); criteria.add(LargePkPeer.LARGE_PK_ID, 2, Criteria.LESS_THAN); LargePkPeer.addSelectColumns(criteria); List result = BasePeer.doPSSelect(criteria); assertTrue("Size of largePk list should be 1 but is " + result.size(), result.size() == 1); } /** * Tests whether shutdown complains about anything * @throws TorqueException if shutdown does not exit cleanly */ public void testShutdown() throws TorqueException { Torque.shutdown(); } /** * Deletes all authors and books in the bookstore tables * @throws Exception if the bookstore could not be cleaned */ protected void cleanBookstore() throws Exception { Criteria criteria = new Criteria(); criteria.add(BookPeer.BOOK_ID, (Long) null, Criteria.NOT_EQUAL); BookPeer.doDelete(criteria); criteria.clear(); criteria.add( AuthorPeer.AUTHOR_ID, (Long) null, Criteria.NOT_EQUAL); AuthorPeer.doDelete(criteria); } /** * Strips the schema and table name from a fully qualified colum name * This is useful for creating Query with aliases, as the constants * for the colum names in the data objects are fully qualified. * @param fullyQualifiedColumnName the fully qualified column name, not null * @return the column name stripped from the table (and schema) prefixes */ public static String getRawColumnName(String fullyQualifiedColumnName) { int dotPosition = fullyQualifiedColumnName.lastIndexOf("."); if (dotPosition == -1) { return fullyQualifiedColumnName; } String result = fullyQualifiedColumnName.substring( dotPosition + 1, fullyQualifiedColumnName.length()); return result; } /** * Subclass of BookPeer to make the doSelectJoinAuthors() visible */ static class MyBookPeer extends BookPeer { public static List doSelectJoinAuthor(Criteria criteria) throws TorqueException { return BookPeer.doSelectJoinAuthor(criteria); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -