001/* 002 * Copyright (c) 2021, The Dattack team (http://www.dattack.com) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.dattack.junit; 017 018import java.util.Objects; 019import java.util.regex.Pattern; 020 021/** 022 * Utility methods that extend the functionality of the Assertions class. 023 * 024 * @author cvarela 025 * @since 0.1 026 */ 027public class AssertionsExt extends org.junit.jupiter.api.Assertions { 028 029 protected AssertionsExt() { 030 super(); 031 } 032 033 public static void assertContains(final String text, final String substring) { 034 assertContains(text, substring, false); 035 } 036 037 private static void assertContains(final String text, final String substring, final boolean ignoreCase) { 038 assertTrue(checkContains(text, substring, ignoreCase), 039 String.format("'%s' doesn't contains the substring '%s'", text, substring)); 040 } 041 042 private static boolean checkContains(final String text, final String substring, final boolean ignoreCase) { 043 assertTrue(Objects.nonNull(text), "The text to be searched can't be null"); 044 assertTrue(Objects.nonNull(substring), "The substring to searching for can't be null"); 045 return ignoreCase ? Pattern.compile(Pattern.quote(substring), Pattern.CASE_INSENSITIVE).matcher(text).find() 046 : text.contains(substring); 047 } 048 049 public static void assertContainsIgnoreCase(final String text, final String substring) { 050 assertContains(text, substring, true); 051 } 052 053 public static void assertNotContains(final String text, final String substring) { 054 assertNotContains(text, substring, false); 055 } 056 057 private static void assertNotContains(final String text, final String substring, final boolean ignoreCase) { 058 assertFalse(checkContains(text, substring, ignoreCase), 059 String.format("'%s' contains the substring '%s'", text, substring)); 060 } 061 062 public static void assertNotContainsIgnoreCase(final String text, final String substring) { 063 assertNotContains(text, substring, true); 064 } 065}