001/** 002 * Powerunit - A JDK1.8 test framework 003 * Copyright (C) 2014 Mathieu Boretti. 004 * 005 * This file is part of Powerunit 006 * 007 * Powerunit is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation, either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * Powerunit is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with Powerunit. If not, see <http://www.gnu.org/licenses/>. 019 */ 020package ch.powerunit; 021 022import java.util.regex.Pattern; 023 024import org.hamcrest.Matchers; 025 026/** 027 * DSL for assertion on string result. 028 * <p> 029 * This interface is returned by the various methods 030 * {@link TestSuite#assertThat(String) assertThat} exposed by {@link TestSuite}. 031 * 032 * @author borettim 033 * 034 */ 035public interface AssertThatString extends AssertThatCastableObject<String> { 036 /** 037 * Check that a string another string. 038 * <p> 039 * This method is a shortcut to <code>is(containsString(substring))</code>. 040 * <br> 041 * <br> 042 * <i>By default, assertion can only be used from the main thread of the 043 * test ; When used from another thread, the assertion will be lost. In the 044 * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test} 045 * annotation is used, the assertion may not be lost, in case the thread use 046 * an assertion method from the test object instance. </i> 047 * 048 * @param substring 049 * the other string. 050 * @return true if the assertion is valid ; If the assertion is false, 051 * depending on {@link Test#fastFail()} : If <code>true</code>, then 052 * fail the test, else, return false and the test will be failed 053 * later. 054 * @see Test#fastFail() The documentation of the <code>fastFail</code> 055 * attribute of the <code>@Test</code> annotation, regarding the action 056 * done by this assertion. 057 */ 058 default boolean containsString(String substring) { 059 return is(Matchers.containsString(substring)); 060 } 061 062 /** 063 * Check that a string starts with another one. 064 * <p> 065 * This method is a shortcut to <code>is(startsWith(prefix))</code>. <br> 066 * <br> 067 * <i>By default, assertion can only be used from the main thread of the 068 * test ; When used from another thread, the assertion will be lost. In the 069 * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test} 070 * annotation is used, the assertion may not be lost, in case the thread use 071 * an assertion method from the test object instance. </i> 072 * 073 * @param prefix 074 * the prefix. 075 * @return true if the assertion is valid ; If the assertion is false, 076 * depending on {@link Test#fastFail()} : If <code>true</code>, then 077 * fail the test, else, return false and the test will be failed 078 * later. 079 * @see Test#fastFail() The documentation of the <code>fastFail</code> 080 * attribute of the <code>@Test</code> annotation, regarding the action 081 * done by this assertion. 082 */ 083 default boolean startsWith(String prefix) { 084 return is(Matchers.startsWith(prefix)); 085 } 086 087 /** 088 * Check that a string ends with another one. 089 * <p> 090 * This method is a shortcut to <code>is(endsWith(prefix))</code>. <br> 091 * <br> 092 * <i>By default, assertion can only be used from the main thread of the 093 * test ; When used from another thread, the assertion will be lost. In the 094 * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test} 095 * annotation is used, the assertion may not be lost, in case the thread use 096 * an assertion method from the test object instance. </i> 097 * 098 * @param prefix 099 * the prefix. 100 * @return true if the assertion is valid ; If the assertion is false, 101 * depending on {@link Test#fastFail()} : If <code>true</code>, then 102 * fail the test, else, return false and the test will be failed 103 * later. 104 * @see Test#fastFail() The documentation of the <code>fastFail</code> 105 * attribute of the <code>@Test</code> annotation, regarding the action 106 * done by this assertion. 107 */ 108 default boolean endsWith(String prefix) { 109 return is(Matchers.endsWith(prefix)); 110 } 111 112 /** 113 * Validate a string with a {@link java.util.regex.Pattern}. 114 * <p> 115 * This method is a shortcut to <code>is(matchesRegex(pattern))</code>. <br> 116 * <br> 117 * <i>By default, assertion can only be used from the main thread of the 118 * test ; When used from another thread, the assertion will be lost. In the 119 * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test} 120 * annotation is used, the assertion may not be lost, in case the thread use 121 * an assertion method from the test object instance. </i> 122 * 123 * @param pattern 124 * the pattern to be used. 125 * @return true if the assertion is valid ; If the assertion is false, 126 * depending on {@link Test#fastFail()} : If <code>true</code>, then 127 * fail the test, else, return false and the test will be failed 128 * later. 129 * @see Test#fastFail() The documentation of the <code>fastFail</code> 130 * attribute of the <code>@Test</code> annotation, regarding the action 131 * done by this assertion. 132 */ 133 default boolean matchesRegex(Pattern pattern) { 134 return is(TestSuite.DSL.matchesRegex(pattern)); 135 } 136 137 /** 138 * Validate a string with a regex. 139 * <p> 140 * This method is a shortcut to <code>is(matchesRegex(regex))</code>. <br> 141 * <br> 142 * <i>By default, assertion can only be used from the main thread of the 143 * test ; When used from another thread, the assertion will be lost. In the 144 * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test} 145 * annotation is used, the assertion may not be lost, in case the thread use 146 * an assertion method from the test object instance. </i> 147 * 148 * 149 * @param regex 150 * The regex to be used for the validation. 151 * @return true if the assertion is valid ; If the assertion is false, 152 * depending on {@link Test#fastFail()} : If <code>true</code>, then 153 * fail the test, else, return false and the test will be failed 154 * later. 155 * @see Test#fastFail() The documentation of the <code>fastFail</code> 156 * attribute of the <code>@Test</code> annotation, regarding the action 157 * done by this assertion. 158 */ 159 default boolean matchesRegex(String regex) { 160 return is(TestSuite.DSL.matchesRegex(regex)); 161 } 162}