📄 datatest.java
字号:
AuthorPeer.doDelete(criteria); authorResult = AuthorPeer.doSelect(new Criteria()); assertTrue("deleted not enough records", authorResult.size() == 0); } /** * test special cases in the select clause * @throws Exception if the test fails */ public void testSelectClause() throws Exception { // test double functions in select columns Criteria criteria = new Criteria(); criteria.addSelectColumn("count(distinct(" + BookPeer.BOOK_ID + "))"); List result = BookPeer.doSelectVillageRecords(criteria); // test qualifiers in function in select columns criteria = new Criteria(); criteria.addSelectColumn("count(distinct " + BookPeer.BOOK_ID + ")"); result = BookPeer.doSelectVillageRecords(criteria); } /** * test the behaviour if a connection is supplied to access the database, * but it is null. All methods on the user level should be able to * handle this. */ public void testNullConnection() throws Exception { Criteria criteria = new Criteria(); List result = BookPeer.doSelectVillageRecords(criteria, null); criteria = new Criteria(); criteria.add(BookPeer.BOOK_ID, (Long) null, Criteria.NOT_EQUAL); BookPeer.doDelete(criteria, null); Author author = new Author(); author.setName("name"); author.save((Connection) null); } /** * test joins * @throws Exception if the test fails */ public void testJoins() throws Exception { cleanBookstore(); // insert test data Author author = new Author(); author.setName("Author with one book"); author.save(); Book book = new Book(); book.setAuthor(author); book.setTitle("Book 1"); book.setIsbn("unknown"); book.save(); author = new Author(); author.setName("Author without book"); author.save(); author = new Author(); author.setName("Author with three books"); author.save(); for (int bookNr = 2; bookNr <=4; bookNr++) { book = new Book(); book.setAuthor(author); book.setTitle("Book " + bookNr); book.setIsbn("unknown"); book.save(); } // test left join Criteria criteria = new Criteria(); criteria.addJoin(AuthorPeer.AUTHOR_ID, BookPeer.AUTHOR_ID, Criteria.LEFT_JOIN); List authorList = AuthorPeer.doSelect(criteria); // Here we get 5 authors: // the author with one book, the author without books, // and three times the author with three books if (authorList.size() != 5) { fail("author left join book : " + "incorrect numbers of authors found : " + authorList.size() + ", should be 5"); } // test inner join criteria = new Criteria(); criteria.addJoin( AuthorPeer.AUTHOR_ID, BookPeer.AUTHOR_ID, Criteria.INNER_JOIN); authorList = AuthorPeer.doSelect(criteria); // Here we get 4 authors: // the author with one book, // and three times the author with three books if (authorList.size() != 4) { fail("author left join book : " + "incorrect numbers of authors found : " + authorList.size() + ", should be 4"); } if (Torque.getDB(Torque.getDefaultDB()) instanceof DBHypersonicSQL) { log.error("testJoins(): Right joins are not supported by HSQLDB"); // failing is "expected", so exit without error return; } // test right join criteria = new Criteria(); criteria.addJoin( BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID, Criteria.RIGHT_JOIN); authorList = AuthorPeer.doSelect(criteria); // Here we get 4 authors: // the author with one book, the author without books, // and three times the author with three books if (authorList.size() != 5) { fail("book right join author " + "incorrect numbers of authors found : " + authorList.size() + ", should be 5"); } // test double join with aliases criteria = new Criteria(); criteria.addAlias("b", BookPeer.TABLE_NAME); criteria.addJoin( BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID, Criteria.RIGHT_JOIN); criteria.addJoin( AuthorPeer.AUTHOR_ID, "b." + getRawColumnName(BookPeer.AUTHOR_ID), Criteria.LEFT_JOIN); authorList = AuthorPeer.doSelect(criteria); // Here we get 11 authors: // the author with one book, the author without books, // and nine times the author with three books if (authorList.size() != 11) { fail("book right join author left join book b: " + "incorrect numbers of authors found : " + authorList.size() + ", should be 11"); } // test double join with aliases and "reversed" second join criteria = new Criteria(); criteria.addAlias("b", BookPeer.TABLE_NAME); criteria.addJoin(BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID, Criteria.RIGHT_JOIN); criteria.addJoin( "b." + getRawColumnName(BookPeer.AUTHOR_ID), AuthorPeer.AUTHOR_ID, Criteria.RIGHT_JOIN); authorList = AuthorPeer.doSelect(criteria); // Here we get 11 authors: // the author with one book, the author without books, // and nine times the author with three books if (authorList.size() != 11) { fail("book right join author left join book b (reversed): " + "incorrect numbers of authors found : " + authorList.size() + ", should be 11"); } } /** * Test joins using the XPeer.DoSelectJoinYYY methods * @throws Exception if the Test fails */ public void testDoSelectJoinY() throws Exception { // using the test data from testJoins() Criteria criteria = new Criteria(); criteria.addAscendingOrderByColumn(BookPeer.TITLE); List books = MyBookPeer.doSelectJoinAuthor(criteria); assertTrue("books should contain 4 books but contains " + books.size(), books.size() == 4); Book bookTwo = (Book) books.get(1); Book bookThree = (Book) books.get(2); assertTrue ("the authors of BookTwo and BookThree" + " should point to the same instance", bookTwo.getAuthor() == bookThree.getAuthor()); } /** * test the order by, especially in joins and with aliases * @throws Exception if the test fails */ public void testOrderBy() throws Exception { cleanBookstore(); // insert test data Author firstAuthor = new Author(); firstAuthor.setName("Author 1"); firstAuthor.save(); Book book = new Book(); book.setAuthor(firstAuthor); book.setTitle("Book 1"); book.setIsbn("unknown"); book.save(); Author secondAuthor = new Author(); secondAuthor.setName("Author 2"); secondAuthor.save(); for (int bookNr = 2; bookNr <=4; bookNr++) { book = new Book(); book.setAuthor(secondAuthor); book.setTitle("Book " + bookNr); book.setIsbn("unknown"); book.save(); } // test simple ascending order by Criteria criteria = new Criteria(); criteria.addAscendingOrderByColumn(BookPeer.TITLE); List bookList = BookPeer.doSelect(criteria); if (bookList.size() != 4) { fail("Ascending Order By: " + "incorrect numbers of books found : " + bookList.size() + ", should be 4"); } if (! "Book 1".equals(((Book) bookList.get(0)).getTitle())) { fail("Ascending Order By: " + "Title of first Book is " + ((Book) bookList.get(0)).getTitle() + ", should be \"Book 1\""); } if (! "Book 4".equals(((Book) bookList.get(3)).getTitle())) { fail("Ascending Order By: " + "Title of fourth Book is " + ((Book) bookList.get(3)).getTitle() + ", should be \"Book 4\""); } // test simple descending order by criteria = new Criteria(); criteria.addDescendingOrderByColumn(BookPeer.TITLE); bookList = BookPeer.doSelect(criteria); if (bookList.size() != 4) { fail("Descending Order By: " + "incorrect numbers of books found : " + bookList.size() + ", should be 4"); } if (! "Book 1".equals(((Book) bookList.get(3)).getTitle())) { fail("Descending Order By: " + "Title of fourth Book is " + ((Book) bookList.get(3)).getTitle() + ", should be \"Book 1\""); } if (! "Book 4".equals(((Book) bookList.get(0)).getTitle())) { fail("Descending Order By: " + "Title of first Book is " + ((Book) bookList.get(0)).getTitle() + ", should be \"Book 4\""); } // test ordering by Aliases and in joins criteria = new Criteria(); criteria.addAlias("b", BookPeer.TABLE_NAME); criteria.addJoin(BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID); criteria.addJoin( AuthorPeer.AUTHOR_ID, "b." + getRawColumnName(BookPeer.AUTHOR_ID)); criteria.addAscendingOrderByColumn( "b." + getRawColumnName(BookPeer.TITLE)); criteria.addDescendingOrderByColumn(BookPeer.TITLE); // the retrieved columns are // author book b // author1 book1 book1 // author2 book4 book2 // author2 book3 book2 // author2 book2 book2 // author2 book4 book3 // ... bookList = BookPeer.doSelect(criteria); if (bookList.size() != 10) { fail("ordering by Aliases: " + "incorrect numbers of books found : " + bookList.size() + ", should be 10"); } if (!"Book 4".equals(((Book)bookList.get(1)).getTitle())) { fail("ordering by Aliases: " + "Title of second Book is " + ((Book) bookList.get(1)).getTitle() + ", should be \"Book 4\""); } if (!"Book 3".equals(((Book)bookList.get(2)).getTitle())) { fail("ordering by Aliases: " + "Title of third Book is " + ((Book) bookList.get(2)).getTitle() + ", should be \"Book 3\""); } criteria = new Criteria(); criteria.addAlias("b", BookPeer.TABLE_NAME); criteria.addJoin(BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID); criteria.addJoin( AuthorPeer.AUTHOR_ID, "b." + getRawColumnName(BookPeer.AUTHOR_ID)); criteria.addAscendingOrderByColumn(BookPeer.TITLE); criteria.addDescendingOrderByColumn( "b." + getRawColumnName(BookPeer.TITLE)); // the retrieved columns are // author book b // author1 book1 book1 // author2 book2 book4 // author2 book2 book3 // author2 book2 book2 // author2 book3 book4 // ... bookList = BookPeer.doSelect(criteria); if (bookList.size() != 10) { fail("ordering by Aliases (2): " + "incorrect numbers of books found : " + bookList.size() + ", should be 10"); } if (!"Book 2".equals(((Book)bookList.get(1)).getTitle())) { fail("ordering by Aliases (2, PS): " + "Title of second Book is " + ((Book) bookList.get(1)).getTitle() + ", should be \"Book 2\""); } if (!"Book 2".equals(((Book)bookList.get(2)).getTitle())) { fail("ordering by Aliases (2, PS): " + "Title of third Book is " + ((Book) bookList.get(2)).getTitle() + ", should be \"Book 2\""); } // test usage of Expressions in order by criteria = new Criteria(); criteria.addAscendingOrderByColumn("UPPER(" + BookPeer.TITLE + ")"); criteria.setIgnoreCase(true); BookPeer.doSelect(criteria); } /** * Tests whether ignoreCase works correctly * @throws Exception if the test fails */ public void testIgnoreCase() throws Exception { cleanBookstore(); Author author = new Author(); author.setName("AuTHor"); author.save(); Criteria criteria = new Criteria(); criteria.add(AuthorPeer.NAME, author.getName().toLowerCase()); criteria.setIgnoreCase(true); List result = AuthorPeer.doSelect(criteria); if (result.size() != 1) { fail("Size of result is not 1, but " + result.size()); } } /** * tests whether AsColumns produce valid SQL code * @throws Exception if the test fails */ public void testAsColumn() throws Exception { Criteria criteria = new Criteria(); criteria.addAsColumn("ALIASNAME", AuthorPeer.NAME); // we need an additional column to select from, // to indicate the table we want use criteria.addSelectColumn(AuthorPeer.AUTHOR_ID); BasePeer.doSelect(criteria); } /** * Test whether same column name in different tables
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -