001/* 002 * #%L 003 * Netarchivesuite - common - test 004 * %% 005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023 024package dk.netarkivet.testutils; 025 026import java.util.ArrayList; 027import java.util.List; 028import java.util.regex.Pattern; 029 030import junit.framework.TestCase; 031 032/** 033 * More complex asserts for strings 034 */ 035 036public class StringAsserts { 037 /** 038 * Assert that one string contains another. 039 * 040 * @param msg the error message to show in case of failure 041 * @param toFind The string to find. 042 * @param toCheck The string to look in 043 */ 044 public static void assertStringContains(String msg, String toFind, String toCheck) { 045 if (!toCheck.contains(toFind)) { 046 TestCase.fail(msg + ": String '" + toFind + "' not in target:\n" + toCheck); 047 } 048 } 049 050 /** 051 * Asserts that one string contains a set of substrings 052 * 053 * @param msg the error message to show in case of failure 054 * @param toCheck The string to look in 055 * @param contents the strings to look for 056 */ 057 public static void assertStringContains(String msg, String toCheck, String... contents) { 058 List<String> failures = new ArrayList<String>(); 059 for (String content : contents) { 060 if (!toCheck.contains(content)) { 061 failures.add(content); 062 } 063 } 064 if (!failures.isEmpty()) { 065 StringBuffer message = new StringBuffer("'" + toCheck + "' should contain "); 066 for (String failure : failures) { 067 message.append("'" + failure + "'"); 068 } 069 message.append(" but doesn't"); 070 TestCase.fail(msg + ": " + message); 071 } 072 } 073 074 /** 075 * Assert that a string matches a regular expression. 076 * 077 * @param msg the error message to show in case of failure 078 * @param toFind The string to find 079 * @param toCheck The string to look in 080 */ 081 public static void assertStringMatches(String msg, String toFind, String toCheck) { 082 if (!Pattern.compile(toFind, Pattern.MULTILINE | Pattern.DOTALL).matcher(toCheck).find()) { 083 TestCase.fail(msg + ": Regex '" + toFind + "' not in target:\n" + toCheck); 084 } 085 } 086 087 /** 088 * Assert that one string doesn not contain another. 089 * 090 * @param msg the error message to show in case of failure 091 * @param toFind The string to find. 092 * @param toCheck The string to look in 093 */ 094 public static void assertStringNotContains(String msg, String toFind, String toCheck) { 095 if (toCheck.contains(toFind)) { 096 TestCase.fail(msg + ": String '" + toFind + "' in target:\n" + toCheck); 097 } 098 099 } 100 101}