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.regex.Matcher;
025    
026    import cascading.flow.FlowProcess;
027    import cascading.operation.AssertionLevel;
028    import cascading.operation.PlannerLevel;
029    import cascading.operation.ValueAssertion;
030    import cascading.operation.ValueAssertionCall;
031    import cascading.operation.regex.RegexMatcher;
032    import cascading.tuple.Tuple;
033    import cascading.tuple.TupleEntry;
034    
035    /**
036     * Class AssertMatches matches the given regular expression patternString against the whole argument
037     * {@link cascading.tuple.Tuple} by joining each individual element of the Tuple with a tab character (\t).
038     * See {@link AssertMatchesAll} if you need to match the patternString regex against each individual tuple element.
039     * <p/>
040     * This operation uses {@link java.util.regex.Matcher} internally, specifically the method {@link java.util.regex.Matcher#find()}.
041     * <p/>
042     * Note a {@code null} valued argument passed to the parser will be converted to an empty string ({@code ""}) before
043     * the regex is applied.
044     * <p/>
045     * Any Object value will be coerced to a String type via its {@code toString()} method.
046     *
047     * @see java.util.regex.Matcher
048     * @see java.util.regex.Pattern
049     */
050    public class AssertMatches extends RegexMatcher implements ValueAssertion<Matcher>
051      {
052      /** Field message */
053      private final static String message = "argument tuple: %s did not match: %s";
054    
055      /**
056       * Constructor AssertMatches creates a new AssertMatches instance.
057       *
058       * @param patternString of type String
059       */
060      @ConstructorProperties({"patternString"})
061      public AssertMatches( String patternString )
062        {
063        super( patternString, false );
064        }
065    
066      /**
067       * Constructor AssertMatches creates a new AssertMatches instance.
068       *
069       * @param patternString of type String
070       * @param negateMatch   of type boolean
071       */
072      @ConstructorProperties({"patternString", "negateMatch"})
073      public AssertMatches( String patternString, boolean negateMatch )
074        {
075        super( patternString, negateMatch );
076        }
077    
078      @Override
079      public boolean supportsPlannerLevel( PlannerLevel plannerLevel )
080        {
081        return plannerLevel instanceof AssertionLevel;
082        }
083    
084      @Override
085      public void doAssert( FlowProcess flowProcess, ValueAssertionCall<Matcher> assertionCall )
086        {
087        TupleEntry tupleEntry = assertionCall.getArguments();
088    
089        if( matchWholeTuple( assertionCall.getContext(), tupleEntry ) )
090          BaseAssertion.throwFail( message, tupleEntry.getTuple().print(), patternString );
091        }
092      }