Package org.apache.torque.test.dbobject

Examples of org.apache.torque.test.dbobject.Author


     */
    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();
View Full Code Here


            //expected
        }

        try
        {
            Author author = new Author();
            author.setName("name");
            author.save((Connection) null);
            fail("TorqueException expected");
        }
        catch (TorqueException e)
        {
            //expected
View Full Code Here

    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);
View Full Code Here

    public void testIgnoreCase() throws Exception
    {
        cleanBookstore();

        // check ignore case in selects
        Author author = new Author();
        author.setName("AuTHor");
        author.save();

        Criteria criteria = new Criteria();
        criteria.where(AuthorPeer.NAME, author.getName().toLowerCase());
        criteria.setIgnoreCase(true);
        List<Author> result = AuthorPeer.doSelect(criteria);
        assertTrue("Size of result is not 1, but " + result.size(),
                result.size() == 1);

        // LIKE treatment might be different (e.g. postgres), so check extra
        criteria = new Criteria();
        criteria.where(
                AuthorPeer.NAME,
                author.getName().toLowerCase().replace('r', '%'),
                Criteria.LIKE);
        criteria.setIgnoreCase(true);
        result = AuthorPeer.doSelect(criteria);
        assertTrue("Size of result is not 1, but " + result.size(),
                result.size() == 1);

        // Test ignore case in criterion
        criteria = new Criteria();
        Criterion criterion1 = new Criterion(
                AuthorPeer.NAME,
                author.getName().toLowerCase(),
                Criteria.EQUAL);
        criterion1.setIgnoreCase(true);
        Criterion criterion2 = new Criterion(
                AuthorPeer.AUTHOR_ID, null, Criteria.NOT_EQUAL);
        criterion1.and(criterion2);

        result = AuthorPeer.doSelect(criteria);

        // ignore case should not be set either in Criteria
        // nor in other criterions
        assertFalse(criteria.isIgnoreCase());
        assertFalse(criterion2.isIgnoreCase());
        assertTrue("Size of result is not 1, but " + result.size(),
                result.size() == 1);


        // Test ignore case in attached criterion
        criteria = new Criteria();
        criterion1 = new Criterion(
                AuthorPeer.AUTHOR_ID, null, Criteria.NOT_EQUAL);
        criterion2 = new Criterion(
                AuthorPeer.NAME,
                author.getName().toLowerCase(),
                Criteria.EQUAL);
        criterion2.setIgnoreCase(true);
        criterion1.and(criterion2);

        result = AuthorPeer.doSelect(criteria);

        // ignore case should not be set either in Criteria
        // nor in other criterions
        assertFalse(criteria.isIgnoreCase());
        assertFalse(criterion1.isIgnoreCase());

        assertTrue("Size of result is not 1, but " + result.size(),
                result.size() == 1);

        // ignore case in "in" query
        {
            criteria = new Criteria();
            Set<String> names = new HashSet<String>();
            names.add(author.getName().toLowerCase());
            criteria.where(AuthorPeer.NAME, names, Criteria.IN);
            criteria.setIgnoreCase(true);

            result = AuthorPeer.doSelect(criteria);
            assertEquals("Expected result of size 1 but got " + result.size(),
                    result.size(),
                    1);
        }

        // Check that case is not ignored if ignoreCase is not set
        // This is known not to work for mysql
        author = new Author();
        author.setName("author");
        author.save();

        Adapter adapter = Torque.getAdapter(Torque.getDefaultDB());
        if (adapter instanceof MysqlAdapter
                || adapter instanceof MssqlAdapter)
        {
            log.error("testIgnoreCase(): "
                    + "Case sensitive comparisons are known not to work"
                    + " with Mysql and MSSQL");
            // failing is "expected", so bypass without error
        }
        else
        {
            criteria = new Criteria();
            criteria.where(AuthorPeer.NAME, author.getName());
            result = AuthorPeer.doSelect(criteria);
            assertTrue("Size of result is not 1, but " + result.size(),
                    result.size() == 1);

            // again check LIKE treatment
            criteria = new Criteria();
            criteria.where(
                    AuthorPeer.NAME,
                    author.getName().replace('r', '%'),
                    Criteria.LIKE);
            result = AuthorPeer.doSelect(criteria);
            assertTrue("Size of result is not 1, but " + result.size(),
                    result.size() == 1);

            // Test different ignore cases in criterions
            criteria = new Criteria();
            criterion1 = new Criterion(
                    AuthorPeer.NAME,
                    author.getName().toLowerCase(),
                    Criteria.NOT_EQUAL);
            criterion2 = new Criterion(
                    AuthorPeer.NAME,
                    author.getName().toLowerCase(),
                    Criteria.EQUAL);
            criterion2.setIgnoreCase(true);
            criterion1.and(criterion2);
            criteria.where(criterion1);

            result = AuthorPeer.doSelect(criteria);
            assertTrue("Size of result is not 1, but " + result.size(),
                    result.size() == 1);

            // ignore case in "in" query
            {
                criteria = new Criteria();
                Set<String> names = new HashSet<String>();
                names.add(author.getName());
                criteria.where(AuthorPeer.NAME, names, Criteria.IN);

                result = AuthorPeer.doSelect(criteria);
                assertEquals("Expected result of size 1 but got " + result.size(),
                        result.size(),
                        1);
            }
        }

        cleanBookstore();
        author = new Author();
        author.setName("AA");
        author.save();
        author = new Author();
        author.setName("BB");
        author.save();
        author = new Author();
        author.setName("ba");
        author.save();
        author = new Author();
        author.setName("ab");
        author.save();

        // check ignoreCase in Criteria
        criteria = new Criteria();
        criteria.setIgnoreCase(true);
        criteria.addAscendingOrderByColumn(AuthorPeer.NAME);
View Full Code Here

     * @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);
        CompositeMapper mapper = new CompositeMapper();
        mapper.addMapper(new BookRecordMapper(), 0);
        mapper.addMapper(
                new AuthorRecordMapper(),
                BookPeer.numColumns);

        List<List<Object>> queryResult
                = BookPeer.doSelect(criteria, mapper);
        List<Object> mappedRow = queryResult.get(0);
        book = (Book) mappedRow.get(0);
        author = (Author) mappedRow.get(1);

        if (book.getAuthorId() == author.getAuthorId())
        {
            fail("wrong Ids read");
        }
    }
View Full Code Here

     * @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,
View Full Code Here

     */
    public void testSingleQuotes() throws Exception
    {
        cleanBookstore();

        Author author = new Author();
        author.setName("has Single ' Quote");
        author.save();
    }
View Full Code Here

     * Test whether equals() is working correctly
     * @throws Exception
     */
    public void testEquals() throws Exception
    {
        Author author = new Author();
        author.setAuthorId(1000);

        Book book = new Book();
        book.setBookId(1000);

        Book bookNotEqual = new Book();
        bookNotEqual.setBookId(2000);

        Book bookEqual = new Book();
        bookEqual.setBookId(1000);

        assertFalse("Author and Book should not be equal",
                author.equals(book));
        assertTrue("Book compared with itself should be equal",
                book.equals(book));
        assertTrue("Book compared with book with same id should be equal",
                book.equals(bookEqual));
        assertFalse("Book compared with book with different id "
View Full Code Here

     * @throws Exception if a problem occurs.
     */
    public void testUnqualifiedColumnNames() throws Exception
    {
        cleanBookstore();
        Author author = new Author();
        author.setName("Joshua Bloch");
        author.save();

        Criteria criteria = new Criteria();
        criteria.where(AuthorPeer.AUTHOR_ID, (Object) null, Criteria.NOT_EQUAL);
        criteria.and(new ColumnImpl("name"), "Joshua Bloch", Criteria.EQUAL);
        List<Author> authors = AuthorPeer.doSelect(criteria);
View Full Code Here

        cleanBookstore();

        // Save authors
        for (int i = 0; i < authorNames.length; ++i)
        {
            Author author = new Author();
            author.setName(authorNames[i]);
            author.save();
        }

        // Check authors are in the database
        for (int i = 0; i < authorNames.length; ++i)
        {
            Criteria criteria = new Criteria();
            criteria.where(AuthorPeer.NAME, authorNames[i]);
            List<Author> authorList = AuthorPeer.doSelect(criteria);
            assertEquals(
                    "AuthorList should contain one author"
                        + " when querying for " + authorNames[i],
                    1,
                    authorList.size());
            Author author = authorList.get(0);
            assertEquals("Name of author should be " + authorNames[i],
                    authorNames[i],
                    author.getName());
        }

        for (Map.Entry<String, String> likeResult : likeResults.entrySet())
        {
            Criteria criteria = new Criteria();
            criteria.where(
                    AuthorPeer.NAME,
                    likeResult.getKey(),
                    Criteria.LIKE);
            List<Author> authorList;
            try
            {
                authorList = AuthorPeer.doSelect(criteria);
            }
            catch (Exception e)
            {
                throw new Exception(
                        "error rxecuting select using like content "
                        + likeResult.getKey(),
                    e);
            }
            assertEquals(
                    "AuthorList should contain one author"
                        + " when querying for " + likeResult.getKey(),
                    1,
                    authorList.size());
            Author author = authorList.get(0);
            assertEquals("Name of author should be "
                        + likeResult.getValue()
                        + " when querying for "
                        + likeResult.getKey(),
                    likeResult.getValue(),
                    author.getName());
        }

        // check that case insensitivity is maintained if
        // a like is replaced with an equals (no wildcard present)
        // This might be a problem for databases which use ILIKE
        Criteria criteria = new Criteria();
        criteria.where(AuthorPeer.NAME, "AbC", Criteria.LIKE);
        criteria.setIgnoreCase(true);
        List<Author> authorList = AuthorPeer.doSelect(criteria);
        assertEquals(
                "AuthorList should contain one author",
                1,
                authorList.size());
        Author author = authorList.get(0);
        assertEquals("Name of author should be abc",
                "abc",
                author.getName());

        // check that the escape clause (where needed) also works
        // with limit, offset and order by
        criteria = new Criteria();
        Criterion criterion1 = new Criterion(
                AuthorPeer.NAME,
                "b%",
                Criteria.LIKE);
        Criterion criterion2 = new Criterion(
                AuthorPeer.NAME,
                "a\\%%",
                Criteria.LIKE);
        Criterion criterion3 = new Criterion(
                AuthorPeer.NAME,
                "cbc",
                Criteria.LIKE);
        criteria.where(criterion1.or(criterion2).or(criterion3));
        criteria.addAscendingOrderByColumn(AuthorPeer.NAME);
        criteria.setOffset(1);
        criteria.setLimit(1);
        authorList = AuthorPeer.doSelect(criteria);
        assertEquals(
                "AuthorList should contain one author",
                1,
                authorList.size());
        author = authorList.get(0);
        assertEquals("Name of author should be bbc",
                "bbc",
                author.getName());
    }
View Full Code Here

TOP

Related Classes of org.apache.torque.test.dbobject.Author

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.