Die isVanityURL-Methode aktualisiert. Siehe unten für die ursprüngliche Frage und den Code basierend auf Shahids Empfehlung. Sehen Sie sich auch die Pfadklasse an, wie von assylias vorgeschlagen.Junit nicht übergeben, obwohl es sollte
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;
}
}
Dies ist der aktualisierte JUnit-Test
@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(""));
}
Ich habe die folgende Klasse (ResourcePathUtil) mit einer statischen Methode. Ich möchte es mit einem JUnit (URLTest) testen. Einige der Tests (testVanityURLWhenRoot, testVanityURLWhenValidVanity) scheinen jedoch nicht bestanden zu werden, obwohl dies der Fall sein sollte. Irgendwelche Hinweise darauf, was ich falsch mache?
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;
}
}
}
}
Die JUnit-Klasse ist unter
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(""));
}
}
String Pool Klasse unter
public class StringPool {
public static final String BLANK = "";
public static final String FORWARD_SLASH = "/";
}
Warum verwenden Sie nicht die 'Path' Klasse? – assylias
Welcher Testfall schlägt fehl? Alle von ihnen? – nasukkin
@assylias, meinst du [dies] (https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html) – Pragrammer