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