更新了isVanityURL方法。请参阅以下有关Shahid建议的原始问题和代码。另请查看assylias建议的Path类。
public static boolean isVanityPath(String resourcePath) {
String resPath = resourcePath;
if (resPath == null) {
return false;
} else {
resPath = resPath.trim();
if (!resPath.equalsIgnoreCase(StringPool.BLANK)) {
int len = resPath.length();
String startChar = resPath.substring(0, 1);
if (startChar.equals(StringPool.FORWARD_SLASH) && len > 1) {
resPath = resPath.substring(1, len--);
}else{
return false;
}
int lastIndexOfSlash = resPath.lastIndexOf(StringPool.FORWARD_SLASH);
int slashIndex = resPath.indexOf(StringPool.FORWARD_SLASH);
if (slashIndex != -1)
return slashIndex == lastIndexOfSlash && lastIndexOfSlash == len - 1;
else
return true;
} else {
return false;
}
}
这是更新的JUnit测试
@Before
public void setUp() {
vu = Mockito.mock(ResourcePathUtil.class);
}
@Test
public void testVanityURLWhenRoot() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/"));
}
@Test
public void testVanityURLWhenNull() {
Assert.assertFalse(ResourcePathUtil.isVanityPath(null));
}
@Test
public void testVanityURLWhenValidVanity() {
Assert.assertTrue(ResourcePathUtil.isVanityPath("/vanitycode"));
}
@Test
public void testVanityURLWhenValidVanityWithTrailingSlash() {
boolean retValue = ResourcePathUtil.isVanityPath("/vanitycode/");
Assert.assertFalse("Returned True", retValue);
}
@Test
public void testVanityURLWhenInvalidVanityWithTrailingSlash() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/invalidwithslash/"));
}
@Test
public void testVanityURLWhenInvalidVanity() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/justinvalid"));
}
@Test
public void testVanityURLWhenBlank() {
Assert.assertFalse(ResourcePathUtil.isVanityPath(""));
}
我有以下类(ResourcePathUtil)和静态方法。我想用JUnit(URLTest)测试它。然而,一些测试(testVanityURLWhenRoot,testVanityURLWhenValidVanity)似乎没有通过,尽管它应该。关于我做错了什么的指示?
public class ResourcePathUtil {
/**
*
* @param url
* @param data
* @return result
*/
public static boolean isVanityPath(String resourcePath) {
String resPath = resourcePath;
if (resPath == null) {
return false;
} else {
resPath = resPath.trim();
if (!resPath.equalsIgnoreCase(StringPool.BLANK)) {
int len = resPath.length();
String startChar = resPath.substring(0, 1);
if (startChar.equals(StringPool.FORWARD_SLASH)) {
resPath = resPath.substring(1, len--);
}
int lastIndexOfSlash = resPath.lastIndexOf(StringPool.FORWARD_SLASH);
int slashIndex = resPath.indexOf(StringPool.FORWARD_SLASH);
if (slashIndex != -1)
return slashIndex == lastIndexOfSlash && lastIndexOfSlash == len - 1;
else
return true;
} else {
return false;
}
}
}
}
JUnit类位于
之下import junit.framework.Assert;
import org.mockito.Mockito;
import org.junit.Before;
import org.junit.Test;
public class URLTest {
@Before
public void setUp() {
vu = Mockito.mock(ResourcePathUtil.class);
}
@Test
public void testVanityURLWhenRoot() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/"));
}
@Test
public void testVanityURLWhenNull() {
Assert.assertFalse(ResourcePathUtil.isVanityPath(null));
}
@Test
public void testVanityURLWhenValidVanity() {
Assert.assertTrue(!ResourcePathUtil.isVanityPath("/vanitycode"));
}
@Test
public void testVanityURLWhenValidVanityWithTrailingSlash() {
boolean retValue = ResourcePathUtil.isVanityPath("/vanitycode/");
Assert.assertTrue("Returned False", !retValue);
}
@Test
public void testVanityURLWhenInvalidVanityWithTrailingSlash() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/invalidwithslash/"));
}
@Test
public void testVanityURLWhenInvalidVanity() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/justinvalid"));
}
@Test
public void testVanityURLWhenBlank() {
Assert.assertFalse(ResourcePathUtil.isVanityPath(""));
}
}
字符串池类位于
之下public class StringPool {
public static final String BLANK = "";
public static final String FORWARD_SLASH = "/";
}
答案 0 :(得分:0)
@Test
public void testVanityURLWhenRoot() {
// expecting isVanityPath() to return false
Assert.assertFalse(ResourcePathUtil.isVanityPath("/"));
}
@Test
public void testVanityURLWhenValidVanity() {
// expecting isVanityPath() to return false
Assert.assertTrue(!ResourcePathUtil.isVanityPath("/vanitycode"));
}
在testVanityURLWhenRoot
和testVanityURLWhenValidVanity
中,您期待false
。但是你得到了true
。原因在于方法isVanityPath()
。
在这两种情况下, slashIndex
的值为-1 。当slashIndex等于-1时,您将返回true
。这就是为什么在这两种情况下你都会得到true
结果,尽管你期望false
。
if (slashIndex != -1) {
return slashIndex == lastIndexOfSlash && lastIndexOfSlash == len - 1;
} else { // it executes when slashIndex == -1
return true;
}
<强>建议:强>
而不是:
Assert.assertTrue(!ResourcePathUtil.isVanityPath("/vanitycode"));
写:
Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode"));
后者更具可读性。