-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Why doesn't SimpleExpression implement hashcode and equals?
PostPosted: Mon Mar 22, 2010 8:54 pm 
Newbie

Joined: Mon Mar 22, 2010 8:45 pm
Posts: 1
I have a class called WhereClause that has the following method...

Code:
    public void appendWhereClause( Criteria query )
    {
        if ( !CollectionUtils.isEmpty( this.getProperties() ) )
        {
            Object propertyValue = null;

            for ( MapIterator mapIt = properties.mapIterator(); mapIt.hasNext(); )
            {
                String property = (String) mapIt.next();
                propertyValue = properties.get( property );
                if ( propertyValue == null )
                {
                    query.add( Restrictions.isNull( property ) );
                }
                else
                {
                    String[] props = property.split( "\\|" );
                    if ( props[1].equals( IS_EQUAL ) )
                    {
                        query.add( Restrictions.eq( props[0], propertyValue ) );
                    }
                    else
                    {
                        query.add( Restrictions.like( props[0], propertyValue ) );
                    }
                }

            }
        }
    }



As you can see, it adds restrictions to the query, based on the contents of properties.

I was hoping to test this method using...

Code:
    public void testAppendWhereClause()
    {
        // set up
        WhereClause clause = new WhereClause();
        clause.put( "BLAH", "1" );

        criteria = context.mock( Criteria.class );

        final SimpleExpression RESTRICT_BLAH_EQ_1 = Restrictions.eq( "BLAH", "1" );

        // expectation
        context.checking( new Expectations() {
            {
                allowing( criteria ).add( with( RESTRICT_BLAH_EQ_1 ) );
            }
        } );

        // execute
        clause.appendWhereClause( criteria );

        // verify
        context.assertIsSatisfied();
    }



But this test never passes, because the SimpleExpression instance, RESTRICT_BLAH_EQ_1, has a different reference to the instance created inside the WhereClause method above. Therefore there seems to be no way to test anything ivolving these *Expression classes. Naturally, I tried replacing Restrictions and SimpleExpression in my code, but unfortunately Restrictions uses a lot of package private instantiators, so it ios not possible to override or subclass.

So the question is, why is hashcode and equals not implemented in teh *Expression classes?


Top
 Profile  
 
 Post subject: Re: Why doesn't SimpleExpression implement hashcode and equals?
PostPosted: Thu Aug 11, 2011 9:49 am 
Newbie

Joined: Thu Aug 11, 2011 8:43 am
Posts: 2
I know this post is over a year old but it remains a very good unanswered question. As of the latest 3.6.x release there is still no equals/hashcode implementation in SimpleExpression.java Is there a specific reason why or has it simply been oversight? The implementation(s) would presumably consist of the propertyName, value, and op field values.

A major implication of not having at least an equals implementation is that you cannot reliably unit test any criteria based queries containing SimpleExpression. For example, consider the following Restrictions constraint ...
Code:
criteria.add(Restrictions.eq("brandName", someBrand)).list()

The best I can do to unit test this line of code would be to verify that my criteria merely contains some instance of SimpleExpression. For example (EasyMock) ...
Code:
expect(mockCriteria.add(isA(SimpleExpression.class))).andReturn(mockCriteria)

Not exactly a reliable test there. If there's an alternative way to unit test SimpleExpression conditions like this then someone please enlighten me. Otherwise I consider this is a serious inadequacy of the Criteria API.


Top
 Profile  
 
 Post subject: Re: Why doesn't SimpleExpression implement hashcode and equals?
PostPosted: Tue Aug 16, 2011 12:14 pm 
Newbie

Joined: Thu Aug 11, 2011 8:43 am
Posts: 2
Anyone???

I have to think that someone has a thought or opinion on this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.