Chris Brown
Chris Brown

Reputation: 31

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:

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 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();
    }
}

Upvotes: 3

Views: 814

Answers (1)

limc
limc

Reputation: 40176

Just a few thoughts...

  1. You might want to test if either (or both) your URL input is null. :)
  2. If the target URL has parameters (ex: http://host/app/bla?param1=value&param2=value), does the generated relative URL contain the parameters?
  3. If the target URL is just http://host, will it cause IndexOutOfBoundException on targetString.lastIndexOf("/")... the same applies to base URL.

Upvotes: 1

Related Questions