commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 37690] - RandomStringUtils.random() family of methods create invalid unicode sequences
Date Mon, 05 Dec 2005 20:35:22 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=37690>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=37690





------- Additional Comments From stefanhoehne@fastmail.fm  2005-12-05 21:35 -------
Sorry, have never added a unit test patch. 

Here is a junit test case showing the problem. 

	/**
	 * Creates a human readable representation of any unicode character.
	 * @param c - a unicode character
	 * @return human readable representation for c
	 */
	static public String makeStringFromChar(char c)
	{
		// dirty for getting the char code from a character
		return Integer.toHexString((new Character(c)).hashCode());
	}

	/**
	 * Checks if the string got by {@link RandomStringUtils#random(int)} 
	 * can be converted to UTF-8 and back without loss.
	 * 
	 * @author stefanhoehne@fastmail.fm
	 * @throws Exception
	 */
	public void testRandom() throws Exception
	{
		final int size = 5000;
		final String encoding = "UTF-8";
		final String orig = RandomStringUtils.random(size);
		final byte[] bytes = orig.getBytes(encoding);
		final String copy = new String(bytes, encoding);

		// for a verbose compare:
		for (int i=0; i<orig.length() && i<copy.length(); ++i)
		{
			char o = orig.charAt(i);
			char c = copy.charAt(i);
			assertEquals("differs at " + i + "(" + makeStringFromChar(o) + "," +
makeStringFromChar(c) + ")", o, c);
		}
		// compare length also
		assertEquals(orig.length(), copy.length());
		// just to be complete
		assertEquals(orig, copy);
	}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message