001    /*
002     * Copyright (c) 2007-2015 Concurrent, Inc. All Rights Reserved.
003     *
004     * Project and contact information: http://www.cascading.org/
005     *
006     * This file is part of the Cascading project.
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     *     http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing, software
015     * distributed under the License is distributed on an "AS IS" BASIS,
016     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017     * See the License for the specific language governing permissions and
018     * limitations under the License.
019     */
020    
021    package cascading.operation.assertion;
022    
023    import java.beans.ConstructorProperties;
024    import java.util.Collection;
025    
026    import cascading.flow.FlowProcess;
027    import cascading.management.annotation.Property;
028    import cascading.management.annotation.PropertyDescription;
029    import cascading.management.annotation.Visibility;
030    import cascading.operation.ValueAssertion;
031    import cascading.operation.ValueAssertionCall;
032    import cascading.tuple.Tuple;
033    import cascading.tuple.Tuples;
034    
035    /**
036     * Class AssertEquals asserts the number of constructor values is equal
037     * to the number of argument values {@link Tuple} and each constructor value is {@code .equals()} to its corresponding argument value.
038     */
039    public class AssertEquals extends BaseAssertion implements ValueAssertion
040      {
041      /** Field values */
042      private Tuple values;
043    
044      /**
045       * Constructor AssertEquals creates a new AssertEquals instance.
046       *
047       * @param values of type Object...
048       */
049      @ConstructorProperties({"values"})
050      public AssertEquals( Object... values )
051        {
052        // set to 1 if null, will fail immediately afterwards
053        super( values == null ? 1 : values.length, "argument tuple: %s was not equal to values: %s" );
054    
055        if( values == null )
056          throw new IllegalArgumentException( "values may not be null" );
057    
058        if( values.length == 0 )
059          throw new IllegalArgumentException( "values may not be empty" );
060    
061        this.values = new Tuple( values );
062        }
063    
064      @Property(name = "values", visibility = Visibility.PRIVATE)
065      @PropertyDescription("The expected values.")
066      public Collection getValues()
067        {
068        return Tuples.asCollection( values );
069        }
070    
071      @Override
072      public void doAssert( FlowProcess flowProcess, ValueAssertionCall assertionCall )
073        {
074        Tuple tuple = assertionCall.getArguments().getTuple();
075    
076        if( !tuple.equals( values ) )
077          fail( tuple.print(), values.print() );
078        }
079    
080      @Override
081      public boolean equals( Object object )
082        {
083        if( this == object )
084          return true;
085        if( !( object instanceof AssertEquals ) )
086          return false;
087        if( !super.equals( object ) )
088          return false;
089    
090        AssertEquals that = (AssertEquals) object;
091    
092        if( values != null ? !values.equals( that.values ) : that.values != null )
093          return false;
094    
095        return true;
096        }
097    
098      @Override
099      public int hashCode()
100        {
101        int result = super.hashCode();
102        result = 31 * result + ( values != null ? values.hashCode() : 0 );
103        return result;
104        }
105      }