Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exceptions usage #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions src/main/java/net/spy/memcached/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,7 @@ public final class StringUtils {
* Maximum supported key length.
*/
private static final int MAX_KEY_LENGTH = MemcachedClientIF.MAX_KEY_LENGTH;

/**
* Exception thrown if the input key is too long.
*/
private static final IllegalArgumentException KEY_TOO_LONG_EXCEPTION =
new IllegalArgumentException("Key is too long (maxlen = "
+ MAX_KEY_LENGTH + ')');

/**
* Exception thrown if the input key is empty.
*/
private static final IllegalArgumentException KEY_EMPTY_EXCEPTION =
new IllegalArgumentException("Key must contain at least one character.");

/**
* Preset the stack traces for the static exceptions.
*/
static {
KEY_TOO_LONG_EXCEPTION.setStackTrace(new StackTraceElement[0]);
KEY_EMPTY_EXCEPTION.setStackTrace(new StackTraceElement[0]);
}


/**
* Private constructor, since this is a purely static class.
*/
Expand Down Expand Up @@ -118,20 +97,23 @@ public static boolean isJsonObject(final String s) {
*
* @param key the key to check.
* @param binary if binary protocol is used.
*
* @throws IllegalArgumentException if the input key is too long
* @throws IllegalArgumentException Exception thrown if the input key is empty
*/
public static void validateKey(final String key, final boolean binary) {
byte[] keyBytes = KeyUtil.getKeyBytes(key);
int keyLength = keyBytes.length;

if (keyLength > MAX_KEY_LENGTH) {
throw KEY_TOO_LONG_EXCEPTION;
throw new IllegalArgumentException("Key is too long (maxlen = " + MAX_KEY_LENGTH + ')');
}

if (keyLength == 0) {
throw KEY_EMPTY_EXCEPTION;
throw new IllegalArgumentException("Key must contain at least one character.");
}

if(!binary) {
if (!binary) {
for (byte b : keyBytes) {
if (b == ' ' || b == '\n' || b == '\r' || b == 0) {
throw new IllegalArgumentException(
Expand Down
1 change: 0 additions & 1 deletion src/test/java/net/spy/memcached/util/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public void shouldWorkWithEmptyOrNullStrings() {
@Test
public void shouldValidateAsciiKey() {
StringUtils.validateKey("mykey1234", false);
assertTrue(true);
}

@Test(expected = IllegalArgumentException.class)
Expand Down