Unittest and javadoc.

This commit is contained in:
Daniel Bevenius 2009-06-13 11:30:02 +00:00
parent 666f57a35e
commit 40bf0a63f1
2 changed files with 120 additions and 1 deletions

View File

@ -23,12 +23,28 @@
package org.jboss.netty.util.internal;
/**
* String utility class.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*/
public class StringUtil {
private StringUtil() {
}
/**
* Strip an Object of it's ISO control characters.
*
* @param value
* The Object that should be stripped. This objects toString method will
* called and the result passed to {@link #stripControlCharacters(String)}.
* @return {@code String}
* A new String instance with its hexadecimal control characters replaced
* by a space. Or the unmodified String if it does not contain any ISO
* controll characters.
*/
public static String stripControlCharacters(Object value) {
if (value == null) {
return null;
@ -37,6 +53,16 @@ public class StringUtil {
return stripControlCharacters(value.toString());
}
/**
* Strip a String of it's ISO control characters.
*
* @param value
* The String that should be stripped.
* @return {@code String}
* A new String instance with its hexadecimal control characters replaced
* by a space. Or the unmodified String if it does not contain any ISO
* controll characters.
*/
public static String stripControlCharacters(String value) {
if (value == null) {
return null;

View File

@ -0,0 +1,93 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.util.internal;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit test for {@link StringUtil}.
* <p/>
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Daniel Bevenius (dbevenius@jboss.com)
* @version $Rev$, $Date$
*/
public class StringUtilTest {
@Test
public void stripControlCharactersObjectNull() {
assertNull(StringUtil.stripControlCharacters(null));
}
@Test
public void stripControlCharactersNull() {
assertNull(StringUtil.stripControlCharacters((String) null));
}
@Test
public void stripControlCharactersRightTrim() {
final char controlCode = 0x0000;
final Object object = "abbb" + controlCode;
assertEquals(5, ((String) object).length());
final String stripped = StringUtil.stripControlCharacters(object);
assertFalse(object.equals(stripped));
assertEquals(4, stripped.length());
}
@Test
public void stripControlCharactersLeftTrim() {
final char controlCode = 0x0000;
final String string = controlCode + "abbb";
assertEquals(5, string.length());
final String stripped = StringUtil.stripControlCharacters(string);
assertFalse(string.equals(stripped));
assertEquals(4, stripped.length());
}
@Test
public void stripControlCharacters() {
for (char i = 0x0000; i <= 0x001F; i ++) {
assertStripped(i);
}
for (char i = 0x007F; i <= 0x009F; i ++) {
assertStripped(i);
}
}
private void assertStripped(final char controlCode) {
final Object object = "aaa" + controlCode + "bbb";
final String stripped = StringUtil.stripControlCharacters(object);
assertEquals("aaa bbb", stripped);
}
@Test
public void stripNonControlCharacter() {
final char controlCode = 0x002F;
final String string = controlCode + "abbb";
final String stripped = StringUtil.stripControlCharacters(string);
assertEquals("The string should be unchanged", string, stripped);
}
}