Skip to content

Commit

Permalink
#978 extract duplicated code
Browse files Browse the repository at this point in the history
 add StringUtils.isEmpty()
 add StringUtils.splitAndTrim()
  • Loading branch information
emeroad committed Sep 18, 2015
1 parent 666d006 commit 5335cc5
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 177 deletions.
@@ -1,7 +1,10 @@
package com.navercorp.pinpoint.bootstrap.config;

import com.navercorp.pinpoint.bootstrap.util.StringUtils;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ExcludeMethodFilter implements Filter<String> {
Expand All @@ -12,31 +15,19 @@ public ExcludeMethodFilter(String excludeFormat) {
}

public ExcludeMethodFilter(String excludeFormat, String separator) {
if (excludeFormat == null || excludeFormat.isEmpty()) {
if (StringUtils.isEmpty(separator)) {
this.excludeMethods = Collections.emptySet();
return;
}

final String[] split = excludeFormat.split(separator);
final List<String> splitList = StringUtils.splitAndTrim(excludeFormat, separator);
this.excludeMethods = new HashSet<String>();
for (String method : split) {
if (isEmpty(method)) {
continue;
}
method = method.trim();
if (method.isEmpty()) {
continue;
}
excludeMethods.add(method.toUpperCase());
for (String method : splitList) {
this.excludeMethods.add(method.toUpperCase());
}
}

private boolean isEmpty(String string) {
return string == null || string.isEmpty();
}

@Override
public boolean filter(String value) {
return excludeMethods.contains(value);
public boolean filter(String method) {
return excludeMethods.contains(method);
}
}
Expand Up @@ -19,6 +19,7 @@
import com.navercorp.pinpoint.bootstrap.util.AntPathMatcher;
import com.navercorp.pinpoint.bootstrap.util.PathMatcher;
import com.navercorp.pinpoint.bootstrap.util.EqualsPathMatcher;
import com.navercorp.pinpoint.bootstrap.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -36,20 +37,13 @@ public ExcludeUrlFilter(String excludeFormat) {
}

public ExcludeUrlFilter(String excludeFormat, String separator) {
if (isEmpty(excludeFormat)) {
if (StringUtils.isEmpty(excludeFormat)) {
this.excludeMatcherList = Collections.emptyList();
return;
}
final String[] split = excludeFormat.split(separator);
final List<PathMatcher> buildList = new ArrayList<PathMatcher>();
for (String path : split) {
if (isEmpty(path)) {
continue;
}
path = path.trim();
if (path.isEmpty()) {
continue;
}
final List<String> splitList = StringUtils.splitAndTrim(excludeFormat, separator);
final List<PathMatcher> buildList = new ArrayList<PathMatcher>(splitList.size());
for (String path : splitList) {
final PathMatcher pathMatcher = createPathMatcher(path);
buildList.add(pathMatcher);
}
Expand All @@ -64,10 +58,6 @@ protected PathMatcher createPathMatcher(String pattern) {
return new EqualsPathMatcher(pattern);
}

private boolean isEmpty(String string) {
return string == null || string.isEmpty();
}

@Override
public boolean filter(String requestURI) {
for (PathMatcher excludePathMatcher : this.excludeMatcherList) {
Expand Down
Expand Up @@ -16,6 +16,11 @@

package com.navercorp.pinpoint.bootstrap.util;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class StringUtils {

private StringUtils() {
Expand All @@ -25,13 +30,42 @@ public static String defaultString(final String str, final String defaultStr) {
return str == null ? defaultStr : str;
}

public static boolean isEmpty(String string) {
return string == null || string.isEmpty();
}

public static String toString(final Object object) {
if (object == null) {
return "null";
}
return object.toString();
}

public static List<String> splitAndTrim(String value, String separator) {
if(isEmpty(value)) {
return Collections.emptyList();
}
if (separator == null) {
throw new NullPointerException("separator must not be null");
}
final List<String> result = new ArrayList<String>();
// TODO remove regex 'separator'
final String[] split = value.split(separator);
for (String method : split) {
if (isEmpty(method)) {
continue;
}
method = method.trim();
if (method.isEmpty()) {
continue;
}
result.add(method);
}
return result;
}



public static String drop(final String str) {
return drop(str, 64);
}
Expand Down
Expand Up @@ -19,85 +19,142 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
* @author poap
*/
public class StringUtilsTest {
private String longString = "This is a very long string for testing drop function. Length of this string is more than sixty four.";
private String shortString = "This is ashort string.";

@Test
public void defaultString() {
Assert.assertNull(StringUtils.defaultString(null, null));
Assert.assertEquals(StringUtils.defaultString(null, shortString), shortString);
Assert.assertEquals(StringUtils.defaultString(shortString, null), shortString);
Assert.assertEquals(StringUtils.defaultString(shortString, shortString), shortString);
}

@Test
public void toStringTest() {
int array[] = {0, 1};

Assert.assertEquals(StringUtils.toString(null), "null");
Assert.assertEquals(StringUtils.toString(1), "1");
Assert.assertEquals(StringUtils.toString(1234.567), "1234.567");
Assert.assertEquals(StringUtils.toString(shortString), shortString);
Assert.assertEquals(StringUtils.toString(array), array.toString());
}

@Test
public void drop() {

Assert.assertEquals(StringUtils.drop(null), "null");
Assert.assertEquals(StringUtils.drop(null, 4), "null");
Assert.assertEquals(StringUtils.drop(null, 0), "null");
Assert.assertEquals(StringUtils.drop(null, -4), "null");

Assert.assertEquals(StringUtils.drop(longString), "This is a very long string for testing drop function. Length of ...(100)");
Assert.assertEquals(StringUtils.drop(longString, 4), "This...(100)");
Assert.assertEquals(StringUtils.drop(longString, 0), "...(100)");
try {
StringUtils.drop(longString, -4);
Assert.fail();
}
catch(IllegalArgumentException e) {
}
catch(Exception e) {
Assert.fail();
}

Assert.assertEquals(StringUtils.drop(shortString), shortString);
Assert.assertEquals(StringUtils.drop(shortString, 4), "This...(22)");
Assert.assertEquals(StringUtils.drop(shortString, 0), "...(22)");
try {
StringUtils.drop(shortString, -4);
Assert.fail();
}
catch(IllegalArgumentException e) {
}
catch(Exception e) {
Assert.fail();
}
}

@Test
public void appendDrop() {
StringBuilder buffer = new StringBuilder();

StringUtils.appendDrop(buffer, null, 4);
Assert.assertEquals(buffer.toString(), "");

StringUtils.appendDrop(buffer, null, 0);
Assert.assertEquals(buffer.toString(), "");

StringUtils.appendDrop(buffer, null, -4);
Assert.assertEquals(buffer.toString(), "");

StringUtils.appendDrop(buffer, shortString, 4);
Assert.assertEquals(buffer.toString(), "This...(22)");

StringUtils.appendDrop(buffer, longString, 16);
Assert.assertEquals(buffer.toString(), "This...(22)This is a very l...(100)");
}
private String longString = "This is a very long string for testing drop function. Length of this string is more than sixty four.";
private String shortString = "This is ashort string.";

@Test
public void defaultString() {
Assert.assertNull(StringUtils.defaultString(null, null));
Assert.assertEquals(StringUtils.defaultString(null, shortString), shortString);
Assert.assertEquals(StringUtils.defaultString(shortString, null), shortString);
Assert.assertEquals(StringUtils.defaultString(shortString, shortString), shortString);
}

@Test
public void toStringTest() {
int array[] = {0, 1};

Assert.assertEquals(StringUtils.toString(null), "null");
Assert.assertEquals(StringUtils.toString(1), "1");
Assert.assertEquals(StringUtils.toString(1234.567), "1234.567");
Assert.assertEquals(StringUtils.toString(shortString), shortString);
Assert.assertEquals(StringUtils.toString(array), array.toString());
}

@Test
public void drop() {

Assert.assertEquals(StringUtils.drop(null), "null");
Assert.assertEquals(StringUtils.drop(null, 4), "null");
Assert.assertEquals(StringUtils.drop(null, 0), "null");
Assert.assertEquals(StringUtils.drop(null, -4), "null");

Assert.assertEquals(StringUtils.drop(longString), "This is a very long string for testing drop function. Length of ...(100)");
Assert.assertEquals(StringUtils.drop(longString, 4), "This...(100)");
Assert.assertEquals(StringUtils.drop(longString, 0), "...(100)");
try {
StringUtils.drop(longString, -4);
Assert.fail();
} catch (IllegalArgumentException e) {
} catch (Exception e) {
Assert.fail();
}

Assert.assertEquals(StringUtils.drop(shortString), shortString);
Assert.assertEquals(StringUtils.drop(shortString, 4), "This...(22)");
Assert.assertEquals(StringUtils.drop(shortString, 0), "...(22)");
try {
StringUtils.drop(shortString, -4);
Assert.fail();
} catch (IllegalArgumentException e) {
} catch (Exception e) {
Assert.fail();
}
}

@Test
public void appendDrop() {
StringBuilder buffer = new StringBuilder();

StringUtils.appendDrop(buffer, null, 4);
Assert.assertEquals(buffer.toString(), "");

StringUtils.appendDrop(buffer, null, 0);
Assert.assertEquals(buffer.toString(), "");

StringUtils.appendDrop(buffer, null, -4);
Assert.assertEquals(buffer.toString(), "");

StringUtils.appendDrop(buffer, shortString, 4);
Assert.assertEquals(buffer.toString(), "This...(22)");

StringUtils.appendDrop(buffer, longString, 16);
Assert.assertEquals(buffer.toString(), "This...(22)This is a very l...(100)");
}


@Test
public void testDrop1() throws Exception {
String string = "abc";
String drop = StringUtils.drop(string, 1);
Assert.assertEquals("a...(3)", drop);
}

@Test
public void testDrop2() throws Exception {
String string = "abc";
String drop = StringUtils.drop(string, 5);
Assert.assertEquals("abc", drop);
}

@Test
public void testDrop3() throws Exception {
String string = "abc";
String drop = StringUtils.drop(string, 3);
Assert.assertEquals("abc", drop);
}

@Test
public void testDrop4() throws Exception {
String string = "abc";
String drop = StringUtils.drop(string, 0);
Assert.assertEquals("...(3)", drop);

}

@Test
public void testDropNegative() throws Exception {
String string = "abc";
try {
StringUtils.drop(string, -1);
Assert.fail();
} catch (Exception ignore) {
// skip
}
}

@Test
public void testIsEmpty() {
Assert.assertTrue(StringUtils.isEmpty(""));
Assert.assertTrue(StringUtils.isEmpty(null));

Assert.assertFalse(StringUtils.isEmpty("a"));
}

@Test
public void testSplitAndTrim() throws Exception {

List<String> strings = StringUtils.splitAndTrim("a, b, ,, c", ",");
Assert.assertEquals(Arrays.asList(new String[]{"a", "b", "c"}), strings);

}


}

0 comments on commit 5335cc5

Please sign in to comment.