Popular posts
Schedule Jenkins build periodically How to configure Jenkins build schedule... Jenkins schedule format
Spring Boot + TestNG. How to get started Minimal required code...
Open Source
How to publish Open Source library Step by step guide
Recent posts
How to render a random image Step by step instructions on how to render a random image on HTML refresh for static sites
Log all client requests and responses - Apache HTTP client, RESTEasy
Spring RestTemplate - log all client requests and responses
Projects
Free forms for sites
Guide
import com.citrix.mstest.Application; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; @SpringApplicationConfiguration(Application.class) public class Some1Test extends AbstractTestNGSpringContextTests { @DataProvider public Object[][] replaceAllParameters() { return new Object[][]{ {"hello Eric Erik", "E", "H", "hello Hric Hrik"}, {"hello Eric Erik", "Eric", "Erik", "hello Erik Erik"}, // oldStr is at the end {"hello Eric Erik", "Erik", "Eric", "hello Eric Eric"}, // oldStr is at the begining {"hello Eric Erik", "hello", "Hello", "Hello Eric Erik"}, // No oldStr in sourceStr {"hello Eric Erik", "H", "E", "hello Eric Erik"}, // Length of oldStr is more than length of sourceStr {"hello Eric Erik", "hello Eric Erik!", "Hello", "hello Eric Erik"}, // "" cases {"hello Eric Erik", "", "H", "hello Eric Erik"}, {"hello Eric Erik", "", "", "hello Eric Erik"}, {"hello Eric Erik", "Eric", "", "hello Erik"}, {"", "Eric", "Joe", ""}, // No changes if there is null {"hello Eric Erik", null, "Erik", "hello Eric Erik"}, {"hello Eric Erik", "hello", null, "hello Eric Erik"}, {null, "hello", "bye", null}, {null, null, null, null} }; } @Test(dataProvider = "replaceAllParameters") public void replaceAllTest(String sourceStr, String oldStr, String newStr, String finalStr) { assertEquals(replaceAll(sourceStr, oldStr, newStr), finalStr, "Unexpected result: "); } public String replaceAll(String sourceStr, String oldStr, String newStr) { if (sourceStr == null || oldStr == null || newStr == null) return sourceStr; if (oldStr.length() == 0) return sourceStr; if (sourceStr.length() < oldStr.length()) return sourceStr; String result = ""; int i = 0; while (i < sourceStr.length()) { if (foundStr(sourceStr, i, oldStr)) { result = result + newStr; i = i + oldStr.length(); } else { result = result + sourceStr.charAt(i); i++; } } return result; } private boolean foundStr(String source, int i, String str) { if (source.length() - i - str.length() < 0) return false; for (int j = 0; j < str.length(); j++) { if (str.charAt(j) != source.charAt(i + j)) return false; } return true; } }