JUnit testing for this particular method
I'm trying to write some JUnit tests for a Java method that takes a base URL and target URL and returns the target URL relative to the given base URL.
I'm using the category based partition to make my test set. Currently i'm testing to check the following:
- check the two input URL's have the same protocol and host;
- check for when the paths aren't the same and that the relative URL adjusts correctly;
- check for when the base URL is longer than the target URL;
- check for when the target URL is longer than the base URL;
- check for when the base URL and target URL are identical;
I was wondering how other people would test this method using JUnit? Am i missing any criteria?
/**
* This method converts an absolute url to an url relative to a given base-url.
* The algorithm is somewhat chaotic, but it works (Maybe rewrite it).
* Be careful, the method is ".mm"-specific. Something like this should be included
* in the librarys, but I couldn't find it. You can create a new absolute url with
* "new URL(URL context, URL relative)".
*/
public static String toRelativeURL(URL base, URL target) {
// Precondition: If URL is a path to folder, then it must end with '/' character.
if( (base.getProtocol().equals(target.getProtocol())) &&
(base.getHost().equals(target.getHost()))) {
String baseString = base.getFile();
String targetString = target.getFile();
String result = "";
//remove filename from URL
baseString = baseString.substring(0, baseString.lastIndexOf("/")+1);
//remove filename from URL
targetString = targetString.substring(0, targetString.lastIndexOf("/")+1);
StringTokenizer baseTokens = new StringTokenizer(baseString,"/");//Maybe this causes problems under开发者_JAVA百科 windows
StringTokenizer targetTokens = new StringTokenizer(targetString,"/");//Maybe this causes problems under windows
String nextBaseToken = "", nextTargetToken = "";
//Algorithm
while(baseTokens.hasMoreTokens() && targetTokens.hasMoreTokens()) {
nextBaseToken = baseTokens.nextToken();
nextTargetToken = targetTokens.nextToken();
System.out.println("while1");
if (!(nextBaseToken.equals(nextTargetToken))) {
System.out.println("if1");
while(true) {
result = result.concat("../");
System.out.println(result);
if (!baseTokens.hasMoreTokens()) {
System.out.println("break1");
break;
}
System.out.println("break2");
nextBaseToken = baseTokens.nextToken();
}
while(true) {
result = result.concat(nextTargetToken+"/");
System.out.println(result);
if (!targetTokens.hasMoreTokens()) {
System.out.println("break3");
break;
}
System.out.println("break4");
nextTargetToken = targetTokens.nextToken();
}
String temp = target.getFile();
result = result.concat(temp.substring(temp.lastIndexOf("/")+1,temp.length()));
System.out.println("1");
return result;
}
}
while(baseTokens.hasMoreTokens()) {
result = result.concat("../");
baseTokens.nextToken();
}
while(targetTokens.hasMoreTokens()) {
nextTargetToken = targetTokens.nextToken();
result = result.concat(nextTargetToken + "/");
}
String temp = target.getFile();
result = result.concat(temp.substring(temp.lastIndexOf("/")+1,temp.length()));
System.out.println("2");
return result;
}
System.out.println("3");
return target.toString();
}
}
Just a few thoughts...
- You might want to test if either (or both) your URL input is null. :)
- If the target URL has parameters (ex:
http://host/app/bla?param1=value¶m2=value
), does the generated relative URL contain the parameters? - If the target URL is just
http://host
, will it causeIndexOutOfBoundException
ontargetString.lastIndexOf("/")
... the same applies to base URL.
精彩评论