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?