JUnit check for this particular method

I am trying to write some JUnit tests for a Java method that uses a base URL and a destination URL and returns a destination URL relative to a given base URL.

I am using a category-based section to make my test suite. I am currently testing to verify the following:

  • check the two input urls. the same protocol and host;
  • check when the paths do not match and that the relative URL is configured correctly;
  • check when the base url is longer than the destination url;
  • check when the destination URL is longer than the base URL;
  • Check when the base URL and destination 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 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();
    }
}
+3
1

...

  • , , ( ) URL- .:)
  • URL (ex: http://host/app/bla?param1=value¶m2=value), URL ?
  • URL - http://host, IndexOutOfBoundException on targetString.lastIndexOf("/")... URL.
+1

Source: https://habr.com/ru/post/1791899/


All Articles