Last active
January 15, 2018 15:06
-
-
Save tamboer/cbbf17fa929ad5ce6b0777ca70e3b76a to your computer and use it in GitHub Desktop.
Common String validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.tvh.assetmanagement.common.validation; | |
| public abstract class StringValidation { | |
| public static String normalize(final String input){ | |
| if(input == null){ | |
| return null; | |
| } | |
| final String str = input.trim(); | |
| final StringBuilder b = new StringBuilder(str.length()); | |
| for (int i = 0; i < str.length(); i++) { | |
| char c = str.charAt(i); | |
| if (Character.isWhitespace(c)) { | |
| if (i > 0 && !Character.isWhitespace(str.charAt(i - 1))) { | |
| b.append(' '); | |
| } | |
| } else { | |
| b.append(c); | |
| } | |
| } | |
| return b.toString(); | |
| } | |
| public static Boolean isEmpty(final String str) { | |
| int strLen; | |
| if(str != null && (strLen = str.length()) != 0) { | |
| for(int i = 0; i < strLen; ++i) { | |
| if(!Character.isWhitespace(str.charAt(i))) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } else { | |
| return true; | |
| } | |
| } | |
| public static String requireNonEmpty(final String str) { | |
| if(isEmpty(str)){ | |
| throw new NullPointerException(); | |
| } | |
| return str; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.tvh.assetmanagement.common.validation; | |
| import org.junit.Rule; | |
| import org.junit.Test; | |
| import org.junit.rules.ExpectedException; | |
| import java.util.HashMap; | |
| import java.util.Iterator; | |
| import java.util.Map; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| public class StringValidationTest { | |
| @Rule | |
| public ExpectedException expectedException = ExpectedException.none(); | |
| @Test | |
| public void isEmpty() throws Exception { | |
| String str = " "; | |
| Boolean aBoolean = StringValidation.isEmpty(str); | |
| assertThat(aBoolean).isTrue(); | |
| } | |
| @Test | |
| public void testIsEmptyWithSetOfStringCases() throws Exception { | |
| /** | |
| isEmpty(null) -> true | |
| isEmpty("") -> true | |
| isEmpty(" ") -> true | |
| isEmpty("text") -> false | |
| isEmpty(" text") -> false | |
| isEmpty("\n") -> true | |
| isEmpty("\r") -> true | |
| */ | |
| HashMap<String,Boolean> tests = new HashMap<>(); | |
| tests.put(null,true); | |
| tests.put("",true); | |
| tests.put(" ",true); | |
| tests.put("text",false); | |
| tests.put(" text",false); | |
| tests.put("\n",true); | |
| tests.put("\r",true); | |
| Iterator iterator = tests.entrySet().iterator(); | |
| while(iterator.hasNext()){ | |
| Map.Entry pair = (Map.Entry)iterator.next(); | |
| if (pair.getKey() == null){ | |
| expectedException.expect(NullPointerException.class); | |
| } | |
| Boolean aBoolean = StringValidation.isEmpty(pair.getKey().toString()); | |
| assertThat(aBoolean).isEqualTo(pair.getValue()); | |
| } | |
| } | |
| @Test | |
| public void requireNonEmpty() throws Exception { | |
| expectedException.expect(NullPointerException.class); | |
| String str = " "; | |
| String result = StringValidation.requireNonEmpty(str); | |
| assertThat(result).isNotNull(); | |
| } | |
| @Test | |
| public void isEmpty(){ | |
| assertThat(StringValidation.isEmpty(null)).isTrue(); | |
| assertThat(StringValidation.isEmpty("")).isTrue(); | |
| assertThat(StringValidation.isEmpty(" ")).isTrue(); | |
| assertThat(StringValidation.isEmpty("text")).isFalse(); | |
| assertThat(StringValidation.isEmpty(" text")).isFalse(); | |
| assertThat(StringValidation.isEmpty("\n")).isTrue(); | |
| assertThat(StringValidation.isEmpty("\r")).isTrue(); | |
| assertThat(StringValidation.isEmpty("\t")).isTrue(); | |
| assertThat(StringValidation.isEmpty("\f")).isTrue(); | |
| } | |
| @Test | |
| public void requireNonEmpty() { | |
| expectedException.expect(NullPointerException.class); | |
| StringValidation.requireNonEmpty( " "); | |
| } | |
| @Test | |
| public void normalizeSpace(){ | |
| assertThat(StringValidation.normalize(" ")).isEqualTo(""); | |
| assertThat(StringValidation.normalize(" a")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("a ")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize(" a ")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize(" a a ")).isEqualTo("a a"); | |
| } | |
| @Test | |
| public void normalizeCarriageReturns(){ | |
| assertThat(StringValidation.normalize("\r")).isEqualTo(""); | |
| assertThat(StringValidation.normalize("\ra")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("a\r")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("\ra\r")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("\r\ra\r\ra\r\r")).isEqualTo("a a"); | |
| } | |
| @Test | |
| public void normalizeTabs(){ | |
| assertThat(StringValidation.normalize("\t")).isEqualTo(""); | |
| assertThat(StringValidation.normalize("\ta")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("a\t")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("\ta\t")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("\t\ta\t\ta\t\t")).isEqualTo("a a"); | |
| } | |
| @Test | |
| public void normalizeFeed(){ | |
| assertThat(StringValidation.normalize("\f")).isEqualTo(""); | |
| assertThat(StringValidation.normalize("\fa")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("a\f")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("\fa\f")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("\f\fa\f\fa\f\f")).isEqualTo("a a"); | |
| } | |
| @Test | |
| public void normalizeNewlines(){ | |
| assertThat(StringValidation.normalize("\n")).isEqualTo(""); | |
| assertThat(StringValidation.normalize("\na")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("a\n")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("\na\n")).isEqualTo("a"); | |
| assertThat(StringValidation.normalize("\n\na\n\na\n\n")).isEqualTo("a a"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment