- Revision
- 1285
- Author
- rfscholte
- Date
- 2011-08-04 16:46:19 -0500 (Thu, 04 Aug 2011)
Log Message
Fix QDOX-235: Cast-_expression_ should be able to cast to every kind of Object added visit tests for EvaluatingVisitor fixed EvaluatingVisitor.visit(NotEquals) for Objects
Modified Paths
Diff
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/builder/EvaluatingVisitor.java (1284 => 1285)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/builder/EvaluatingVisitor.java 2011-08-03 20:44:12 UTC (rev 1284) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/builder/EvaluatingVisitor.java 2011-08-04 21:46:19 UTC (rev 1285) @@ -212,7 +212,7 @@ return type; } - public Object visit( Annotation annotation ) + public Object visit( Annotation annotation ) throws UnsupportedOperationException { throw new UnsupportedOperationException( "Illegal annotation value '" + annotation + "'." ); } @@ -865,7 +865,7 @@ } else { - result = ( left == right ); + result = ( left != right ); } return result ? Boolean.TRUE : Boolean.FALSE; @@ -930,21 +930,17 @@ throw new IllegalArgumentException( "Cannot evaluate '" + annotationCast + "'." ); } } - else if ( value instanceof String ) + else { - if ( type.equals( "java.lang.String" ) ) + try { - result = value; + result = Class.forName( type ).cast( value ); } - else + catch ( ClassNotFoundException e ) { throw new IllegalArgumentException( "Cannot evaluate '" + annotationCast + "'." ); } } - else - { - throw new IllegalArgumentException( "Cannot evaluate '" + annotationCast + "'." ); - } return result; }
Modified: trunk/qdox/src/test/java/com/thoughtworks/qdox/builder/EvaluatingVisitorTest.java (1284 => 1285)
--- trunk/qdox/src/test/java/com/thoughtworks/qdox/builder/EvaluatingVisitorTest.java 2011-08-03 20:44:12 UTC (rev 1284) +++ trunk/qdox/src/test/java/com/thoughtworks/qdox/builder/EvaluatingVisitorTest.java 2011-08-04 21:46:19 UTC (rev 1285) @@ -1,11 +1,53 @@ package com.thoughtworks.qdox.builder; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.List; + +import org.junit.Ignore; import org.junit.Test; +import com.thoughtworks.qdox.model.Annotation; +import com.thoughtworks.qdox.model.JavaField; +import com.thoughtworks.qdox.model.Type; +import com.thoughtworks.qdox.model._expression_.Add; +import com.thoughtworks.qdox.model._expression_.And; +import com.thoughtworks.qdox.model._expression_.AnnotationValue; +import com.thoughtworks.qdox.model._expression_.Cast; +import com.thoughtworks.qdox.model._expression_.Divide; +import com.thoughtworks.qdox.model._expression_.Equals; +import com.thoughtworks.qdox.model._expression_.ExclusiveOr; +import com.thoughtworks.qdox.model._expression_.GreaterEquals; +import com.thoughtworks.qdox.model._expression_.GreaterThan; +import com.thoughtworks.qdox.model._expression_.LessEquals; +import com.thoughtworks.qdox.model._expression_.LessThan; +import com.thoughtworks.qdox.model._expression_.LogicalAnd; +import com.thoughtworks.qdox.model._expression_.LogicalNot; +import com.thoughtworks.qdox.model._expression_.LogicalOr; +import com.thoughtworks.qdox.model._expression_.MinusSign; +import com.thoughtworks.qdox.model._expression_.Multiply; +import com.thoughtworks.qdox.model._expression_.Not; +import com.thoughtworks.qdox.model._expression_.NotEquals; +import com.thoughtworks.qdox.model._expression_.Or; +import com.thoughtworks.qdox.model._expression_.ParenExpression; +import com.thoughtworks.qdox.model._expression_.PlusSign; +import com.thoughtworks.qdox.model._expression_.Query; +import com.thoughtworks.qdox.model._expression_.Remainder; +import com.thoughtworks.qdox.model._expression_.ShiftLeft; +import com.thoughtworks.qdox.model._expression_.ShiftRight; +import com.thoughtworks.qdox.model._expression_.Subtract; +import com.thoughtworks.qdox.model._expression_.UnsignedShiftRight; + public class EvaluatingVisitorTest { - + private EvaluatingVisitor visitor = new EvaluatingVisitorStub(); + @Test public void testUnaryNumericResultTypeInteger() throws Exception @@ -187,4 +229,1010 @@ assertEquals( void.class, EvaluatingVisitor.resultType( null, (long) 0 ) ); assertEquals( void.class, EvaluatingVisitor.resultType( null, (int) 0 ) ); } + + @Test + public void testVisitAdd() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( any( EvaluatingVisitor.class ) ) ).thenReturn( 7.0D ); + when( rhs.accept( any( EvaluatingVisitor.class ) ) ).thenReturn( 2.0D ); + assertEquals( 7.0D + 2.0D, visitor.visit( new Add( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( any( EvaluatingVisitor.class ) ) ).thenReturn( 7.0F ); + when( rhs.accept( any( EvaluatingVisitor.class ) ) ).thenReturn( 2.0F ); + assertEquals( 7.0F + 2.0F, visitor.visit( new Add( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L + 2L, visitor.visit( new Add( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 + 2, visitor.visit( new Add( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new Add( lhs, rhs ) ); + fail( "Additive operations (+ and -) can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitAnd() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L & 2L, visitor.visit( new And( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 & 2, visitor.visit( new And( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new And( lhs, rhs ) ); + fail( "The and(&) operator can only be performed on integral types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitAnnotation() + { + try{ + visitor.visit( new Annotation( new Type("Ignore"), -1 ) ); + fail( "Visiting an annotation is not supported and should throw an UnsupportedOperationException" ); + } + catch (UnsupportedOperationException e) { + } + } + + @Ignore + @Test + public void testVisitAnnotationValueList() { + } + + @Test + public void testVisitCast() + { + AnnotationValue value = mock( AnnotationValue.class ); + + when( value.accept( visitor ) ).thenReturn( 7 ); + assertEquals( (byte) 7, visitor.visit( new Cast( new Type( "byte" ), value ) ) ); + + when( value.accept( visitor ) ).thenReturn( 7 ); + assertEquals( (char) 7, visitor.visit( new Cast( new Type( "char" ), value ) ) ); + + when( value.accept( visitor ) ).thenReturn( 7 ); + assertEquals( (short) 7, visitor.visit( new Cast( new Type( "short" ), value ) ) ); + + when( value.accept( visitor ) ).thenReturn( 7 ); + assertEquals( (int) 7, visitor.visit( new Cast( new Type( "int" ), value ) ) ); + + when( value.accept( visitor ) ).thenReturn( 7 ); + assertEquals( (long) 7, visitor.visit( new Cast( new Type( "long" ), value ) ) ); + + when( value.accept( visitor ) ).thenReturn( 7 ); + assertEquals( (float) 7, visitor.visit( new Cast( new Type( "float" ), value ) ) ); + + when( value.accept( visitor ) ).thenReturn( 7 ); + assertEquals( (double) 7, visitor.visit( new Cast( new Type( "double" ), value ) ) ); + + when( value.accept( visitor ) ).thenReturn( "hello world" ); + assertEquals( (String) "hello world", visitor.visit( new Cast( new Type( "java.lang.String" ), value ) ) ); + + Object list = Collections.EMPTY_LIST; + when( value.accept( visitor ) ).thenReturn( list ); + assertEquals( (List<?>) list, visitor.visit( new Cast( new Type( "java.util.List" ), value ) ) ); + + } + + @Test + public void testVisitDivide() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( visitor ) ).thenReturn( 7.0D ); + when( rhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 7.0D / 2.0D, visitor.visit( new Divide( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( visitor ) ).thenReturn( 7.0F ); + when( rhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 7.0F / 2.0F, visitor.visit( new Divide( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L / 2L, visitor.visit( new Divide( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 / 2, visitor.visit( new Divide( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new Divide( lhs, rhs ) ); + fail( "The divide(/) operator can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitEquals() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( visitor ) ).thenReturn( 7.0D ); + when( rhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 7.0D == 2.0D, visitor.visit( new Equals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0D ); + assertEquals( 7.0D == 7.0D, visitor.visit( new Equals( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( visitor ) ).thenReturn( 7.0F ); + when( rhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 7.0F == 2.0F, visitor.visit( new Equals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0F ); + assertEquals( 7.0F == 7.0F, visitor.visit( new Equals( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L == 2L, visitor.visit( new Equals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7L ); + assertEquals( 7L == 7L, visitor.visit( new Equals( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 == 2, visitor.visit( new Equals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7 ); + assertEquals( 7 == 7, visitor.visit( new Equals( lhs, rhs ) ) ); + + // Objects + Object object1 = new Object(); + Object object2 = new Object(); + when( lhs.accept( visitor ) ).thenReturn( object1 ); + when( rhs.accept( visitor ) ).thenReturn( object2 ); + assertEquals( object1 == object2, visitor.visit( new Equals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( object1 ); + assertEquals( object1 == object1, visitor.visit( new Equals( lhs, rhs ) ) ); + } + + @Test + public void testVisitExlusiveOr() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L ^ 2L, visitor.visit( new ExclusiveOr( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 ^ 2, visitor.visit( new ExclusiveOr( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new ExclusiveOr( lhs, rhs ) ); + fail( "The exclusive-or(^) operator can only be performed on integral types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Ignore + @Test + public void testVisitFieldRef() { + } + + + @Test + public void testVisitGreaterEquals() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( visitor ) ).thenReturn( 7.0D ); + when( rhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 7.0D >= 2.0D, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0D ); + assertEquals( 7.0D >= 7.0D, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 2.0D >= 7.0D, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( visitor ) ).thenReturn( 7.0F ); + when( rhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 7.0F >= 2.0F, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0F ); + assertEquals( 7.0F >= 7.0F, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 2.0F >= 7.0F, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L >= 2L, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7L ); + assertEquals( 7L >= 7L, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 2L >= 7L, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 >= 2, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7 ); + assertEquals( 7 >=7, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 2 >= 7, visitor.visit( new GreaterEquals( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new GreaterEquals( lhs, rhs ) ); + fail( "The greater-equals(>=) operator can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitGreaterThan() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( visitor ) ).thenReturn( 7.0D ); + when( rhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 7.0D > 2.0D, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0D ); + assertEquals( 7.0D > 7.0D, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 2.0D > 7.0D, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( visitor ) ).thenReturn( 7.0F ); + when( rhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 7.0F > 2.0F, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0F ); + assertEquals( 7.0F > 7.0F, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 2.0F > 7.0F, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L > 2L, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7L ); + assertEquals( 7L > 7L, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 2L > 7L, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 > 2, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7 ); + assertEquals( 7 > 7, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 2 > 7, visitor.visit( new GreaterThan( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new GreaterThan( lhs, rhs ) ); + fail( "The greater-than(>) operator can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitLessEquals() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( visitor ) ).thenReturn( 7.0D ); + when( rhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 7.0D <= 2.0D, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0D ); + assertEquals( 7.0D <= 7.0D, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 2.0D <= 7.0D, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( visitor ) ).thenReturn( 7.0F ); + when( rhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 7.0F <= 2.0F, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0F ); + assertEquals( 7.0F <= 7.0F, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 2.0F <= 7.0F, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L <= 2L, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7L ); + assertEquals( 7L <= 7L, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 2L <= 7L, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 <= 2, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7 ); + assertEquals( 7 <= 7, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 2 <= 7, visitor.visit( new LessEquals( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new LessEquals( lhs, rhs ) ); + fail( "The less-equals(<=) operator can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitLessThan() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( visitor ) ).thenReturn( 7.0D ); + when( rhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 7.0D < 2.0D, visitor.visit( new LessThan( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0D ); + assertEquals( 7.0D < 7.0D, visitor.visit( new LessThan( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 2.0D < 7.0D, visitor.visit( new LessThan( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( visitor ) ).thenReturn( 7.0F ); + when( rhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 7.0F < 2.0F, visitor.visit( new LessThan( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0F ); + assertEquals( 7.0F < 7.0F, visitor.visit( new LessThan( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 2.0F < 7.0F, visitor.visit( new LessThan( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L < 2L, visitor.visit( new LessThan( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7L ); + assertEquals( 7L < 7L, visitor.visit( new LessThan( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 2L < 7L, visitor.visit( new LessThan( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 < 2, visitor.visit( new LessThan( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7 ); + assertEquals( 7 < 7, visitor.visit( new LessThan( lhs, rhs ) ) ); + + when( lhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 2 < 7, visitor.visit( new LessThan( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new LessThan( lhs, rhs ) ); + fail( "The less-than(<) operator can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void visitLogicalAnd() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + when( lhs.accept( visitor ) ).thenReturn( true ); + when( rhs.accept( visitor ) ).thenReturn( true ); + assertEquals( true && true, visitor.visit( new LogicalAnd( lhs, rhs ) ) ); + + + when( lhs.accept( visitor ) ).thenReturn( false ); + when( rhs.accept( visitor ) ).thenReturn( false ); + assertEquals( false && false, visitor.visit( new LogicalAnd( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new LogicalAnd( lhs, rhs ) ); + fail( "The logical and(&&) operator can only be performed on booleans" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void visitLogicalNot() + { + AnnotationValue value = mock( AnnotationValue.class ); + + when( value.accept( visitor ) ).thenReturn( true ); + assertEquals( !true, visitor.visit( new LogicalNot( value ) ) ); + + when( value.accept( visitor ) ).thenReturn( false ); + assertEquals( !false, visitor.visit( new LogicalNot( value ) ) ); + + // Objects + when( value.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new LogicalNot( value ) ); + fail( "The logical not(!) operator can only be performed on booleans" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void visitLogicalOr() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + when( lhs.accept( visitor ) ).thenReturn( true ); + when( rhs.accept( visitor ) ).thenReturn( true ); + assertEquals( true || true, visitor.visit( new LogicalOr( lhs, rhs ) ) ); + + + when( lhs.accept( visitor ) ).thenReturn( false ); + when( rhs.accept( visitor ) ).thenReturn( false ); + assertEquals( false || false, visitor.visit( new LogicalOr( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new LogicalOr( lhs, rhs ) ); + fail( "The logical or(||) operator can only be performed on booleans" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitMinusSign() + { + AnnotationValue value = mock( AnnotationValue.class ); + + // Double + when( value.accept( visitor ) ).thenReturn( 7.0D ); + assertEquals( -7.0D, visitor.visit( new MinusSign( value ) ) ); + + // Float + when( value.accept( visitor ) ).thenReturn( 7.0F ); + assertEquals( -7.0F, visitor.visit( new MinusSign( value ) ) ); + + // Long + when( value.accept( visitor ) ).thenReturn( 7L ); + assertEquals( -7L, visitor.visit( new MinusSign( value ) ) ); + + // Integer + when( value.accept( visitor ) ).thenReturn( 7 ); + assertEquals( -7, visitor.visit( new MinusSign( value ) ) ); + + when( value.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new MinusSign( value ) ); + fail( "The minus(-) sign operator can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitMultiply() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( visitor ) ).thenReturn( 7.0D ); + when( rhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 7.0D * 2.0D, visitor.visit( new Multiply( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( visitor ) ).thenReturn( 7.0F ); + when( rhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 7.0F * 2.0F, visitor.visit( new Multiply( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L * 2L, visitor.visit( new Multiply( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 * 2, visitor.visit( new Multiply( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new Multiply( lhs, rhs ) ); + fail( "The multiply(*) operator can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitNot() + { + AnnotationValue value = mock( AnnotationValue.class ); + + // Longs + when( value.accept( visitor ) ).thenReturn( 7L ); + assertEquals( ~7L, visitor.visit( new Not( value ) ) ); + + // Integers + when( value.accept( visitor ) ).thenReturn( 7 ); + assertEquals( ~7, visitor.visit( new Not( value) ) ); + + // Objects + when( value.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new Not( value ) ); + fail( "The not(~) operator can only be performed on integral types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitNotEquals() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( visitor ) ).thenReturn( 7.0D ); + when( rhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 7.0D != 2.0D, visitor.visit( new NotEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0D ); + assertEquals( 7.0D != 7.0D, visitor.visit( new NotEquals( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( visitor ) ).thenReturn( 7.0F ); + when( rhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 7.0F != 2.0F, visitor.visit( new NotEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7.0F ); + assertEquals( 7.0F != 7.0F, visitor.visit( new NotEquals( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L != 2L, visitor.visit( new NotEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7L ); + assertEquals( 7L != 7L, visitor.visit( new NotEquals( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 != 2, visitor.visit( new NotEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( 7 ); + assertEquals( 7 != 7, visitor.visit( new NotEquals( lhs, rhs ) ) ); + + // Objects + Object object1 = new Object(); + Object object2 = new Object(); + when( lhs.accept( visitor ) ).thenReturn( object1 ); + when( rhs.accept( visitor ) ).thenReturn( object2 ); + assertEquals( object1 != object2, visitor.visit( new NotEquals( lhs, rhs ) ) ); + + when( rhs.accept( visitor ) ).thenReturn( object1 ); + assertEquals( object1 != object1, visitor.visit( new NotEquals( lhs, rhs ) ) ); + } + + @Test + public void testVisitOr() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L | 2L, visitor.visit( new Or( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 | 2, visitor.visit( new Or( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new Or( lhs, rhs ) ); + fail( "The or(|) operator can only be performed on integral types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitParenExpression() + { + AnnotationValue value = mock( AnnotationValue.class ); + + Object acceptResult = new Object(); + when( value.accept( visitor ) ).thenReturn( acceptResult ); + assertSame( acceptResult, visitor.visit( new ParenExpression( value ) ) ); + } + + @Test + public void testVisitPlusSign() + { + AnnotationValue value = mock( AnnotationValue.class ); + + // Double + when( value.accept( visitor ) ).thenReturn( 7.0D ); + assertEquals( 7.0D, visitor.visit( new PlusSign( value ) ) ); + + // Float + when( value.accept( visitor ) ).thenReturn( 7.0F ); + assertEquals( 7.0F, visitor.visit( new PlusSign( value ) ) ); + + // Long + when( value.accept( visitor ) ).thenReturn( 7L ); + assertEquals( 7L, visitor.visit( new PlusSign( value ) ) ); + + // Integer + when( value.accept( visitor ) ).thenReturn( 7 ); + assertEquals( 7, visitor.visit( new PlusSign( value ) ) ); + + when( value.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new PlusSign( value ) ); + fail( "The plus sign operator can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + + } + } + + @Test + public void testVisitQuery() + { + AnnotationValue condition = mock( AnnotationValue.class ); + AnnotationValue trueExpr = mock( AnnotationValue.class ); + AnnotationValue falseExpr = mock( AnnotationValue.class ); + + when( trueExpr.accept( visitor ) ).thenReturn( "consequent" ); + when( falseExpr.accept( visitor ) ).thenReturn( "alternative" ); + + // true condition + when( condition.accept( visitor ) ).thenReturn( Boolean.TRUE ); + assertEquals( "consequent", visitor.visit( new Query( condition, trueExpr, falseExpr ) ) ); + + when( condition.accept( visitor ) ).thenReturn( true ); + assertEquals( "consequent", visitor.visit( new Query( condition, trueExpr, falseExpr ) ) ); + + // false condition + when( condition.accept( visitor ) ).thenReturn( Boolean.FALSE ); + assertEquals( "alternative", visitor.visit( new Query( condition, trueExpr, falseExpr ) ) ); + + when( condition.accept( visitor ) ).thenReturn( false ); + assertEquals( "alternative", visitor.visit( new Query( condition, trueExpr, falseExpr ) ) ); + + when( condition.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new Query( condition, trueExpr, falseExpr ) ); + fail( "The condition of the query( ? : ) must be a boolean" ); + } + catch ( IllegalArgumentException iae ) + { + + } + } + + @Test + public void testVisitRemainder() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( visitor ) ).thenReturn( 7.0D ); + when( rhs.accept( visitor ) ).thenReturn( 2.0D ); + assertEquals( 7.0D % 2.0D, visitor.visit( new Remainder( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( visitor ) ).thenReturn( 7.0F ); + when( rhs.accept( visitor ) ).thenReturn( 2.0F ); + assertEquals( 7.0F % 2.0F, visitor.visit( new Remainder( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L % 2L, visitor.visit( new Remainder( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 % 2, visitor.visit( new Remainder( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new Remainder( lhs, rhs ) ); + fail( "The remainder(%) operator can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitShiftLeft() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L << 2L, visitor.visit( new ShiftLeft( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 << 2, visitor.visit( new ShiftLeft( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( any( EvaluatingVisitor.class ) ) ).thenReturn( new Object() ); + when( rhs.accept( any( EvaluatingVisitor.class ) ) ).thenReturn( new Object() ); + try + { + visitor.visit( new ShiftLeft( lhs, rhs ) ); + fail( "Bitwise and bit shift operations can only be performed on integral types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitShiftRight() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L >> 2L, visitor.visit( new ShiftRight( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 >> 2, visitor.visit( new ShiftRight( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new ShiftRight( lhs, rhs ) ); + fail( "Bitwise and bit shift operations can only be performed on integral types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Test + public void testVisitSubtract() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Doubles + when( lhs.accept( any( EvaluatingVisitor.class ) ) ).thenReturn( 7.0D ); + when( rhs.accept( any( EvaluatingVisitor.class ) ) ).thenReturn( 2.0D ); + assertEquals( 7.0D - 2.0D, visitor.visit( new Subtract( lhs, rhs ) ) ); + + // Floats + when( lhs.accept( any( EvaluatingVisitor.class ) ) ).thenReturn( 7.0F ); + when( rhs.accept( any( EvaluatingVisitor.class ) ) ).thenReturn( 2.0F ); + assertEquals( 7.0F - 2.0F, visitor.visit( new Subtract( lhs, rhs ) ) ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L - 2L, visitor.visit( new Subtract( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 - 2, visitor.visit( new Subtract( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new Subtract( lhs, rhs ) ); + fail( "Additive operations (+ and -) can only be performed on numeric types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + @Ignore + @Test + public void testVisitTypeRef() { + + } + + @Test + public void testVisitUnsignedShiftRight() + { + AnnotationValue lhs = mock( AnnotationValue.class ); + AnnotationValue rhs = mock( AnnotationValue.class ); + + // Longs + when( lhs.accept( visitor ) ).thenReturn( 7L ); + when( rhs.accept( visitor ) ).thenReturn( 2L ); + assertEquals( 7L >>> 2L, visitor.visit( new UnsignedShiftRight( lhs, rhs ) ) ); + + // Integers + when( lhs.accept( visitor ) ).thenReturn( 7 ); + when( rhs.accept( visitor ) ).thenReturn( 2 ); + assertEquals( 7 >>> 2, visitor.visit( new UnsignedShiftRight( lhs, rhs ) ) ); + + // Objects + when( lhs.accept( visitor ) ).thenReturn( new Object() ); + when( rhs.accept( visitor ) ).thenReturn( new Object() ); + try + { + visitor.visit( new UnsignedShiftRight( lhs, rhs ) ); + fail( "Bitwise and bit shift operations can only be performed on integral types" ); + } + catch ( IllegalArgumentException iae ) + { + } + } + + private class EvaluatingVisitorStub extends EvaluatingVisitor { + + @Override + protected Object getFieldReferenceValue( JavaField javaField ) + { + return null; + } + } }
To unsubscribe from this list please visit:
