@Test(expected = IllegalArgumentException.class) public void testPutShouldThrowAnExceptionWhenKeyIsNull() { // given String testKey = null; String testValue = "test value"; String testInputString = "Sample text {" + testKey + "} rest of the sentence."; // when Kirai.from(testInputString).put(testKey, testValue).format(); // then throw an exception }
public static Kirai from(String string) { return new Kirai(string); }
private Kirai(String string) { Preconditions.checkNotEmpty(string, "string is empty"); input = string; if (!isInputBalanced()) { throw new IllegalArgumentException("Braces in provided string are not balanced"); } if (!areTagsValid()) { throw new IllegalArgumentException( "Tags have to start from lower case letter and can contain " + "only lower case letters [a-z] numbers [0-9] and underscore [_]"); } }
@Test(expected = IllegalArgumentException.class) public void testShouldThrowExceptionWhenPieceIsNull() { // given Piece nullPiece = null; String testTagOne = "tag_one"; String testInputString = "Sample text {" + testTagOne + "} rest of the sentence."; // when Kirai.from(testInputString).put(nullPiece); // then // throw an exception }
@Test(expected = IllegalArgumentException.class) public void testFromShouldThrowExceptionWhenKeyStartsFromNumber() { // given String incorrectTestTag = "1_incorrect_tag"; String testInputString = "Sample text {" + incorrectTestTag + "} rest of the sentence."; // when Kirai.from(testInputString); // then // throw an exception }
@Test public void testFormatterShouldFormatInput() { // given // formatter below is created for test purposes and it can do anything Formatter upperCaseTestFormatter = new Formatter() { @Override public CharSequence format(String input) { return input.toString().toUpperCase(); } }; String input = "value"; String expectedOutput = "VALUE"; // when CharSequence output = Kirai.from(input).format(upperCaseTestFormatter); // then assertThat(output).isEqualTo(expectedOutput); } }
/** * Returns CharSequence formatted with custom formatter implementation * * @param formatter implementation * @return formatted CharSequence */ public CharSequence format(Formatter formatter) { CharSequence input = format(); return formatter.format(String.valueOf(input)); }
@SuppressWarnings("PMD") private boolean areTagsValid() { tags = getTags(input); for (String tag : tags) { if (tag.matches(REGEX_VALID_TAG)) continue; return false; } return true; }
@Test(expected = IllegalArgumentException.class) public void testPutShouldThrowExceptionWhenKeyIsNotDefinedInInputString() { // given String presentTestTag = "test_tag_present"; String notPresentTestTag = "test_tag_NOT_present"; String testValue = "test value"; String testInputString = "Sample text {" + presentTestTag + "} rest of the sentence."; // when Kirai.from(testInputString).put(notPresentTestTag, testValue); // then throw an exception }
@Test(expected = IllegalArgumentException.class) public void testFromShouldThrowExceptionWhenKeyContainsSpecialCharacters() { // given String incorrectTestTag = "#!@ tag &^*_"; String testInputString = "Sample text {" + incorrectTestTag + "} rest of the sentence."; // when Kirai.from(testInputString); // then // throw an exception }
@Test(expected = IllegalArgumentException.class) public void testPutShouldThrowAnExceptionWhenKeyIsAnEmptyString() { // given String testKey = ""; String testValue = "test value"; String testInputString = "Sample text {" + testKey + "} rest of the sentence."; // when Kirai.from(testInputString).put(testKey, testValue).format(); // then throw an exception }
@Test(expected = IllegalArgumentException.class) public void testShouldThrowExceptionWhenKeyAndValueAreNull() { // given String testTagOne = "tag_one"; String testTagTwo = "tag_two"; String testBalancedString = "Sample text {" + testTagOne + "} and {" + testTagTwo + "} rest of the sentence."; String nullTestKey = null; String nullTestValue = null; // when Kirai.from(testBalancedString).put(nullTestKey, nullTestValue); // then // throw an exception }
@Test(expected = IllegalArgumentException.class) public void testShouldThrowExceptionWhenStringHasNoStartingBrace() { // given String testTag = "tag_one"; String testNotBalancedString = testTag + "}"; // when Kirai.from(testNotBalancedString); // then // throw an exception }
@Test(expected = IllegalArgumentException.class) public void testPutShouldThrowAnExceptionWhenValueIsAnEmptyString() { String testKey = "testKey"; String testValue = ""; String testInputString = "Sample text {" + testKey + "} rest of the sentence."; // when Kirai.from(testInputString).put(testKey, testValue).format(); // then throw an exception }
@Test(expected = IllegalArgumentException.class) public void testShouldThrowExceptionWhenKeyAndValueAreEmpty() { // given String testTagOne = "tag_one"; String testTagTwo = "tag_two"; String testBalancedString = "Sample text {" + testTagOne + "} and {" + testTagTwo + "} rest of the sentence."; String emptyTestKey = ""; String emptyTestValue = ""; // when Kirai.from(testBalancedString).put(emptyTestKey, emptyTestValue); // then // throw an exception }
@Test(expected = IllegalArgumentException.class) public void testShouldThrowExceptionWhenStringHasNoEndingBrace() { // given String testTag = "tag_one"; String testNotBalancedString = "{" + testTag; // when Kirai.from(testNotBalancedString); // then // throw an exception }
@Test(expected = IllegalArgumentException.class) public void testPutShouldThrowAnExceptionWhenValueIsNull() { String testKey = "testKey"; String testValue = null; String testInputString = "Sample text {" + testKey + "} rest of the sentence."; // when Kirai.from(testInputString).put(testKey, testValue).format(); // then throw an exception }
@Test(expected = IllegalArgumentException.class) public void testShouldThrowExceptionWhenPieceHasKeyNotDefinedInInputString() { // given String testValue = "test value"; String notPresentTestTag = "test_tag_NOT_present"; Piece testPiece = HtmlPiece.put(notPresentTestTag, testValue); String testTagOne = "tag_one"; String testInputString = "Sample text {" + testTagOne + "} rest of the sentence."; // when Kirai.from(testInputString).put(testPiece); // then // throw an exception }
@Test(expected = IllegalArgumentException.class) public void testShouldThrowExceptionWhenStringIsNotBeBalanced() { // given String testTagOne = "tag_one"; String testTagTwo = "tag_two"; String testNotBalancedString = "Sample text } {{ {" + testTagOne + "} and {" + testTagTwo + "} { rest of the sentence."; // when Kirai.from(testNotBalancedString); // then // throw an exception }
@Test public void testPutShouldAddKeyAndValue() { // given String testKey = "test_tag_present"; String testValue = "test value"; String testInputString = "Sample text {" + testKey + "} rest of the sentence."; String expectedOutPutString = "Sample text " + testValue + " rest of the sentence."; // when CharSequence generatedCharSequence = Kirai.from(testInputString).put(testKey, testValue).format(); // then assertThat(generatedCharSequence).isEqualTo(expectedOutPutString); }