SubstringFunction implements Function { public String execute(List<String> parameterList, TestContext context) { if (parameterList == null || parameterList.size() < 2) { throw new InvalidFunctionUsageException("Insufficient function parameters - parameter usage: (targetString, beginIndex, [endIndex])"); } String targetString = parameterList.get(0); String beginIndex = parameterList.get(1); String endIndex = null; if (!StringUtils.hasText(beginIndex)) { throw new InvalidFunctionUsageException("Invalid beginIndex - please check function parameters"); } if (parameterList.size() > 2) { endIndex = parameterList.get(2); } if (StringUtils.hasText(endIndex)) { targetString = targetString.substring(Integer.valueOf(beginIndex), Integer.valueOf(endIndex)); } else { targetString = targetString.substring(Integer.valueOf(beginIndex)); } return targetString; }  String execute(List<String> parameterList, TestContext context);  }
@Test public void testFunction() { List<String> params = new ArrayList<String>(); params.add("Hallo,TestFramework"); params.add("6"); Assert.assertEquals(function.execute(params, context), "TestFramework"); params.clear(); params.add("This is a test"); params.add("0"); Assert.assertEquals(function.execute(params, context), "This is a test"); }
@Test public void testEndIndex() { List<String> params = new ArrayList<String>(); params.add("Hallo,TestFramework"); params.add("6"); params.add("10"); Assert.assertEquals(function.execute(params, context), "Test"); params.clear(); params.add("This is a test"); params.add("0"); params.add("4"); Assert.assertEquals(function.execute(params, context), "This"); }
@Test(expectedExceptions = {StringIndexOutOfBoundsException.class}) public void testIndexOutOfBounds() { List<String> params = new ArrayList<String>(); params.add("Test"); params.add("-1"); function.execute(params, context); }
@Test(expectedExceptions = {InvalidFunctionUsageException.class}) public void testMissingBeginIndex() { function.execute(Collections.singletonList("This is a test"), context); }
@Test(expectedExceptions = {NumberFormatException.class}) public void testNotANumber() { List<String> params = new ArrayList<String>(); params.add("Hallo,TestFramework"); params.add("one"); function.execute(params, context); }
@Test(expectedExceptions = {InvalidFunctionUsageException.class}) public void testNoParameters() { function.execute(Collections.<String>emptyList(), context); }